From a08b234d7d23c48f225f8e0c6e6aa5b09a7c7d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malcolm=20Nihl=C3=A9n?= Date: Tue, 16 Aug 2022 17:34:16 +0200 Subject: [PATCH 1/3] fix: don't break on nested segments --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index f3f9bdf..b0ceadb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -87,7 +87,7 @@ function getSegments (sqlString) { const segments = [] let upperBound = 0 for (let i = 0; i < sortedMatches.length; i++) { - if (sortedMatches[i].start < upperBound) { break } + if (sortedMatches[i].start < upperBound) { continue } // If no match, add a default segment if (sortedMatches[i].start > upperBound) { From 7c6a28b0afa295f814b6d2a58601d5d9fd914b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malcolm=20Nihl=C3=A9n?= Date: Tue, 16 Aug 2022 17:47:43 +0200 Subject: [PATCH 2/3] test: add test cases --- index.test.js | 20 ++++++++++++++++++++ test/index.js | 6 +++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/index.test.js b/index.test.js index caf7909..6a21445 100644 --- a/index.test.js +++ b/index.test.js @@ -99,6 +99,16 @@ describe('unicode', () => { expect(hlUni("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = 'test@example.com' AND `foo` = 'BAR' OR 1=1")) .toBe("[keyword]SELECT[clear] [function]COUNT[clear][bracket]([clear]id[bracket])[clear][special],[clear] [string]`id`[clear][special],[clear] [string]`username`[clear] [keyword]FROM[clear] [string]`users`[clear] [keyword]WHERE[clear] [string]`email`[clear] [special]=[clear] [string]'test@example.com'[clear] [keyword]AND[clear] [string]`foo`[clear] [special]=[clear] [string]'BAR'[clear] [keyword]OR[clear] [number]1[clear][special]=[clear][number]1[clear]") }) + + it('query with nested segments (minus in string)', () => { + expect(hlUni('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;')) + .toBe('[keyword]DROP[clear] [keyword]PROCEDURE[clear] [keyword]IF[clear] [keyword]EXISTS[clear] [string]`some-database`[clear].[string]`some-table`[clear][special];[clear]') + }) + + it('multiple queries', () => { + 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]') + }) }) describe('html', () => { @@ -181,6 +191,16 @@ describe('html', () => { expect(hlHtml('SELECT id FROM users')) .toBe('SELECT id FROM users') }) + + it('query with nested segments (minus in string)', () => { + expect(hlHtml('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;')) + .toBe('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;') + }) + + it('multiple queries', () => { + expect(hlHtml('SELECT * FROM a;SELECT * FROM b;')) + .toBe('SELECT * FROM a;SELECT * FROM b;') + }) }) describe('getSegments', () => { diff --git a/test/index.js b/test/index.js index 41c6088..c0f492f 100644 --- a/test/index.js +++ b/test/index.js @@ -3,7 +3,7 @@ * that. It's just to see that it's working in the console. */ -const { highlight } = require('../index') +const { highlight } = require('../lib') console.log(highlight("SELECT COUNT(id), COUNT(id), `id`, `username` FROM `users` WHERE `email` = 'test@example.com' AND `something` = 'oke' AND 1=1")) console.log(highlight('SELECT "users".* FROM "users"')) @@ -21,3 +21,7 @@ console.log(highlight('SELECT id FROM listings WHERE status = "not available"')) console.log(highlight('SELECT \'{"json_index":"json_value"}\' AS test;')) console.log(highlight('SELECT "This is a \\"text\\" test" AS text;')) + +console.log(highlight('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;')) + +console.log(highlight('SELECT * FROM a;SELECT * FROM b;')) From 3268cce86b8b1e02537cc60896f3b9176e610e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malcolm=20Nihl=C3=A9n?= Date: Tue, 16 Aug 2022 17:47:49 +0200 Subject: [PATCH 3/3] test: add missing test case --- index.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.test.js b/index.test.js index 6a21445..de18053 100644 --- a/index.test.js +++ b/index.test.js @@ -100,6 +100,11 @@ describe('unicode', () => { .toBe("[keyword]SELECT[clear] [function]COUNT[clear][bracket]([clear]id[bracket])[clear][special],[clear] [string]`id`[clear][special],[clear] [string]`username`[clear] [keyword]FROM[clear] [string]`users`[clear] [keyword]WHERE[clear] [string]`email`[clear] [special]=[clear] [string]'test@example.com'[clear] [keyword]AND[clear] [string]`foo`[clear] [special]=[clear] [string]'BAR'[clear] [keyword]OR[clear] [number]1[clear][special]=[clear][number]1[clear]") }) + it('query with identifiers without apostrophes', () => { + expect(hlUni('SELECT id FROM users')) + .toBe('[keyword]SELECT[clear] id [keyword]FROM[clear] users') + }) + it('query with nested segments (minus in string)', () => { expect(hlUni('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;')) .toBe('[keyword]DROP[clear] [keyword]PROCEDURE[clear] [keyword]IF[clear] [keyword]EXISTS[clear] [string]`some-database`[clear].[string]`some-table`[clear][special];[clear]')