Skip to content

Commit 5546301

Browse files
authored
fix: validate unexpected closing paren in expressions (#4803)
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.
1 parent a0189c1 commit 5546301

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/sdk/src/expression.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ describe("lint expression", () => {
4040
expect(lintExpression({ expression: `a + ` })).toEqual([
4141
error(4, 4, "Unexpected token"),
4242
]);
43+
expect(lintExpression({ expression: `"string" + a)` })).toEqual([
44+
error(13, 13, "Unexpected token"),
45+
]);
4346
});
4447

4548
test("restrict expression syntax", () => {

packages/sdk/src/expression.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { type Expression, type Identifier, parseExpressionAt } from "acorn";
1+
import {
2+
type Expression,
3+
type Identifier,
4+
parse,
5+
parseExpressionAt,
6+
} from "acorn";
27
import { simple } from "acorn-walk";
38
import type { DataSources } from "./schema/data-sources";
49
import type { Scope } from "./scope";
@@ -51,8 +56,7 @@ export const lintExpression = ({
5156
try {
5257
// wrap expression with parentheses to force acorn parse whole expression
5358
// instead of just first valid part
54-
// https://github.com/acornjs/acorn/tree/master/acorn
55-
const root = parseExpressionAt(`(${expression})`, 0, {
59+
const root = parse(`(${expression})`, {
5660
ecmaVersion: "latest",
5761
// support parsing import to forbid explicitly
5862
sourceType: "module",

0 commit comments

Comments
 (0)