Disallow private name in object elements and TS type elements (#10980)
* fix: disallow private name in object member and TS type elements * chore: update test262 whitelist * chore: make flow happy * Update packages/babel-parser/src/parser/expression.js Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com> * chore: update test fixtures * Update packages/babel-parser/src/parser/expression.js Co-Authored-By: Brian Ng <bng412@gmail.com> * chore: update test fixtures Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com> Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
@@ -606,7 +606,7 @@ export default class ExpressionParser extends LValParser {
|
||||
? this.parseExpression()
|
||||
: optional
|
||||
? this.parseIdentifier(true)
|
||||
: this.parseMaybePrivateName();
|
||||
: this.parseMaybePrivateName(true);
|
||||
node.computed = computed;
|
||||
|
||||
if (node.property.type === "PrivateName") {
|
||||
@@ -1131,11 +1131,19 @@ export default class ExpressionParser extends LValParser {
|
||||
return this.finishNode(node, "BooleanLiteral");
|
||||
}
|
||||
|
||||
parseMaybePrivateName(): N.PrivateName | N.Identifier {
|
||||
parseMaybePrivateName(
|
||||
isPrivateNameAllowed: boolean,
|
||||
): N.PrivateName | N.Identifier {
|
||||
const isPrivate = this.match(tt.hash);
|
||||
|
||||
if (isPrivate) {
|
||||
this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
|
||||
if (!isPrivateNameAllowed) {
|
||||
this.raise(
|
||||
this.state.pos,
|
||||
"Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p).",
|
||||
);
|
||||
}
|
||||
const node = this.startNode();
|
||||
this.next();
|
||||
this.assertNoSpace("Unexpected space between # and identifier");
|
||||
@@ -1596,12 +1604,12 @@ export default class ExpressionParser extends LValParser {
|
||||
}
|
||||
|
||||
const containsEsc = this.state.containsEsc;
|
||||
this.parsePropertyName(prop);
|
||||
this.parsePropertyName(prop, /* isPrivateNameAllowed */ false);
|
||||
|
||||
if (!isPattern && !containsEsc && !isGenerator && this.isAsyncProp(prop)) {
|
||||
isAsync = true;
|
||||
isGenerator = this.eat(tt.star);
|
||||
this.parsePropertyName(prop);
|
||||
this.parsePropertyName(prop, /* isPrivateNameAllowed */ false);
|
||||
} else {
|
||||
isAsync = false;
|
||||
}
|
||||
@@ -1688,7 +1696,7 @@ export default class ExpressionParser extends LValParser {
|
||||
if (!containsEsc && this.isGetterOrSetterMethod(prop, isPattern)) {
|
||||
if (isGenerator || isAsync) this.unexpected();
|
||||
prop.kind = prop.key.name;
|
||||
this.parsePropertyName(prop);
|
||||
this.parsePropertyName(prop, /* isPrivateNameAllowed */ false);
|
||||
this.parseMethod(
|
||||
prop,
|
||||
/* isGenerator */ false,
|
||||
@@ -1780,6 +1788,7 @@ export default class ExpressionParser extends LValParser {
|
||||
|
||||
parsePropertyName(
|
||||
prop: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase,
|
||||
isPrivateNameAllowed: boolean,
|
||||
): N.Expression | N.Identifier {
|
||||
if (this.eat(tt.bracketL)) {
|
||||
(prop: $FlowSubtype<N.ObjectOrClassMember>).computed = true;
|
||||
@@ -1792,7 +1801,7 @@ export default class ExpressionParser extends LValParser {
|
||||
(prop: $FlowFixMe).key =
|
||||
this.match(tt.num) || this.match(tt.string) || this.match(tt.bigint)
|
||||
? this.parseExprAtom()
|
||||
: this.parseMaybePrivateName();
|
||||
: this.parseMaybePrivateName(isPrivateNameAllowed);
|
||||
|
||||
if (prop.key.type !== "PrivateName") {
|
||||
// ClassPrivateProperty is never computed, so we don't assign in that case.
|
||||
|
||||
@@ -1474,7 +1474,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
}
|
||||
|
||||
parseClassPropertyName(member: N.ClassMember): N.Expression | N.Identifier {
|
||||
const key = this.parsePropertyName(member);
|
||||
const key = this.parsePropertyName(member, /* isPrivateNameAllowed */ true);
|
||||
|
||||
if (
|
||||
!member.computed &&
|
||||
|
||||
@@ -2266,9 +2266,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
|
||||
parsePropertyName(
|
||||
node: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase,
|
||||
isPrivateNameAllowed: boolean,
|
||||
): N.Identifier {
|
||||
const variance = this.flowParseVariance();
|
||||
const key = super.parsePropertyName(node);
|
||||
const key = super.parsePropertyName(node, isPrivateNameAllowed);
|
||||
// $FlowIgnore ("variance" not defined on TsNamedTypeElementBase)
|
||||
node.variance = variance;
|
||||
return key;
|
||||
|
||||
@@ -507,7 +507,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return idx;
|
||||
}
|
||||
|
||||
this.parsePropertyName(node);
|
||||
this.parsePropertyName(node, /* isPrivateNameAllowed */ false);
|
||||
return this.tsParsePropertyOrMethodSignature(node, readonly);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user