From 16e7e5b5804382ed4d3b6fce589764f94010b8ac Mon Sep 17 00:00:00 2001 From: erikn69 Date: Mon, 11 Sep 2023 12:41:32 -0500 Subject: [PATCH] feat: add comments support Fix #133 --- README.md | 1 + lib/index.js | 5 +++++ test/debug.js | 2 ++ test/index.test.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/README.md b/README.md index 8b391a4..7b0cc6a 100644 --- a/README.md +++ b/README.md @@ -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) + bracket: '\x1b[90m', // Comments clear: '\x1b[0m' // Clear (inserted after each match) } ``` diff --git a/lib/index.js b/lib/index.js index 9370645..42f7c83 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,6 +14,7 @@ const DEFAULT_OPTIONS = { string: '\x1b[32m', special: '\x1b[33m', bracket: '\x1b[33m', + comment: '\x1b[90m', clear: '\x1b[0m' } } @@ -28,6 +29,10 @@ const highlighters = [ group: 1, regex: new RegExp(`(^|${SPLIT_CHARS})(${keywords.join('|')})(?=${SPLIT_CHARS}|$)`, 'gi') }, + { + name: 'comment', + regex: /(\/\*(?:\*|.)*?\*\/|--.*?(?:\n|$)|#.*?(?:\n|$))/gms + }, { name: 'special', regex: /(=|!=|%|\/|\*|-|,|;|:|\+|<|>)/g diff --git a/test/debug.js b/test/debug.js index bbd096c..8e0c532 100644 --- a/test/debug.js +++ b/test/debug.js @@ -27,3 +27,5 @@ console.log(highlight('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;')) console.log(highlight('SELECT * FROM a;SELECT * FROM b;')) console.log(highlight("select * from a where b = 'array>';", { html: true })) + +console.log(highlight('SELECT */* table.* */ FROM a;\nSELECT * FROM b; # comment\nSELECT * FROM c; -- comment\nSELECT * FROM d; /* multi\nline\ncomment */')) diff --git a/test/index.test.js b/test/index.test.js index 775d5a0..818a5bc 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -8,6 +8,7 @@ const OPTIONS = { string: '[string]', special: '[special]', bracket: '[bracket]', + comment: '[comment]', clear: '[clear]' } } @@ -85,6 +86,21 @@ describe('unicode', () => { .toBe('[bracket]([clear]f1[bracket])[clear]') }) + it('comment /**/', () => { + expect(hlUni('/*/*comment\nsecondline*/*/')) + .toBe('[comment]/*/*comment\nsecondline*/[clear][special]*[clear][special]/[clear]') + }) + + it('comment #', () => { + expect(hlUni('#comment\n#comment')) + .toBe('[comment]#comment\n[clear][comment]#comment[clear]') + }) + + it('comment --', () => { + expect(hlUni('--comment\n--comment')) + .toBe('[comment]--comment\n[clear][comment]--comment[clear]') + }) + it('functions', () => { expect(hlUni('COUNT(`id`)')) .toBe('[function]COUNT[clear][bracket]([clear][string]`id`[clear][bracket])[clear]') @@ -177,6 +193,21 @@ describe('html', () => { .toBe('(f1)') }) + it('comment /**/', () => { + expect(hlHtml('/*/*comment\nsecondline*/*/')) + .toBe('/*/*comment\nsecondline*/*/') + }) + + it('comment #', () => { + expect(hlHtml('#comment\n#comment')) + .toBe('#comment\n#comment') + }) + + it('comment --', () => { + expect(hlHtml('--comment\n--comment')) + .toBe('--comment\n--comment') + }) + it('functions', () => { expect(hlHtml('COUNT(`id`)')) .toBe('COUNT(`id`)')