Skip to content

Commit 997ee9d

Browse files
authored
fix: don't break on nested segments (#62)
1 parent 97d911e commit 997ee9d

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ describe('unicode', () => {
9999
expect(hlUni("SELECT COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND `foo` = 'BAR' OR 1=1"))
100100
.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]'[email protected]'[clear] [keyword]AND[clear] [string]`foo`[clear] [special]=[clear] [string]'BAR'[clear] [keyword]OR[clear] [number]1[clear][special]=[clear][number]1[clear]")
101101
})
102+
103+
it('query with identifiers without apostrophes', () => {
104+
expect(hlUni('SELECT id FROM users'))
105+
.toBe('[keyword]SELECT[clear] id [keyword]FROM[clear] users')
106+
})
107+
108+
it('query with nested segments (minus in string)', () => {
109+
expect(hlUni('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;'))
110+
.toBe('[keyword]DROP[clear] [keyword]PROCEDURE[clear] [keyword]IF[clear] [keyword]EXISTS[clear] [string]`some-database`[clear].[string]`some-table`[clear][special];[clear]')
111+
})
112+
113+
it('multiple queries', () => {
114+
expect(hlUni('SELECT * FROM a;SELECT * FROM b;'))
115+
.toBe('[keyword]SELECT[clear] [special]*[clear] [keyword]FROM[clear] a[special];[clear][keyword]SELECT[clear] [special]*[clear] [keyword]FROM[clear] b[special];[clear]')
116+
})
102117
})
103118

104119
describe('html', () => {
@@ -181,6 +196,16 @@ describe('html', () => {
181196
expect(hlHtml('SELECT id FROM users'))
182197
.toBe('<span class="sql-hl-keyword">SELECT</span> id <span class="sql-hl-keyword">FROM</span> users')
183198
})
199+
200+
it('query with nested segments (minus in string)', () => {
201+
expect(hlHtml('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;'))
202+
.toBe('<span class="sql-hl-keyword">DROP</span> <span class="sql-hl-keyword">PROCEDURE</span> <span class="sql-hl-keyword">IF</span> <span class="sql-hl-keyword">EXISTS</span> <span class="sql-hl-string">`some-database`</span>.<span class="sql-hl-string">`some-table`</span><span class="sql-hl-special">;</span>')
203+
})
204+
205+
it('multiple queries', () => {
206+
expect(hlHtml('SELECT * FROM a;SELECT * FROM b;'))
207+
.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-special">;</span><span class="sql-hl-keyword">SELECT</span> <span class="sql-hl-special">*</span> <span class="sql-hl-keyword">FROM</span> b<span class="sql-hl-special">;</span>')
208+
})
184209
})
185210

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

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function getSegments (sqlString) {
8787
const segments = []
8888
let upperBound = 0
8989
for (let i = 0; i < sortedMatches.length; i++) {
90-
if (sortedMatches[i].start < upperBound) { break }
90+
if (sortedMatches[i].start < upperBound) { continue }
9191

9292
// If no match, add a default segment
9393
if (sortedMatches[i].start > upperBound) {

test/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* that. It's just to see that it's working in the console.
44
*/
55

6-
const { highlight } = require('../index')
6+
const { highlight } = require('../lib')
77

88
console.log(highlight("SELECT COUNT(id), COUNT(id), `id`, `username` FROM `users` WHERE `email` = '[email protected]' AND `something` = 'oke' AND 1=1"))
99
console.log(highlight('SELECT "users".* FROM "users"'))
@@ -21,3 +21,7 @@ console.log(highlight('SELECT id FROM listings WHERE status = "not available"'))
2121

2222
console.log(highlight('SELECT \'{"json_index":"json_value"}\' AS test;'))
2323
console.log(highlight('SELECT "This is a \\"text\\" test" AS text;'))
24+
25+
console.log(highlight('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;'))
26+
27+
console.log(highlight('SELECT * FROM a;SELECT * FROM b;'))

0 commit comments

Comments
 (0)