Skip to content

Commit

Permalink
update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 14, 2024
1 parent 0ad663d commit ca0ad1d
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 95 deletions.
45 changes: 0 additions & 45 deletions .eslintrc.json

This file was deleted.

44 changes: 44 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const js = require("@eslint/js");
const globals = require("globals");

module.exports = [
js.configs.recommended,
{
languageOptions: {
globals: {
...globals.es6,
...globals.node,
...globals.jasmine,
...globals.BigInt,
},
parserOptions: {
ecmaFeatures: { globalReturn: true },
},
sourceType: "commonjs",
ecmaVersion: 2022,
},
rules: {
"no-var": "error",
"prefer-const": "error",
"prefer-arrow-callback": "error",
"no-else-return": "error",
"no-multi-spaces": "error",
"no-whitespace-before-property": "error",
camelcase: "error",
"new-cap": "error",
"no-console": "error",
"comma-dangle": "error",
"no-shadow": "error",
"object-shorthand": ["error", "properties"],
indent: [
"error",
4,
{
SwitchCase: 1,
},
],
quotes: ["error", "single"],
semi: ["error", "always"],
},
},
];
2 changes: 1 addition & 1 deletion lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SQLParsingError.prototype.toString = function (level) {
level = level > 0 ? parseInt(level) : 0;
const gap = messageGap(level + 1);
const lines = [
`SQLParsingError {`,
'SQLParsingError {',
`${gap}code: parsingErrorCode.${errorMessages[this.code].name}`,
`${gap}error: "${this.error}"`,
`${gap}position: {line: ${this.position.line}, col: ${this.position.column}}`,
Expand Down
6 changes: 3 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ function minify(sql, options) {
continue;
}

if (s === `'`) {
if (s === '\'') {
closeIdx = idx;
do {
closeIdx = sql.indexOf(`'`, closeIdx + 1);
closeIdx = sql.indexOf('\'', closeIdx + 1);
if (closeIdx > 0) {
let i = closeIdx;
while (sql[--i] === '\\') ;
if ((closeIdx - i) % 2) {
let step = closeIdx;
while (++step < len && sql[step] === `'`) ;
while (++step < len && sql[step] === '\'') ;
if ((step - closeIdx) % 2) {
closeIdx = step - 1;
break;
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
"node": ">=14.0.0"
},
"devDependencies": {
"eslint": "8.57.0",
"@eslint/js": "9.7.0",
"@types/node": "20.14.10",
"eslint": "9.7.0",
"istanbul": "0.4.5",
"jasmine-node": "3.0.0",
"typescript": "5.4.4"
"typescript": "5.5.3"
}
}
8 changes: 4 additions & 4 deletions test/compressSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Compress', () => {

describe('with a text block', () => {
it('must remove all gaps', () => {
expect(minify(`some 'text' here`)).toBe(`some'text'here`);
expect(minify('some \'text\' here')).toBe('some\'text\'here');
});
});

Expand All @@ -48,15 +48,15 @@ describe('Compress', () => {

describe('for multi-line text', () => {
it('must preserve the prefix space', () => {
expect(minify(`select '\nvalue'`, {compress: true})).toBe(`select E'\\nvalue'`);
expect(minify('select \'\nvalue\'', {compress: true})).toBe('select E\'\\nvalue\'');
});
it('must not add a space to an empty string', () => {
expect(minify(`'\nvalue'`, {compress: true})).toBe(`E'\\nvalue'`);
expect(minify('\'\nvalue\'', {compress: true})).toBe('E\'\\nvalue\'');
});
it('must not add a space after special symbols', () => {
for (let i = 0; i < compressors.length; i++) {
const c = compressors[i];
expect(minify(c + `'\nvalue'`, {compress: true})).toBe(c + `E'\\nvalue'`);
expect(minify(c + '\'\nvalue\'', {compress: true})).toBe(c + 'E\'\\nvalue\'');
}
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/identSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Quoted Identifier / Positive', () => {
});

it('must allow single-quotes inside', () => {
expect(minify(`"'some'text'"`)).toBe(`"'some'text'"`);
expect(minify('"\'some\'text\'"')).toBe('"\'some\'text\'"');
});
});

Expand All @@ -37,7 +37,7 @@ describe('Quoted Identifier / Negative', () => {
}

it('must report unclosed quotes', () => {
const e = getError(`"`);
const e = getError('"');
expect(e instanceof SQLParsingError);
expect(e.code).toBe(PEC.unclosedQI);
expect(e.position).toEqual({
Expand Down
76 changes: 38 additions & 38 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe('Minify/Positive', () => {

describe('comments in strings', () => {
it('must be skipped', () => {
expect(minify(`'--comment'`)).toBe(`'--comment'`);
expect(minify(`'--comment` + EOL + `text'`)).toBe(`E'--comment\\ntext'`);
expect(minify(`'/*comment*/'`)).toBe(`'/*comment*/'`);
expect(minify('\'--comment\'')).toBe('\'--comment\'');
expect(minify('\'--comment' + EOL + 'text\'')).toBe('E\'--comment\\ntext\'');
expect(minify('\'/*comment*/\'')).toBe('\'/*comment*/\'');
});
});

Expand All @@ -56,31 +56,31 @@ describe('Minify/Positive', () => {
describe('empty text', () => {
it('must be returned empty', () => {
expect(minify('')).toBe('');
expect(minify(`''`)).toBe(`''`);
expect(minify('\'\'')).toBe('\'\'');
});
});

describe('multi-line text', () => {
it('must be returned with E', () => {
expect(minify(`'` + EOL + `'`)).toBe(`E'\\n'`);
expect(minify(`'` + EOL + EOL + `'`)).toBe(`E'\\n\\n'`);
expect(minify(`text '` + EOL + `'`)).toBe(`text E'\\n'`);
expect(minify(`text e'` + EOL + `'`)).toBe(`text e'\\n'`);
expect(minify(`e'` + EOL + `'`)).toBe(`e'\\n'`);
expect(minify(`E'` + EOL + `'`)).toBe(`E'\\n'`);
expect(minify('\'' + EOL + '\'')).toBe('E\'\\n\'');
expect(minify('\'' + EOL + EOL + '\'')).toBe('E\'\\n\\n\'');
expect(minify('text \'' + EOL + '\'')).toBe('text E\'\\n\'');
expect(minify('text e\'' + EOL + '\'')).toBe('text e\'\\n\'');
expect(minify('e\'' + EOL + '\'')).toBe('e\'\\n\'');
expect(minify('E\'' + EOL + '\'')).toBe('E\'\\n\'');
});

it('must truncate text correctly', () => {
expect(minify(`' first ` + EOL + ` last '`)).toBe(`E' first\\nlast '`);
expect(minify(`' first ` + EOL + ` second ` + EOL + ` third '`)).toBe(`E' first\\nsecond\\nthird '`);
expect(minify('\' first ' + EOL + ' last \'')).toBe('E\' first\\nlast \'');
expect(minify('\' first ' + EOL + ' second ' + EOL + ' third \'')).toBe('E\' first\\nsecond\\nthird \'');
});

it('must add a space where necessary', () => {
expect(minify(`select'\nvalue'`)).toBe(`select E'\\nvalue'`);
expect(minify('select\'\nvalue\'')).toBe('select E\'\\nvalue\'');
});

it('must not add a space to an empty string', () => {
expect(minify(`'\nvalue'`)).toBe(`E'\\nvalue'`);
expect(minify('\'\nvalue\'')).toBe('E\'\\nvalue\'');
});

});
Expand Down Expand Up @@ -121,20 +121,20 @@ describe('Minify/Positive', () => {
describe('tabs in text', () => {
it('must be replaced', () => {
expect(minify('\t')).toBe('');
expect(minify(`'\t'`)).toBe(`E'\\t'`);
expect(minify(`'\\t'`)).toBe(`'\\t'`);
expect(minify(`e' \t '`)).toBe(`e' \\t '`);
expect(minify(`'\t first ` + EOL + `\t second \t` + EOL + `\t third \t'`)).toBe(`E'\\t first\\nsecond\\nthird \\t'`);
expect(minify('\'\t\'')).toBe('E\'\\t\'');
expect(minify('\'\\t\'')).toBe('\'\\t\'');
expect(minify('e\' \t \'')).toBe('e\' \\t \'');
expect(minify('\'\t first ' + EOL + '\t second \t' + EOL + '\t third \t\'')).toBe('E\'\\t first\\nsecond\\nthird \\t\'');
});
});

describe('quotes in strings', () => {
it('must be ignored', () => {
expect(minify(`''`)).toBe(`''`);
expect(minify(`text ''`)).toBe(`text ''`);
expect(minify(`'text\\\\'`)).toBe(`'text\\\\'`);
expect(minify(`''`)).toBe(`''`);
expect(minify(`''''''`)).toBe(`''''''`);
expect(minify('\'\'')).toBe('\'\'');
expect(minify('text \'\'')).toBe('text \'\'');
expect(minify('\'text\\\\\'')).toBe('\'text\\\\\'');
expect(minify('\'\'')).toBe('\'\'');
expect(minify('\'\'\'\'\'\'')).toBe('\'\'\'\'\'\'');
});
});

Expand Down Expand Up @@ -217,24 +217,24 @@ describe('Minify/Negative', () => {
expect(getErrorPos('hello/*world!/**/').column).toBe(14);
});
it('must ignore closures in text', () => {
expect(errorCode(`/*'*/'*/`)).toBe(PEC.unclosedText);
expect(errorCode('/*\'*/\'*/')).toBe(PEC.unclosedText);
});
});

describe('quotes in strings', () => {

it('must report an error', () => {
expect(errorCode(`'`)).toBe(PEC.unclosedText);
expect(errorCode(`'''`)).toBe(PEC.unclosedText);
expect(errorCode(`''' `)).toBe(PEC.unclosedText);
expect(errorCode(`'\\'`)).toBe(PEC.unclosedText);
expect(errorCode('\'')).toBe(PEC.unclosedText);
expect(errorCode('\'\'\'')).toBe(PEC.unclosedText);
expect(errorCode('\'\'\' ')).toBe(PEC.unclosedText);
expect(errorCode('\'\\\'')).toBe(PEC.unclosedText);
});

it('must report positions correctly', () => {
expect(getErrorPos(`'`).column).toBe(1);
expect(getErrorPos(` '`).column).toBe(2);
expect(getErrorPos(`s'`).column).toBe(2);
expect(getErrorPos(`s '`).column).toBe(3);
expect(getErrorPos('\'').column).toBe(1);
expect(getErrorPos(' \'').column).toBe(2);
expect(getErrorPos('s\'').column).toBe(2);
expect(getErrorPos('s \'').column).toBe(3);
});

});
Expand All @@ -253,15 +253,15 @@ describe('Minify/Negative', () => {
// are present, they are expected to affect the comment block.
it('must throw on extra openers', () => {
expect(() => {
minify(`1/*0'/*'*//2`);
minify('1/*0\'/*\'*//2');
}).toThrow('Error parsing SQL at {line:1,col:6}: Unclosed multi-line comment.');
expect(() => {
minify('3/*0"/*"*//4');
}).toThrow('Error parsing SQL at {line:1,col:6}: Unclosed multi-line comment.');
});
it('must throw on extra closures', () => {
expect(() => {
minify(`1/*0'*/'*//2`);
minify('1/*0\'*/\'*//2');
}).toThrow('Error parsing SQL at {line:1,col:8}: Unclosed text block.');
expect(() => {
minify('3/*0"*/"*//4');
Expand All @@ -273,17 +273,17 @@ describe('Minify/Negative', () => {
const gap = messageGap(1);
let err;
try {
minify(`'test`);
minify('\'test');
} catch (e) {
err = e.toString();
}
it('must contain error', () => {
const txt = [
`SQLParsingError {`,
'SQLParsingError {',
`${gap}code: parsingErrorCode.unclosedText`,
`${gap}error: "Unclosed text block."`,
`${gap}position: {line: 1, col: 1}`,
`}`
'}'
];
expect(err).toBe(txt.join(EOL));
});
Expand All @@ -293,7 +293,7 @@ describe('Minify/Negative', () => {
it('must produce the same output', () => {
let error;
try {
minify(`'test`);
minify('\'test');
} catch (e) {
error = e;
}
Expand Down

0 comments on commit ca0ad1d

Please sign in to comment.