From 118e8e0acc816265426d678dc9e046dd79eb4a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 17 Aug 2020 14:28:51 -0400 Subject: [PATCH] fix: do not transform ClassPrivateMethods in estree --- .../test/integration/eslint/eslint.js | 22 ++++++ packages/babel-parser/src/plugins/estree.js | 13 ++++ .../class-private-method/basic/input.js | 3 + .../class-private-method/basic/options.json | 3 + .../class-private-method/basic/output.json | 68 +++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/estree/class-private-method/basic/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/class-private-method/basic/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/class-private-method/basic/output.json diff --git a/eslint/babel-eslint-tests/test/integration/eslint/eslint.js b/eslint/babel-eslint-tests/test/integration/eslint/eslint.js index 5a8ed44b23b9..533bd6c7d837 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/eslint.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/eslint.js @@ -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 }, + ); + }); }); }); diff --git a/packages/babel-parser/src/plugins/estree.js b/packages/babel-parser/src/plugins/estree.js index 579f45e0b279..5c4252ed642a 100644 --- a/packages/babel-parser/src/plugins/estree.js +++ b/packages/babel-parser/src/plugins/estree.js @@ -287,6 +287,19 @@ export default (superClass: Class): Class => 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( diff --git a/packages/babel-parser/test/fixtures/estree/class-private-method/basic/input.js b/packages/babel-parser/test/fixtures/estree/class-private-method/basic/input.js new file mode 100644 index 000000000000..83ead2348fae --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/class-private-method/basic/input.js @@ -0,0 +1,3 @@ +class A { + #foo(arg, ...others) {} +} diff --git a/packages/babel-parser/test/fixtures/estree/class-private-method/basic/options.json b/packages/babel-parser/test/fixtures/estree/class-private-method/basic/options.json new file mode 100644 index 000000000000..9a3d6c4ac482 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/class-private-method/basic/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["flow", "jsx", "estree", "classPrivateMethods"] +} diff --git a/packages/babel-parser/test/fixtures/estree/class-private-method/basic/output.json b/packages/babel-parser/test/fixtures/estree/class-private-method/basic/output.json new file mode 100644 index 000000000000..71f9a7512d4b --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/class-private-method/basic/output.json @@ -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": [] + } + } + ] + } + } + ] + } +} \ No newline at end of file