Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/utils/queryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,23 @@ export function normalizeQuery(query: string): string {

export function normalizeQSQLQuery(query: string): string {
return (
normalizeQuery(query)
queryLimitCheck(query)
// Remove block comments
.replace(/^\/[\t ]*$[^]*?^\\[\t ]*$/gm, "")
// Remove terminate comments
.replace(/^\\[\t ]*(?:\r\n|[\r\n])[^]*/gm, "")
// Remove single line comments
.replace(/^\/.+/gm, "")
// Replace system commands
.replace(/^\\([a-zA-Z_1-2\\]+)[\t ]*(.*)/gm, (matched, command, args) =>
matched === "\\\\"
? 'system"\\\\"'
: `system"${command} ${args.trim()}"`,
)
// Trim white space
.trim()
// Replace end of statements
.replace(/\r\n/gs, ";")
// Remove start of file
.replace(/^[;\s]+/gs, "")
// Remove end of file
.replace(/[;\s]+$/gs, "")
.replace(/(?<!;[\t ]*)(\r\n|[\r\n])+(?![\t\r\n ])/gs, ";$1")
);
}

Expand Down
26 changes: 12 additions & 14 deletions test/suite/utils/queryUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,9 @@ describe("queryUtils", () => {
assert.strictEqual(res, "a:1");
});

it("should remove line comment", () => {
it("should preserve line comment", () => {
const res = queryUtils.normalizeQSQLQuery("a:1 / line comment");

assert.strictEqual(res, "a:1");
assert.strictEqual(res, "a:1 / line comment");
});

it("should ignore line comment in a string", () => {
Expand All @@ -470,18 +469,18 @@ describe("queryUtils", () => {
assert.strictEqual(res, 'a:"1 / not line comment"');
});

it("should replace EOS with semicolon", () => {
it("should replace EOS with semicolon preserve new lines", () => {
let res = queryUtils.normalizeQSQLQuery("a:1\na");
assert.strictEqual(res, "a:1;a");
assert.strictEqual(res, "a:1;\na");
res = queryUtils.normalizeQSQLQuery("a:1\r\na");
assert.strictEqual(res, "a:1;a");
assert.strictEqual(res, "a:1;\r\na");
});

it("should escpae new lines in strings", () => {
let res = queryUtils.normalizeQSQLQuery('a:"a\n \nb"');
assert.strictEqual(res, 'a:"a\\n \\nb"');
res = queryUtils.normalizeQSQLQuery('a:"a\r\n \r\nb"');
assert.strictEqual(res, 'a:"a\\n \\nb"');
it("should preserve new lines in strings", () => {
let res = queryUtils.normalizeQSQLQuery('a:"a\n\n b"');
assert.strictEqual(res, 'a:"a\n\n b"');
res = queryUtils.normalizeQSQLQuery('a:"a\r\n\r\n b"');
assert.strictEqual(res, 'a:"a\r\n\r\n b"');
});
});

Expand Down Expand Up @@ -602,10 +601,9 @@ describe("queryUtils", () => {
describe("getQSQLWrapper", () => {
let queryWrappeStub: sinon.SinonStub;

it("should normalize q code", () => {
it("should not add extra semicolon", () => {
const res = queryUtils.getQSQLWrapper("a:1;\na");

assert.strictEqual(res, "a:1;;a");
assert.strictEqual(res, "a:1;\na");
});

it("should normalize python code using wrapper", () => {
Expand Down