From 35840edcbb9de250ea687d4c6f96ef2be57054fd Mon Sep 17 00:00:00 2001 From: dnalborczyk Date: Fri, 9 Apr 2021 00:34:11 -0400 Subject: [PATCH] feat: add support for private class methods (#4034) * feat: add support for private class methods * reactivate tests * Try to fix windows dependencies Co-authored-by: Lukas Taegert-Atkinson --- LICENSE.md | 27 +++ package-lock.json | 13 +- package.json | 3 +- src/utils/options/normalizeInputOptions.ts | 2 + test/form/samples/class-fields/_expected.js | 22 ++- test/form/samples/class-fields/main.js | 23 ++- test/function/samples/options-hook/_config.js | 2 +- typings/declarations.d.ts | 169 ++++++++++++++++-- 8 files changed, 223 insertions(+), 38 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 03e865a05b3..3a03706b1d2 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -111,6 +111,33 @@ Repository: https://github.com/acornjs/acorn-private-class-elements --------------------------------------- +## acorn-private-methods +License: MIT +By: Adrian Heine +Repository: https://github.com/acornjs/acorn-private-methods + +> Copyright (C) 2017-2018 by Adrian Heine +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. + +--------------------------------------- + ## acorn-static-class-features License: MIT By: Adrian Heine diff --git a/package-lock.json b/package-lock.json index 5d1f5a50bae..6e0b0e8e4b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -610,6 +610,15 @@ "integrity": "sha512-zYNcZtxKgVCg1brS39BEou86mIao1EV7eeREG+6WMwKbuYTeivRRs6S2XdWnboRde6G9wKh2w+WBydEyJsJ6mg==", "dev": true }, + "acorn-private-methods": { + "version": "1.0.0", + "resolved": "https://greenlight.jfrog.io/artifactory/api/npm/npm/acorn-private-methods/-/acorn-private-methods-1.0.0.tgz", + "integrity": "sha1-tI9MA6FRzIJi9vXKj1f3xawkUYQ=", + "dev": true, + "requires": { + "acorn-private-class-elements": "^1.0.0" + } + }, "acorn-static-class-features": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/acorn-static-class-features/-/acorn-static-class-features-1.0.0.tgz", @@ -2212,8 +2221,8 @@ "dev": true }, "is-reference": { - "version": "github:lukastaegert/is-reference#54335717efde771de5395d00d6a9edf2d557f2ff", - "from": "github:lukastaegert/is-reference#update-class-features", + "version": "git+https://github.com/lukastaegert/is-reference.git#54335717efde771de5395d00d6a9edf2d557f2ff", + "from": "git+https://github.com/lukastaegert/is-reference.git#update-class-features", "dev": true, "requires": { "@types/estree": "*" diff --git a/package.json b/package.json index c5df5a7a335..5810ae840db 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "acorn": "^8.0.5", "acorn-class-fields": "^1.0.0", "acorn-jsx": "^5.3.1", + "acorn-private-methods": "^1.0.0", "acorn-static-class-features": "^1.0.0", "acorn-walk": "^8.0.2", "buble": "^0.20.0", @@ -90,7 +91,7 @@ "fixturify": "^2.1.0", "hash.js": "^1.1.7", "husky": "^5.0.9", - "is-reference": "lukastaegert/is-reference#update-class-features", + "is-reference": "git+https://github.com/lukastaegert/is-reference.git#update-class-features", "lint-staged": "^10.5.4", "locate-character": "^2.0.5", "magic-string": "^0.25.7", diff --git a/src/utils/options/normalizeInputOptions.ts b/src/utils/options/normalizeInputOptions.ts index b85e848cc74..4d707964870 100644 --- a/src/utils/options/normalizeInputOptions.ts +++ b/src/utils/options/normalizeInputOptions.ts @@ -1,5 +1,6 @@ import * as acorn from 'acorn'; import injectClassFields from 'acorn-class-fields'; +import injectPrivateMethods from 'acorn-private-methods'; import injectStaticClassFeatures from 'acorn-static-class-features'; import { ExternalOption, @@ -102,6 +103,7 @@ const getAcorn = (config: GenericConfigObject): acorn.Options => ({ const getAcornInjectPlugins = (config: GenericConfigObject): Function[] => [ injectClassFields, + injectPrivateMethods, injectStaticClassFeatures, ...(ensureArray(config.acornInjectPlugins) as any) ]; diff --git a/test/form/samples/class-fields/_expected.js b/test/form/samples/class-fields/_expected.js index 38c3bd37922..a215dbd44cc 100644 --- a/test/form/samples/class-fields/_expected.js +++ b/test/form/samples/class-fields/_expected.js @@ -9,18 +9,16 @@ class Example extends SomeClass { #uninitializedPrivateField; - // Those are apparently unsupported at the moment - // get #getter() { - // return this.#uninitializedPrivateField; - // } - - // set #setter(value) { - // this.#uninitializedPrivateField = value; - // } - - // #privateMethod() { - // const foo = 'tree-shaken'; - // } + get #getter() { + return this.#uninitializedPrivateField; + } + + set #setter(value) { + this.#uninitializedPrivateField = value; + } + + #privateMethod() { + } static firstPublicStaticField = 2; static secondPublicStaticField = this.firstPublicStaticField + super.someStaticField; diff --git a/test/form/samples/class-fields/main.js b/test/form/samples/class-fields/main.js index bbece02c467..888aba32e55 100644 --- a/test/form/samples/class-fields/main.js +++ b/test/form/samples/class-fields/main.js @@ -10,18 +10,17 @@ class Example extends SomeClass { #uninitializedPrivateField; - // Those are apparently unsupported at the moment - // get #getter() { - // return this.#uninitializedPrivateField; - // } - - // set #setter(value) { - // this.#uninitializedPrivateField = value; - // } - - // #privateMethod() { - // const foo = 'tree-shaken'; - // } + get #getter() { + return this.#uninitializedPrivateField; + } + + set #setter(value) { + this.#uninitializedPrivateField = value; + } + + #privateMethod() { + const foo = 'tree-shaken'; + } static firstPublicStaticField = 2; static secondPublicStaticField = this.firstPublicStaticField + super.someStaticField; diff --git a/test/function/samples/options-hook/_config.js b/test/function/samples/options-hook/_config.js index 8c2289935d3..9c9f60220ee 100644 --- a/test/function/samples/options-hook/_config.js +++ b/test/function/samples/options-hook/_config.js @@ -15,7 +15,7 @@ module.exports = { preserveParens: false, sourceType: 'module' }, - acornInjectPlugins: [null, null], + acornInjectPlugins: [null, null, null], context: 'undefined', experimentalCacheExpiry: 10, input: ['used'], diff --git a/typings/declarations.d.ts b/typings/declarations.d.ts index 68e9ab66649..16e35c5db26 100644 --- a/typings/declarations.d.ts +++ b/typings/declarations.d.ts @@ -5,12 +5,17 @@ declare module 'help.md' { } // external libs -declare module 'acorn-static-class-features' { +declare module 'acorn-class-fields' { const plugin: (BaseParser: typeof acorn.Parser) => typeof acorn.Parser; export default plugin; } -declare module 'acorn-class-fields' { +declare module 'acorn-private-methods' { + const plugin: (BaseParser: typeof acorn.Parser) => typeof acorn.Parser; + export default plugin; +} + +declare module 'acorn-static-class-features' { const plugin: (BaseParser: typeof acorn.Parser) => typeof acorn.Parser; export default plugin; } @@ -27,15 +32,159 @@ declare module 'acorn-walk' { callback: WalkerCallback ) => void; export type BaseWalker = Record>; - export const base: BaseWalker + export const base: BaseWalker; } declare module 'is-reference' { - export default function is_reference(node: NodeWithFieldDefinition, parent: NodeWithFieldDefinition): any; - export type Node = import("estree").Identifier | import("estree").SimpleLiteral | import("estree").RegExpLiteral | import("estree").Program | import("estree").FunctionDeclaration | import("estree").FunctionExpression | import("estree").ArrowFunctionExpression | import("estree").SwitchCase | import("estree").CatchClause | import("estree").VariableDeclarator | import("estree").ExpressionStatement | import("estree").BlockStatement | import("estree").EmptyStatement | import("estree").DebuggerStatement | import("estree").WithStatement | import("estree").ReturnStatement | import("estree").LabeledStatement | import("estree").BreakStatement | import("estree").ContinueStatement | import("estree").IfStatement | import("estree").SwitchStatement | import("estree").ThrowStatement | import("estree").TryStatement | import("estree").WhileStatement | import("estree").DoWhileStatement | import("estree").ForStatement | import("estree").ForInStatement | import("estree").ForOfStatement | import("estree").VariableDeclaration | import("estree").ClassDeclaration | import("estree").ThisExpression | import("estree").ArrayExpression | import("estree").ObjectExpression | import("estree").YieldExpression | import("estree").UnaryExpression | import("estree").UpdateExpression | import("estree").BinaryExpression | import("estree").AssignmentExpression | import("estree").LogicalExpression | import("estree").MemberExpression | import("estree").ConditionalExpression | import("estree").SimpleCallExpression | import("estree").NewExpression | import("estree").SequenceExpression | import("estree").TemplateLiteral | import("estree").TaggedTemplateExpression | import("estree").ClassExpression | import("estree").MetaProperty | import("estree").AwaitExpression | import("estree").ImportExpression | import("estree").ChainExpression | import("estree").Property | import("estree").AssignmentProperty | import("estree").Super | import("estree").TemplateElement | import("estree").SpreadElement | import("estree").ObjectPattern | import("estree").ArrayPattern | import("estree").RestElement | import("estree").AssignmentPattern | import("estree").ClassBody | import("estree").MethodDefinition | import("estree").ImportDeclaration | import("estree").ExportNamedDeclaration | import("estree").ExportDefaultDeclaration | import("estree").ExportAllDeclaration | import("estree").ImportSpecifier | import("estree").ImportDefaultSpecifier | import("estree").ImportNamespaceSpecifier | import("estree").ExportSpecifier; - export type NodeWithFieldDefinition = import("estree").Identifier | import("estree").SimpleLiteral | import("estree").RegExpLiteral | import("estree").Program | import("estree").FunctionDeclaration | import("estree").FunctionExpression | import("estree").ArrowFunctionExpression | import("estree").SwitchCase | import("estree").CatchClause | import("estree").VariableDeclarator | import("estree").ExpressionStatement | import("estree").BlockStatement | import("estree").EmptyStatement | import("estree").DebuggerStatement | import("estree").WithStatement | import("estree").ReturnStatement | import("estree").LabeledStatement | import("estree").BreakStatement | import("estree").ContinueStatement | import("estree").IfStatement | import("estree").SwitchStatement | import("estree").ThrowStatement | import("estree").TryStatement | import("estree").WhileStatement | import("estree").DoWhileStatement | import("estree").ForStatement | import("estree").ForInStatement | import("estree").ForOfStatement | import("estree").VariableDeclaration | import("estree").ClassDeclaration | import("estree").ThisExpression | import("estree").ArrayExpression | import("estree").ObjectExpression | import("estree").YieldExpression | import("estree").UnaryExpression | import("estree").UpdateExpression | import("estree").BinaryExpression | import("estree").AssignmentExpression | import("estree").LogicalExpression | import("estree").MemberExpression | import("estree").ConditionalExpression | import("estree").SimpleCallExpression | import("estree").NewExpression | import("estree").SequenceExpression | import("estree").TemplateLiteral | import("estree").TaggedTemplateExpression | import("estree").ClassExpression | import("estree").MetaProperty | import("estree").AwaitExpression | import("estree").ImportExpression | import("estree").ChainExpression | import("estree").Property | import("estree").AssignmentProperty | import("estree").Super | import("estree").TemplateElement | import("estree").SpreadElement | import("estree").ObjectPattern | import("estree").ArrayPattern | import("estree").RestElement | import("estree").AssignmentPattern | import("estree").ClassBody | import("estree").MethodDefinition | import("estree").ImportDeclaration | import("estree").ExportNamedDeclaration | import("estree").ExportDefaultDeclaration | import("estree").ExportAllDeclaration | import("estree").ImportSpecifier | import("estree").ImportDefaultSpecifier | import("estree").ImportNamespaceSpecifier | import("estree").ExportSpecifier | { - computed: boolean; - type: 'FieldDefinition'; - value: Node; - }; + export default function is_reference( + node: NodeWithFieldDefinition, + parent: NodeWithFieldDefinition + ): any; + export type Node = + | import('estree').Identifier + | import('estree').SimpleLiteral + | import('estree').RegExpLiteral + | import('estree').Program + | import('estree').FunctionDeclaration + | import('estree').FunctionExpression + | import('estree').ArrowFunctionExpression + | import('estree').SwitchCase + | import('estree').CatchClause + | import('estree').VariableDeclarator + | import('estree').ExpressionStatement + | import('estree').BlockStatement + | import('estree').EmptyStatement + | import('estree').DebuggerStatement + | import('estree').WithStatement + | import('estree').ReturnStatement + | import('estree').LabeledStatement + | import('estree').BreakStatement + | import('estree').ContinueStatement + | import('estree').IfStatement + | import('estree').SwitchStatement + | import('estree').ThrowStatement + | import('estree').TryStatement + | import('estree').WhileStatement + | import('estree').DoWhileStatement + | import('estree').ForStatement + | import('estree').ForInStatement + | import('estree').ForOfStatement + | import('estree').VariableDeclaration + | import('estree').ClassDeclaration + | import('estree').ThisExpression + | import('estree').ArrayExpression + | import('estree').ObjectExpression + | import('estree').YieldExpression + | import('estree').UnaryExpression + | import('estree').UpdateExpression + | import('estree').BinaryExpression + | import('estree').AssignmentExpression + | import('estree').LogicalExpression + | import('estree').MemberExpression + | import('estree').ConditionalExpression + | import('estree').SimpleCallExpression + | import('estree').NewExpression + | import('estree').SequenceExpression + | import('estree').TemplateLiteral + | import('estree').TaggedTemplateExpression + | import('estree').ClassExpression + | import('estree').MetaProperty + | import('estree').AwaitExpression + | import('estree').ImportExpression + | import('estree').ChainExpression + | import('estree').Property + | import('estree').AssignmentProperty + | import('estree').Super + | import('estree').TemplateElement + | import('estree').SpreadElement + | import('estree').ObjectPattern + | import('estree').ArrayPattern + | import('estree').RestElement + | import('estree').AssignmentPattern + | import('estree').ClassBody + | import('estree').MethodDefinition + | import('estree').ImportDeclaration + | import('estree').ExportNamedDeclaration + | import('estree').ExportDefaultDeclaration + | import('estree').ExportAllDeclaration + | import('estree').ImportSpecifier + | import('estree').ImportDefaultSpecifier + | import('estree').ImportNamespaceSpecifier + | import('estree').ExportSpecifier; + export type NodeWithFieldDefinition = + | import('estree').Identifier + | import('estree').SimpleLiteral + | import('estree').RegExpLiteral + | import('estree').Program + | import('estree').FunctionDeclaration + | import('estree').FunctionExpression + | import('estree').ArrowFunctionExpression + | import('estree').SwitchCase + | import('estree').CatchClause + | import('estree').VariableDeclarator + | import('estree').ExpressionStatement + | import('estree').BlockStatement + | import('estree').EmptyStatement + | import('estree').DebuggerStatement + | import('estree').WithStatement + | import('estree').ReturnStatement + | import('estree').LabeledStatement + | import('estree').BreakStatement + | import('estree').ContinueStatement + | import('estree').IfStatement + | import('estree').SwitchStatement + | import('estree').ThrowStatement + | import('estree').TryStatement + | import('estree').WhileStatement + | import('estree').DoWhileStatement + | import('estree').ForStatement + | import('estree').ForInStatement + | import('estree').ForOfStatement + | import('estree').VariableDeclaration + | import('estree').ClassDeclaration + | import('estree').ThisExpression + | import('estree').ArrayExpression + | import('estree').ObjectExpression + | import('estree').YieldExpression + | import('estree').UnaryExpression + | import('estree').UpdateExpression + | import('estree').BinaryExpression + | import('estree').AssignmentExpression + | import('estree').LogicalExpression + | import('estree').MemberExpression + | import('estree').ConditionalExpression + | import('estree').SimpleCallExpression + | import('estree').NewExpression + | import('estree').SequenceExpression + | import('estree').TemplateLiteral + | import('estree').TaggedTemplateExpression + | import('estree').ClassExpression + | import('estree').MetaProperty + | import('estree').AwaitExpression + | import('estree').ImportExpression + | import('estree').ChainExpression + | import('estree').Property + | import('estree').AssignmentProperty + | import('estree').Super + | import('estree').TemplateElement + | import('estree').SpreadElement + | import('estree').ObjectPattern + | import('estree').ArrayPattern + | import('estree').RestElement + | import('estree').AssignmentPattern + | import('estree').ClassBody + | import('estree').MethodDefinition + | import('estree').ImportDeclaration + | import('estree').ExportNamedDeclaration + | import('estree').ExportDefaultDeclaration + | import('estree').ExportAllDeclaration + | import('estree').ImportSpecifier + | import('estree').ImportDefaultSpecifier + | import('estree').ImportNamespaceSpecifier + | import('estree').ExportSpecifier + | { + computed: boolean; + type: 'FieldDefinition'; + value: Node; + }; }