Skip to content

Commit

Permalink
Support ES2022 class-private-fields-in syntax (#1068)
Browse files Browse the repository at this point in the history
FIX #1067

FEATURE: Support class private fields with the `in` operator.

Co-authored-by: 成仕伟 <[email protected]>
  • Loading branch information
Lucian-4a25 and 成仕伟 authored Sep 13, 2021
1 parent 230ac74 commit 9cff83e
Show file tree
Hide file tree
Showing 4 changed files with 565 additions and 2 deletions.
2 changes: 2 additions & 0 deletions acorn-loose/src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ lp.parseMaybeUnary = function(sawUnary) {
this.next()
node.argument = this.parseMaybeUnary(sawUnary)
expr = this.finishNode(node, "SpreadElement")
} else if (!sawUnary && this.tok.type === tt.privateId) {
expr = this.parsePrivateIdent()
} else {
expr = this.parseExprSubscripts()
while (this.tok.type.postfix && !this.canInsertSemicolon()) {
Expand Down
6 changes: 6 additions & 0 deletions acorn/src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, forInit) {
}

pp.buildBinary = function(startPos, startLoc, left, right, op, logical) {
if (right.type === "PrivateIdentifier") this.raise(right.start, "Private identifier can only be left side of binary expression")
let node = this.startNodeAt(startPos, startLoc)
node.left = left
node.operator = op
Expand Down Expand Up @@ -244,6 +245,11 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary, incDec, forInit)
this.raiseRecoverable(node.start, "Private fields can not be deleted")
else sawUnary = true
expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
} else if (!sawUnary && this.type === tt.privateId) {
if (forInit || this.privateNameStack.length === 0) this.unexpected()
expr = this.parsePrivateIdent()
// only could be private fields in 'in', such as #x in obj
if (this.type !== tt._in) this.unexpected()
} else {
expr = this.parseExprSubscripts(refDestructuringErrors, forInit)
if (this.checkExpressionErrors(refDestructuringErrors)) return expr
Expand Down
1 change: 0 additions & 1 deletion bin/run_test262.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const parse = require("../acorn").parse
const unsupportedFeatures = [
"regexp-match-indices",
"arbitrary-module-namespace-names",
"class-fields-private-in",
"import-assertions"
];

Expand Down
Loading

0 comments on commit 9cff83e

Please sign in to comment.