rolldown-plugin-access-privates 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -23
- package/dist/index.d.mts +52 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +98 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +24 -27
- package/src/index.ts +148 -144
- package/dist/index.d.ts +0 -38
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -124
- package/dist/index.js.map +0 -1
- package/rolldown-plugin-access-privates.code-workspace +0 -8
package/README.md
CHANGED
|
@@ -44,28 +44,14 @@ type options = {
|
|
|
44
44
|
* declaration.
|
|
45
45
|
* @default true
|
|
46
46
|
*/
|
|
47
|
-
exports?:
|
|
48
|
-
| ("variable" | "function" | "class")[]
|
|
49
|
-
| boolean
|
|
50
|
-
| ((
|
|
51
|
-
id: string,
|
|
52
|
-
astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class,
|
|
53
|
-
) => boolean)
|
|
54
|
-
| undefined;
|
|
47
|
+
exports?: ("variable" | "function" | "class")[] | boolean | ((id: string, astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class) => boolean) | undefined;
|
|
55
48
|
/**
|
|
56
49
|
* Which class members to generate accessors for.
|
|
57
50
|
* Can be an array of "method", "get", "set", and "property", or a function that will
|
|
58
51
|
* be called with the module ID and AST node of each method or property definition.
|
|
59
52
|
* @default true
|
|
60
53
|
*/
|
|
61
|
-
classMembers?:
|
|
62
|
-
| ("method" | "get" | "set" | "property")[]
|
|
63
|
-
| boolean
|
|
64
|
-
| ((
|
|
65
|
-
id: string,
|
|
66
|
-
astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition,
|
|
67
|
-
) => boolean)
|
|
68
|
-
| undefined;
|
|
54
|
+
classMembers?: ("method" | "get" | "set" | "property")[] | boolean | ((id: string, astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition) => boolean) | undefined;
|
|
69
55
|
/**
|
|
70
56
|
* The suffix to use for the generated accessors.
|
|
71
57
|
* For example, if you have a private field `#foo`, the plugin will generate
|
|
@@ -120,10 +106,14 @@ type options = {
|
|
|
120
106
|
* }}
|
|
121
107
|
* ```
|
|
122
108
|
*/
|
|
123
|
-
type GeneralHookFilter =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
109
|
+
type GeneralHookFilter =
|
|
110
|
+
| string
|
|
111
|
+
| RegExp
|
|
112
|
+
| (string | RegExp)[]
|
|
113
|
+
| {
|
|
114
|
+
include?: string | RegExp | (string | RegExp)[];
|
|
115
|
+
exclude?: string | RegExp | (string | RegExp)[];
|
|
116
|
+
};
|
|
127
117
|
````
|
|
128
118
|
|
|
129
119
|
## Example
|
|
@@ -174,9 +164,6 @@ export class Counter {
|
|
|
174
164
|
get resetPrivate() {
|
|
175
165
|
return this.#reset;
|
|
176
166
|
}
|
|
177
|
-
set resetPrivate(value) {
|
|
178
|
-
this.#reset = value;
|
|
179
|
-
}
|
|
180
167
|
inc() {
|
|
181
168
|
this.#value++;
|
|
182
169
|
globalCounter++;
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { HookFilter, Plugin } from "rolldown";
|
|
2
|
+
import { ESTree } from "rolldown/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
7
|
+
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
8
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
9
|
+
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Options for the plugin.
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
declare function AccessPrivates({
|
|
15
|
+
exports,
|
|
16
|
+
classMembers,
|
|
17
|
+
suffix,
|
|
18
|
+
idFilter
|
|
19
|
+
}?: {
|
|
20
|
+
/**
|
|
21
|
+
* Whether to export all top-level variables, functions, and classes in the module.
|
|
22
|
+
* Can be an array of "variable", "function", and "class", or a function that will be called with the module ID and AST node of each variable, function, or class declaration.
|
|
23
|
+
*
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
exports?: ("variable" | "function" | "class")[] | boolean | ((id: string, astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class) => boolean) | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Which class members to generate accessors for.
|
|
29
|
+
* Can be an array of "method", "get", "set", and "property", or a function that will be called with the module ID and AST node of each method or property definition.
|
|
30
|
+
*
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
classMembers?: ("method" | "get" | "set" | "property")[] | boolean | ((id: string, astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition) => boolean) | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* The suffix to use for the generated accessors.
|
|
36
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
37
|
+
*
|
|
38
|
+
* @default "Private"
|
|
39
|
+
*/
|
|
40
|
+
suffix?: string | ((name: string) => string) | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Filter for which modules the plugin should apply.
|
|
43
|
+
* Can be a string, a RegExp, an array of strings and RegExps, or an object with an `include` and `exclude` property, each of which can be a string, a RegExp, or an array of strings and RegExps.
|
|
44
|
+
* The filter will be applied to the module ID.
|
|
45
|
+
*
|
|
46
|
+
* @default /\.[jt]sx?$/
|
|
47
|
+
*/
|
|
48
|
+
idFilter?: HookFilter["id"] | undefined;
|
|
49
|
+
}): Plugin;
|
|
50
|
+
//#endregion
|
|
51
|
+
export { AccessPrivates as default };
|
|
52
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;AAE0E;;;;;;;iBAkBlD,cAAA,CAAA;EACtB,OAAA;EACA,YAAA;EACA,MAAA;EACA;AAAA;EAQgH;;;;;;EAAhH,OAAA,uDAA8D,EAAA,UAAY,OAAA,EAAS,MAAA,CAAO,mBAAA,GAAsB,MAAA,CAAO,QAAA,GAAW,MAAA,CAAO,KAAA;EAuB5H;;;;;;EAhBb,YAAA,2DAAuE,EAAA,UAAY,OAAA,EAAS,MAAA,CAAO,gBAAA,GAAmB,MAAA,CAAO,kBAAA;EAf7H;;;;;;EAsBA,MAAA,cAAoB,IAAA;EAdmG;;;;;;;EAsBvH,QAAA,GAAW,UAAA;AAAA,IACJ,MAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import "rolldown";
|
|
2
|
+
import { withMagicString } from "rolldown-string";
|
|
3
|
+
import { Visitor } from "rolldown/utils";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/** Map exports types to their corresponding AST node types. */
|
|
6
|
+
const exportsToType = {
|
|
7
|
+
variable: "VariableDeclaration",
|
|
8
|
+
function: "FunctionDeclaration",
|
|
9
|
+
class: "ClassDeclaration"
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
13
|
+
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
14
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
15
|
+
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
16
|
+
*
|
|
17
|
+
* @param options - Options for the plugin.
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
function AccessPrivates({ exports = true, classMembers = true, suffix = "Private", idFilter = /\.[jt]sx?$/ } = {}) {
|
|
21
|
+
const canNotExport = exports === false || Array.isArray(exports) && exports.length === 0;
|
|
22
|
+
const canNotPropertyDefinition = classMembers === false || Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.includes("property"));
|
|
23
|
+
const canNotMethodDefinition = classMembers === false || Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.some((m) => m === "method" || m === "get" || m === "set"));
|
|
24
|
+
if (typeof exports === "boolean") {
|
|
25
|
+
const shouldExport = exports;
|
|
26
|
+
exports = () => shouldExport;
|
|
27
|
+
} else if (Array.isArray(exports)) {
|
|
28
|
+
const mappedExports = exports.map((e) => exportsToType[e]);
|
|
29
|
+
exports = (_id, astNode) => mappedExports.includes(astNode.type);
|
|
30
|
+
}
|
|
31
|
+
if (typeof classMembers === "boolean") {
|
|
32
|
+
const shouldGenerate = classMembers;
|
|
33
|
+
classMembers = () => shouldGenerate;
|
|
34
|
+
} else if (Array.isArray(classMembers)) {
|
|
35
|
+
const members = classMembers;
|
|
36
|
+
classMembers = (_id, astNode) => {
|
|
37
|
+
if (astNode.type === "PropertyDefinition") return members.includes("property");
|
|
38
|
+
if (astNode.type === "MethodDefinition") {
|
|
39
|
+
if (astNode.kind === "method") return members.includes("method");
|
|
40
|
+
if (astNode.kind === "get") return members.includes("get");
|
|
41
|
+
if (astNode.kind === "set") return members.includes("set");
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (typeof suffix === "string") {
|
|
47
|
+
const suffixString = suffix;
|
|
48
|
+
suffix = (name) => name + suffixString;
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
name: "rolldown-plugin-access-privates",
|
|
52
|
+
transform: {
|
|
53
|
+
filter: {
|
|
54
|
+
id: idFilter,
|
|
55
|
+
...canNotExport ? { code: "#" } : {}
|
|
56
|
+
},
|
|
57
|
+
handler: withMagicString(function(code, id, meta) {
|
|
58
|
+
let ast = meta.ast !== void 0 ? meta.ast : this.parse(code.original, { lang: id.endsWith(".tsx") ? "tsx" : id.endsWith(".ts") ? "ts" : id.endsWith(".jsx") ? "jsx" : "js" });
|
|
59
|
+
const visitors = {};
|
|
60
|
+
if (!canNotExport) visitors.Program = function(node) {
|
|
61
|
+
for (const child of node.body) {
|
|
62
|
+
if (child.type !== "VariableDeclaration" && child.type !== "FunctionDeclaration" && child.type !== "ClassDeclaration" || child.declare) continue;
|
|
63
|
+
if (!exports(id, child)) continue;
|
|
64
|
+
code.appendLeft(child.start, "export ");
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
if (!canNotPropertyDefinition) visitors.PropertyDefinition = function(node) {
|
|
68
|
+
if (node.type !== "PropertyDefinition" || node.key.type !== "PrivateIdentifier" || node.declare || node.override) return;
|
|
69
|
+
if (!classMembers(id, node)) return;
|
|
70
|
+
const name = suffix(node.key.name);
|
|
71
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
72
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
73
|
+
};
|
|
74
|
+
if (!canNotMethodDefinition) visitors.MethodDefinition = function(node) {
|
|
75
|
+
if (node.type !== "MethodDefinition" || node.key.type !== "PrivateIdentifier" || node.override) return;
|
|
76
|
+
if (!classMembers(id, node)) return;
|
|
77
|
+
const name = suffix(node.key.name);
|
|
78
|
+
switch (node.kind) {
|
|
79
|
+
case "method":
|
|
80
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
81
|
+
break;
|
|
82
|
+
case "get":
|
|
83
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
84
|
+
break;
|
|
85
|
+
case "set":
|
|
86
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
new Visitor(visitors).visit(ast);
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { AccessPrivates as default };
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { type HookFilter, type Plugin } from \"rolldown\";\nimport { withMagicString } from \"rolldown-string\";\nimport { Visitor, type ESTree, type VisitorObject } from \"rolldown/utils\";\n\n/** Map exports types to their corresponding AST node types. */\nconst exportsToType = {\n variable: \"VariableDeclaration\",\n function: \"FunctionDeclaration\",\n class: \"ClassDeclaration\",\n} as const;\n\n/**\n * A Vite plugin to generate accessors for private fields to allow testing them.\n * This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.\n * For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.\n * Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.\n *\n * @param options - Options for the plugin.\n * @returns\n */\nexport default function AccessPrivates({\n exports = true,\n classMembers = true,\n suffix = \"Private\",\n idFilter = /\\.[jt]sx?$/,\n}: {\n /**\n * Whether to export all top-level variables, functions, and classes in the module.\n * Can be an array of \"variable\", \"function\", and \"class\", or a function that will be called with the module ID and AST node of each variable, function, or class declaration.\n *\n * @default true\n */\n exports?: (\"variable\" | \"function\" | \"class\")[] | boolean | ((id: string, astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class) => boolean) | undefined;\n /**\n * Which class members to generate accessors for.\n * Can be an array of \"method\", \"get\", \"set\", and \"property\", or a function that will be called with the module ID and AST node of each method or property definition.\n *\n * @default true\n */\n classMembers?: (\"method\" | \"get\" | \"set\" | \"property\")[] | boolean | ((id: string, astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition) => boolean) | undefined;\n /**\n * The suffix to use for the generated accessors.\n * For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.\n *\n * @default \"Private\"\n */\n suffix?: string | ((name: string) => string) | undefined;\n /**\n * Filter for which modules the plugin should apply.\n * Can be a string, a RegExp, an array of strings and RegExps, or an object with an `include` and `exclude` property, each of which can be a string, a RegExp, or an array of strings and RegExps.\n * The filter will be applied to the module ID.\n *\n * @default /\\.[jt]sx?$/\n */\n idFilter?: HookFilter[\"id\"] | undefined;\n} = {}): Plugin {\n // Calculate which code path can be optimized.\n const canNotExport = exports === false || (Array.isArray(exports) && exports.length === 0);\n const canNotPropertyDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.includes(\"property\")));\n const canNotMethodDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.some((m) => m === \"method\" || m === \"get\" || m === \"set\")));\n // Transform filters in to a unified format\n if (typeof exports === \"boolean\") {\n const shouldExport = exports;\n exports = () => shouldExport;\n } else if (Array.isArray(exports)) {\n const mappedExports: ESTree.Node[\"type\"][] = exports.map((e) => exportsToType[e]);\n exports = (_id, astNode) => mappedExports.includes(astNode.type);\n }\n if (typeof classMembers === \"boolean\") {\n const shouldGenerate = classMembers;\n classMembers = () => shouldGenerate;\n } else if (Array.isArray(classMembers)) {\n const members = classMembers;\n classMembers = (_id, astNode) => {\n if (astNode.type === \"PropertyDefinition\") return members.includes(\"property\");\n if (astNode.type === \"MethodDefinition\") {\n if (astNode.kind === \"method\") return members.includes(\"method\");\n if (astNode.kind === \"get\") return members.includes(\"get\");\n if (astNode.kind === \"set\") return members.includes(\"set\");\n }\n return false;\n };\n }\n if (typeof suffix === \"string\") {\n const suffixString = suffix;\n suffix = (name) => name + suffixString;\n }\n // Definition of the Plugin\n return {\n name: \"rolldown-plugin-access-privates\",\n transform: {\n // Filters to optimize the plugin by skipping modules that don't need to be transformed.\n filter: {\n id: idFilter,\n // if there is no need to add exports, we only need to process modules that have private class members.\n ...(canNotExport ? { code: \"#\" } : {}),\n },\n // Function implementing the transformation logic.\n handler: withMagicString(function (code, id, meta) {\n // If ast is provided in meta, use it. Otherwise, parse the code with the appropriate language based on the file extension.\n let ast = meta.ast !== undefined ? meta.ast : this.parse(code.original, { lang: id.endsWith(\".tsx\") ? \"tsx\" : id.endsWith(\".ts\") ? \"ts\" : id.endsWith(\".jsx\") ? \"jsx\" : \"js\" });\n const visitors: VisitorObject = {};\n // only add the visitors needed.\n if (!canNotExport) {\n // Visitor to add export keyword to top-level variable, function, and class declarations.\n visitors.Program = function (node) {\n for (const child of node.body) {\n if ((child.type !== \"VariableDeclaration\" && child.type !== \"FunctionDeclaration\" && child.type !== \"ClassDeclaration\") || child.declare) continue;\n if (!exports(id, child)) continue;\n code.appendLeft(child.start, \"export \");\n }\n };\n }\n if (!canNotPropertyDefinition) {\n // Visitor to add getter and setter for private fields defined with PropertyDefinition.\n visitors.PropertyDefinition = function (node) {\n if (node.type !== \"PropertyDefinition\" || node.key.type !== \"PrivateIdentifier\" || node.declare || node.override) return;\n if (!classMembers(id, node)) return;\n const name = suffix(node.key.name);\n code.appendRight(node.end, `\\n${node.static ? \"static \" : \"\"}get [\"${name}\"]() {return this.#${node.key.name};}`);\n code.appendRight(node.end, `\\n${node.static ? \"static \" : \"\"}set [\"${name}\"](value) {this.#${node.key.name} = value;}`);\n };\n }\n if (!canNotMethodDefinition) {\n // Visitor to add getter and setter for private fields defined with MethodDefinition.\n visitors.MethodDefinition = function (node) {\n if (node.type !== \"MethodDefinition\" || node.key.type !== \"PrivateIdentifier\" || node.override) return;\n if (!classMembers(id, node)) return;\n const name = suffix(node.key.name);\n switch (node.kind) {\n case \"method\":\n code.appendRight(node.end, `\\n${node.static ? \"static \" : \"\"}get [\"${name}\"]() {return this.#${node.key.name};}`);\n break;\n case \"get\":\n code.appendRight(node.end, `\\n${node.static ? \"static \" : \"\"}get [\"${name}\"]() {return this.#${node.key.name};}`);\n break;\n case \"set\":\n code.appendRight(node.end, `\\n${node.static ? \"static \" : \"\"}set [\"${name}\"](value) {this.#${node.key.name} = value;}`);\n break;\n }\n };\n }\n // Walk the AST with the defined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AE;KAC7B,MAAM,OAAO,OAAO,KAAK,IAAI,KAAK;AAClC,aAAQ,KAAK,MAAb;MACE,KAAK;AACH,YAAK,YAAY,KAAK,KAAK,KAAK,KAAK,SAAS,YAAY,GAAG,QAAQ,KAAK,qBAAqB,KAAK,IAAI,KAAK,IAAI;AACjH;MACF,KAAK;AACH,YAAK,YAAY,KAAK,KAAK,KAAK,KAAK,SAAS,YAAY,GAAG,QAAQ,KAAK,qBAAqB,KAAK,IAAI,KAAK,IAAI;AACjH;MACF,KAAK;AACH,YAAK,YAAY,KAAK,KAAK,KAAK,KAAK,SAAS,YAAY,GAAG,QAAQ,KAAK,mBAAmB,KAAK,IAAI,KAAK,YAAY;AACvH;;;AAKR,QAAI,QAAQ,SAAS,CAAC,MAAM,IAAI;KAChC;GACH;EACF"}
|
package/package.json
CHANGED
|
@@ -1,40 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rolldown-plugin-access-privates",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Rolldown Plugin to add accessors for class Private Fields",
|
|
5
|
-
"publishConfig": {
|
|
6
|
-
"access": "public"
|
|
7
|
-
},
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"import": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"default": "./dist/index.js"
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"type": "module",
|
|
17
|
-
"repository": {
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/IIIMADDINIII/rolldown-plugin-access-privates"
|
|
20
|
-
},
|
|
21
|
-
"author": "IIIMADDINIII <martin@elend.name>",
|
|
22
|
-
"license": "MIT",
|
|
23
5
|
"keywords": [
|
|
24
|
-
"
|
|
6
|
+
"accessors",
|
|
25
7
|
"class",
|
|
26
8
|
"fields",
|
|
27
|
-
"
|
|
9
|
+
"private",
|
|
28
10
|
"rolldown",
|
|
29
11
|
"rolldown-plugin",
|
|
30
12
|
"testing"
|
|
31
13
|
],
|
|
14
|
+
"homepage": "https://github.com/IIIMADDINIII/rolldown-plugin-access-privates#readme",
|
|
32
15
|
"bugs": {
|
|
33
16
|
"url": "https://github.com/IIIMADDINIII/rolldown-plugin-access-privates/issues"
|
|
34
17
|
},
|
|
35
|
-
"
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "IIIMADDINIII <martin@elend.name>",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/IIIMADDINIII/rolldown-plugin-access-privates"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.mjs",
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"rolldown-string": "^0.3.0",
|
|
34
|
+
"tslib": ">=2.8.1"
|
|
35
|
+
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@typescript/native-preview": "^7.0.0-dev.20260324.1"
|
|
37
|
+
"@typescript/native-preview": "^7.0.0-dev.20260324.1",
|
|
38
|
+
"vite-plus": "^0.1.15"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|
|
40
41
|
"rolldown": ">=1.0.0-rc.12"
|
|
@@ -44,10 +45,6 @@
|
|
|
44
45
|
"optional": true
|
|
45
46
|
}
|
|
46
47
|
},
|
|
47
|
-
"dependencies": {
|
|
48
|
-
"rolldown-string": "^0.3.0",
|
|
49
|
-
"tslib": ">=2.8.1"
|
|
50
|
-
},
|
|
51
48
|
"compatiblePackages": {
|
|
52
49
|
"schemaVersion": 1,
|
|
53
50
|
"rollup": {
|
package/src/index.ts
CHANGED
|
@@ -1,144 +1,148 @@
|
|
|
1
|
-
import { type HookFilter, type Plugin } from "rolldown";
|
|
2
|
-
import { withMagicString } from "rolldown-string";
|
|
3
|
-
import { Visitor, type ESTree, type VisitorObject } from "rolldown/utils";
|
|
4
|
-
|
|
5
|
-
/** Map exports types to their corresponding AST node types */
|
|
6
|
-
const exportsToType = {
|
|
7
|
-
variable: "VariableDeclaration",
|
|
8
|
-
function: "FunctionDeclaration",
|
|
9
|
-
class: "ClassDeclaration",
|
|
10
|
-
} as const;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
14
|
-
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
15
|
-
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
16
|
-
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
1
|
+
import { type HookFilter, type Plugin } from "rolldown";
|
|
2
|
+
import { withMagicString } from "rolldown-string";
|
|
3
|
+
import { Visitor, type ESTree, type VisitorObject } from "rolldown/utils";
|
|
4
|
+
|
|
5
|
+
/** Map exports types to their corresponding AST node types. */
|
|
6
|
+
const exportsToType = {
|
|
7
|
+
variable: "VariableDeclaration",
|
|
8
|
+
function: "FunctionDeclaration",
|
|
9
|
+
class: "ClassDeclaration",
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
14
|
+
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
15
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
16
|
+
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
17
|
+
*
|
|
18
|
+
* @param options - Options for the plugin.
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export default function AccessPrivates({
|
|
22
|
+
exports = true,
|
|
23
|
+
classMembers = true,
|
|
24
|
+
suffix = "Private",
|
|
25
|
+
idFilter = /\.[jt]sx?$/,
|
|
26
|
+
}: {
|
|
27
|
+
/**
|
|
28
|
+
* Whether to export all top-level variables, functions, and classes in the module.
|
|
29
|
+
* Can be an array of "variable", "function", and "class", or a function that will be called with the module ID and AST node of each variable, function, or class declaration.
|
|
30
|
+
*
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
exports?: ("variable" | "function" | "class")[] | boolean | ((id: string, astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class) => boolean) | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Which class members to generate accessors for.
|
|
36
|
+
* Can be an array of "method", "get", "set", and "property", or a function that will be called with the module ID and AST node of each method or property definition.
|
|
37
|
+
*
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
classMembers?: ("method" | "get" | "set" | "property")[] | boolean | ((id: string, astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition) => boolean) | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* The suffix to use for the generated accessors.
|
|
43
|
+
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
44
|
+
*
|
|
45
|
+
* @default "Private"
|
|
46
|
+
*/
|
|
47
|
+
suffix?: string | ((name: string) => string) | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Filter for which modules the plugin should apply.
|
|
50
|
+
* Can be a string, a RegExp, an array of strings and RegExps, or an object with an `include` and `exclude` property, each of which can be a string, a RegExp, or an array of strings and RegExps.
|
|
51
|
+
* The filter will be applied to the module ID.
|
|
52
|
+
*
|
|
53
|
+
* @default /\.[jt]sx?$/
|
|
54
|
+
*/
|
|
55
|
+
idFilter?: HookFilter["id"] | undefined;
|
|
56
|
+
} = {}): Plugin {
|
|
57
|
+
// Calculate which code path can be optimized.
|
|
58
|
+
const canNotExport = exports === false || (Array.isArray(exports) && exports.length === 0);
|
|
59
|
+
const canNotPropertyDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.includes("property")));
|
|
60
|
+
const canNotMethodDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.some((m) => m === "method" || m === "get" || m === "set")));
|
|
61
|
+
// Transform filters in to a unified format
|
|
62
|
+
if (typeof exports === "boolean") {
|
|
63
|
+
const shouldExport = exports;
|
|
64
|
+
exports = () => shouldExport;
|
|
65
|
+
} else if (Array.isArray(exports)) {
|
|
66
|
+
const mappedExports: ESTree.Node["type"][] = exports.map((e) => exportsToType[e]);
|
|
67
|
+
exports = (_id, astNode) => mappedExports.includes(astNode.type);
|
|
68
|
+
}
|
|
69
|
+
if (typeof classMembers === "boolean") {
|
|
70
|
+
const shouldGenerate = classMembers;
|
|
71
|
+
classMembers = () => shouldGenerate;
|
|
72
|
+
} else if (Array.isArray(classMembers)) {
|
|
73
|
+
const members = classMembers;
|
|
74
|
+
classMembers = (_id, astNode) => {
|
|
75
|
+
if (astNode.type === "PropertyDefinition") return members.includes("property");
|
|
76
|
+
if (astNode.type === "MethodDefinition") {
|
|
77
|
+
if (astNode.kind === "method") return members.includes("method");
|
|
78
|
+
if (astNode.kind === "get") return members.includes("get");
|
|
79
|
+
if (astNode.kind === "set") return members.includes("set");
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (typeof suffix === "string") {
|
|
85
|
+
const suffixString = suffix;
|
|
86
|
+
suffix = (name) => name + suffixString;
|
|
87
|
+
}
|
|
88
|
+
// Definition of the Plugin
|
|
89
|
+
return {
|
|
90
|
+
name: "rolldown-plugin-access-privates",
|
|
91
|
+
transform: {
|
|
92
|
+
// Filters to optimize the plugin by skipping modules that don't need to be transformed.
|
|
93
|
+
filter: {
|
|
94
|
+
id: idFilter,
|
|
95
|
+
// if there is no need to add exports, we only need to process modules that have private class members.
|
|
96
|
+
...(canNotExport ? { code: "#" } : {}),
|
|
97
|
+
},
|
|
98
|
+
// Function implementing the transformation logic.
|
|
99
|
+
handler: withMagicString(function (code, id, meta) {
|
|
100
|
+
// If ast is provided in meta, use it. Otherwise, parse the code with the appropriate language based on the file extension.
|
|
101
|
+
let ast = meta.ast !== undefined ? meta.ast : this.parse(code.original, { lang: id.endsWith(".tsx") ? "tsx" : id.endsWith(".ts") ? "ts" : id.endsWith(".jsx") ? "jsx" : "js" });
|
|
102
|
+
const visitors: VisitorObject = {};
|
|
103
|
+
// only add the visitors needed.
|
|
104
|
+
if (!canNotExport) {
|
|
105
|
+
// Visitor to add export keyword to top-level variable, function, and class declarations.
|
|
106
|
+
visitors.Program = function (node) {
|
|
107
|
+
for (const child of node.body) {
|
|
108
|
+
if ((child.type !== "VariableDeclaration" && child.type !== "FunctionDeclaration" && child.type !== "ClassDeclaration") || child.declare) continue;
|
|
109
|
+
if (!exports(id, child)) continue;
|
|
110
|
+
code.appendLeft(child.start, "export ");
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (!canNotPropertyDefinition) {
|
|
115
|
+
// Visitor to add getter and setter for private fields defined with PropertyDefinition.
|
|
116
|
+
visitors.PropertyDefinition = function (node) {
|
|
117
|
+
if (node.type !== "PropertyDefinition" || node.key.type !== "PrivateIdentifier" || node.declare || node.override) return;
|
|
118
|
+
if (!classMembers(id, node)) return;
|
|
119
|
+
const name = suffix(node.key.name);
|
|
120
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
121
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (!canNotMethodDefinition) {
|
|
125
|
+
// Visitor to add getter and setter for private fields defined with MethodDefinition.
|
|
126
|
+
visitors.MethodDefinition = function (node) {
|
|
127
|
+
if (node.type !== "MethodDefinition" || node.key.type !== "PrivateIdentifier" || node.override) return;
|
|
128
|
+
if (!classMembers(id, node)) return;
|
|
129
|
+
const name = suffix(node.key.name);
|
|
130
|
+
switch (node.kind) {
|
|
131
|
+
case "method":
|
|
132
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
133
|
+
break;
|
|
134
|
+
case "get":
|
|
135
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
136
|
+
break;
|
|
137
|
+
case "set":
|
|
138
|
+
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
// Walk the AST with the defined visitors to apply the transformations.
|
|
144
|
+
new Visitor(visitors).visit(ast);
|
|
145
|
+
}),
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { type HookFilter, type Plugin } from "rolldown";
|
|
2
|
-
import { type ESTree } from "rolldown/utils";
|
|
3
|
-
/**
|
|
4
|
-
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
5
|
-
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
6
|
-
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
7
|
-
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
8
|
-
* @param options - Options for the plugin.
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
|
-
export default function AccessPrivates({ exports, classMembers, suffix, idFilter }?: {
|
|
12
|
-
/**
|
|
13
|
-
* Whether to export all top-level variables, functions, and classes in the module.
|
|
14
|
-
* Can be an array of "variable", "function", and "class", or a function that will be called with the module ID and AST node of each variable, function, or class declaration.
|
|
15
|
-
* @default true
|
|
16
|
-
*/
|
|
17
|
-
exports?: ("variable" | "function" | "class")[] | boolean | ((id: string, astNode: ESTree.VariableDeclaration | ESTree.Function | ESTree.Class) => boolean) | undefined;
|
|
18
|
-
/**
|
|
19
|
-
* Which class members to generate accessors for.
|
|
20
|
-
* Can be an array of "method", "get", "set", and "property", or a function that will be called with the module ID and AST node of each method or property definition.
|
|
21
|
-
* @default true
|
|
22
|
-
*/
|
|
23
|
-
classMembers?: ("method" | "get" | "set" | "property")[] | boolean | ((id: string, astNode: ESTree.MethodDefinition | ESTree.PropertyDefinition) => boolean) | undefined;
|
|
24
|
-
/**
|
|
25
|
-
* The suffix to use for the generated accessors.
|
|
26
|
-
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
27
|
-
* @default "Private"
|
|
28
|
-
*/
|
|
29
|
-
suffix?: string | ((name: string) => string) | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Filter for which modules the plugin should apply.
|
|
32
|
-
* Can be a string, a RegExp, an array of strings and RegExps, or an object with an `include` and `exclude` property, each of which can be a string, a RegExp, or an array of strings and RegExps.
|
|
33
|
-
* The filter will be applied to the module ID.
|
|
34
|
-
* @default /\.[jt]sx?$/
|
|
35
|
-
*/
|
|
36
|
-
idFilter?: HookFilter["id"] | undefined;
|
|
37
|
-
}): Plugin;
|
|
38
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AAExD,OAAO,EAAW,KAAK,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AAS1E;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,EACrC,OAAc,EACd,YAAmB,EACnB,MAAkB,EAClB,QAAuB,EACxB,GAAE;IACD;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,UAAU,GAAG,UAAU,GAAG,OAAO,CAAC,EAAE,GAAG,OAAO,GAAG,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,mBAAmB,GAAG,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,KAAK,KAAK,OAAO,CAAC,GAAG,SAAS,CAAC;IACxK;;;;OAIG;IACH,YAAY,CAAC,EAAE,CAAC,QAAQ,GAAG,KAAK,GAAG,KAAK,GAAG,UAAU,CAAC,EAAE,GAAG,OAAO,GAAG,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,KAAK,OAAO,CAAC,GAAG,SAAS,CAAC;IACzK;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,SAAS,CAAC;IACzD;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;CACpC,GAAG,MAAM,CA6Fd"}
|
package/dist/index.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import {} from "rolldown";
|
|
2
|
-
import { withMagicString } from "rolldown-string";
|
|
3
|
-
import { Visitor } from "rolldown/utils";
|
|
4
|
-
/** Map exports types to their corresponding AST node types */
|
|
5
|
-
const exportsToType = {
|
|
6
|
-
variable: "VariableDeclaration",
|
|
7
|
-
function: "FunctionDeclaration",
|
|
8
|
-
class: "ClassDeclaration",
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* A Vite plugin to generate accessors for private fields to allow testing them.
|
|
12
|
-
* This plugin will generate getter and setter methods for private fields and methods, allowing you to access them in your tests.
|
|
13
|
-
* For example, if you have a private field `#foo`, the plugin will generate `fooPrivate` getter and setter.
|
|
14
|
-
* Also automatically exports all top-level variables, functions, and classes in the module, allowing you to import them in your tests without needing to use `export` in your source code.
|
|
15
|
-
* @param options - Options for the plugin.
|
|
16
|
-
* @returns
|
|
17
|
-
*/
|
|
18
|
-
export default function AccessPrivates({ exports = true, classMembers = true, suffix = "Private", idFilter = /\.[jt]sx?$/, } = {}) {
|
|
19
|
-
// Calculate which code path can be optimized.
|
|
20
|
-
const canNotExport = exports === false || (Array.isArray(exports) && exports.length === 0);
|
|
21
|
-
const canNotPropertyDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.includes("property")));
|
|
22
|
-
const canNotMethodDefinition = classMembers === false || (Array.isArray(classMembers) && (classMembers.length === 0 || !classMembers.some(m => m === "method" || m === "get" || m === "set")));
|
|
23
|
-
// Transform filters in to a unified format
|
|
24
|
-
if (typeof exports === "boolean") {
|
|
25
|
-
const shouldExport = exports;
|
|
26
|
-
exports = () => shouldExport;
|
|
27
|
-
}
|
|
28
|
-
else if (Array.isArray(exports)) {
|
|
29
|
-
const mappedExports = exports.map(e => exportsToType[e]);
|
|
30
|
-
exports = (_id, astNode) => mappedExports.includes(astNode.type);
|
|
31
|
-
}
|
|
32
|
-
if (typeof classMembers === "boolean") {
|
|
33
|
-
const shouldGenerate = classMembers;
|
|
34
|
-
classMembers = () => shouldGenerate;
|
|
35
|
-
}
|
|
36
|
-
else if (Array.isArray(classMembers)) {
|
|
37
|
-
const members = classMembers;
|
|
38
|
-
classMembers = (_id, astNode) => {
|
|
39
|
-
if (astNode.type === "PropertyDefinition")
|
|
40
|
-
return members.includes("property");
|
|
41
|
-
if (astNode.type === "MethodDefinition") {
|
|
42
|
-
if (astNode.kind === "method")
|
|
43
|
-
return members.includes("method");
|
|
44
|
-
if (astNode.kind === "get")
|
|
45
|
-
return members.includes("get");
|
|
46
|
-
if (astNode.kind === "set")
|
|
47
|
-
return members.includes("set");
|
|
48
|
-
}
|
|
49
|
-
return false;
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
if (typeof suffix === "string") {
|
|
53
|
-
const suffixString = suffix;
|
|
54
|
-
suffix = (name) => name + suffixString;
|
|
55
|
-
}
|
|
56
|
-
// Definition of the Plugin
|
|
57
|
-
return {
|
|
58
|
-
name: "rolldown-plugin-access-privates",
|
|
59
|
-
transform: {
|
|
60
|
-
// Filters to optimize the plugin by skipping modules that don't need to be transformed.
|
|
61
|
-
filter: {
|
|
62
|
-
id: idFilter,
|
|
63
|
-
// if there is no need to add exports, we only need to process modules that have private class members.
|
|
64
|
-
...(canNotExport ? { code: "#" } : {}),
|
|
65
|
-
},
|
|
66
|
-
// Function implementing the transformation logic.
|
|
67
|
-
handler: withMagicString(function (code, id, meta) {
|
|
68
|
-
// If ast is provided in meta, use it. Otherwise, parse the code with the appropriate language based on the file extension.
|
|
69
|
-
let ast = meta.ast !== undefined ? meta.ast : this.parse(code.original, { lang: id.endsWith('.tsx') ? 'tsx' : id.endsWith('.ts') ? 'ts' : id.endsWith('.jsx') ? 'jsx' : 'js' });
|
|
70
|
-
const visitors = {};
|
|
71
|
-
// only add the visitors needed.
|
|
72
|
-
if (!canNotExport) {
|
|
73
|
-
// Visitor to add export keyword to top-level variable, function, and class declarations.
|
|
74
|
-
visitors.Program = function (node) {
|
|
75
|
-
for (const child of node.body) {
|
|
76
|
-
if (child.type !== "VariableDeclaration" && child.type !== "FunctionDeclaration" && child.type !== "ClassDeclaration" || child.declare)
|
|
77
|
-
continue;
|
|
78
|
-
if (!exports(id, child))
|
|
79
|
-
continue;
|
|
80
|
-
code.appendLeft(child.start, "export ");
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
if (!canNotPropertyDefinition) {
|
|
85
|
-
// Visitor to add getter and setter for private fields defined with PropertyDefinition.
|
|
86
|
-
visitors.PropertyDefinition = function (node) {
|
|
87
|
-
if (node.type !== "PropertyDefinition" || node.key.type !== "PrivateIdentifier" || node.declare || node.override)
|
|
88
|
-
return;
|
|
89
|
-
if (!classMembers(id, node))
|
|
90
|
-
return;
|
|
91
|
-
const name = suffix(node.key.name);
|
|
92
|
-
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
93
|
-
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
if (!canNotMethodDefinition) {
|
|
97
|
-
// Visitor to add getter and setter for private fields defined with MethodDefinition.
|
|
98
|
-
visitors.MethodDefinition = function (node) {
|
|
99
|
-
if (node.type !== "MethodDefinition" || node.key.type !== "PrivateIdentifier" || node.override)
|
|
100
|
-
return;
|
|
101
|
-
if (!classMembers(id, node))
|
|
102
|
-
return;
|
|
103
|
-
const name = suffix(node.key.name);
|
|
104
|
-
switch (node.kind) {
|
|
105
|
-
case "method":
|
|
106
|
-
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
107
|
-
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
108
|
-
break;
|
|
109
|
-
case "get":
|
|
110
|
-
code.appendRight(node.end, `\n${node.static ? "static " : ""}get ["${name}"]() {return this.#${node.key.name};}`);
|
|
111
|
-
break;
|
|
112
|
-
case "set":
|
|
113
|
-
code.appendRight(node.end, `\n${node.static ? "static " : ""}set ["${name}"](value) {this.#${node.key.name} = value;}`);
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
// Walk the AST with the defined visitors to apply the transformations.
|
|
119
|
-
new Visitor(visitors).visit(ast);
|
|
120
|
-
}),
|
|
121
|
-
},
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAmC,MAAM,gBAAgB,CAAC;AAE1E,8DAA8D;AAC9D,MAAM,aAAa,GAAG;IACpB,QAAQ,EAAE,qBAAqB;IAC/B,QAAQ,EAAE,qBAAqB;IAC/B,KAAK,EAAE,kBAAkB;CACjB,CAAC;AAEX;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,EACrC,OAAO,GAAG,IAAI,EACd,YAAY,GAAG,IAAI,EACnB,MAAM,GAAG,SAAS,EAClB,QAAQ,GAAG,YAAY,GACxB,GA0BG,EAAE;IACJ,8CAA8C;IAC9C,MAAM,YAAY,GAAG,OAAO,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;IAC3F,MAAM,wBAAwB,GAAG,YAAY,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAC9J,MAAM,sBAAsB,GAAG,YAAY,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE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