Skip to content

Commit

Permalink
fix: private name can not be accessed in destructuring patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jan 10, 2020
1 parent ae06baf commit 634b4df
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 7 deletions.
19 changes: 13 additions & 6 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ export default class ExpressionParser extends LValParser {
? this.parseExpression()
: optional
? this.parseIdentifier(true)
: this.parseMaybePrivateName();
: this.parseMaybePrivateName(/* isPattern */ false);
node.computed = computed;

if (node.property.type === "PrivateName") {
Expand Down Expand Up @@ -1131,11 +1131,17 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "BooleanLiteral");
}

parseMaybePrivateName(): N.PrivateName | N.Identifier {
parseMaybePrivateName(isPattern: boolean): N.PrivateName | N.Identifier {
const isPrivate = this.match(tt.hash);

if (isPrivate) {
this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
if (isPattern) {
this.raise(
this.state.pos,
"Private name can not be accessed in object destructuring pattern",
);
}
const node = this.startNode();
this.next();
this.assertNoSpace("Unexpected space between # and identifier");
Expand Down Expand Up @@ -1596,12 +1602,12 @@ export default class ExpressionParser extends LValParser {
}

const containsEsc = this.state.containsEsc;
this.parsePropertyName(prop);
this.parsePropertyName(prop, isPattern);

if (!isPattern && !containsEsc && !isGenerator && this.isAsyncProp(prop)) {
isAsync = true;
isGenerator = this.eat(tt.star);
this.parsePropertyName(prop);
this.parsePropertyName(prop, /* isPattern */ false);
} else {
isAsync = false;
}
Expand Down Expand Up @@ -1688,7 +1694,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, /* isPattern */ false);
this.parseMethod(
prop,
/* isGenerator */ false,
Expand Down Expand Up @@ -1780,6 +1786,7 @@ export default class ExpressionParser extends LValParser {

parsePropertyName(
prop: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase,
isPattern: boolean,
): N.Expression | N.Identifier {
if (this.eat(tt.bracketL)) {
(prop: $FlowSubtype<N.ObjectOrClassMember>).computed = true;
Expand All @@ -1792,7 +1799,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(isPattern);

if (prop.key.type !== "PrivateName") {
// ClassPrivateProperty is never computed, so we don't assign in that case.
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-parser/src/parser/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export default class LValParser extends NodeUtils {
break;

case "ObjectProperty":
if (node.key.type === "PrivateName") {
this.raise(
node.key.start,
"Private name can not be accessed in object destructuring pattern",
);
}
this.toAssignable(node.value, isBinding, contextDescription);
break;

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/statement.js
Original file line number Diff line number Diff line change
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, /* isPattern */ false);

if (
!member.computed &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class C {
#x = 1;
#p = ({ #x: x }) => {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["classPrivateProperties"]
}

0 comments on commit 634b4df

Please sign in to comment.