Skip to content

Commit

Permalink
sort tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmel committed May 21, 2024
1 parent d755abd commit 341ebe1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
8 changes: 8 additions & 0 deletions server/src/linter/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
9 changes: 6 additions & 3 deletions server/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions server/src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions test/suite/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 341ebe1

Please sign in to comment.