Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for private class methods #4034

Merged
merged 3 commits into from Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions LICENSE.md
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions 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,
Expand Down Expand Up @@ -102,6 +103,7 @@ const getAcorn = (config: GenericConfigObject): acorn.Options => ({

const getAcornInjectPlugins = (config: GenericConfigObject): Function[] => [
injectClassFields,
injectPrivateMethods,
injectStaticClassFeatures,
...(ensureArray(config.acornInjectPlugins) as any)
];
Expand Down
22 changes: 10 additions & 12 deletions test/form/samples/class-fields/_expected.js
Expand Up @@ -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;
Expand Down
23 changes: 11 additions & 12 deletions test/form/samples/class-fields/main.js
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/function/samples/options-hook/_config.js
Expand Up @@ -15,7 +15,7 @@ module.exports = {
preserveParens: false,
sourceType: 'module'
},
acornInjectPlugins: [null, null],
acornInjectPlugins: [null, null, null],
context: 'undefined',
experimentalCacheExpiry: 10,
input: ['used'],
Expand Down
169 changes: 159 additions & 10 deletions typings/declarations.d.ts
Expand Up @@ -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;
}
Expand All @@ -27,15 +32,159 @@ declare module 'acorn-walk' {
callback: WalkerCallback<TState>
) => void;
export type BaseWalker<TState> = Record<string, RecursiveWalkerFn<TState>>;
export const base: BaseWalker<unknown>
export const base: BaseWalker<unknown>;
}

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;
};
}