Skip to content

Commit

Permalink
fix: validate unexpected closing paren in expressions (#4803)
Browse files Browse the repository at this point in the history
User found syntax error which was accepted by linter. Turns out
parseExpressionAt allows something like this
```js
(a + b))
```

Switched back to `parse` which actually gives proper result when
expression is wrapped with parens.
  • Loading branch information
TrySound authored Jan 29, 2025
1 parent a0189c1 commit 5546301
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/sdk/src/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ describe("lint expression", () => {
expect(lintExpression({ expression: `a + ` })).toEqual([
error(4, 4, "Unexpected token"),
]);
expect(lintExpression({ expression: `"string" + a)` })).toEqual([
error(13, 13, "Unexpected token"),
]);
});

test("restrict expression syntax", () => {
Expand Down
10 changes: 7 additions & 3 deletions packages/sdk/src/expression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type Expression, type Identifier, parseExpressionAt } from "acorn";
import {
type Expression,
type Identifier,
parse,
parseExpressionAt,
} from "acorn";
import { simple } from "acorn-walk";
import type { DataSources } from "./schema/data-sources";
import type { Scope } from "./scope";
Expand Down Expand Up @@ -51,8 +56,7 @@ export const lintExpression = ({
try {
// wrap expression with parentheses to force acorn parse whole expression
// instead of just first valid part
// https://github.com/acornjs/acorn/tree/master/acorn
const root = parseExpressionAt(`(${expression})`, 0, {
const root = parse(`(${expression})`, {
ecmaVersion: "latest",
// support parsing import to forbid explicitly
sourceType: "module",
Expand Down

0 comments on commit 5546301

Please sign in to comment.