one 2.22.0 → 2.23.0
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/dist/cjs/platform/streams.cjs +17 -0
- package/dist/cjs/platform/streams.native.cjs +19 -0
- package/dist/cjs/platform/streams.native.js +21 -0
- package/dist/cjs/platform/streams.native.js.map +1 -0
- package/dist/cjs/routeTypes.cjs +17 -0
- package/dist/cjs/routeTypes.native.cjs +19 -0
- package/dist/cjs/routeTypes.native.js +21 -0
- package/dist/cjs/routeTypes.native.js.map +1 -0
- package/dist/cjs/vite/plugins/iosLinkSetPlugin.cjs +22 -6
- package/dist/cjs/vite/plugins/iosLinkSetPlugin.native.cjs +100 -25
- package/dist/cjs/vite/plugins/iosLinkSetPlugin.native.js +100 -25
- package/dist/cjs/vite/plugins/iosLinkSetPlugin.native.js.map +1 -1
- package/dist/cjs/vite/plugins/oneNativeMembers.cjs +288 -57
- package/dist/cjs/vite/plugins/oneNativeMembers.native.cjs +537 -169
- package/dist/cjs/vite/plugins/oneNativeMembers.native.js +537 -169
- package/dist/cjs/vite/plugins/oneNativeMembers.native.js.map +1 -1
- package/dist/esm/platform/streams.mjs +0 -0
- package/dist/esm/platform/streams.native.js +0 -0
- package/dist/esm/routeTypes.mjs +0 -0
- package/dist/esm/routeTypes.native.js +0 -0
- package/dist/esm/vite/plugins/iosLinkSetPlugin.mjs +22 -6
- package/dist/esm/vite/plugins/iosLinkSetPlugin.mjs.map +1 -1
- package/dist/esm/vite/plugins/iosLinkSetPlugin.native.js +100 -25
- package/dist/esm/vite/plugins/iosLinkSetPlugin.native.js.map +1 -1
- package/dist/esm/vite/plugins/oneNativeMembers.mjs +289 -58
- package/dist/esm/vite/plugins/oneNativeMembers.mjs.map +1 -1
- package/dist/esm/vite/plugins/oneNativeMembers.native.js +538 -170
- package/dist/esm/vite/plugins/oneNativeMembers.native.js.map +1 -1
- package/package.json +13 -13
- package/src/Root.tsx +1 -1
- package/src/constants.ts +1 -1
- package/src/createApp.native.tsx +1 -1
- package/src/createApp.tsx +3 -2
- package/src/headless.tsx +1 -1
- package/src/interfaces/router.ts +3 -27
- package/src/platform/file-system/classes.native.ts +6 -5
- package/src/platform/file-system/classes.ts +6 -5
- package/src/platform/streams.ts +14 -0
- package/src/routeTypes.ts +83 -0
- package/src/router/FlagsContext.ts +1 -1
- package/src/router/Route.tsx +1 -1
- package/src/router/getRoutes.ts +1 -1
- package/src/router/matchers.ts +1 -1
- package/src/router/router.ts +1 -1
- package/src/router/useInitializeOneRouter.ts +1 -1
- package/src/router/useViteRoutes.tsx +1 -1
- package/src/router/warmState.ts +1 -1
- package/src/types.ts +1 -1
- package/src/useMatches.ts +1 -1
- package/src/vite/one-server-only.tsx +5 -2
- package/src/vite/plugins/iosLinkSetPlugin.ts +39 -6
- package/src/vite/plugins/oneNativeMembers.ts +417 -82
- package/src/vite/types.ts +12 -51
- package/types/Root.d.ts +1 -1
- package/types/config.d.ts +1 -2
- package/types/config.d.ts.map +1 -1
- package/types/constants.d.ts +1 -1
- package/types/createApp.d.ts +3 -2
- package/types/createApp.d.ts.map +1 -1
- package/types/daemon/server.d.ts +1 -1
- package/types/headless.d.ts +1 -1
- package/types/interfaces/router.d.ts +3 -19
- package/types/interfaces/router.d.ts.map +1 -1
- package/types/platform/file-system/classes.d.ts +4 -3
- package/types/platform/file-system/classes.d.ts.map +1 -1
- package/types/platform/file-system/classes.native.d.ts +4 -3
- package/types/platform/file-system/classes.native.d.ts.map +1 -1
- package/types/platform/streams.d.ts +12 -0
- package/types/platform/streams.d.ts.map +1 -0
- package/types/routeTypes.d.ts +66 -0
- package/types/routeTypes.d.ts.map +1 -0
- package/types/router/FlagsContext.d.ts +1 -1
- package/types/router/Route.d.ts +1 -1
- package/types/router/getRoutes.d.ts +1 -1
- package/types/router/matchers.d.ts +1 -1
- package/types/router/router.d.ts +1 -1
- package/types/router/useInitializeOneRouter.d.ts +1 -1
- package/types/router/useViteRoutes.d.ts +1 -1
- package/types/router/warmState.d.ts +1 -1
- package/types/types.d.ts +1 -1
- package/types/useMatches.d.ts +1 -1
- package/types/vite/one-server-only.d.ts +5 -3
- package/types/vite/one-server-only.d.ts.map +1 -1
- package/types/vite/plugins/oneNativeMembers.d.ts +1 -1
- package/types/vite/plugins/oneNativeMembers.d.ts.map +1 -1
- package/types/vite/types.d.ts +12 -49
- package/types/vite/types.d.ts.map +1 -1
|
@@ -32,42 +32,168 @@ var import_node_fs = require("node:fs");
|
|
|
32
32
|
var import_node_path = require("node:path");
|
|
33
33
|
var import_magic_string = __toESM(require("magic-string"), 1);
|
|
34
34
|
var import_oxc_parser = require("oxc-parser");
|
|
35
|
-
function nativeOneMembers(file) {
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
35
|
+
function nativeOneMembers(file, platform = "ios") {
|
|
36
|
+
const modules = /* @__PURE__ */ new Map();
|
|
37
|
+
const load = (path) => {
|
|
38
|
+
if (modules.has(path)) return modules.get(path);
|
|
39
|
+
let code;
|
|
40
|
+
try {
|
|
41
|
+
code = (0, import_node_fs.readFileSync)(path, "utf8");
|
|
42
|
+
} catch {
|
|
43
|
+
modules.set(path, void 0);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const module2 = {
|
|
47
|
+
path,
|
|
48
|
+
imports: /* @__PURE__ */ new Map(),
|
|
49
|
+
values: /* @__PURE__ */ new Map(),
|
|
50
|
+
exports: /* @__PURE__ */ new Map(),
|
|
51
|
+
reexports: /* @__PURE__ */ new Map(),
|
|
52
|
+
stars: []
|
|
53
|
+
};
|
|
54
|
+
modules.set(path, module2);
|
|
55
|
+
const from = (specifier) => {
|
|
56
|
+
if (!specifier.startsWith(".")) return specifier;
|
|
57
|
+
const base = (0, import_node_path.resolve)((0, import_node_path.dirname)(path), specifier);
|
|
58
|
+
return [
|
|
59
|
+
"",
|
|
60
|
+
`.${platform}.js`,
|
|
61
|
+
".native.js",
|
|
62
|
+
".js",
|
|
63
|
+
`/index.${platform}.js`,
|
|
64
|
+
"/index.native.js",
|
|
65
|
+
"/index.js"
|
|
66
|
+
].map((suffix) => base + suffix).find((candidate) => (0, import_node_fs.statSync)(candidate, { throwIfNoEntry: false })?.isFile()) ?? base;
|
|
67
|
+
};
|
|
68
|
+
const name = (node) => String(node.name ?? node.value);
|
|
69
|
+
for (const statement of (0, import_oxc_parser.parseSync)(path, code).program.body) {
|
|
70
|
+
if (statement.type === "ExportAllDeclaration") {
|
|
71
|
+
if (statement.exported) module2.reexports.set(name(statement.exported), {
|
|
72
|
+
source: from(statement.source.value),
|
|
73
|
+
imported: "*"
|
|
46
74
|
});
|
|
75
|
+
else module2.stars.push(from(statement.source.value));
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (statement.type === "ExportNamedDeclaration") {
|
|
79
|
+
for (const item of statement.specifiers ?? []) {
|
|
80
|
+
if (statement.source) module2.reexports.set(name(item.exported), {
|
|
81
|
+
source: from(statement.source.value),
|
|
82
|
+
imported: name(item.local)
|
|
83
|
+
});
|
|
84
|
+
else module2.exports.set(name(item.exported), name(item.local));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const node = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
88
|
+
if (node?.type === "ImportDeclaration") {
|
|
89
|
+
for (const item of node.specifiers) {
|
|
90
|
+
module2.imports.set(item.local.name, {
|
|
91
|
+
source: from(node.source.value),
|
|
92
|
+
imported: item.type === "ImportSpecifier" ? name(item.imported) : item.type === "ImportDefaultSpecifier" ? "default" : "*"
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
} else if (node?.type === "VariableDeclaration") {
|
|
96
|
+
for (const declaration of node.declarations) if (declaration.id.type === "Identifier") {
|
|
97
|
+
if (declaration.init) module2.values.set(declaration.id.name, declaration.init);
|
|
98
|
+
if (node !== statement) module2.exports.set(declaration.id.name, declaration.id.name);
|
|
99
|
+
}
|
|
100
|
+
} else if (node && node !== statement && node.id) {
|
|
101
|
+
module2.exports.set(node.id.name, node.id.name);
|
|
47
102
|
}
|
|
48
|
-
} else if (node?.type === "VariableDeclaration") {
|
|
49
|
-
for (const declaration of node.declarations) if (declaration.id.type === "Identifier" && declaration.init) values.set(declaration.id.name, declaration.init);
|
|
50
103
|
}
|
|
51
|
-
|
|
52
|
-
|
|
104
|
+
return module2;
|
|
105
|
+
};
|
|
106
|
+
const resolveExport = (source, name, seen = /* @__PURE__ */ new Set()) => {
|
|
107
|
+
const module2 = load(source);
|
|
108
|
+
if (!module2 || seen.has(source)) return;
|
|
109
|
+
seen.add(source);
|
|
110
|
+
const here = {
|
|
111
|
+
source,
|
|
112
|
+
imported: name
|
|
113
|
+
};
|
|
114
|
+
const follow = (binding) => !load(binding.source) ? here : binding.imported === "*" ? binding : resolveExport(binding.source, binding.imported, seen) ?? here;
|
|
115
|
+
const reexport = module2.reexports.get(name);
|
|
116
|
+
if (reexport) return follow(reexport);
|
|
117
|
+
const local = module2.exports.get(name);
|
|
118
|
+
if (local !== void 0) {
|
|
119
|
+
const imported = module2.imports.get(local);
|
|
120
|
+
return imported && !module2.values.has(local) ? follow(imported) : here;
|
|
121
|
+
}
|
|
122
|
+
for (const star of module2.stars) {
|
|
123
|
+
if (!load(star)) return here;
|
|
124
|
+
const found = resolveExport(star, name, seen);
|
|
125
|
+
if (found) return found;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const bind = (imported) => !load(imported.source) ? void 0 : imported.imported === "*" ? imported : resolveExport(imported.source, imported.imported) ?? imported;
|
|
129
|
+
const descend = (binding, path, consumed) => {
|
|
130
|
+
const target = load(binding.source);
|
|
131
|
+
if (!target || !path.length) return {
|
|
132
|
+
binding,
|
|
133
|
+
consumed
|
|
134
|
+
};
|
|
135
|
+
if (binding.imported === "*") {
|
|
136
|
+
const inner = resolveExport(binding.source, path[0]);
|
|
137
|
+
return inner && descend(inner, path.slice(1), consumed + 1);
|
|
138
|
+
}
|
|
139
|
+
const value = target.values.get(target.exports.get(binding.imported) ?? "");
|
|
140
|
+
const found = value && member(target, value, path, consumed);
|
|
141
|
+
if (found) return found;
|
|
142
|
+
const object = value?.type === "CallExpression" && value.callee.property?.name === "freeze" ? value.arguments[0] : value;
|
|
143
|
+
const index = object?.type === "ObjectExpression" ? object.properties.findLastIndex((property) => property.type === "SpreadElement" || !property.computed && (property.key.name ?? property.key.value) === path[0]) : -1;
|
|
144
|
+
return index >= 0 && object.properties[index].type === "Property" ? {
|
|
145
|
+
binding,
|
|
146
|
+
consumed: consumed + 1,
|
|
147
|
+
suffix: [path[0]]
|
|
148
|
+
} : {
|
|
149
|
+
binding,
|
|
150
|
+
consumed
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
const member = (module2, expression, path, consumed = 0) => {
|
|
53
154
|
if (expression.type === "Identifier") {
|
|
54
|
-
const
|
|
55
|
-
if (
|
|
56
|
-
binding
|
|
155
|
+
const imported = module2.imports.get(expression.name);
|
|
156
|
+
if (imported) {
|
|
157
|
+
const binding = bind(imported);
|
|
158
|
+
return binding && descend(binding, path, consumed);
|
|
159
|
+
}
|
|
160
|
+
const value = module2.values.get(expression.name);
|
|
161
|
+
const found = value && member(module2, value, path, consumed);
|
|
162
|
+
if (found || module2.path === file) return found;
|
|
163
|
+
for (const [exported, local] of module2.exports) if (local === expression.name) return {
|
|
164
|
+
binding: {
|
|
165
|
+
source: module2.path,
|
|
166
|
+
imported: exported
|
|
167
|
+
},
|
|
57
168
|
consumed
|
|
58
169
|
};
|
|
59
|
-
|
|
60
|
-
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (expression.type === "CallExpression" && expression.callee.type === "MemberExpression" && expression.callee.object.name === "Object" && expression.callee.property.name === "freeze") return member(module2, expression.arguments[0], path, consumed);
|
|
173
|
+
if (expression.type === "MemberExpression" && !expression.computed) {
|
|
174
|
+
const found = member(module2, expression.object, [expression.property.name, ...path], consumed - 1);
|
|
175
|
+
if (!found || found.consumed >= consumed) return found;
|
|
176
|
+
if (found.call || found.consumed < consumed - 1) return;
|
|
177
|
+
return {
|
|
178
|
+
...found,
|
|
179
|
+
consumed,
|
|
180
|
+
suffix: [...found.suffix ?? [], expression.property.name]
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
if (expression.type === "ConditionalExpression") {
|
|
184
|
+
const test = expression.test;
|
|
185
|
+
if (test.type === "BinaryExpression" && test.left.type === "MemberExpression" && test.left.property.name === "OS" && typeof test.right.value === "string" && ["===", "!=="].includes(test.operator)) return member(module2, test.right.value === platform === (test.operator === "===") ? expression.consequent : expression.alternate, path, consumed);
|
|
186
|
+
return;
|
|
61
187
|
}
|
|
62
|
-
if (expression.type === "CallExpression" && expression.callee.type === "MemberExpression" && expression.callee.object.name === "Object" && expression.callee.property.name === "freeze") return member(expression.arguments[0], path, consumed);
|
|
63
188
|
if (expression.type !== "ObjectExpression" || !path.length) return;
|
|
64
189
|
for (const property of expression.properties.toReversed()) {
|
|
65
190
|
if (property.type === "Property" && !property.computed && (property.key.name ?? property.key.value) === path[0]) {
|
|
66
|
-
if (property.kind === "init") return member(property.value, path.slice(1), consumed + 1);
|
|
191
|
+
if (property.kind === "init") return member(module2, property.value, path.slice(1), consumed + 1);
|
|
67
192
|
const body = property.value.body.body;
|
|
68
193
|
const returned = body.length === 1 && body[0].type === "ReturnStatement" ? body[0].argument : void 0;
|
|
69
194
|
if (property.kind === "get" && returned?.type === "CallExpression" && returned.callee.type === "Identifier" && !returned.arguments.length) {
|
|
70
|
-
const
|
|
195
|
+
const imported = module2.imports.get(returned.callee.name);
|
|
196
|
+
const binding = imported && bind(imported);
|
|
71
197
|
if (binding) return {
|
|
72
198
|
binding,
|
|
73
199
|
consumed: consumed + 1,
|
|
@@ -77,24 +203,69 @@ function nativeOneMembers(file) {
|
|
|
77
203
|
return;
|
|
78
204
|
}
|
|
79
205
|
if (property.type === "SpreadElement") {
|
|
80
|
-
const found = member(property.argument, path, consumed);
|
|
81
|
-
if (found) return found;
|
|
206
|
+
const found = member(module2, property.argument, path, consumed);
|
|
207
|
+
if (found && found.consumed > consumed) return found;
|
|
82
208
|
}
|
|
83
209
|
}
|
|
84
210
|
};
|
|
85
|
-
|
|
211
|
+
const shared = load(file);
|
|
212
|
+
const one = shared.values.get("One");
|
|
213
|
+
return (source, id, escape = () => {}) => {
|
|
214
|
+
if (/\b(require|import)\s*\(\s*["'`]one["'`]\s*\)/.test(source) || /export\s*(\*(\s*as\s+[\w$]+)?|\{[^}]*\bOne\b[^}]*\})\s*from\s*["']one["']/.test(source)) escape("dynamic or re-exported one");
|
|
86
215
|
const parsed = (0, import_oxc_parser.parseSync)(id, source, { lang: /\.tsx$/.test(id) ? "tsx" : /\.[cm]?ts$/.test(id) ? "ts" : "jsx" });
|
|
87
|
-
if (parsed.errors.length)
|
|
216
|
+
if (parsed.errors.length) {
|
|
217
|
+
escape("parse error");
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
88
220
|
const app = parsed.program;
|
|
89
221
|
const roots = /* @__PURE__ */ new Map();
|
|
90
222
|
for (const node of app.body) {
|
|
91
|
-
if (node.type !== "ImportDeclaration" || node.source.value !== "one") continue;
|
|
223
|
+
if (node.type !== "ImportDeclaration" || node.source.value !== "one" || node.importKind === "type") continue;
|
|
92
224
|
for (const item of node.specifiers) {
|
|
93
|
-
if (item.type === "ImportSpecifier" && (item.imported.name ?? item.imported.value) === "One") roots.set(item.local.name,
|
|
94
|
-
|
|
225
|
+
if (item.type === "ImportSpecifier" && item.importKind !== "type" && (item.imported.name ?? item.imported.value) === "One") roots.set(item.local.name, {
|
|
226
|
+
prefix: [],
|
|
227
|
+
base: []
|
|
228
|
+
});
|
|
229
|
+
else if (item.type === "ImportNamespaceSpecifier") roots.set(item.local.name, {
|
|
230
|
+
prefix: ["One"],
|
|
231
|
+
base: []
|
|
232
|
+
});
|
|
95
233
|
}
|
|
96
234
|
}
|
|
97
235
|
if (!roots.size) return;
|
|
236
|
+
const chain = (node) => {
|
|
237
|
+
const path = [];
|
|
238
|
+
const nodes = [];
|
|
239
|
+
let root = node;
|
|
240
|
+
while (root.type === "MemberExpression" && !root.computed || root.type === "JSXMemberExpression") {
|
|
241
|
+
path.unshift(root.property.name);
|
|
242
|
+
nodes.unshift(root);
|
|
243
|
+
root = root.object;
|
|
244
|
+
}
|
|
245
|
+
const read = roots.get(root.name);
|
|
246
|
+
return {
|
|
247
|
+
root,
|
|
248
|
+
path,
|
|
249
|
+
nodes,
|
|
250
|
+
read,
|
|
251
|
+
onePath: read && read.prefix.every((name, index) => path[index] === name) ? [...read.base, ...path.slice(read.prefix.length)] : void 0
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
const aliases = /* @__PURE__ */ new Set();
|
|
255
|
+
for (const statement of app.body) {
|
|
256
|
+
if (statement.type !== "VariableDeclaration" || statement.kind !== "const") continue;
|
|
257
|
+
for (const declaration of statement.declarations) {
|
|
258
|
+
if (declaration.id.type !== "Identifier" || !declaration.init) continue;
|
|
259
|
+
const { onePath } = chain(declaration.init);
|
|
260
|
+
if (!onePath?.length) continue;
|
|
261
|
+
if (member(shared, one, onePath)?.consumed === onePath.length) continue;
|
|
262
|
+
roots.set(declaration.id.name, {
|
|
263
|
+
prefix: [],
|
|
264
|
+
base: onePath
|
|
265
|
+
});
|
|
266
|
+
aliases.add(declaration);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
98
269
|
const children = (node, visit2) => {
|
|
99
270
|
for (const [key, value] of Object.entries(node)) {
|
|
100
271
|
if (key === "typeAnnotation" || key === "returnType" || key === "typeParameters" || key === "typeArguments") continue;
|
|
@@ -104,11 +275,14 @@ function nativeOneMembers(file) {
|
|
|
104
275
|
const pattern = (node) => {
|
|
105
276
|
if (node.type === "Identifier") roots.delete(node.name);
|
|
106
277
|
else if (node.type === "Property") pattern(node.value);
|
|
107
|
-
else
|
|
278
|
+
else if (node.type === "AssignmentPattern") pattern(node.left);
|
|
279
|
+
else if (node.type === "RestElement") pattern(node.argument);
|
|
280
|
+
else if (node.type === "TSParameterProperty") pattern(node.parameter);
|
|
281
|
+
else if (node.type === "ArrayPattern" || node.type === "ObjectPattern") children(node, pattern);
|
|
108
282
|
};
|
|
109
283
|
const shadows = (node) => {
|
|
110
284
|
if (node.type === "ImportDeclaration") return;
|
|
111
|
-
if (node.type === "VariableDeclarator") pattern(node.id);
|
|
285
|
+
if (node.type === "VariableDeclarator" && !aliases.has(node)) pattern(node.id);
|
|
112
286
|
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
113
287
|
if (node.id) pattern(node.id);
|
|
114
288
|
for (const parameter of node.params) pattern(parameter);
|
|
@@ -119,46 +293,103 @@ function nativeOneMembers(file) {
|
|
|
119
293
|
if (node.type === "CatchClause" && node.param) pattern(node.param);
|
|
120
294
|
children(node, shadows);
|
|
121
295
|
};
|
|
296
|
+
const imported = roots.size;
|
|
122
297
|
shadows(app);
|
|
298
|
+
if (roots.size < imported) escape("shadowed One");
|
|
123
299
|
const output = new import_magic_string.default(source);
|
|
124
300
|
const added = /* @__PURE__ */ new Map();
|
|
301
|
+
const rootOf = (node) => node.type === "MemberExpression" ? rootOf(node.object) : node;
|
|
302
|
+
const escaped = /* @__PURE__ */ new Set();
|
|
303
|
+
const kept = (name, reason) => {
|
|
304
|
+
escaped.add(name);
|
|
305
|
+
escape(reason);
|
|
306
|
+
};
|
|
307
|
+
const typeOnly = (node) => node.declare || node.type === "TSTypeAliasDeclaration" || node.type === "TSInterfaceDeclaration" || node.type === "TSDeclareFunction" || node.type.startsWith("TS") && node.type.endsWith("Type");
|
|
308
|
+
let rewritten = 0;
|
|
125
309
|
const visit = (node) => {
|
|
126
|
-
if (node.type === "ImportDeclaration" ||
|
|
127
|
-
if (node.type === "
|
|
128
|
-
|
|
310
|
+
if (node.type === "ImportDeclaration" || aliases.has(node) || typeOnly(node)) return;
|
|
311
|
+
if (node.type === "Identifier") {
|
|
312
|
+
if (roots.has(node.name)) kept(node.name, `One read at ${node.start}`);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if ((node.type === "Property" || node.type === "MethodDefinition" || node.type === "PropertyDefinition") && !node.computed) {
|
|
316
|
+
if (node.value) visit(node.value);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const target = node.type === "AssignmentExpression" ? node.left : node.type === "UpdateExpression" || node.type === "UnaryExpression" && node.operator === "delete" ? node.argument : void 0;
|
|
320
|
+
if (target) {
|
|
321
|
+
if (roots.has(rootOf(target).name)) kept(rootOf(target).name, `One written at ${node.start}`);
|
|
322
|
+
if (node.type === "AssignmentExpression") visit(node.right);
|
|
129
323
|
return;
|
|
130
324
|
}
|
|
131
|
-
if (node.type === "UpdateExpression" || node.type === "UnaryExpression" && node.operator === "delete") return;
|
|
132
325
|
if (node.type === "MemberExpression" || node.type === "JSXMemberExpression") {
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
const prefix = roots.get(root.name);
|
|
142
|
-
if (prefix && prefix.every((name, index) => path[index] === name)) {
|
|
143
|
-
const found = member(values.get("One"), path.slice(prefix.length));
|
|
144
|
-
if (found && found.consumed && !(found.call && node.type === "JSXMemberExpression")) {
|
|
145
|
-
let alias = added.get(found.binding);
|
|
146
|
-
if (!alias) {
|
|
147
|
-
alias = `__one_member_${added.size}`;
|
|
148
|
-
while (new RegExp(`\\b${alias}\\b`).test(source)) alias += "_";
|
|
149
|
-
added.set(found.binding, alias);
|
|
150
|
-
}
|
|
151
|
-
output.overwrite(root.start, nodes[prefix.length + found.consumed - 1].end, found.call ? `(${alias}())` : alias);
|
|
326
|
+
const { root, nodes, read, onePath } = chain(node);
|
|
327
|
+
if (read) {
|
|
328
|
+
if (!onePath) return;
|
|
329
|
+
const found = member(shared, one, onePath);
|
|
330
|
+
const consumed = (found?.consumed ?? 0) - read.base.length;
|
|
331
|
+
if (!found || consumed < 1 || found.call && node.type === "JSXMemberExpression") {
|
|
332
|
+
kept(root.name, `One.${onePath.join(".")}`);
|
|
152
333
|
return;
|
|
153
334
|
}
|
|
335
|
+
const key = `${found.binding.source}\0${found.binding.imported}`;
|
|
336
|
+
let alias = added.get(key)?.alias;
|
|
337
|
+
if (!alias) {
|
|
338
|
+
alias = `__one_member_${added.size}`;
|
|
339
|
+
while (new RegExp(`\\b${alias}\\b`).test(source)) alias += "_";
|
|
340
|
+
added.set(key, {
|
|
341
|
+
binding: found.binding,
|
|
342
|
+
alias
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
rewritten++;
|
|
346
|
+
output.overwrite(root.start, nodes[read.prefix.length + consumed - 1].end, found.call ? `(${alias}())` : alias + (found.suffix ?? []).map((name) => `.${name}`).join(""));
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (nodes.length) {
|
|
350
|
+
visit(root);
|
|
351
|
+
return;
|
|
154
352
|
}
|
|
155
353
|
}
|
|
156
354
|
children(node, visit);
|
|
157
355
|
};
|
|
158
356
|
visit(app);
|
|
159
|
-
|
|
357
|
+
let references = 0;
|
|
358
|
+
const count = (node) => {
|
|
359
|
+
if (node.type === "ImportDeclaration" || aliases.has(node) || typeOnly(node)) return;
|
|
360
|
+
if (node.type === "Identifier" || node.type === "JSXIdentifier") {
|
|
361
|
+
if (roots.has(node.name)) references++;
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (node.type === "MemberExpression" || node.type === "JSXMemberExpression") {
|
|
365
|
+
count(node.object);
|
|
366
|
+
if (node.computed) count(node.property);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if ((node.type === "Property" || node.type === "MethodDefinition" || node.type === "PropertyDefinition" || node.type === "JSXAttribute") && !node.computed) {
|
|
370
|
+
if (node.value) count(node.value);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (node.type === "LabeledStatement" || node.type === "BreakStatement" || node.type === "ContinueStatement") {
|
|
374
|
+
if (node.body) count(node.body);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (node.type === "ExportSpecifier") {
|
|
378
|
+
count(node.local);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
children(node, count);
|
|
382
|
+
};
|
|
383
|
+
count(app);
|
|
384
|
+
const complete = references === rewritten && roots.size === imported;
|
|
385
|
+
if (!complete) escape("One reference left unrewritten");
|
|
386
|
+
for (const declaration of [...aliases].toReversed()) {
|
|
387
|
+
if (complete && roots.has(declaration.id.name) && !escaped.has(declaration.id.name)) output.overwrite(declaration.init.start, declaration.init.end, "void 0");
|
|
388
|
+
else escaped.add(chain(declaration.init).root.name);
|
|
389
|
+
}
|
|
390
|
+
if (!added.size && !aliases.size) return;
|
|
160
391
|
const lastImport = app.body.filter((node) => node.type === "ImportDeclaration").at(-1);
|
|
161
|
-
output.appendLeft(lastImport.end, "\n" + [...added].map((
|
|
392
|
+
output.appendLeft(lastImport.end, "\n" + [...added.values()].map(({ binding, alias }) => `import ${binding.imported === "*" ? `* as ${alias}` : `{ ${binding.imported} as ${alias} }`} from ${JSON.stringify(binding.source)};
|
|
162
393
|
`).join(""));
|
|
163
394
|
return {
|
|
164
395
|
code: output.toString(),
|