diff --git a/src/utils/index.ts b/src/utils/index.ts index 44952a2..da92fa4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -82,7 +82,7 @@ function lexer(input: string): Token[] { let value = ''; const start = current; - while (char !== '\n') { + while (char !== '\n' && current < input.length) { value += char; char = input[++current]; } diff --git a/test/utils/index.test.ts b/test/utils/index.test.ts index 3b69ff6..c50d9b4 100644 --- a/test/utils/index.test.ts +++ b/test/utils/index.test.ts @@ -1,4 +1,5 @@ import { lexer, splitSql, cleanSql } from '../../src'; +import { TokenType } from '../../src/utils/token'; describe('utils', () => { test('split single sql', () => { @@ -28,6 +29,15 @@ describe('utils', () => { const result = lexer(sql); expect(result.length).toEqual(4); }); + test('lexer for comments', () => { + const sql = `select * from a;--comments`; + const expected = `--comments`; + const result = lexer(sql); + const comments = result.find((token) => + token.type === TokenType.Comment, + ); + expect(comments.value).toEqual(expected); + }); test('cleanSql', () => { const sql = `-- a ; select * from a;