Skip to content

Commit

Permalink
Fix minor ParenthesizedExpression-related issues (#1277)
Browse files Browse the repository at this point in the history
And run test262 test suite with preserveParens enabled

FIX: Fix a bug where some invalid `delete` expressions were let through when the operand
was parenthesized and `preserveParens` was enabled.
  • Loading branch information
adams85 committed Feb 22, 2024
1 parent 80b3562 commit a3456a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions acorn/src/expression.js
Expand Up @@ -242,8 +242,7 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary, incDec, forInit)
node.argument = this.parseMaybeUnary(null, true, update, forInit)
this.checkExpressionErrors(refDestructuringErrors, true)
if (update) this.checkLValSimple(node.argument)
else if (this.strict && node.operator === "delete" &&
node.argument.type === "Identifier")
else if (this.strict && node.operator === "delete" && isLocalVariableAccess(node.argument))
this.raiseRecoverable(node.start, "Deleting local variable in strict mode")
else if (node.operator === "delete" && isPrivateFieldAccess(node.argument))
this.raiseRecoverable(node.start, "Private fields can not be deleted")
Expand Down Expand Up @@ -278,10 +277,18 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary, incDec, forInit)
}
}

function isLocalVariableAccess(node) {
return (
node.type === "Identifier" ||
node.type === "ParenthesizedExpression" && isLocalVariableAccess(node.expression)
)
}

function isPrivateFieldAccess(node) {
return (
node.type === "MemberExpression" && node.property.type === "PrivateIdentifier" ||
node.type === "ChainExpression" && isPrivateFieldAccess(node.expression)
node.type === "ChainExpression" && isPrivateFieldAccess(node.expression) ||
node.type === "ParenthesizedExpression" && isPrivateFieldAccess(node.expression)
)
}

Expand Down
2 changes: 1 addition & 1 deletion bin/run_test262.js
Expand Up @@ -10,7 +10,7 @@ function loadList(filename) {
}

run(
(content, {sourceType}) => parse(content, {sourceType, ecmaVersion: "latest"}),
(content, {sourceType}) => parse(content, {sourceType, ecmaVersion: "latest", preserveParens: true}),
{
testsDirectory: path.dirname(require.resolve("test262/package.json")),
skip: test => test.attrs.features &&
Expand Down

0 comments on commit a3456a8

Please sign in to comment.