Skip to content

Commit

Permalink
fix delete obj?.#field
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and marijnh committed Apr 24, 2021
1 parent 2f90bf9 commit 0f9aabb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions acorn/src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary) {
else if (this.strict && node.operator === "delete" &&
node.argument.type === "Identifier")
this.raiseRecoverable(node.start, "Deleting local variable in strict mode")
else if (node.operator === "delete" && node.argument.type === "MemberExpression" &&
node.argument.property.type === "PrivateIdentifier")
else if (node.operator === "delete" && isPrivateFieldAccess(node.argument))
this.raiseRecoverable(node.start, "Private fields can not be deleted")
else sawUnary = true
expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
Expand All @@ -262,6 +261,13 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary) {
return expr
}

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

// Parse call, dot, and `[]`-subscript expressions.

pp.parseExprSubscripts = function(refDestructuringErrors) {
Expand Down
7 changes: 7 additions & 0 deletions test/tests-class-features-2022.js
Original file line number Diff line number Diff line change
Expand Up @@ -3671,6 +3671,13 @@ test("class C { #aaa; f(f) { f()?.#aaa } }", {
// super
testFail("class C extends Base { f() { return super.#aaa } }", "Unexpected token (1:42)", {ecmaVersion: 13})

// delete
test("class C { #aaa; f() { delete this.#aaa.foo } }", {}, {ecmaVersion: 13})
test("class C { #aaa; f() { delete this.#aaa?.foo } }", {}, {ecmaVersion: 13})
testFail("class C { #aaa; f() { delete this.#aaa } }", "Private fields can not be deleted (1:22)", {ecmaVersion: 13})
testFail("class C { #aaa; f() { delete obj?.#aaa } }", "Private fields can not be deleted (1:22)", {ecmaVersion: 13})
testFail("class C { #aaa; f() { delete obj?.p.#aaa } }", "Private fields can not be deleted (1:22)", {ecmaVersion: 13})

// unexpected token
testFail("const obj = #aaa", "Unexpected token (1:12)", {ecmaVersion: 13})
testFail("const obj = { #aaa }", "Unexpected token (1:14)", {ecmaVersion: 13})
Expand Down

0 comments on commit 0f9aabb

Please sign in to comment.