Skip to content

Commit

Permalink
fix: disallow private name in object member and TS type elements
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jan 11, 2020
1 parent 903ad61 commit b836d28
Show file tree
Hide file tree
Showing 16 changed files with 1,107 additions and 11 deletions.
23 changes: 16 additions & 7 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 name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or 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 @@ -1779,7 +1787,8 @@ export default class ExpressionParser extends LValParser {
}

parsePropertyName(
prop: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase,
prop: N.ObjectOrClassMember | 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
5 changes: 3 additions & 2 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2265,10 +2265,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

parsePropertyName(
node: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase,
node: N.ObjectOrClassMember | 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 b836d28

Please sign in to comment.