solid-optimizer 1.0.0-next.8 → 1.0.0-next.9
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/{compile-O1Z60VsW.mjs → compile-DptOANJi.mjs} +120 -14
- package/dist/compile-DptOANJi.mjs.map +1 -0
- package/dist/index.mjs +1 -1
- package/dist/vite.mjs +1 -1
- package/package.json +1 -1
- package/src/fold.ts +104 -0
- package/src/inline.ts +3 -1
- package/src/scope.ts +115 -9
- package/dist/compile-O1Z60VsW.mjs.map +0 -1
|
@@ -258,6 +258,30 @@ function isWithinScope(scope, ancestor) {
|
|
|
258
258
|
}
|
|
259
259
|
return false;
|
|
260
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Whether a parameter list holds an expression: a default value or a computed
|
|
263
|
+
* key, at any depth.
|
|
264
|
+
*/
|
|
265
|
+
function hasParameterExpressions(params) {
|
|
266
|
+
for (const param of params) if (hasPatternExpression(param)) return true;
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
function hasPatternExpression(pattern) {
|
|
270
|
+
switch (pattern.type) {
|
|
271
|
+
case "AssignmentPattern": return true;
|
|
272
|
+
case "ObjectPattern":
|
|
273
|
+
for (const property of pattern.properties) if (property.type === "RestElement") {
|
|
274
|
+
if (hasPatternExpression(property.argument)) return true;
|
|
275
|
+
} else if (property.computed || hasPatternExpression(property.value)) return true;
|
|
276
|
+
return false;
|
|
277
|
+
case "ArrayPattern":
|
|
278
|
+
for (const element of pattern.elements) if (element && hasPatternExpression(element)) return true;
|
|
279
|
+
return false;
|
|
280
|
+
case "RestElement": return hasPatternExpression(pattern.argument);
|
|
281
|
+
case "TSParameterProperty": return hasPatternExpression(pattern.parameter);
|
|
282
|
+
default: return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
261
285
|
function exportName$1(name) {
|
|
262
286
|
return name.type === "Literal" ? name.value : name.name;
|
|
263
287
|
}
|
|
@@ -266,6 +290,7 @@ var Analyzer = class {
|
|
|
266
290
|
this.scopes = /* @__PURE__ */ new Map();
|
|
267
291
|
this.names = /* @__PURE__ */ new Set();
|
|
268
292
|
this.pending = [];
|
|
293
|
+
this.bodies = /* @__PURE__ */ new Set();
|
|
269
294
|
this.root = {
|
|
270
295
|
node: program,
|
|
271
296
|
parent: void 0,
|
|
@@ -303,7 +328,10 @@ var Analyzer = class {
|
|
|
303
328
|
}
|
|
304
329
|
declare(identifier, kind, declaration, source, imported) {
|
|
305
330
|
let target = this.scope;
|
|
306
|
-
if (kind === "var")
|
|
331
|
+
if (kind === "var") {
|
|
332
|
+
while (!target.isFunction && target.parent) target = target.parent;
|
|
333
|
+
if (this.bodies.has(target.node) && target.parent?.bindings.has(identifier.name)) target = target.parent;
|
|
334
|
+
}
|
|
307
335
|
this.names.add(identifier.name);
|
|
308
336
|
const existing = target.bindings.get(identifier.name);
|
|
309
337
|
if (existing) {
|
|
@@ -399,10 +427,23 @@ var Analyzer = class {
|
|
|
399
427
|
this.withScope(node, true, () => {
|
|
400
428
|
if (node.type === "FunctionExpression" && node.id) this.declare(node.id, "other", node);
|
|
401
429
|
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
402
|
-
if (node.body)
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
430
|
+
if (node.body) this.visitBody(node.body, node.params);
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Visits the block body of a function. When a parameter holds an expression,
|
|
435
|
+
* the body gets its own scope, so the expression cannot see the body's
|
|
436
|
+
* declarations.
|
|
437
|
+
*/
|
|
438
|
+
visitBody(body, params) {
|
|
439
|
+
if (!hasParameterExpressions(params)) {
|
|
440
|
+
this.scopes.set(body, this.scope);
|
|
441
|
+
this.visitStatements(body.body);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this.bodies.add(body);
|
|
445
|
+
this.withScope(body, true, () => {
|
|
446
|
+
this.visitStatements(body.body);
|
|
406
447
|
});
|
|
407
448
|
}
|
|
408
449
|
visitClass(node) {
|
|
@@ -458,10 +499,8 @@ var Analyzer = class {
|
|
|
458
499
|
case "ArrowFunctionExpression":
|
|
459
500
|
this.withScope(node, true, () => {
|
|
460
501
|
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
461
|
-
if (node.body.type === "BlockStatement")
|
|
462
|
-
|
|
463
|
-
this.visitStatements(node.body.body);
|
|
464
|
-
} else this.visit(node.body);
|
|
502
|
+
if (node.body.type === "BlockStatement") this.visitBody(node.body, node.params);
|
|
503
|
+
else this.visit(node.body);
|
|
465
504
|
});
|
|
466
505
|
break;
|
|
467
506
|
case "ClassDeclaration":
|
|
@@ -562,13 +601,27 @@ var Analyzer = class {
|
|
|
562
601
|
for (const child of node.children) this.visit(child);
|
|
563
602
|
if (node.closingElement) this.visitJSXName(node.closingElement.name);
|
|
564
603
|
break;
|
|
565
|
-
case "TSEnumDeclaration":
|
|
566
604
|
case "TSImportEqualsDeclaration":
|
|
567
605
|
this.declare(node.id, "other", node);
|
|
568
606
|
break;
|
|
569
|
-
case "
|
|
570
|
-
|
|
607
|
+
case "TSEnumDeclaration":
|
|
608
|
+
this.declare(node.id, "other", node);
|
|
609
|
+
if (node.declare) break;
|
|
610
|
+
this.withScope(node, false, () => {
|
|
611
|
+
for (const member of node.body.members) if (member.id.type === "Identifier") this.declare(member.id, "other", member);
|
|
612
|
+
for (const member of node.body.members) if (member.initializer) this.visit(member.initializer);
|
|
613
|
+
});
|
|
614
|
+
break;
|
|
615
|
+
case "TSModuleDeclaration": {
|
|
616
|
+
let id = node.id;
|
|
617
|
+
while (id.type === "TSQualifiedName") id = id.left;
|
|
618
|
+
if (id.type === "Identifier") this.declare(id, "other", node);
|
|
619
|
+
const { body } = node;
|
|
620
|
+
if (!node.declare && body) this.withScope(node, true, () => {
|
|
621
|
+
this.visitStatements(body.body);
|
|
622
|
+
});
|
|
571
623
|
break;
|
|
624
|
+
}
|
|
572
625
|
default: forEachChild(node, (child) => {
|
|
573
626
|
this.visit(child);
|
|
574
627
|
});
|
|
@@ -1467,9 +1520,11 @@ var Folder = class {
|
|
|
1467
1520
|
constructor(context) {
|
|
1468
1521
|
this.context = context;
|
|
1469
1522
|
this.changed = false;
|
|
1523
|
+
this.removed = /* @__PURE__ */ new Set();
|
|
1470
1524
|
this.lookup = new ConstantTable(context.scopes, context.options).lookup;
|
|
1471
1525
|
}
|
|
1472
1526
|
walk(node) {
|
|
1527
|
+
if (this.removed.has(node)) return;
|
|
1473
1528
|
forEachChild(node, (child) => {
|
|
1474
1529
|
this.walk(child);
|
|
1475
1530
|
});
|
|
@@ -2160,6 +2215,56 @@ var Folder = class {
|
|
|
2160
2215
|
this.changed = true;
|
|
2161
2216
|
}
|
|
2162
2217
|
}
|
|
2218
|
+
/**
|
|
2219
|
+
* Removes each declaration inside a function that nothing references. The
|
|
2220
|
+
* references are those of the code this pass parsed, so a binding whose last
|
|
2221
|
+
* read this pass folds is removed in the next round.
|
|
2222
|
+
*/
|
|
2223
|
+
removeUnusedDeclarations() {
|
|
2224
|
+
for (const scope of this.context.scopes.scopes.values()) {
|
|
2225
|
+
if (scope === this.context.scopes.root) continue;
|
|
2226
|
+
for (const binding of scope.bindings.values()) {
|
|
2227
|
+
const declaration = this.unusedDeclaration(binding);
|
|
2228
|
+
if (declaration && !this.removed.has(declaration)) {
|
|
2229
|
+
this.removed.add(declaration);
|
|
2230
|
+
this.context.s.remove(declaration.start, declaration.end);
|
|
2231
|
+
this.changed = true;
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
/**
|
|
2237
|
+
* The declaration statement of `binding` when every binding it declares is
|
|
2238
|
+
* unused and every initializer can be skipped.
|
|
2239
|
+
*/
|
|
2240
|
+
unusedDeclaration(binding) {
|
|
2241
|
+
if (binding.declaration.type !== "VariableDeclarator") return;
|
|
2242
|
+
const statement = this.context.parents.get(binding.declaration);
|
|
2243
|
+
if (statement?.type !== "VariableDeclaration" || statement.kind !== "const" && statement.kind !== "let" || statement.declare) return;
|
|
2244
|
+
const container = this.context.parents.get(statement)?.type;
|
|
2245
|
+
if (container !== "BlockStatement" && container !== "StaticBlock" && container !== "SwitchCase") return;
|
|
2246
|
+
for (const declarator of statement.declarations) {
|
|
2247
|
+
if (declarator.id.type !== "Identifier") return;
|
|
2248
|
+
const declared = binding.scope.bindings.get(declarator.id.name);
|
|
2249
|
+
if (!declared || declared.mutated || declared.references.length > 0) return;
|
|
2250
|
+
if (declarator.init && !this.isSkippableInit(declarator.init)) return;
|
|
2251
|
+
}
|
|
2252
|
+
return statement;
|
|
2253
|
+
}
|
|
2254
|
+
/**
|
|
2255
|
+
* Whether an unused initializer can be dropped: an expression with no side
|
|
2256
|
+
* effects, or a Solid call that only creates a value from such arguments.
|
|
2257
|
+
*/
|
|
2258
|
+
isSkippableInit(init) {
|
|
2259
|
+
if (isSideEffectFree(init)) return true;
|
|
2260
|
+
const call = unwrap(init);
|
|
2261
|
+
if (call.type !== "CallExpression" || call.callee.type !== "Identifier") return false;
|
|
2262
|
+
const callee = this.context.scopes.references.get(call.callee);
|
|
2263
|
+
const identity = callee ? solidExport(callee, this.context.options) : void 0;
|
|
2264
|
+
if (identity === void 0 || !PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) return false;
|
|
2265
|
+
for (const argument of call.arguments) if (argument.type === "SpreadElement" || !isSideEffectFree(argument)) return false;
|
|
2266
|
+
return true;
|
|
2267
|
+
}
|
|
2163
2268
|
replaceStatement(statement, text) {
|
|
2164
2269
|
if (text === "") {
|
|
2165
2270
|
this.context.s.remove(statement.start, statement.end);
|
|
@@ -2208,6 +2313,7 @@ var Folder = class {
|
|
|
2208
2313
|
*/
|
|
2209
2314
|
function fold(context) {
|
|
2210
2315
|
const folder = new Folder(context);
|
|
2316
|
+
folder.removeUnusedDeclarations();
|
|
2211
2317
|
folder.walk(context.program);
|
|
2212
2318
|
return folder.changed;
|
|
2213
2319
|
}
|
|
@@ -3387,7 +3493,7 @@ var Inliner = class {
|
|
|
3387
3493
|
if (init.type !== "CallExpression" || callee !== "merge" && callee !== "omit") continue;
|
|
3388
3494
|
const view = this.resolveView(callee, init, param, views, args);
|
|
3389
3495
|
if (view === null) continue;
|
|
3390
|
-
const binding = scope.bindings.get(declarator.id.name);
|
|
3496
|
+
const binding = ((fn.body && this.context.scopes.scopes.get(fn.body)) ?? scope).bindings.get(declarator.id.name);
|
|
3391
3497
|
if (!view || !binding || binding.mutated) return;
|
|
3392
3498
|
views.set(binding, view);
|
|
3393
3499
|
declarations.push(statement);
|
|
@@ -5796,4 +5902,4 @@ function readModuleConstants(code, options = {}) {
|
|
|
5796
5902
|
//#endregion
|
|
5797
5903
|
export { analyzeScopes as a, collectParents as c, parse$1 as d, unwrap as f, runtimeImportSources as i, forEachChild as l, readModuleConstants as n, lookup as o, CONTEXT_SAFE_PRIMITIVES as r, scopeAt as s, compile as t, isIntrinsicTag as u };
|
|
5798
5904
|
|
|
5799
|
-
//# sourceMappingURL=compile-
|
|
5905
|
+
//# sourceMappingURL=compile-DptOANJi.mjs.map
|