Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow private name in object elements and TS type elements #10980

Merged
merged 7 commits into from Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"]
}