Skip to content

Commit 9cff83e

Browse files
Lucian-4a25成仕伟
andauthored
Support ES2022 class-private-fields-in syntax (#1068)
FIX #1067 FEATURE: Support class private fields with the `in` operator. Co-authored-by: 成仕伟 <chengsw@heywhale.com>
1 parent 230ac74 commit 9cff83e

File tree

4 files changed

+565
-2
lines changed

4 files changed

+565
-2
lines changed

acorn-loose/src/expression.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ lp.parseMaybeUnary = function(sawUnary) {
131131
this.next()
132132
node.argument = this.parseMaybeUnary(sawUnary)
133133
expr = this.finishNode(node, "SpreadElement")
134+
} else if (!sawUnary && this.tok.type === tt.privateId) {
135+
expr = this.parsePrivateIdent()
134136
} else {
135137
expr = this.parseExprSubscripts()
136138
while (this.tok.type.postfix && !this.canInsertSemicolon()) {

acorn/src/expression.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ pp.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, forInit) {
215215
}
216216

217217
pp.buildBinary = function(startPos, startLoc, left, right, op, logical) {
218+
if (right.type === "PrivateIdentifier") this.raise(right.start, "Private identifier can only be left side of binary expression")
218219
let node = this.startNodeAt(startPos, startLoc)
219220
node.left = left
220221
node.operator = op
@@ -244,6 +245,11 @@ pp.parseMaybeUnary = function(refDestructuringErrors, sawUnary, incDec, forInit)
244245
this.raiseRecoverable(node.start, "Private fields can not be deleted")
245246
else sawUnary = true
246247
expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
248+
} else if (!sawUnary && this.type === tt.privateId) {
249+
if (forInit || this.privateNameStack.length === 0) this.unexpected()
250+
expr = this.parsePrivateIdent()
251+
// only could be private fields in 'in', such as #x in obj
252+
if (this.type !== tt._in) this.unexpected()
247253
} else {
248254
expr = this.parseExprSubscripts(refDestructuringErrors, forInit)
249255
if (this.checkExpressionErrors(refDestructuringErrors)) return expr

bin/run_test262.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const parse = require("../acorn").parse
66
const unsupportedFeatures = [
77
"regexp-match-indices",
88
"arbitrary-module-namespace-names",
9-
"class-fields-private-in",
109
"import-assertions"
1110
];
1211

0 commit comments

Comments
 (0)