Skip to content

Commit 9dad7b2

Browse files
Qtaxscriptcoded
authored andcommitted
feat: SQL comments support
Add SQL comments support, including MySQL # comments. Refs: #133
1 parent 1472619 commit 9dad7b2

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The following options may be passed to the `highlight` function.
8989
string: '\x1b[32m', // Strings
9090
special: '\x1b[33m', // Special characters
9191
bracket: '\x1b[33m', // Brackets (parentheses)
92+
comment: '\x1b[2m\x1b[90m', // Comments
9293
clear: '\x1b[0m' // Clear (inserted after each match)
9394
}
9495
```

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare module 'sql-highlight' {
1010
string: string;
1111
special: string;
1212
bracket: string;
13+
comment: string;
1314
clear: string;
1415
};
1516
}

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const DEFAULT_OPTIONS = {
1414
string: '\x1b[32m',
1515
special: '\x1b[33m',
1616
bracket: '\x1b[33m',
17+
comment: '\x1b[2m\x1b[90m',
1718
clear: '\x1b[0m'
1819
}
1920
}
@@ -26,6 +27,9 @@ const highlighters = [
2627
// Note: Repeating string escapes like 'sql''server' will also work as they are just repeating strings
2728
/(?<string>'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*`)/,
2829

30+
/(?<comment>--[^\n\r]*|#[^\n\r]*|\/\*(?:[^*]|\*(?!\/))*\*\/)/,
31+
32+
// Future improvement: Comments should be allowed between the function name and the opening parenthesis
2933
/\b(?<function>\w+)(?=\s*\()/,
3034

3135
/(?<bracket>[()])/,

test/debug.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ console.log(highlight('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;'))
2626

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

29+
console.log(highlight('SELECT foo /* comment, not "keyword" WHERE GROUP */ FROM bar; -- comment\nSELECT * FROM baz;'))
30+
2931
console.log(highlight("select * from a where b = 'array<map<string,string>>';", { html: true }))

test/index.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const OPTIONS = {
88
string: '[string]',
99
special: '[special]',
1010
bracket: '[bracket]',
11+
comment: '[comment]',
1112
clear: '[clear]'
1213
}
1314
}
@@ -114,6 +115,26 @@ describe('unicode', () => {
114115
expect(hlUni('SELECT * FROM a;SELECT * FROM b;'))
115116
.toBe('[keyword]SELECT[clear] [special]*[clear] [keyword]FROM[clear] a[special];[clear][keyword]SELECT[clear] [special]*[clear] [keyword]FROM[clear] b[special];[clear]')
116117
})
118+
119+
it('comment single line', () => {
120+
expect(hlUni('-- comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; -- comment 3'))
121+
.toBe('[comment]-- comment 1 "comment" /* still */ comment 2[clear]\n[keyword]SELECT[clear] [string]`not comment`[clear][special];[clear] [comment]-- comment 3[clear]')
122+
})
123+
124+
it('comment mysql', () => {
125+
expect(hlUni('# comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; # comment 3'))
126+
.toBe('[comment]# comment 1 "comment" /* still */ comment 2[clear]\n[keyword]SELECT[clear] [string]`not comment`[clear][special];[clear] [comment]# comment 3[clear]')
127+
})
128+
129+
it('comment multiline', () => {
130+
expect(hlUni('SELECT /* this is, a "comment" */ "not /*comment*/" /***also*comment***/'))
131+
.toBe('[keyword]SELECT[clear] [comment]/* this is, a "comment" */[clear] [string]"not /*comment*/"[clear] [comment]/***also*comment***/[clear]')
132+
})
133+
134+
it('not a comment', () => {
135+
expect(hlUni('"id -- not comment /* still */ not"'))
136+
.toBe('[string]"id -- not comment /* still */ not"[clear]')
137+
})
117138
})
118139

119140
describe('html', () => {
@@ -211,6 +232,26 @@ describe('html', () => {
211232
expect(hlHtml("select * from a where b = 'array<map<string,string>>';"))
212233
.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>')
213234
})
235+
236+
it('comment single line', () => {
237+
expect(hlHtml('-- comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; -- comment 3'))
238+
.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>')
239+
})
240+
241+
it('comment mysql', () => {
242+
expect(hlHtml('# comment 1 "comment" /* still */ comment 2\nSELECT `not comment`; # comment 3'))
243+
.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>')
244+
})
245+
246+
it('comment multiline', () => {
247+
expect(hlHtml('SELECT /* this is, a "comment" */ "not /*comment*/" /***also*comment***/'))
248+
.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>')
249+
})
250+
251+
it('not a comment', () => {
252+
expect(hlHtml('"id -- not comment /* still */ not"'))
253+
.toBe('<span class="sql-hl-string">&quot;id -- not comment /* still */ not&quot;</span>')
254+
})
214255
})
215256

216257
describe('getSegments', () => {

0 commit comments

Comments
 (0)