From 341ebe128c7b23f39d3e685e4ed475d42e15c849 Mon Sep 17 00:00:00 2001 From: ecmel Date: Wed, 22 May 2024 00:17:38 +0300 Subject: [PATCH] sort tokens --- server/src/linter/checks.ts | 8 ++++++++ server/src/parser/parser.ts | 9 ++++++--- server/src/parser/utils.ts | 4 ++-- test/suite/parser.test.ts | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/server/src/linter/checks.ts b/server/src/linter/checks.ts index 6643c100..c2991406 100644 --- a/server/src/linter/checks.ts +++ b/server/src/linter/checks.ts @@ -67,6 +67,14 @@ export function unusedVar(tokens: Token[]): Token[] { export function declaredAfterUse(tokens: Token[]): Token[] { return tokens .filter((token) => !inParam(token) && assigned(token) && assignable(token)) + .sort((a, b) => + identifier(a) < identifier(b) + ? -1 + : identifier(a) > identifier(b) + ? 1 + : (a.order || 0) - (b.order || 0), + ) + .filter((e, i, a) => !a[i - 1] || identifier(e) !== identifier(a[i - 1])) .filter((token) => tokens.find( (target) => diff --git a/server/src/parser/parser.ts b/server/src/parser/parser.ts index 302a1b39..f02fd265 100644 --- a/server/src/parser/parser.ts +++ b/server/src/parser/parser.ts @@ -186,9 +186,6 @@ export function parse(text: string): Token[] { switch (token.tokenType) { case LBracket: - case LParen: - case LCurly: - case LSql: next = peek(cache); if (next) { next.tangled = token; @@ -197,6 +194,12 @@ export function parse(text: string): Token[] { scope.push(token); cache.push(token); break; + case LParen: + case LCurly: + case LSql: + scope.push(token); + cache.push(token); + break; case RParen: case RBracket: case RCurly: diff --git a/server/src/parser/utils.ts b/server/src/parser/utils.ts index 2a743fd5..09b27d4a 100644 --- a/server/src/parser/utils.ts +++ b/server/src/parser/utils.ts @@ -67,8 +67,8 @@ export function inSql(token: Token) { } export function inTable(token: Token) { - const paren = inList(token); - return paren && paren.tangled?.tokenType === LBracket; + const list = inList(token); + return list && list.tangled?.tokenType === LBracket; } export function inParam(token: Token) { diff --git a/test/suite/parser.test.ts b/test/suite/parser.test.ts index a6e9301c..113d0f38 100644 --- a/test/suite/parser.test.ts +++ b/test/suite/parser.test.ts @@ -54,4 +54,19 @@ describe("TSQLint", () => { assert.strictEqual(result.length, 1); assert.strictEqual(result[0].code, "DECLARED_AFTER_USE"); }); + it("should not lint declaredAfterUse", () => { + const text = "a:1;a;a:1"; + const result = lint(parse(text)); + assert.strictEqual(result.length, 0); + }); + it("should not lint declaredAfterUse", () => { + const text = "a;{a:1;a}"; + const result = lint(parse(text)); + assert.strictEqual(result.length, 0); + }); + it("should not lint declaredAfterUse", () => { + const text = "[1=1;[a:1;b:1];[a;b]]"; + const result = lint(parse(text)); + assert.strictEqual(result.length, 0); + }); });