Skip to content

Commit

Permalink
Allowing comma separated fields
Browse files Browse the repository at this point in the history
- Fixes babel#540
- Fixes location for publicFields (should not contain semi-colon)
- Allow decorators for comma separated fields
- Added tests
  • Loading branch information
Diego committed Jul 1, 2017
1 parent 5e1e949 commit c2068d1
Show file tree
Hide file tree
Showing 36 changed files with 1,802 additions and 137 deletions.
29 changes: 24 additions & 5 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,9 @@ export default class StatementParser extends ExpressionParser {

while (!this.eat(tt.braceR)) {
if (this.eat(tt.semi)) {
if (this.state.inCommaSeparatedFields) {
this.unexpected();
}
if (decorators.length > 0) {
this.raise(
this.state.lastTokEnd,
Expand All @@ -881,10 +884,26 @@ export default class StatementParser extends ExpressionParser {
if (this.hasPlugin("decorators2")) {
this.resetStartLocationFromNode(member, decorators[0]);
}
decorators = [];
}

this.parseClassMember(classBody, member, state);
const isClassField = member.kind == null;

if (isClassField) {
if (this.eat(tt.comma)) {
this.state.inCommaSeparatedFields = true;
// Copy decorators for cases like `@foo x, y;`
if (decorators.length) {
member.decorators = decorators;
}
} else {
decorators = [];
this.state.inCommaSeparatedFields = false;
this.semicolon();
}
} else {
decorators = [];
}

if (
this.hasPlugin("decorators2") &&
Expand All @@ -899,7 +918,7 @@ export default class StatementParser extends ExpressionParser {
}
}

if (decorators.length) {
if (!this.state.inCommaSeparatedFields && decorators.length) {
this.raise(
this.state.start,
"You have trailing decorators with no method",
Expand Down Expand Up @@ -1071,7 +1090,7 @@ export default class StatementParser extends ExpressionParser {
/* isConstructor */ false,
);
this.checkGetterSetterParamCount(method);
} else if (this.isLineTerminator()) {
} else if (this.isLineTerminator() || this.match(tt.comma)) {
// an uninitialized class property (due to ASI, since we don't otherwise recognize the next token)
if (this.isNonstaticConstructor(prop)) {
this.raise(
Expand Down Expand Up @@ -1135,7 +1154,7 @@ export default class StatementParser extends ExpressionParser {
} else {
node.value = null;
}
this.semicolon();

this.state.inClassProperty = false;
return this.finishNode(node, "ClassPrivateProperty");
}
Expand All @@ -1158,7 +1177,7 @@ export default class StatementParser extends ExpressionParser {
} else {
node.value = null;
}
this.semicolon();

this.state.inClassProperty = false;
return this.finishNode(node, "ClassProperty");
}
Expand Down
1 change: 1 addition & 0 deletions src/tokenizer/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default class State {

// Check whether we are in a (nested) class or not.
classLevel: number;
inCommaSeparatedFields: boolean;

// Labels in scope.
labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class C {
#p1,#p2;
#p3 = 1, #p4 = 1;
#p5 = 1, #p6, #p7;
}
Loading

0 comments on commit c2068d1

Please sign in to comment.