Modify grammar to support Private Fields proposal: (#260)
* Modify grammar to support Private Fields proposal: - Adding optional plugin `classPrivateProperties` - Adding PrivateName type identifier - Adding ClassPrivateProperty to ClassBody - Allow PrivateName in MemberExpression - Allow PrivateName as a reference - Adding tests * Remove unnecesary liberal parameter * Guarding for plugin dependecy for future versioning * update spec.md [skip ci] * move comment [skip ci] * remove unused param [skip ci] * Refactor PrivateName to contain Identifier in name property
This commit is contained in:
committed by
Henry Zhu
parent
6c4acecf00
commit
01da62283c
@@ -650,6 +650,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
// class bodies are implicitly strict
|
||||
const oldStrict = this.state.strict;
|
||||
this.state.strict = true;
|
||||
this.state.inClass = true;
|
||||
|
||||
let hadConstructor = false;
|
||||
let decorators = [];
|
||||
@@ -680,6 +681,13 @@ export default class StatementParser extends ExpressionParser {
|
||||
decorators = [];
|
||||
}
|
||||
|
||||
if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) { // Private property
|
||||
this.next();
|
||||
this.parsePropertyName(method);
|
||||
classBody.body.push(this.parsePrivateClassProperty(method));
|
||||
continue;
|
||||
}
|
||||
|
||||
method.static = false;
|
||||
if (this.match(tt.name) && this.state.value === "static") {
|
||||
const key = this.parseIdentifier(true); // eats 'static'
|
||||
@@ -774,9 +782,26 @@ export default class StatementParser extends ExpressionParser {
|
||||
|
||||
node.body = this.finishNode(classBody, "ClassBody");
|
||||
|
||||
this.state.inClass = false;
|
||||
this.state.strict = oldStrict;
|
||||
|
||||
}
|
||||
|
||||
parsePrivateClassProperty(node: N.ClassPrivateProperty): N.ClassPrivateProperty {
|
||||
this.state.inClassProperty = true;
|
||||
|
||||
if (this.match(tt.eq)) {
|
||||
this.next();
|
||||
node.value = this.parseMaybeAssign();
|
||||
} else {
|
||||
node.value = null;
|
||||
}
|
||||
this.semicolon();
|
||||
this.state.inClassProperty = false;
|
||||
return this.finishNode(node, "ClassPrivateProperty");
|
||||
}
|
||||
|
||||
|
||||
parseClassProperty(node: N.ClassProperty): N.ClassProperty {
|
||||
const hasPlugin = this.hasPlugin("classProperties");
|
||||
const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled.";
|
||||
|
||||
Reference in New Issue
Block a user