Skip to content

Commit

Permalink
Implement class features in estree (#12370)
Browse files Browse the repository at this point in the history
Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
  • Loading branch information
JLHwung and kaicataldo committed Feb 21, 2021
1 parent 9c567ba commit 03d7911
Show file tree
Hide file tree
Showing 26 changed files with 656 additions and 20 deletions.
4 changes: 4 additions & 0 deletions eslint/babel-eslint-parser/src/analyze-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class Referencer extends OriginalReferencer {
this._visitClassProperty(node);
}

PropertyDefinition(node) {
this._visitClassProperty(node);
}

// TODO: Update to visit type annotations when TypeScript/Flow support this syntax.
ClassPrivateMethod(node) {
super.MethodDefinition(node);
Expand Down
6 changes: 5 additions & 1 deletion eslint/babel-eslint-parser/src/visitor-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ export const newTypes = {
Literal: ESLINT_VISITOR_KEYS.Literal,
MethodDefinition: ["decorators"].concat(ESLINT_VISITOR_KEYS.MethodDefinition),
Property: ["decorators"].concat(ESLINT_VISITOR_KEYS.Property),
// todo: remove this when Acorn supports class properties
PropertyDefinition: t.VISITOR_KEYS.ClassProperty,
// todo: remove this when Acorn supports class properties
PrivateIdentifier: [],
};

// AST Types that shares `"type"` property with Babel but have different shape
export const conflictTypes = {
// todo: remove this when class features are supported
// todo: remove this when we drop Babel 7 support
ClassPrivateMethod: ["decorators"].concat(
ESLINT_VISITOR_KEYS.MethodDefinition,
),
Expand Down
5 changes: 4 additions & 1 deletion eslint/babel-eslint-plugin/src/rules/no-invalid-this.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export default ruleComposer.filterReports(noInvalidThisRule, problem => {
if (
node.type === "ClassPrivateMethod" ||
node.type === "ClassPrivateProperty" ||
node.type === "ClassProperty"
node.type === "ClassProperty" ||
node.type === "PropertyDefinition" ||
(node.type === "MethodDefinition" &&
node.key.type === "PrivateIdentifier")
) {
inClassMember = true;
return;
Expand Down
2 changes: 1 addition & 1 deletion eslint/babel-eslint-plugin/src/rules/semi.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function report(context, node, missing) {
export default ruleComposer.joinReports([
rule,
context => ({
"ClassProperty, ClassPrivateProperty"(node) {
"ClassProperty, ClassPrivateProperty, PropertyDefinition"(node) {
const options = context.options[1];
const exceptOneLine = options && options.omitLastInOneLineBlock === true;
const sourceCode = context.getSourceCode();
Expand Down
34 changes: 34 additions & 0 deletions eslint/babel-eslint-tests/test/integration/eslint/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,15 @@ describe("verify", () => {
{ "no-unused-vars": 1 },
);
});

it("type annotations should work", () => {
verifyAndAssertMessages(
`class C {
#p: Array<number>
}`,
{ "no-undef": 1 },
);
});
});

describe("private methods", () => {
Expand Down Expand Up @@ -1794,6 +1803,31 @@ describe("verify", () => {
{ "no-unreachable": 1 },
);
});

it("should work with func-names", () => {
verifyAndAssertMessages(
`
export class C {
#d() {};
}
`,
{ "func-names": 1 },
);
});

it("should work with space-before-function-paren", () => {
verifyAndAssertMessages(
`
export class C {
#d() {};
}
`,
{ "space-before-function-paren": 1 },
[
"2:5 Missing space before function parentheses. space-before-function-paren",
],
);
});
});
});

Expand Down
7 changes: 5 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 45 additions & 2 deletions packages/babel-parser/src/plugins/estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-parser/src/plugins/flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-parser/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// === === === ===
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class A {
#foo(arg, ...others) {}
static #bar(arg, ...others) {}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"type": "File",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"start":0,"end":70,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"program": {
"type": "Program",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"start":0,"end":70,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"start":0,"end":70,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"},
Expand All @@ -18,20 +18,16 @@
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":37,"loc":{"start":{"line":1,"column":8},"end":{"line":3,"column":1}},
"start":8,"end":70,"loc":{"start":{"line":1,"column":8},"end":{"line":4,"column":1}},
"body": [
{
"type": "ClassPrivateMethod",
"type": "MethodDefinition",
"start":12,"end":35,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":25}},
"static": false,
"key": {
"type": "PrivateName",
"type": "PrivateIdentifier",
"start":12,"end":16,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6}},
"id": {
"type": "Identifier",
"start":13,"end":16,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":6},"identifierName":"foo"},
"name": "foo"
}
"name": "foo"
},
"kind": "method",
"value": {
Expand Down Expand Up @@ -62,7 +58,49 @@
"start":33,"end":35,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":25}},
"body": []
}
}
},
"computed": false
},
{
"type": "MethodDefinition",
"start":38,"end":68,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":32}},
"static": true,
"key": {
"type": "PrivateIdentifier",
"start":45,"end":49,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":13}},
"name": "bar"
},
"kind": "method",
"value": {
"type": "FunctionExpression",
"start":49,"end":68,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":32}},
"id": null,
"generator": false,
"async": false,
"expression": false,
"params": [
{
"type": "Identifier",
"start":50,"end":53,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":17},"identifierName":"arg"},
"name": "arg"
},
{
"type": "RestElement",
"start":55,"end":64,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":28}},
"argument": {
"type": "Identifier",
"start":58,"end":64,"loc":{"start":{"line":3,"column":22},"end":{"line":3,"column":28},"identifierName":"others"},
"name": "others"
}
}
],
"body": {
"type": "BlockStatement",
"start":66,"end":68,"loc":{"start":{"line":3,"column":30},"end":{"line":3,"column":32}},
"body": []
}
},
"computed": false
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class A {
#foo = "bar";
static #bar = foo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["estree", "classPrivateProperties"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"type": "File",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"program": {
"type": "Program",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":48,"loc":{"start":{"line":1,"column":8},"end":{"line":4,"column":1}},
"body": [
{
"type": "PropertyDefinition",
"start":12,"end":25,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":15}},
"static": false,
"key": {
"type": "PrivateIdentifier",
"start":12,"end":16,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6}},
"name": "foo"
},
"value": {
"type": "Literal",
"start":19,"end":24,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":14}},
"value": "bar",
"raw": "\"bar\""
},
"computed": false
},
{
"type": "PropertyDefinition",
"start":28,"end":46,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},
"static": true,
"key": {
"type": "PrivateIdentifier",
"start":35,"end":39,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":13}},
"name": "bar"
},
"value": {
"type": "Identifier",
"start":42,"end":45,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19},"identifierName":"foo"},
"name": "foo"
},
"computed": false
}
]
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A {
#foo = "bar";
static #bar = foo;
declare #qux: Array<string>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["flow", "estree", "classPrivateProperties"]
}

0 comments on commit 03d7911

Please sign in to comment.