Throw on attempt to delete a private field

Given that private fields can only be used within classes, any code
using them must be in a strict mode context. As private fields cannot
be deleted, throw an early SyntaxError.
This commit is contained in:
Karl Cheng
2017-06-23 16:10:06 +10:00
parent f976bdd21c
commit 43dba7e7c1
8 changed files with 320 additions and 2 deletions

View File

@@ -252,8 +252,16 @@ export default class ExpressionParser extends LValParser {
if (update) {
this.checkLVal(node.argument, undefined, undefined, "prefix operation");
} else if (this.state.strict && node.operator === "delete" && node.argument.type === "Identifier") {
this.raise(node.start, "Deleting local variable in strict mode");
} else if (this.state.strict && node.operator === "delete") {
const arg = node.argument;
if (arg.type === "Identifier") {
this.raise(node.start, "Deleting local variable in strict mode");
} else if (this.hasPlugin("classPrivateProperties")) {
if (arg.type === "PrivateName" || (arg.type === "MemberExpression" && arg.property.type === "PrivateName")) {
this.raise(node.start, "Deleting a private field is not allowed");
}
}
}
return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");