fix: lexer for comment

This commit is contained in:
xiaowei 2021-05-11 13:33:03 +08:00 committed by Ziv
parent dc49697302
commit 5f5af0a4ab
2 changed files with 11 additions and 1 deletions

View File

@ -82,7 +82,7 @@ function lexer(input: string): Token[] {
let value = ''; let value = '';
const start = current; const start = current;
while (char !== '\n') { while (char !== '\n' && current < input.length) {
value += char; value += char;
char = input[++current]; char = input[++current];
} }

View File

@ -1,4 +1,5 @@
import { lexer, splitSql, cleanSql } from '../../src'; import { lexer, splitSql, cleanSql } from '../../src';
import { TokenType } from '../../src/utils/token';
describe('utils', () => { describe('utils', () => {
test('split single sql', () => { test('split single sql', () => {
@ -28,6 +29,15 @@ describe('utils', () => {
const result = lexer(sql); const result = lexer(sql);
expect(result.length).toEqual(4); 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', () => { test('cleanSql', () => {
const sql = `-- a ; const sql = `-- a ;
select * from a; select * from a;