Skip to content

Commit

Permalink
feat: SQL comments support
Browse files Browse the repository at this point in the history
Add SQL comments support, including MySQL # comments.

Refs: #133
  • Loading branch information
Qtax committed Oct 4, 2023
1 parent f282666 commit a03ec17
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The following options may be passed to the `highlight` function.
string: '\x1b[32m', // Strings
special: '\x1b[33m', // Special characters
bracket: '\x1b[33m', // Brackets (parentheses)
comment: '\x1b[2m\x1b[90m', // Comments
clear: '\x1b[0m' // Clear (inserted after each match)
}
```
Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare module 'sql-highlight' {
string: string;
special: string;
bracket: string;
comment: string;
clear: string;
};
}
Expand Down
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DEFAULT_OPTIONS = {
string: '\x1b[32m',
special: '\x1b[33m',
bracket: '\x1b[33m',
comment: '\x1b[2m\x1b[90m',
clear: '\x1b[0m'
}
}
Expand All @@ -26,6 +27,9 @@ const highlighters = [
// Note: Repeating string escapes like 'sql''server' will also work as they are just repeating strings
/(?<string>'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*`)/,

/(?<comment>--[^\n\r]*|#[^\n\r]*|\/\*(?:[^*]|\*(?!\/))*\*\/)/,

// Future improvement: Comments should be allowed between the function name and the opening parenthesis
/\b(?<function>\w+)(?=\s*\()/,

/(?<bracket>[()])/,
Expand Down
2 changes: 2 additions & 0 deletions test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ console.log(highlight('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;'))

console.log(highlight('SELECT * FROM a;SELECT * FROM b;'))

console.log(highlight('SELECT foo /* comment, not "keyword" WHERE GROUP */ FROM bar; -- comment\nSELECT * FROM baz;'))

console.log(highlight("select * from a where b = 'array<map<string,string>>';", { html: true }))
41 changes: 41 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const OPTIONS = {
string: '[string]',
special: '[special]',
bracket: '[bracket]',
comment: '[comment]',
clear: '[clear]'
}
}
Expand Down Expand Up @@ -114,6 +115,26 @@ describe('unicode', () => {
expect(hlUni('SELECT * FROM a;SELECT * FROM b;'))
.toBe('[keyword]SELECT[clear] [special]*[clear] [keyword]FROM[clear] a[special];[clear][keyword]SELECT[clear] [special]*[clear] [keyword]FROM[clear] b[special];[clear]')
})

it('comment single line', () => {
expect(hlUni('-- comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; -- comment 3'))
.toBe('[comment]-- comment 1 "comment" /* still */ comment 2[clear]\n[keyword]SELECT[clear] [string]`not comment`[clear][special];[clear] [comment]-- comment 3[clear]')
})

it('comment mysql', () => {
expect(hlUni('# comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; # comment 3'))
.toBe('[comment]# comment 1 "comment" /* still */ comment 2[clear]\n[keyword]SELECT[clear] [string]`not comment`[clear][special];[clear] [comment]# comment 3[clear]')
})

it('comment multiline', () => {
expect(hlUni('SELECT /* this is, a "comment" */ "not /*comment*/" /***also*comment***/'))
.toBe('[keyword]SELECT[clear] [comment]/* this is, a "comment" */[clear] [string]"not /*comment*/"[clear] [comment]/***also*comment***/[clear]')
})

it('not a comment', () => {
expect(hlUni('"id -- not comment /* still */ not"'))
.toBe('[string]"id -- not comment /* still */ not"[clear]')
})
})

describe('html', () => {
Expand Down Expand Up @@ -211,6 +232,26 @@ describe('html', () => {
expect(hlHtml("select * from a where b = 'array<map<string,string>>';"))
.toBe('<span class="sql-hl-keyword">select</span> <span class="sql-hl-special">*</span> <span class="sql-hl-keyword">from</span> a <span class="sql-hl-keyword">where</span> b <span class="sql-hl-special">=</span> <span class="sql-hl-string">&#39;array&lt;map&lt;string,string&gt;&gt;&#39;</span><span class="sql-hl-special">;</span>')
})

it('comment single line', () => {
expect(hlHtml('-- comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; -- comment 3'))
.toBe('<span class="sql-hl-comment">-- comment 1 &quot;comment&quot; /* still */ comment 2</span>\n<span class="sql-hl-keyword">SELECT</span> <span class="sql-hl-string">`not comment`</span><span class="sql-hl-special">;</span> <span class="sql-hl-comment">-- comment 3</span>')
})

it('comment mysql', () => {
expect(hlHtml('# comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; # comment 3'))
.toBe('<span class="sql-hl-comment"># comment 1 &quot;comment&quot; /* still */ comment 2</span>\n<span class="sql-hl-keyword">SELECT</span> <span class="sql-hl-string">`not comment`</span><span class="sql-hl-special">;</span> <span class="sql-hl-comment"># comment 3</span>')
})

it('comment multiline', () => {
expect(hlHtml('SELECT /* this is, a "comment" */ "not /*comment*/" /***also*comment***/'))
.toBe('<span class="sql-hl-keyword">SELECT</span> <span class="sql-hl-comment">/* this is, a &quot;comment&quot; */</span> <span class="sql-hl-string">&quot;not /*comment*/&quot;</span> <span class="sql-hl-comment">/***also*comment***/</span>')
})

it('not a comment', () => {
expect(hlHtml('"id -- not comment /* still */ not"'))
.toBe('<span class="sql-hl-string">&quot;id -- not comment /* still */ not&quot;</span>')
})
})

describe('getSegments', () => {
Expand Down

0 comments on commit a03ec17

Please sign in to comment.