Implement class features in estree (#12370)

Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
This commit is contained in:
Huáng Jùnliàng
2021-02-21 14:12:12 -05:00
committed by GitHub
parent 9c567baa9b
commit 03d7911be6
26 changed files with 656 additions and 20 deletions

View File

@@ -1172,12 +1172,15 @@ export default class ExpressionParser extends LValParser {
node = (this.parseMaybePrivateName(true): N.PrivateName);
if (this.match(tt._in)) {
this.expectPlugin("privateIn");
this.classScope.usePrivateName(node.id.name, node.start);
this.classScope.usePrivateName(
this.getPrivateNameSV(node),
node.start,
);
} else if (this.hasPlugin("privateIn")) {
this.raise(
this.state.start,
Errors.PrivateInExpectedIn,
node.id.name,
this.getPrivateNameSV(node),
);
} else {
throw this.unexpected(start);

View File

@@ -194,6 +194,33 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
parseMaybePrivateName(...args: [boolean]): any {
const node = super.parseMaybePrivateName(...args);
if (node.type === "PrivateName") {
return this.convertPrivateNameToPrivateIdentifier(node);
}
return node;
}
convertPrivateNameToPrivateIdentifier(
node: N.PrivateName,
): N.EstreePrivateIdentifier {
const name = super.getPrivateNameSV(node);
node = (node: any);
delete node.id;
node.name = name;
node.type = "PrivateIdentifier";
return node;
}
isPrivateName(node: N.Node): boolean {
return node.type === "PrivateIdentifier";
}
getPrivateNameSV(node: N.Node): string {
return node.name;
}
parseLiteral<T: N.Literal>(
value: any,
type: /*T["kind"]*/ string,
@@ -240,11 +267,27 @@ export default (superClass: Class<Parser>): Class<Parser> =>
delete funcNode.kind;
// $FlowIgnore
node.value = funcNode;
type = type === "ClassMethod" ? "MethodDefinition" : type;
if (type === "ClassPrivateMethod") {
// $FlowIgnore
node.computed = false;
}
type = "MethodDefinition";
return this.finishNode(node, type);
}
parseClassProperty(...args: [N.ClassProperty]): any {
const propertyNode = (super.parseClassProperty(...args): any);
propertyNode.type = "PropertyDefinition";
return (propertyNode: N.EstreePropertyDefinition);
}
parseClassPrivateProperty(...args: [N.ClassPrivateProperty]): any {
const propertyNode = (super.parseClassPrivateProperty(...args): any);
propertyNode.type = "PropertyDefinition";
propertyNode.computed = false;
return (propertyNode: N.EstreePropertyDefinition);
}
parseObjectMethod(
prop: N.ObjectMethod,
isGenerator: boolean,

View File

@@ -2181,7 +2181,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (member.declare) {
if (
member.type !== "ClassProperty" &&
member.type !== "ClassPrivateProperty"
member.type !== "ClassPrivateProperty" &&
member.type !== "PropertyDefinition" // Used by estree plugin
) {
this.raise(pos, FlowErrors.DeclareClassElement);
} else if (member.value) {

View File

@@ -1077,6 +1077,19 @@ export type EstreeImportExpression = NodeBase & {
source: Expression,
};
export type EstreePrivateIdentifier = NodeBase & {
type: "PrivateIdentifier",
name: string,
};
export type EstreePropertyDefinition = NodeBase & {
type: "PropertyDefinition",
static: boolean,
key: Expression | EstreePrivateIdentifier,
computed: boolean,
value: Expression,
};
// === === === ===
// TypeScript
// === === === ===