diff --git a/acorn/src/expression.js b/acorn/src/expression.js index 2ccc43af4..32d92d9ae 100644 --- a/acorn/src/expression.js +++ b/acorn/src/expression.js @@ -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") @@ -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) { diff --git a/test/tests-class-features-2022.js b/test/tests-class-features-2022.js index 322d0de5d..b488cb07d 100644 --- a/test/tests-class-features-2022.js +++ b/test/tests-class-features-2022.js @@ -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})