Skip to content

Commit

Permalink
Disallow private name in object elements and TS type elements (#10980)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Jan 11, 2020
1 parent e7b80a2 commit 81c5f1f
Show file tree
Hide file tree
Showing 17 changed files with 1,106 additions and 25 deletions.
21 changes: 15 additions & 6 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -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") {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/statement.js
Expand Up @@ -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 &&
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-parser/src/plugins/flow.js
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -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);
}

Expand Down
@@ -0,0 +1,4 @@
class C {
#x = 1;
#p = ({ #x: x }) => {}
}
@@ -0,0 +1,3 @@
{
"plugins": ["classPrivateProperties"]
}

0 comments on commit 81c5f1f

Please sign in to comment.