Skip to content

Commit

Permalink
fix: do not transform ClassPrivateMethods in estree
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 17, 2020
1 parent 028a051 commit 8b36af8
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
22 changes: 22 additions & 0 deletions eslint/babel-eslint-tests/test/integration/eslint/eslint.js
Expand Up @@ -1758,6 +1758,28 @@ describe("verify", () => {
{ "no-unused-vars": 1 },
);
});

it("should visit params", () => {
verifyAndAssertMessages(
`export class C {
constructor() { this.#d(); }
#d(unused) {};
}
`,
{ "no-unused-vars": 1 },
);
});

it("should visit body", () => {
verifyAndAssertMessages(
`export class C {
constructor() { this.#d(); }
#d() { var unused; };
}
`,
{ "no-unused-vars": 1 },
);
});
});
});

Expand Down
14 changes: 14 additions & 0 deletions packages/babel-parser/src/plugins/estree.js
Expand Up @@ -287,6 +287,19 @@ export default (superClass: Class<Parser>): Class<Parser> =>
type: string,
inClassScope: boolean = false,
): T {
if (type === "ClassPrivateMethod") {
// todo: supports ClassPrivateMethod
// see https://github.com/estree/estree/blob/master/experimental/class-features.md
return super.parseMethod(
node,
isGenerator,
isAsync,
isConstructor,
allowDirectSuper,
type,
inClassScope,
);
}
let funcNode = this.startNode();
funcNode.kind = node.kind; // provide kind, so super method correctly sets state
funcNode = super.parseMethod(
Expand All @@ -298,6 +311,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
type,
inClassScope,
);

funcNode.type = "FunctionExpression";
delete funcNode.kind;
// $FlowIgnore
Expand Down
@@ -0,0 +1,3 @@
class A {
#foo(arg, ...others) {}
}
@@ -0,0 +1,3 @@
{
"plugins": ["flow", "jsx", "estree", "classPrivateMethods"]
}
@@ -0,0 +1,68 @@
{
"type": "File",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"program": {
"type": "Program",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"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":37,"loc":{"start":{"line":1,"column":8},"end":{"line":3,"column":1}},
"body": [
{
"type": "ClassPrivateMethod",
"start":12,"end":35,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":25}},
"static": false,
"key": {
"type": "PrivateName",
"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"
}
},
"kind": "method",
"id": null,
"generator": false,
"async": false,
"expression": false,
"params": [
{
"type": "Identifier",
"start":17,"end":20,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":10},"identifierName":"arg"},
"name": "arg"
},
{
"type": "RestElement",
"start":22,"end":31,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":21}},
"argument": {
"type": "Identifier",
"start":25,"end":31,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":21},"identifierName":"others"},
"name": "others"
}
}
],
"body": {
"type": "BlockStatement",
"start":33,"end":35,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":25}},
"body": []
}
}
]
}
}
]
}
}

0 comments on commit 8b36af8

Please sign in to comment.