@wyw-in-js/transform 2.0.0-alpha.1 → 2.0.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/esm/transform/Entrypoint.types.js.map +1 -1
- package/esm/transform/generators/collect.js +1 -0
- package/esm/transform/generators/collect.js.map +1 -1
- package/esm/transform/generators/resolveStaticOxcValues/prune.js +91 -5
- package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -1
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +2 -1
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js.map +1 -1
- package/esm/transform/generators/transform.js +1 -0
- package/esm/transform/generators/transform.js.map +1 -1
- package/esm/utils/EventEmitter.js +13 -0
- package/esm/utils/EventEmitter.js.map +1 -1
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js +1 -1
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js.map +1 -1
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js +102 -16
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js.map +1 -1
- package/esm/utils/collectOxcExportsAndImports.js +8 -0
- package/esm/utils/collectOxcExportsAndImports.js.map +1 -1
- package/esm/utils/collectOxcRuntime/types.js.map +1 -1
- package/esm/utils/oxc/ast.js +42 -12
- package/esm/utils/oxc/ast.js.map +1 -1
- package/esm/utils/oxcPreevalTransforms.js +94 -65
- package/esm/utils/oxcPreevalTransforms.js.map +1 -1
- package/esm/utils/parseOxc.js +6 -1
- package/esm/utils/parseOxc.js.map +1 -1
- package/package.json +5 -5
- package/types/transform/Entrypoint.types.d.ts +1 -0
- package/types/transform/generators/collect.js +1 -0
- package/types/transform/generators/resolveStaticOxcValues/prune.d.ts +1 -1
- package/types/transform/generators/resolveStaticOxcValues/prune.js +96 -4
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +2 -1
- package/types/transform/generators/transform.js +1 -0
- package/types/utils/EventEmitter.js +13 -0
- package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +1 -0
- package/types/utils/applyOxcProcessors/applyOxcProcessors.js +3 -1
- package/types/utils/applyOxcProcessors/cleanupRemovals.d.ts +2 -2
- package/types/utils/applyOxcProcessors/cleanupRemovals.js +104 -19
- package/types/utils/collectOxcExportsAndImports.js +8 -0
- package/types/utils/collectOxcRuntime/types.d.ts +1 -0
- package/types/utils/oxc/ast.js +42 -18
- package/types/utils/oxcPreevalTransforms.js +97 -74
- package/types/utils/parseOxc.js +6 -1
|
@@ -303,26 +303,40 @@ export const mergeEmptyRemovalRanges = (removals) => {
|
|
|
303
303
|
});
|
|
304
304
|
return merged;
|
|
305
305
|
};
|
|
306
|
-
export const collectUnusedImportRemovals = (code, program, referencedNames, removableNames, preserveSideEffectImportLocals) => {
|
|
306
|
+
export const collectUnusedImportRemovals = (code, program, referencedNames, removableNames, preserveSideEffectImportLocals, preserveSideEffectImportOrderLocals = preserveSideEffectImportLocals) => {
|
|
307
307
|
const removals = [];
|
|
308
|
+
const importSourceByLocal = new Map();
|
|
309
|
+
const removedSideEffectImportRanges = [];
|
|
310
|
+
const keptImportRangesBySource = new Map();
|
|
308
311
|
program.body.forEach((statement) => {
|
|
309
312
|
if (statement.type !== "ImportDeclaration") {
|
|
310
313
|
return;
|
|
311
314
|
}
|
|
312
315
|
const localNames = collectImportLocalNames(statement);
|
|
316
|
+
const source = code.slice(statement.source.start, statement.source.end);
|
|
317
|
+
const orderedLocalNames = localNames.filter((localName) => preserveSideEffectImportOrderLocals.has(localName));
|
|
318
|
+
const sideEffectLocalNames = localNames.filter((localName) => preserveSideEffectImportLocals.has(localName));
|
|
319
|
+
[...orderedLocalNames, ...sideEffectLocalNames].forEach((localName) => {
|
|
320
|
+
importSourceByLocal.set(localName, source);
|
|
321
|
+
});
|
|
313
322
|
const removableLocalNames = localNames.filter((localName) => removableNames.has(localName));
|
|
314
323
|
if (removableLocalNames.length > 0 && removableLocalNames.length === localNames.length && removableLocalNames.every((localName) => !referencedNames.has(localName))) {
|
|
315
324
|
if (removableLocalNames.some((localName) => preserveSideEffectImportLocals.has(localName))) {
|
|
316
|
-
|
|
325
|
+
removedSideEffectImportRanges.push({
|
|
317
326
|
end: statement.end,
|
|
318
|
-
start: statement.start
|
|
319
|
-
value: `import ${code.slice(statement.source.start, statement.source.end)};`
|
|
327
|
+
start: statement.start
|
|
320
328
|
});
|
|
321
329
|
return;
|
|
322
330
|
}
|
|
323
331
|
removals.push(expandImportRemovalRange(code, statement.start, statement.end));
|
|
324
332
|
return;
|
|
325
333
|
}
|
|
334
|
+
if (orderedLocalNames.length > 0 && !keptImportRangesBySource.has(source)) {
|
|
335
|
+
keptImportRangesBySource.set(source, {
|
|
336
|
+
end: statement.end,
|
|
337
|
+
start: statement.start
|
|
338
|
+
});
|
|
339
|
+
}
|
|
326
340
|
const { specifiers } = statement;
|
|
327
341
|
if (!Array.isArray(specifiers) || specifiers.length <= 1) {
|
|
328
342
|
return;
|
|
@@ -337,6 +351,65 @@ export const collectUnusedImportRemovals = (code, program, referencedNames, remo
|
|
|
337
351
|
}
|
|
338
352
|
});
|
|
339
353
|
});
|
|
354
|
+
if (removedSideEffectImportRanges.length > 0) {
|
|
355
|
+
const seenSources = new Set();
|
|
356
|
+
const removedRanges = removedSideEffectImportRanges.sort((a, b) => a.start - b.start);
|
|
357
|
+
const [firstRemoved, ...restRemoved] = removedRanges;
|
|
358
|
+
const pendingImports = [];
|
|
359
|
+
let insertionAfterLastKept = null;
|
|
360
|
+
let usedFirstRemovedRange = false;
|
|
361
|
+
const flushBefore = (position) => {
|
|
362
|
+
if (pendingImports.length === 0) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
removals.push({
|
|
366
|
+
end: position,
|
|
367
|
+
start: position,
|
|
368
|
+
value: `${pendingImports.join("\n")}\n`
|
|
369
|
+
});
|
|
370
|
+
pendingImports.length = 0;
|
|
371
|
+
};
|
|
372
|
+
[...preserveSideEffectImportOrderLocals].forEach((localName) => {
|
|
373
|
+
const source = importSourceByLocal.get(localName);
|
|
374
|
+
if (!source) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const keptRange = keptImportRangesBySource.get(source);
|
|
378
|
+
if (keptRange) {
|
|
379
|
+
flushBefore(keptRange.start);
|
|
380
|
+
insertionAfterLastKept = keptRange.end;
|
|
381
|
+
if (preserveSideEffectImportLocals.has(localName)) {
|
|
382
|
+
seenSources.add(source);
|
|
383
|
+
}
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (!preserveSideEffectImportLocals.has(localName) || seenSources.has(source)) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
seenSources.add(source);
|
|
390
|
+
pendingImports.push(`import ${source};`);
|
|
391
|
+
});
|
|
392
|
+
if (pendingImports.length > 0) {
|
|
393
|
+
if (insertionAfterLastKept !== null) {
|
|
394
|
+
removals.push({
|
|
395
|
+
end: insertionAfterLastKept,
|
|
396
|
+
start: insertionAfterLastKept,
|
|
397
|
+
value: `\n${pendingImports.join("\n")}`
|
|
398
|
+
});
|
|
399
|
+
} else if (firstRemoved) {
|
|
400
|
+
usedFirstRemovedRange = true;
|
|
401
|
+
removals.push({
|
|
402
|
+
end: firstRemoved.end,
|
|
403
|
+
start: firstRemoved.start,
|
|
404
|
+
value: pendingImports.join("\n")
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
removals.push(...(usedFirstRemovedRange ? restRemoved : removedRanges).map((range) => ({
|
|
409
|
+
...range,
|
|
410
|
+
value: ""
|
|
411
|
+
})));
|
|
412
|
+
}
|
|
340
413
|
return removals;
|
|
341
414
|
};
|
|
342
415
|
export const collectUnusedTopLevelDeclarationRemovals = (code, program, referencedNames, removableNames) => {
|
|
@@ -393,20 +466,20 @@ export const collectEmptyTopLevelBlockRemovals = (code, program) => {
|
|
|
393
466
|
});
|
|
394
467
|
return removals;
|
|
395
468
|
};
|
|
396
|
-
export const removeUnusedAfterReplacement = (code, filename, initialRemovableNames, removableExpressionRefs, preserveSideEffectImportLocals) => {
|
|
469
|
+
export const removeUnusedAfterReplacement = (code, filename, initialRemovableNames, removableExpressionRefs, preserveSideEffectImportLocals, preserveSideEffectImportOrderLocals = preserveSideEffectImportLocals) => {
|
|
397
470
|
let current = code;
|
|
471
|
+
let program = null;
|
|
398
472
|
const cumulativeRemovableNames = new Set(initialRemovableNames);
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
return current;
|
|
405
|
-
}
|
|
406
|
-
};
|
|
473
|
+
// Incremental cleanup loop: validate-by-parsing the next iteration's
|
|
474
|
+
// candidate code AND reuse that parse as the next iter's `program` input,
|
|
475
|
+
// instead of re-parsing at the top of the next iter. Saves one parse per
|
|
476
|
+
// loop revolution (N+1 parses for an N-iter loop instead of 2N).
|
|
477
|
+
// Also short-circuits a round earlier when no removals were collected.
|
|
407
478
|
for (let idx = 0; idx < 5; idx += 1) {
|
|
408
479
|
const previous = current;
|
|
409
|
-
|
|
480
|
+
if (program === null) {
|
|
481
|
+
program = parseOxc(current, filename);
|
|
482
|
+
}
|
|
410
483
|
const statements = collectTopLevelStatementInfos(program);
|
|
411
484
|
const removableNames = collectRemovableNamesFromStatements(statements, cumulativeRemovableNames);
|
|
412
485
|
removableNames.forEach((name) => cumulativeRemovableNames.add(name));
|
|
@@ -417,11 +490,24 @@ export const removeUnusedAfterReplacement = (code, filename, initialRemovableNam
|
|
|
417
490
|
...collectUnusedScopedDeclarationRemovals(current, scopedBindings, cumulativeRemovableNames),
|
|
418
491
|
...collectUnusedTopLevelDeclarationRemovals(current, program, referencedNames, cumulativeRemovableNames),
|
|
419
492
|
...collectUnusedGeneratedHelperDeclarationRemovals(current, program, referencedNames),
|
|
420
|
-
...collectUnusedImportRemovals(current, program, referencedNames, cumulativeRemovableNames, preserveSideEffectImportLocals),
|
|
493
|
+
...collectUnusedImportRemovals(current, program, referencedNames, cumulativeRemovableNames, preserveSideEffectImportLocals, preserveSideEffectImportOrderLocals),
|
|
421
494
|
...collectTopLevelExpressionStatementRemovals(current, statements, topLevelBindings, removableExpressionRefs),
|
|
422
495
|
...collectEmptyTopLevelBlockRemovals(current, program)
|
|
423
496
|
]);
|
|
424
|
-
|
|
497
|
+
if (removals.length === 0) {
|
|
498
|
+
// Convergence: next iter would parse the same code and see the same
|
|
499
|
+
// removable set. Skip the round of walks + parse.
|
|
500
|
+
return current;
|
|
501
|
+
}
|
|
502
|
+
const next = applyOxcReplacements(current, removals);
|
|
503
|
+
try {
|
|
504
|
+
// Validate + capture the AST for the next iteration in one parse.
|
|
505
|
+
program = parseOxc(next, filename);
|
|
506
|
+
current = next;
|
|
507
|
+
} catch {
|
|
508
|
+
// Pathological removal — drop this iteration and return prior state.
|
|
509
|
+
return current;
|
|
510
|
+
}
|
|
425
511
|
if (current === previous) {
|
|
426
512
|
return current;
|
|
427
513
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAIA,SAAS,oBAAoB,iBAAiB;AAC9C,SAAS,4BAA4B;AACrC,SACE,sBACA,yBACA,wBACA,qCACA,yBACA,uCACA,+BACA,6BACA,uBACK;AACP,SAAS,0BAA0B,gBAAgB;AAUnD,OAAO,MAAM,4BACX,YACwB;CACxB,UAAU,IAAI,KAAK;CACnB;CACD;AAED,OAAO,MAAM,wBACX,OACA,SACkB;CAClB,IAAI,UAAqC;AACzC,QAAO,SAAS;EACd,MAAM,YAAY,QAAQ,SAAS,IAAI,KAAK;AAC5C,MAAI,WAAW;AACb,UAAO;;AAGT,YAAU,QAAQ;;AAGpB,QAAO;;AAGT,OAAO,MAAM,6BACX,YACmC;CACnC,MAAM,WAAW,IAAI,KAAgC;CACrD,IAAI,WAAW;CAEf,MAAM,cACJ,OACA,MACA,MACA,gBACW;EACX,MAAM,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,YAAY,MAAM,GAAG;AACnD,cAAY;AAEZ,WAAS,IAA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AAiB,oCACrB,YACA,yBACD;AACD,iBAAe,SAAS,SAAS,yBAAyB,IAAI,KAAK,CAAC;EACpE,MAAM,kBAAkB,uBAAuB,QAAQ;EACvD,MAAM,mBAAmB,sCAAsC,WAAW;EAC1E,MAAM,iBAAiB,0BAA0B,QAAQ;EACzD,MAAM,WAAW,wBAAwB;GACvC,GAAG,uCACD,SACA,gBACA,yBACD;GACD,GAAG,yCACD,SACA,SACA,iBACA,yBACD;GACD,GAAG,gDACD,SACA,SACA,gBACD;GACD,GAAG,4BACD,SACA,SACA,iBACA,0BACA,+BACD;GACD,GAAG,2CACD,SACA,YACA,kBACA,wBACD;GACD,GAAG,kCAAkC,SAAS,QAAQ;GACvD,CAAC;AACF,YACE,SAAS,SAAS,IACd,gBAAgB,qBAAqB,SAAS,SAAS,CAAC,GACxD;AAEN,MAAI,YAAY,UAAU;AACxB,UAAO;;;AAIX,QAAO","names":[],"sources":["../../../src/utils/applyOxcProcessors/cleanupRemovals.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax,no-continue */\n\nimport type { Node, Program } from 'oxc-parser';\n\nimport { getOxcNodeChildren, isOxcNode } from '../oxc/ast';\nimport { applyOxcReplacements } from '../oxc/replacements';\nimport {\n collectDeclaredNames,\n collectImportLocalNames,\n collectReferencedNames,\n collectRemovableNamesFromStatements,\n collectTopLevelBindings,\n collectTopLevelBindingsFromStatements,\n collectTopLevelStatementInfos,\n getImportSpecifierLocalName,\n isNodeReference,\n} from './cleanupBindings';\nimport { GENERATED_HELPER_NAME_RE, parseOxc } from './shared';\nimport type {\n AnyNode,\n Replacement,\n ScopedBindingInfo,\n ScopedBindingKind,\n ScopedCleanupScope,\n TopLevelStatementInfo,\n} from './types';\n\nexport const createScopedCleanupScope = (\n parent: ScopedCleanupScope | null\n): ScopedCleanupScope => ({\n bindings: new Map(),\n parent,\n});\n\nexport const resolveScopedBinding = (\n scope: ScopedCleanupScope,\n name: string\n): string | null => {\n let current: ScopedCleanupScope | null = scope;\n while (current) {\n const bindingId = current.bindings.get(name);\n if (bindingId) {\n return bindingId;\n }\n\n current = current.parent;\n }\n\n return null;\n};\n\nexport const collectScopedBindingInfos = (\n program: Program\n): Map<string, ScopedBindingInfo> => {\n const bindings = new Map<string, ScopedBindingInfo>();\n let sequence = 0;\n\n const addBinding = (\n scope: ScopedCleanupScope,\n name: string,\n kind: ScopedBindingKind,\n declaration: Node\n ): string => {\n const id = `${kind}:${name}:${declaration.start}:${sequence}`;\n sequence += 1;\n\n bindings.set(id, {\n declaration,\n dependencies: new Set(),\n externalReferences: 0,\n id,\n incomingFromBindings: new Set(),\n kind,\n name,\n });\n scope.bindings.set(name, id);\n return id;\n };\n\n const addPatternBindings = (\n scope: ScopedCleanupScope,\n pattern: Node,\n kind: ScopedBindingKind,\n declaration: Node\n ): void => {\n collectDeclaredNames(pattern).forEach((name) => {\n addBinding(scope, name, kind, declaration);\n });\n };\n\n const recordReference = (\n scope: ScopedCleanupScope,\n name: string,\n ownerBindingId: string | null\n ): void => {\n const targetId = resolveScopedBinding(scope, name);\n if (!targetId) {\n return;\n }\n\n const target = bindings.get(targetId);\n if (!target) {\n return;\n }\n\n if (ownerBindingId && ownerBindingId !== targetId) {\n target.incomingFromBindings.add(ownerBindingId);\n bindings.get(ownerBindingId)?.dependencies.add(targetId);\n return;\n }\n\n target.externalReferences += 1;\n };\n\n type ScopedWalk = (\n node: Node,\n scope: ScopedCleanupScope,\n parent?: Node | null,\n ownerBindingId?: string | null\n ) => void;\n\n let walk: ScopedWalk;\n\n function walkPatternReferenceSubexpressions(\n node: Node,\n scope: ScopedCleanupScope,\n ownerBindingId: string | null\n ): void {\n if (node.type === 'Identifier') {\n return;\n }\n\n if (node.type === 'TSParameterProperty') {\n walkPatternReferenceSubexpressions(node.parameter, scope, ownerBindingId);\n return;\n }\n\n if (node.type === 'RestElement') {\n walkPatternReferenceSubexpressions(node.argument, scope, ownerBindingId);\n return;\n }\n\n if (node.type === 'AssignmentPattern') {\n walkPatternReferenceSubexpressions(node.left, scope, ownerBindingId);\n walk(node.right, scope, node, ownerBindingId);\n return;\n }\n\n if (node.type === 'ObjectPattern') {\n node.properties.forEach((property) => {\n if (property.type === 'RestElement') {\n walkPatternReferenceSubexpressions(\n property.argument,\n scope,\n ownerBindingId\n );\n return;\n }\n\n if (property.computed && isOxcNode(property.key)) {\n walk(property.key, scope, property, ownerBindingId);\n }\n\n walkPatternReferenceSubexpressions(\n property.value,\n scope,\n ownerBindingId\n );\n });\n return;\n }\n\n if (node.type === 'ArrayPattern') {\n node.elements.forEach((element) => {\n if (element && isOxcNode(element)) {\n walkPatternReferenceSubexpressions(element, scope, ownerBindingId);\n }\n });\n }\n }\n\n walk = (\n node: Node,\n scope: ScopedCleanupScope,\n parent: Node | null = null,\n ownerBindingId: string | null = null\n ): void => {\n if (node.type === 'ImportDeclaration') {\n const { specifiers } = node as AnyNode;\n if (Array.isArray(specifiers)) {\n specifiers.forEach((specifier) => {\n const { local } = specifier as AnyNode;\n if (\n isOxcNode(local) &&\n local.type === 'Identifier' &&\n typeof local.name === 'string'\n ) {\n addBinding(scope, local.name, 'import', node);\n }\n });\n }\n return;\n }\n\n if (node.type === 'ExportNamedDeclaration' && node.declaration) {\n walk(node.declaration, scope, node, ownerBindingId);\n collectTopLevelBindings(node).forEach((name) => {\n recordReference(scope, name, ownerBindingId);\n });\n return;\n }\n\n if (node.type === 'ExportDefaultDeclaration') {\n const { declaration } = node as AnyNode;\n if (isOxcNode(declaration)) {\n walk(declaration, scope, node, ownerBindingId);\n if (\n (declaration.type === 'FunctionDeclaration' ||\n declaration.type === 'ClassDeclaration') &&\n declaration.id\n ) {\n recordReference(scope, declaration.id.name, ownerBindingId);\n }\n }\n return;\n }\n\n if (node.type === 'VariableDeclaration') {\n const { declarations } = node as AnyNode;\n if (!Array.isArray(declarations)) {\n return;\n }\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n if (isOxcNode(id)) {\n addPatternBindings(scope, id, 'variable', node);\n }\n });\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n const { init } = declarator as AnyNode;\n if (!isOxcNode(id) || !isOxcNode(init)) {\n return;\n }\n\n const ownerName = collectDeclaredNames(id)[0] ?? null;\n const nextOwner =\n ownerName !== null ? resolveScopedBinding(scope, ownerName) : null;\n walk(init, scope, declarator as Node, nextOwner);\n });\n return;\n }\n\n if (node.type === 'FunctionDeclaration' && node.id) {\n const functionBindingId = addBinding(\n scope,\n node.id.name,\n 'function',\n node\n );\n const fnScope = createScopedCleanupScope(scope);\n\n node.params.forEach((param) => {\n addPatternBindings(fnScope, param, 'param', param);\n walkPatternReferenceSubexpressions(param, fnScope, functionBindingId);\n });\n\n if (node.body) {\n walk(node.body, fnScope, node, functionBindingId);\n }\n return;\n }\n\n if (\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n const fnScope = createScopedCleanupScope(scope);\n if (node.type === 'FunctionExpression' && node.id) {\n addBinding(fnScope, node.id.name, 'function', node);\n }\n\n node.params.forEach((param) => {\n addPatternBindings(fnScope, param, 'param', param);\n walkPatternReferenceSubexpressions(param, fnScope, ownerBindingId);\n });\n\n if (node.body) {\n walk(node.body, fnScope, node, ownerBindingId);\n }\n return;\n }\n\n if (node.type === 'BlockStatement') {\n const blockScope = createScopedCleanupScope(scope);\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, blockScope, node, ownerBindingId)\n );\n return;\n }\n\n if (\n isNodeReference(node, parent) &&\n 'name' in node &&\n typeof node.name === 'string'\n ) {\n recordReference(scope, node.name, ownerBindingId);\n }\n\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, scope, node, ownerBindingId)\n );\n };\n\n walk(program, createScopedCleanupScope(null));\n return bindings;\n};\n\nexport const collectScopedRemovableBindingIds = (\n bindings: Map<string, ScopedBindingInfo>,\n initialNames: Set<string>\n): Set<string> => {\n const removable = new Set<string>();\n let changed = true;\n\n while (changed) {\n changed = false;\n\n for (const binding of bindings.values()) {\n if (\n !removable.has(binding.id) &&\n binding.kind !== 'import' &&\n binding.kind !== 'param' &&\n binding.externalReferences === 0\n ) {\n const seededByName =\n initialNames.has(binding.name) ||\n (GENERATED_HELPER_NAME_RE.test(binding.name) &&\n binding.incomingFromBindings.size === 0);\n const allIncomingRemovable =\n binding.incomingFromBindings.size > 0 &&\n [...binding.incomingFromBindings].every((sourceId) =>\n removable.has(sourceId)\n );\n\n if (\n (seededByName && binding.incomingFromBindings.size === 0) ||\n allIncomingRemovable\n ) {\n removable.add(binding.id);\n changed = true;\n }\n }\n }\n }\n\n return removable;\n};\n\nexport function expandImportRemovalRange(\n code: string,\n start: number,\n end: number\n): Replacement {\n let removalStart = start;\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n\n let removalEnd = end;\n if (code[removalEnd] === ';') {\n removalEnd += 1;\n }\n\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n\n if (code[removalEnd] === '\\r' && code[removalEnd + 1] === '\\n') {\n removalEnd += 2;\n } else if (code[removalEnd] === '\\n') {\n removalEnd += 1;\n }\n\n return {\n end: removalEnd,\n start: removalStart,\n value: '',\n };\n}\n\nexport const collectUnusedScopedDeclarationRemovals = (\n code: string,\n bindings: Map<string, ScopedBindingInfo>,\n initialRemovableNames: Set<string>\n): Replacement[] => {\n const removableBindingIds = collectScopedRemovableBindingIds(\n bindings,\n initialRemovableNames\n );\n const removals = new Map<string, Replacement>();\n\n bindings.forEach((binding) => {\n if (\n !removableBindingIds.has(binding.id) ||\n binding.kind === 'import' ||\n binding.kind === 'param' ||\n binding.externalReferences > 0 ||\n [...binding.incomingFromBindings].some(\n (sourceId) => !removableBindingIds.has(sourceId)\n )\n ) {\n return;\n }\n\n if (\n binding.kind === 'function' &&\n binding.declaration.type === 'FunctionDeclaration'\n ) {\n const range = expandImportRemovalRange(\n code,\n binding.declaration.start,\n binding.declaration.end\n );\n removals.set(`${range.start}:${range.end}`, range);\n return;\n }\n\n if (binding.declaration.type !== 'VariableDeclaration') {\n return;\n }\n\n const { declarations } = binding.declaration as AnyNode;\n if (!Array.isArray(declarations) || declarations.length !== 1) {\n return;\n }\n\n const range = expandImportRemovalRange(\n code,\n binding.declaration.start,\n binding.declaration.end\n );\n removals.set(`${range.start}:${range.end}`, range);\n });\n\n return [...removals.values()];\n};\n\nexport const expandImportSpecifierRemovalRange = (\n code: string,\n start: number,\n end: number\n): Replacement => {\n let removalStart = start;\n let removalEnd = end;\n\n let whitespaceStart = removalStart;\n while (\n whitespaceStart > 0 &&\n (code[whitespaceStart - 1] === ' ' || code[whitespaceStart - 1] === '\\t')\n ) {\n whitespaceStart -= 1;\n }\n if (code[whitespaceStart - 1] !== '{') {\n removalStart = whitespaceStart;\n }\n\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n\n if (code[removalEnd] === ',') {\n removalEnd += 1;\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n } else {\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n\n if (code[removalStart - 1] === ',') {\n removalStart -= 1;\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n }\n }\n\n return {\n end: removalEnd,\n start: removalStart,\n value: '',\n };\n};\n\nexport const mergeEmptyRemovalRanges = (\n removals: Replacement[]\n): Replacement[] => {\n if (removals.length <= 1) {\n return removals;\n }\n\n const sorted = [...removals].sort((a, b) => a.start - b.start);\n const merged: Replacement[] = [];\n\n sorted.forEach((removal) => {\n const previous = merged[merged.length - 1];\n if (\n previous &&\n previous.value === '' &&\n removal.value === '' &&\n removal.start <= previous.end\n ) {\n previous.end = Math.max(previous.end, removal.end);\n return;\n }\n\n merged.push({ ...removal });\n });\n\n return merged;\n};\n\nexport const collectUnusedImportRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>,\n removableNames: Set<string>,\n preserveSideEffectImportLocals: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'ImportDeclaration') {\n return;\n }\n\n const localNames = collectImportLocalNames(statement);\n const removableLocalNames = localNames.filter((localName) =>\n removableNames.has(localName)\n );\n if (\n removableLocalNames.length > 0 &&\n removableLocalNames.length === localNames.length &&\n removableLocalNames.every((localName) => !referencedNames.has(localName))\n ) {\n if (\n removableLocalNames.some((localName) =>\n preserveSideEffectImportLocals.has(localName)\n )\n ) {\n removals.push({\n end: statement.end,\n start: statement.start,\n value: `import ${code.slice(\n statement.source.start,\n statement.source.end\n )};`,\n });\n return;\n }\n\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n return;\n }\n\n const { specifiers } = statement as AnyNode;\n if (!Array.isArray(specifiers) || specifiers.length <= 1) {\n return;\n }\n\n specifiers.forEach((specifier) => {\n if (!isOxcNode(specifier)) {\n return;\n }\n\n const localName = getImportSpecifierLocalName(specifier);\n if (\n localName &&\n removableNames.has(localName) &&\n !referencedNames.has(localName)\n ) {\n removals.push(\n expandImportSpecifierRemovalRange(\n code,\n specifier.start,\n specifier.end\n )\n );\n }\n });\n });\n\n return removals;\n};\n\nexport const collectUnusedTopLevelDeclarationRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>,\n removableNames: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'VariableDeclaration') {\n return;\n }\n\n const localNames = [...collectTopLevelBindings(statement)];\n\n if (\n localNames.length > 0 &&\n localNames.every((localName) => removableNames.has(localName)) &&\n localNames.every((localName) => !referencedNames.has(localName))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectUnusedGeneratedHelperDeclarationRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'VariableDeclaration') {\n return;\n }\n\n const localNames = [...collectTopLevelBindings(statement)];\n if (\n localNames.length > 0 &&\n localNames.every((localName) =>\n GENERATED_HELPER_NAME_RE.test(localName)\n ) &&\n localNames.every((localName) => !referencedNames.has(localName))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectTopLevelExpressionStatementRemovals = (\n code: string,\n statements: TopLevelStatementInfo[],\n topLevelBindings: Set<string>,\n removableExpressionRefs: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n statements.forEach((statement) => {\n if (statement.node.type !== 'ExpressionStatement') {\n return;\n }\n\n const { expression } = statement.node;\n const isPureExpression =\n expression.type === 'Identifier' ||\n expression.type === 'Literal' ||\n expression.type === 'ObjectExpression' ||\n expression.type === 'ArrayExpression' ||\n expression.type === 'ArrowFunctionExpression' ||\n expression.type === 'FunctionExpression' ||\n (expression.type === 'TemplateLiteral' &&\n expression.expressions.length === 0);\n if (!isPureExpression) {\n return;\n }\n\n const localReferences = [...statement.references].filter((name) =>\n topLevelBindings.has(name)\n );\n\n if (\n localReferences.length > 0 &&\n localReferences.every((name) => removableExpressionRefs.has(name))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.node.start, statement.node.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectEmptyTopLevelBlockRemovals = (\n code: string,\n program: Program\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'BlockStatement' || statement.body.length > 0) {\n return;\n }\n\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n });\n\n return removals;\n};\n\nexport const removeUnusedAfterReplacement = (\n code: string,\n filename: string,\n initialRemovableNames: Set<string>,\n removableExpressionRefs: Set<string>,\n preserveSideEffectImportLocals: Set<string>\n): string => {\n let current = code;\n const cumulativeRemovableNames = new Set(initialRemovableNames);\n const applyIfParsable = (next: string): string => {\n try {\n parseOxc(next, filename);\n return next;\n } catch {\n return current;\n }\n };\n\n for (let idx = 0; idx < 5; idx += 1) {\n const previous = current;\n const program = parseOxc(current, filename);\n const statements = collectTopLevelStatementInfos(program);\n const removableNames = collectRemovableNamesFromStatements(\n statements,\n cumulativeRemovableNames\n );\n removableNames.forEach((name) => cumulativeRemovableNames.add(name));\n const referencedNames = collectReferencedNames(program);\n const topLevelBindings = collectTopLevelBindingsFromStatements(statements);\n const scopedBindings = collectScopedBindingInfos(program);\n const removals = mergeEmptyRemovalRanges([\n ...collectUnusedScopedDeclarationRemovals(\n current,\n scopedBindings,\n cumulativeRemovableNames\n ),\n ...collectUnusedTopLevelDeclarationRemovals(\n current,\n program,\n referencedNames,\n cumulativeRemovableNames\n ),\n ...collectUnusedGeneratedHelperDeclarationRemovals(\n current,\n program,\n referencedNames\n ),\n ...collectUnusedImportRemovals(\n current,\n program,\n referencedNames,\n cumulativeRemovableNames,\n preserveSideEffectImportLocals\n ),\n ...collectTopLevelExpressionStatementRemovals(\n current,\n statements,\n topLevelBindings,\n removableExpressionRefs\n ),\n ...collectEmptyTopLevelBlockRemovals(current, program),\n ]);\n current =\n removals.length > 0\n ? applyIfParsable(applyOxcReplacements(current, removals))\n : current;\n\n if (current === previous) {\n return current;\n }\n }\n\n return current;\n};\n"],"file":"cleanupRemovals.js"}
|
|
1
|
+
{"mappings":"AAIA,SAAS,oBAAoB,iBAAiB;AAC9C,SAAS,4BAA4B;AACrC,SACE,sBACA,yBACA,wBACA,qCACA,yBACA,uCACA,+BACA,6BACA,uBACK;AACP,SAAS,0BAA0B,gBAAgB;AAUnD,OAAO,MAAM,4BACX,YACwB;CACxB,UAAU,IAAI,KAAK;CACnB;CACD;AAED,OAAO,MAAM,wBACX,OACA,SACkB;CAClB,IA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;AAEF,MAAI,SAAS,WAAW,GAAG;;;AAGzB,UAAO;;EAGT,MAAM,OAAO,qBAAqB,SAAS,SAAS;AACpD,MAAI;;AAEF,aAAU,SAAS,MAAM,SAAS;AAClC,aAAU;UACJ;;AAEN,UAAO;;AAGT,MAAI,YAAY,UAAU;AACxB,UAAO;;;AAIX,QAAO","names":[],"sources":["../../../src/utils/applyOxcProcessors/cleanupRemovals.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax,no-continue */\n\nimport type { Node, Program } from 'oxc-parser';\n\nimport { getOxcNodeChildren, isOxcNode } from '../oxc/ast';\nimport { applyOxcReplacements } from '../oxc/replacements';\nimport {\n collectDeclaredNames,\n collectImportLocalNames,\n collectReferencedNames,\n collectRemovableNamesFromStatements,\n collectTopLevelBindings,\n collectTopLevelBindingsFromStatements,\n collectTopLevelStatementInfos,\n getImportSpecifierLocalName,\n isNodeReference,\n} from './cleanupBindings';\nimport { GENERATED_HELPER_NAME_RE, parseOxc } from './shared';\nimport type {\n AnyNode,\n Replacement,\n ScopedBindingInfo,\n ScopedBindingKind,\n ScopedCleanupScope,\n TopLevelStatementInfo,\n} from './types';\n\nexport const createScopedCleanupScope = (\n parent: ScopedCleanupScope | null\n): ScopedCleanupScope => ({\n bindings: new Map(),\n parent,\n});\n\nexport const resolveScopedBinding = (\n scope: ScopedCleanupScope,\n name: string\n): string | null => {\n let current: ScopedCleanupScope | null = scope;\n while (current) {\n const bindingId = current.bindings.get(name);\n if (bindingId) {\n return bindingId;\n }\n\n current = current.parent;\n }\n\n return null;\n};\n\nexport const collectScopedBindingInfos = (\n program: Program\n): Map<string, ScopedBindingInfo> => {\n const bindings = new Map<string, ScopedBindingInfo>();\n let sequence = 0;\n\n const addBinding = (\n scope: ScopedCleanupScope,\n name: string,\n kind: ScopedBindingKind,\n declaration: Node\n ): string => {\n const id = `${kind}:${name}:${declaration.start}:${sequence}`;\n sequence += 1;\n\n bindings.set(id, {\n declaration,\n dependencies: new Set(),\n externalReferences: 0,\n id,\n incomingFromBindings: new Set(),\n kind,\n name,\n });\n scope.bindings.set(name, id);\n return id;\n };\n\n const addPatternBindings = (\n scope: ScopedCleanupScope,\n pattern: Node,\n kind: ScopedBindingKind,\n declaration: Node\n ): void => {\n collectDeclaredNames(pattern).forEach((name) => {\n addBinding(scope, name, kind, declaration);\n });\n };\n\n const recordReference = (\n scope: ScopedCleanupScope,\n name: string,\n ownerBindingId: string | null\n ): void => {\n const targetId = resolveScopedBinding(scope, name);\n if (!targetId) {\n return;\n }\n\n const target = bindings.get(targetId);\n if (!target) {\n return;\n }\n\n if (ownerBindingId && ownerBindingId !== targetId) {\n target.incomingFromBindings.add(ownerBindingId);\n bindings.get(ownerBindingId)?.dependencies.add(targetId);\n return;\n }\n\n target.externalReferences += 1;\n };\n\n type ScopedWalk = (\n node: Node,\n scope: ScopedCleanupScope,\n parent?: Node | null,\n ownerBindingId?: string | null\n ) => void;\n\n let walk: ScopedWalk;\n\n function walkPatternReferenceSubexpressions(\n node: Node,\n scope: ScopedCleanupScope,\n ownerBindingId: string | null\n ): void {\n if (node.type === 'Identifier') {\n return;\n }\n\n if (node.type === 'TSParameterProperty') {\n walkPatternReferenceSubexpressions(node.parameter, scope, ownerBindingId);\n return;\n }\n\n if (node.type === 'RestElement') {\n walkPatternReferenceSubexpressions(node.argument, scope, ownerBindingId);\n return;\n }\n\n if (node.type === 'AssignmentPattern') {\n walkPatternReferenceSubexpressions(node.left, scope, ownerBindingId);\n walk(node.right, scope, node, ownerBindingId);\n return;\n }\n\n if (node.type === 'ObjectPattern') {\n node.properties.forEach((property) => {\n if (property.type === 'RestElement') {\n walkPatternReferenceSubexpressions(\n property.argument,\n scope,\n ownerBindingId\n );\n return;\n }\n\n if (property.computed && isOxcNode(property.key)) {\n walk(property.key, scope, property, ownerBindingId);\n }\n\n walkPatternReferenceSubexpressions(\n property.value,\n scope,\n ownerBindingId\n );\n });\n return;\n }\n\n if (node.type === 'ArrayPattern') {\n node.elements.forEach((element) => {\n if (element && isOxcNode(element)) {\n walkPatternReferenceSubexpressions(element, scope, ownerBindingId);\n }\n });\n }\n }\n\n walk = (\n node: Node,\n scope: ScopedCleanupScope,\n parent: Node | null = null,\n ownerBindingId: string | null = null\n ): void => {\n if (node.type === 'ImportDeclaration') {\n const { specifiers } = node as AnyNode;\n if (Array.isArray(specifiers)) {\n specifiers.forEach((specifier) => {\n const { local } = specifier as AnyNode;\n if (\n isOxcNode(local) &&\n local.type === 'Identifier' &&\n typeof local.name === 'string'\n ) {\n addBinding(scope, local.name, 'import', node);\n }\n });\n }\n return;\n }\n\n if (node.type === 'ExportNamedDeclaration' && node.declaration) {\n walk(node.declaration, scope, node, ownerBindingId);\n collectTopLevelBindings(node).forEach((name) => {\n recordReference(scope, name, ownerBindingId);\n });\n return;\n }\n\n if (node.type === 'ExportDefaultDeclaration') {\n const { declaration } = node as AnyNode;\n if (isOxcNode(declaration)) {\n walk(declaration, scope, node, ownerBindingId);\n if (\n (declaration.type === 'FunctionDeclaration' ||\n declaration.type === 'ClassDeclaration') &&\n declaration.id\n ) {\n recordReference(scope, declaration.id.name, ownerBindingId);\n }\n }\n return;\n }\n\n if (node.type === 'VariableDeclaration') {\n const { declarations } = node as AnyNode;\n if (!Array.isArray(declarations)) {\n return;\n }\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n if (isOxcNode(id)) {\n addPatternBindings(scope, id, 'variable', node);\n }\n });\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n const { init } = declarator as AnyNode;\n if (!isOxcNode(id) || !isOxcNode(init)) {\n return;\n }\n\n const ownerName = collectDeclaredNames(id)[0] ?? null;\n const nextOwner =\n ownerName !== null ? resolveScopedBinding(scope, ownerName) : null;\n walk(init, scope, declarator as Node, nextOwner);\n });\n return;\n }\n\n if (node.type === 'FunctionDeclaration' && node.id) {\n const functionBindingId = addBinding(\n scope,\n node.id.name,\n 'function',\n node\n );\n const fnScope = createScopedCleanupScope(scope);\n\n node.params.forEach((param) => {\n addPatternBindings(fnScope, param, 'param', param);\n walkPatternReferenceSubexpressions(param, fnScope, functionBindingId);\n });\n\n if (node.body) {\n walk(node.body, fnScope, node, functionBindingId);\n }\n return;\n }\n\n if (\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n const fnScope = createScopedCleanupScope(scope);\n if (node.type === 'FunctionExpression' && node.id) {\n addBinding(fnScope, node.id.name, 'function', node);\n }\n\n node.params.forEach((param) => {\n addPatternBindings(fnScope, param, 'param', param);\n walkPatternReferenceSubexpressions(param, fnScope, ownerBindingId);\n });\n\n if (node.body) {\n walk(node.body, fnScope, node, ownerBindingId);\n }\n return;\n }\n\n if (node.type === 'BlockStatement') {\n const blockScope = createScopedCleanupScope(scope);\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, blockScope, node, ownerBindingId)\n );\n return;\n }\n\n if (\n isNodeReference(node, parent) &&\n 'name' in node &&\n typeof node.name === 'string'\n ) {\n recordReference(scope, node.name, ownerBindingId);\n }\n\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, scope, node, ownerBindingId)\n );\n };\n\n walk(program, createScopedCleanupScope(null));\n return bindings;\n};\n\nexport const collectScopedRemovableBindingIds = (\n bindings: Map<string, ScopedBindingInfo>,\n initialNames: Set<string>\n): Set<string> => {\n const removable = new Set<string>();\n let changed = true;\n\n while (changed) {\n changed = false;\n\n for (const binding of bindings.values()) {\n if (\n !removable.has(binding.id) &&\n binding.kind !== 'import' &&\n binding.kind !== 'param' &&\n binding.externalReferences === 0\n ) {\n const seededByName =\n initialNames.has(binding.name) ||\n (GENERATED_HELPER_NAME_RE.test(binding.name) &&\n binding.incomingFromBindings.size === 0);\n const allIncomingRemovable =\n binding.incomingFromBindings.size > 0 &&\n [...binding.incomingFromBindings].every((sourceId) =>\n removable.has(sourceId)\n );\n\n if (\n (seededByName && binding.incomingFromBindings.size === 0) ||\n allIncomingRemovable\n ) {\n removable.add(binding.id);\n changed = true;\n }\n }\n }\n }\n\n return removable;\n};\n\nexport function expandImportRemovalRange(\n code: string,\n start: number,\n end: number\n): Replacement {\n let removalStart = start;\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n\n let removalEnd = end;\n if (code[removalEnd] === ';') {\n removalEnd += 1;\n }\n\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n\n if (code[removalEnd] === '\\r' && code[removalEnd + 1] === '\\n') {\n removalEnd += 2;\n } else if (code[removalEnd] === '\\n') {\n removalEnd += 1;\n }\n\n return {\n end: removalEnd,\n start: removalStart,\n value: '',\n };\n}\n\nexport const collectUnusedScopedDeclarationRemovals = (\n code: string,\n bindings: Map<string, ScopedBindingInfo>,\n initialRemovableNames: Set<string>\n): Replacement[] => {\n const removableBindingIds = collectScopedRemovableBindingIds(\n bindings,\n initialRemovableNames\n );\n const removals = new Map<string, Replacement>();\n\n bindings.forEach((binding) => {\n if (\n !removableBindingIds.has(binding.id) ||\n binding.kind === 'import' ||\n binding.kind === 'param' ||\n binding.externalReferences > 0 ||\n [...binding.incomingFromBindings].some(\n (sourceId) => !removableBindingIds.has(sourceId)\n )\n ) {\n return;\n }\n\n if (\n binding.kind === 'function' &&\n binding.declaration.type === 'FunctionDeclaration'\n ) {\n const range = expandImportRemovalRange(\n code,\n binding.declaration.start,\n binding.declaration.end\n );\n removals.set(`${range.start}:${range.end}`, range);\n return;\n }\n\n if (binding.declaration.type !== 'VariableDeclaration') {\n return;\n }\n\n const { declarations } = binding.declaration as AnyNode;\n if (!Array.isArray(declarations) || declarations.length !== 1) {\n return;\n }\n\n const range = expandImportRemovalRange(\n code,\n binding.declaration.start,\n binding.declaration.end\n );\n removals.set(`${range.start}:${range.end}`, range);\n });\n\n return [...removals.values()];\n};\n\nexport const expandImportSpecifierRemovalRange = (\n code: string,\n start: number,\n end: number\n): Replacement => {\n let removalStart = start;\n let removalEnd = end;\n\n let whitespaceStart = removalStart;\n while (\n whitespaceStart > 0 &&\n (code[whitespaceStart - 1] === ' ' || code[whitespaceStart - 1] === '\\t')\n ) {\n whitespaceStart -= 1;\n }\n if (code[whitespaceStart - 1] !== '{') {\n removalStart = whitespaceStart;\n }\n\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n\n if (code[removalEnd] === ',') {\n removalEnd += 1;\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n } else {\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n\n if (code[removalStart - 1] === ',') {\n removalStart -= 1;\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n }\n }\n\n return {\n end: removalEnd,\n start: removalStart,\n value: '',\n };\n};\n\nexport const mergeEmptyRemovalRanges = (\n removals: Replacement[]\n): Replacement[] => {\n if (removals.length <= 1) {\n return removals;\n }\n\n const sorted = [...removals].sort((a, b) => a.start - b.start);\n const merged: Replacement[] = [];\n\n sorted.forEach((removal) => {\n const previous = merged[merged.length - 1];\n if (\n previous &&\n previous.value === '' &&\n removal.value === '' &&\n removal.start <= previous.end\n ) {\n previous.end = Math.max(previous.end, removal.end);\n return;\n }\n\n merged.push({ ...removal });\n });\n\n return merged;\n};\n\nexport const collectUnusedImportRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>,\n removableNames: Set<string>,\n preserveSideEffectImportLocals: Set<string>,\n preserveSideEffectImportOrderLocals: Set<string> = preserveSideEffectImportLocals\n): Replacement[] => {\n const removals: Replacement[] = [];\n const importSourceByLocal = new Map<string, string>();\n const removedSideEffectImportRanges: { end: number; start: number }[] = [];\n const keptImportRangesBySource = new Map<\n string,\n { end: number; start: number }\n >();\n\n program.body.forEach((statement) => {\n if (statement.type !== 'ImportDeclaration') {\n return;\n }\n\n const localNames = collectImportLocalNames(statement);\n const source = code.slice(statement.source.start, statement.source.end);\n const orderedLocalNames = localNames.filter((localName) =>\n preserveSideEffectImportOrderLocals.has(localName)\n );\n const sideEffectLocalNames = localNames.filter((localName) =>\n preserveSideEffectImportLocals.has(localName)\n );\n [...orderedLocalNames, ...sideEffectLocalNames].forEach((localName) => {\n importSourceByLocal.set(localName, source);\n });\n const removableLocalNames = localNames.filter((localName) =>\n removableNames.has(localName)\n );\n if (\n removableLocalNames.length > 0 &&\n removableLocalNames.length === localNames.length &&\n removableLocalNames.every((localName) => !referencedNames.has(localName))\n ) {\n if (\n removableLocalNames.some((localName) =>\n preserveSideEffectImportLocals.has(localName)\n )\n ) {\n removedSideEffectImportRanges.push({\n end: statement.end,\n start: statement.start,\n });\n return;\n }\n\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n return;\n }\n\n if (orderedLocalNames.length > 0 && !keptImportRangesBySource.has(source)) {\n keptImportRangesBySource.set(source, {\n end: statement.end,\n start: statement.start,\n });\n }\n\n const { specifiers } = statement as AnyNode;\n if (!Array.isArray(specifiers) || specifiers.length <= 1) {\n return;\n }\n\n specifiers.forEach((specifier) => {\n if (!isOxcNode(specifier)) {\n return;\n }\n\n const localName = getImportSpecifierLocalName(specifier);\n if (\n localName &&\n removableNames.has(localName) &&\n !referencedNames.has(localName)\n ) {\n removals.push(\n expandImportSpecifierRemovalRange(\n code,\n specifier.start,\n specifier.end\n )\n );\n }\n });\n });\n\n if (removedSideEffectImportRanges.length > 0) {\n const seenSources = new Set<string>();\n const removedRanges = removedSideEffectImportRanges.sort(\n (a, b) => a.start - b.start\n );\n const [firstRemoved, ...restRemoved] = removedRanges;\n const pendingImports: string[] = [];\n let insertionAfterLastKept: number | null = null;\n let usedFirstRemovedRange = false;\n const flushBefore = (position: number): void => {\n if (pendingImports.length === 0) {\n return;\n }\n\n removals.push({\n end: position,\n start: position,\n value: `${pendingImports.join('\\n')}\\n`,\n });\n pendingImports.length = 0;\n };\n\n [...preserveSideEffectImportOrderLocals].forEach((localName) => {\n const source = importSourceByLocal.get(localName);\n if (!source) {\n return;\n }\n\n const keptRange = keptImportRangesBySource.get(source);\n if (keptRange) {\n flushBefore(keptRange.start);\n insertionAfterLastKept = keptRange.end;\n if (preserveSideEffectImportLocals.has(localName)) {\n seenSources.add(source);\n }\n return;\n }\n\n if (\n !preserveSideEffectImportLocals.has(localName) ||\n seenSources.has(source)\n ) {\n return;\n }\n\n seenSources.add(source);\n pendingImports.push(`import ${source};`);\n });\n\n if (pendingImports.length > 0) {\n if (insertionAfterLastKept !== null) {\n removals.push({\n end: insertionAfterLastKept,\n start: insertionAfterLastKept,\n value: `\\n${pendingImports.join('\\n')}`,\n });\n } else if (firstRemoved) {\n usedFirstRemovedRange = true;\n removals.push({\n end: firstRemoved.end,\n start: firstRemoved.start,\n value: pendingImports.join('\\n'),\n });\n }\n }\n\n removals.push(\n ...(usedFirstRemovedRange ? restRemoved : removedRanges).map((range) => ({\n ...range,\n value: '',\n }))\n );\n }\n\n return removals;\n};\n\nexport const collectUnusedTopLevelDeclarationRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>,\n removableNames: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'VariableDeclaration') {\n return;\n }\n\n const localNames = [...collectTopLevelBindings(statement)];\n\n if (\n localNames.length > 0 &&\n localNames.every((localName) => removableNames.has(localName)) &&\n localNames.every((localName) => !referencedNames.has(localName))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectUnusedGeneratedHelperDeclarationRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'VariableDeclaration') {\n return;\n }\n\n const localNames = [...collectTopLevelBindings(statement)];\n if (\n localNames.length > 0 &&\n localNames.every((localName) =>\n GENERATED_HELPER_NAME_RE.test(localName)\n ) &&\n localNames.every((localName) => !referencedNames.has(localName))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectTopLevelExpressionStatementRemovals = (\n code: string,\n statements: TopLevelStatementInfo[],\n topLevelBindings: Set<string>,\n removableExpressionRefs: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n statements.forEach((statement) => {\n if (statement.node.type !== 'ExpressionStatement') {\n return;\n }\n\n const { expression } = statement.node;\n const isPureExpression =\n expression.type === 'Identifier' ||\n expression.type === 'Literal' ||\n expression.type === 'ObjectExpression' ||\n expression.type === 'ArrayExpression' ||\n expression.type === 'ArrowFunctionExpression' ||\n expression.type === 'FunctionExpression' ||\n (expression.type === 'TemplateLiteral' &&\n expression.expressions.length === 0);\n if (!isPureExpression) {\n return;\n }\n\n const localReferences = [...statement.references].filter((name) =>\n topLevelBindings.has(name)\n );\n\n if (\n localReferences.length > 0 &&\n localReferences.every((name) => removableExpressionRefs.has(name))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.node.start, statement.node.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectEmptyTopLevelBlockRemovals = (\n code: string,\n program: Program\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'BlockStatement' || statement.body.length > 0) {\n return;\n }\n\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n });\n\n return removals;\n};\n\nexport const removeUnusedAfterReplacement = (\n code: string,\n filename: string,\n initialRemovableNames: Set<string>,\n removableExpressionRefs: Set<string>,\n preserveSideEffectImportLocals: Set<string>,\n preserveSideEffectImportOrderLocals: Set<string> = preserveSideEffectImportLocals\n): string => {\n let current = code;\n let program: Program | null = null;\n const cumulativeRemovableNames = new Set(initialRemovableNames);\n\n // Incremental cleanup loop: validate-by-parsing the next iteration's\n // candidate code AND reuse that parse as the next iter's `program` input,\n // instead of re-parsing at the top of the next iter. Saves one parse per\n // loop revolution (N+1 parses for an N-iter loop instead of 2N).\n // Also short-circuits a round earlier when no removals were collected.\n for (let idx = 0; idx < 5; idx += 1) {\n const previous = current;\n if (program === null) {\n program = parseOxc(current, filename);\n }\n const statements = collectTopLevelStatementInfos(program);\n const removableNames = collectRemovableNamesFromStatements(\n statements,\n cumulativeRemovableNames\n );\n removableNames.forEach((name) => cumulativeRemovableNames.add(name));\n const referencedNames = collectReferencedNames(program);\n const topLevelBindings = collectTopLevelBindingsFromStatements(statements);\n const scopedBindings = collectScopedBindingInfos(program);\n const removals = mergeEmptyRemovalRanges([\n ...collectUnusedScopedDeclarationRemovals(\n current,\n scopedBindings,\n cumulativeRemovableNames\n ),\n ...collectUnusedTopLevelDeclarationRemovals(\n current,\n program,\n referencedNames,\n cumulativeRemovableNames\n ),\n ...collectUnusedGeneratedHelperDeclarationRemovals(\n current,\n program,\n referencedNames\n ),\n ...collectUnusedImportRemovals(\n current,\n program,\n referencedNames,\n cumulativeRemovableNames,\n preserveSideEffectImportLocals,\n preserveSideEffectImportOrderLocals\n ),\n ...collectTopLevelExpressionStatementRemovals(\n current,\n statements,\n topLevelBindings,\n removableExpressionRefs\n ),\n ...collectEmptyTopLevelBlockRemovals(current, program),\n ]);\n\n if (removals.length === 0) {\n // Convergence: next iter would parse the same code and see the same\n // removable set. Skip the round of walks + parse.\n return current;\n }\n\n const next = applyOxcReplacements(current, removals);\n try {\n // Validate + capture the AST for the next iteration in one parse.\n program = parseOxc(next, filename);\n current = next;\n } catch {\n // Pathological removal — drop this iteration and return prior state.\n return current;\n }\n\n if (current === previous) {\n return current;\n }\n }\n\n return current;\n};\n"],"file":"cleanupRemovals.js"}
|
|
@@ -862,6 +862,14 @@ const getChildren = (node) => {
|
|
|
862
862
|
return result;
|
|
863
863
|
};
|
|
864
864
|
const precollectRequireSources = (node, state) => {
|
|
865
|
+
// Cheap text precheck at the Program entry: if the file body has no
|
|
866
|
+
// 'require(' substring there can be no CommonJS require() init to collect.
|
|
867
|
+
// Skip the full AST walk — meaningful saving on ESM-only modules in large
|
|
868
|
+
// monorepos. Done at Program-level only so nested recursion doesn't pay
|
|
869
|
+
// the indexOf cost per node.
|
|
870
|
+
if (node.type === "Program" && state.code.indexOf("require(") === -1) {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
865
873
|
if (node.type === "VariableDeclarator" && node.id.type === "Identifier") {
|
|
866
874
|
const source = node.init ? sourceFromRequireSyntax(node.init) : null;
|
|
867
875
|
if (source) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AA2BA,SAAS,sBAAsB;AAsF/B,MAAM,eAAe,YAAiC;CACpD,UAAU,IAAI,KAAK;CACnB;CACD;AAED,MAAM,UAAU,UACd,CAAC,CAAC,SACF,OAAO,UAAU,YACjB,UAAU,SACV,OAAQ,MAA6B,SAAS;AAEhD,MAAM,iBAAiB,MAAY,MAAc,UAA6B;CAC5E,MAAM,KAAK,MAAM,KAAK,OAAO,KAAK,IAAI;CACtC,KAAK,KAAK;CACV,MAAM,SAAS,KAAK,SAAS,eAAe,KAAK,OAAO;CACxD,OAAO,KAAK;CACb;AAED,MAAM,wBAAwB,SAC5B,KAAK,SAAS,YAAY,KAAK,QAAQ,KAAK;AAE9C,MAAM,uBAAuB,QAAoC;AAC/D,KAAI,IAAI,SAAS,cAAc;AAC7B,SAAO,IAAI;;AAGb,KAAI,IAAI,SAAS,aAAa,OAAO,IAAI,UAAU,UAAU;AAC3D,SAAO,IAAI;;AAGb,QAAO;;AAGT,MAAM,0BAA0B,SAA0C;AACxE,KAAI,KAAK,UAAU;AACjB,SAAO,KAAK,SAAS,SAAS,aAC5B,OAAO,KAAK,SAAS,UAAU,WAC7B,KAAK,SAAS,QACd;;AAGN,QAAO,KAAK,SAAS,SAAS,eAAe,KAAK,SAAS,OAAO;;AAGpE,MAAM,iBAAiB,OAAc,YAA2B;AAC9D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ACxC,OACE,SACA;EAAE,KAAK;EAAW,QAAQ;EAAM,OAAO;EAAW,EAClD,OACA,cACD;AAED,QAAO,MAAM,OAAO;;AAGtB,OAAO,SAAS,4BACd,MACA,UACmB;CACnB,MAAM,SAAS,eAAe,UAAU,MAAM,cAAc;AAE5D,QAAO,uCACL,OAAO,SACP,MACA,OAAO,OAAO,gBACf","names":[],"sources":["../../src/utils/collectOxcExportsAndImports.ts"],"version":3,"sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define,no-restricted-syntax,no-continue */\n\nimport type {\n AssignmentExpression,\n BindingPattern,\n CallExpression,\n Class as OxcClass,\n ExportAllDeclaration,\n ExportDefaultDeclaration,\n ExportNamedDeclaration,\n ExportSpecifier,\n Expression,\n ExpressionStatement,\n Function as OxcFunction,\n ImportDeclaration,\n ImportExpression,\n ImportSpecifier,\n MemberExpression,\n ModuleExportName,\n Node,\n ObjectExpression,\n ObjectPattern,\n Program,\n PropertyKey,\n VariableDeclarator,\n} from 'oxc-parser';\n\nimport { parseOxcCached } from './parseOxc';\n\ntype ImportKind = 'cjs' | 'dynamic' | 'esm';\n\nexport type OxcLocal = {\n code: string;\n end: number;\n name?: string;\n start: number;\n};\n\nexport type OxcCollectedImport = {\n imported: string | 'default' | '*' | 'side-effect';\n local: OxcLocal;\n source: string;\n type: ImportKind;\n};\n\nexport type OxcCollectedExport = {\n exported: string | 'default' | '*';\n local: OxcLocal;\n};\n\nexport type OxcCollectedReexport = {\n exported: string | 'default' | '*';\n imported: string | 'default' | '*';\n local: OxcLocal;\n source: string;\n};\n\nexport type OxcCollectedState = {\n deadExports: string[];\n exports: Record<string | 'default' | '*', OxcLocal>;\n imports: OxcCollectedImport[];\n isEsModule: boolean;\n reexports: OxcCollectedReexport[];\n};\n\ntype VisitMode = 'all' | 'importsOnly';\n\ntype NamespaceBinding = {\n kind: 'namespace';\n local: OxcLocal;\n name: string;\n source: string;\n type: ImportKind;\n used: boolean;\n};\n\ntype LocalBinding = {\n kind: 'local';\n name: string;\n};\n\ntype Binding = LocalBinding | NamespaceBinding;\n\ntype Scope = {\n bindings: Map<string, Binding>;\n parent: Scope | null;\n};\n\ntype ChildContext = {\n key: string;\n parent: Node | null;\n};\n\ntype VisitContext = ChildContext & {\n scope: Scope;\n};\n\ntype AnyNode = Node & Record<string, unknown>;\n\ntype OxcIdentifier = Node & { name: string; type: 'Identifier' };\n\ntype Destructed = {\n as: Node;\n what: string | '*';\n};\n\ntype AnalyzerState = {\n code: string;\n namespaces: NamespaceBinding[];\n requireSources: Map<string, string>;\n result: OxcCollectedState;\n};\n\nconst createScope = (parent: Scope | null): Scope => ({\n bindings: new Map(),\n parent,\n});\n\nconst isNode = (value: unknown): value is Node =>\n !!value &&\n typeof value === 'object' &&\n 'type' in value &&\n typeof (value as { type?: unknown }).type === 'string';\n\nconst localFromNode = (node: Node, code: string, name?: string): OxcLocal => ({\n code: code.slice(node.start, node.end),\n end: node.end,\n name: name ?? (node.type === 'Identifier' ? node.name : undefined),\n start: node.start,\n});\n\nconst nameFromModuleExport = (node: ModuleExportName): string =>\n node.type === 'Literal' ? node.value : node.name;\n\nconst nameFromPropertyKey = (key: PropertyKey): string | null => {\n if (key.type === 'Identifier') {\n return key.name;\n }\n\n if (key.type === 'Literal' && typeof key.value === 'string') {\n return key.value;\n }\n\n return null;\n};\n\nconst nameFromMemberProperty = (node: MemberExpression): string | null => {\n if (node.computed) {\n return node.property.type === 'Literal' &&\n typeof node.property.value === 'string'\n ? node.property.value\n : null;\n }\n\n return node.property.type === 'Identifier' ? node.property.name : null;\n};\n\nconst defineBinding = (scope: Scope, binding: Binding): void => {\n scope.bindings.set(binding.name, binding);\n};\n\nconst lookupBinding = (scope: Scope, name: string): Binding | null => {\n let current: Scope | null = scope;\n while (current) {\n const binding = current.bindings.get(name);\n if (binding) {\n return binding;\n }\n\n current = current.parent;\n }\n\n return null;\n};\n\nconst collectBindingNames = (pattern: BindingPattern): string[] => {\n if (pattern.type === 'Identifier') {\n return [pattern.name];\n }\n\n if (pattern.type === 'AssignmentPattern') {\n return collectBindingNames(pattern.left);\n }\n\n if (pattern.type === 'ObjectPattern') {\n return pattern.properties.flatMap((property) =>\n property.type === 'RestElement'\n ? collectBindingNames(property.argument)\n : collectBindingNames(property.value)\n );\n }\n\n if (pattern.type === 'ArrayPattern') {\n return pattern.elements.flatMap((element) =>\n element ? collectBindingLikeNames(element) : []\n );\n }\n\n return [];\n};\n\nconst collectBindingLikeNames = (node: Node): string[] => {\n if (node.type === 'RestElement') {\n return collectBindingLikeNames(node.argument);\n }\n\n if (node.type === 'TSParameterProperty') {\n return collectBindingLikeNames(node.parameter);\n }\n\n return collectBindingNames(node as BindingPattern);\n};\n\nconst declareLocalPattern = (scope: Scope, pattern: BindingPattern): void => {\n collectBindingNames(pattern).forEach((name) =>\n defineBinding(scope, { kind: 'local', name })\n );\n};\n\nconst declareLocalBindingLike = (scope: Scope, node: Node): void => {\n collectBindingLikeNames(node).forEach((name) =>\n defineBinding(scope, { kind: 'local', name })\n );\n};\n\nconst isTypeOnlyImport = (\n declaration: ImportDeclaration,\n specifier?: ImportSpecifier\n): boolean =>\n declaration.importKind === 'type' || specifier?.importKind === 'type';\n\nconst isTypeOnlyExport = (\n declaration:\n | ExportAllDeclaration\n | ExportDefaultDeclaration\n | ExportNamedDeclaration\n | ExportSpecifier\n): boolean => 'exportKind' in declaration && declaration.exportKind === 'type';\n\nconst addImport = (\n state: AnalyzerState,\n item: Omit<OxcCollectedImport, 'local'> & { local: Node; name?: string }\n): void => {\n state.result.imports.push({\n imported: item.imported,\n local: localFromNode(item.local, state.code, item.name),\n source: item.source,\n type: item.type,\n });\n};\n\nconst addExport = (\n state: AnalyzerState,\n exported: string | 'default' | '*',\n local: Node,\n name?: string\n): void => {\n const { result } = state;\n result.exports[exported] = localFromNode(local, state.code, name);\n};\n\nconst addReexport = (\n state: AnalyzerState,\n item: Omit<OxcCollectedReexport, 'local'> & { local: Node }\n): void => {\n state.result.reexports.push({\n exported: item.exported,\n imported: item.imported,\n local: localFromNode(item.local, state.code),\n source: item.source,\n });\n};\n\nconst collectDestructed = (pattern: ObjectPattern): Destructed[] =>\n pattern.properties.flatMap((property) => {\n if (property.type === 'RestElement') {\n return collectBindingNames(property.argument).map(() => ({\n as: property.argument,\n what: '*' as const,\n }));\n }\n\n const firstKey = nameFromPropertyKey(property.key);\n if (!firstKey) {\n return [];\n }\n\n if (property.value.type === 'ObjectPattern') {\n return collectBindingNames(property.value).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n }\n\n if (property.value.type === 'ArrayPattern') {\n return collectBindingNames(property.value).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n }\n\n if (property.value.type === 'AssignmentPattern') {\n return collectBindingNames(property.value.left).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n }\n\n return collectBindingNames(property.value).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n });\n\nconst getStringConstant = (expression: Expression): string | null => {\n if (expression.type === 'Literal' && typeof expression.value === 'string') {\n return expression.value;\n }\n\n if (\n expression.type === 'TemplateLiteral' &&\n expression.expressions.length === 0\n ) {\n return expression.quasis[0]?.value.cooked ?? null;\n }\n\n if (expression.type === 'BinaryExpression' && expression.operator === '+') {\n const left = getStringConstant(expression.left);\n const right = getStringConstant(expression.right);\n return left === null || right === null ? null : left + right;\n }\n\n if (\n expression.type === 'CallExpression' &&\n expression.callee.type === 'MemberExpression' &&\n nameFromMemberProperty(expression.callee) === 'concat'\n ) {\n const base = getStringConstant(expression.callee.object);\n if (base === null) {\n return null;\n }\n\n const parts = expression.arguments.map((arg) =>\n arg.type === 'SpreadElement' ? null : getStringConstant(arg)\n );\n if (parts.some((part) => part === null)) {\n return null;\n }\n\n return [base, ...(parts as string[])].join('');\n }\n\n if (\n expression.type === 'TSAsExpression' ||\n expression.type === 'TSSatisfiesExpression' ||\n expression.type === 'TSNonNullExpression' ||\n expression.type === 'TSTypeAssertion' ||\n expression.type === 'ParenthesizedExpression'\n ) {\n return getStringConstant(expression.expression);\n }\n\n return null;\n};\n\nconst isRequireCall = (node: Node, scope: Scope): boolean =>\n node.type === 'CallExpression' &&\n node.callee.type === 'Identifier' &&\n node.callee.name === 'require' &&\n lookupBinding(scope, 'require') === null;\n\nconst sourceFromRequireLike = (\n node: Node,\n scope: Scope,\n state: AnalyzerState\n): string | null => {\n if (isRequireCall(node, scope)) {\n const call = node as CallExpression;\n const [sourceArg] = call.arguments;\n if (!sourceArg || sourceArg.type === 'SpreadElement') {\n return null;\n }\n\n return getStringConstant(sourceArg);\n }\n\n if (node.type === 'Identifier') {\n return state.requireSources.get(node.name) ?? null;\n }\n\n if (node.type === 'CallExpression') {\n for (const arg of node.arguments) {\n if (arg.type === 'SpreadElement') {\n continue;\n }\n\n const source = sourceFromRequireLike(arg, scope, state);\n if (source) {\n return source;\n }\n }\n }\n\n return null;\n};\n\nconst sourceFromDirectRequireBinding = (\n node: Node,\n scope: Scope,\n state: AnalyzerState\n): string | null => {\n if (isRequireCall(node, scope)) {\n const call = node as CallExpression;\n const [sourceArg] = call.arguments;\n if (!sourceArg || sourceArg.type === 'SpreadElement') {\n return null;\n }\n\n return getStringConstant(sourceArg);\n }\n\n if (node.type === 'Identifier') {\n return state.requireSources.get(node.name) ?? null;\n }\n\n return null;\n};\n\nconst sourceFromRequireSyntax = (node: Node): string | null => {\n if (\n node.type === 'CallExpression' &&\n node.callee.type === 'Identifier' &&\n node.callee.name === 'require'\n ) {\n const [sourceArg] = node.arguments;\n return sourceArg && sourceArg.type !== 'SpreadElement'\n ? getStringConstant(sourceArg)\n : null;\n }\n\n if (node.type === 'CallExpression') {\n for (const arg of node.arguments) {\n if (arg.type === 'SpreadElement') {\n continue;\n }\n\n const source = sourceFromRequireSyntax(arg);\n if (source) {\n return source;\n }\n }\n }\n\n return null;\n};\n\nconst sourceFromImportedMember = (\n node: Node,\n scope: Scope,\n state: AnalyzerState\n): { imported: string | '*' | 'default'; source: string } | null => {\n if (node.type !== 'MemberExpression') {\n return null;\n }\n\n const source = sourceFromRequireLike(node.object, scope, state);\n const imported = nameFromMemberProperty(node);\n if (!source || !imported) {\n return null;\n }\n\n return { imported, source };\n};\n\nconst isExportsObject = (node: Node): boolean =>\n node.type === 'Identifier' && node.name === 'exports';\n\nconst getExportAssignmentName = (node: Node): string | 'default' | null => {\n if (node.type !== 'MemberExpression') {\n return null;\n }\n\n if (isExportsObject(node.object)) {\n return nameFromMemberProperty(node);\n }\n\n if (\n node.object.type === 'Identifier' &&\n node.object.name === 'module' &&\n nameFromMemberProperty(node) === 'exports'\n ) {\n return 'default';\n }\n\n return null;\n};\n\nconst getCalleeName = (node: Expression): string | null => {\n if (node.type === 'Identifier') {\n return node.name;\n }\n\n if (node.type === 'MemberExpression') {\n return nameFromMemberProperty(node);\n }\n\n return null;\n};\n\nconst getObjectProperty = (\n objectExpression: ObjectExpression,\n name: string\n): Node | null => {\n for (const property of objectExpression.properties) {\n if (property.type === 'SpreadElement') {\n continue;\n }\n\n if (nameFromPropertyKey(property.key) === name) {\n return property.value;\n }\n }\n\n return null;\n};\n\nconst getReturnedExpression = (node: Node): Expression | null => {\n if (node.type === 'ArrowFunctionExpression') {\n return node.body.type === 'BlockStatement'\n ? getReturnedExpression(node.body)\n : node.body;\n }\n\n if (\n node.type === 'FunctionExpression' ||\n node.type === 'FunctionDeclaration'\n ) {\n return node.body ? getReturnedExpression(node.body) : null;\n }\n\n if (node.type === 'BlockStatement') {\n const returned = node.body.find(\n (statement) => statement.type === 'ReturnStatement'\n );\n return returned?.type === 'ReturnStatement' ? returned.argument : null;\n }\n\n return null;\n};\n\nconst collectFromImportDeclaration = (\n node: ImportDeclaration,\n scope: Scope,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyImport(node)) {\n return;\n }\n\n const source = node.source.value;\n if (node.specifiers.length === 0) {\n addImport(state, {\n imported: 'side-effect',\n local: node,\n source,\n type: 'esm',\n });\n return;\n }\n\n node.specifiers.forEach((specifier) => {\n if (\n specifier.type === 'ImportSpecifier' &&\n isTypeOnlyImport(node, specifier)\n ) {\n return;\n }\n\n if (specifier.type === 'ImportNamespaceSpecifier') {\n const binding: NamespaceBinding = {\n kind: 'namespace',\n local: localFromNode(specifier.local, state.code, specifier.local.name),\n name: specifier.local.name,\n source,\n type: 'esm',\n used: false,\n };\n defineBinding(scope, binding);\n state.namespaces.push(binding);\n return;\n }\n\n if (specifier.type === 'ImportDefaultSpecifier') {\n defineBinding(scope, { kind: 'local', name: specifier.local.name });\n addImport(state, {\n imported: 'default',\n local: specifier.local,\n name: specifier.local.name,\n source,\n type: 'esm',\n });\n return;\n }\n\n defineBinding(scope, { kind: 'local', name: specifier.local.name });\n addImport(state, {\n imported: nameFromModuleExport(specifier.imported),\n local: specifier.local,\n name: specifier.local.name,\n source,\n type: 'esm',\n });\n });\n};\n\nconst collectExportedDeclaration = (\n declaration: ExportNamedDeclaration['declaration'],\n state: AnalyzerState\n): void => {\n if (!declaration) {\n return;\n }\n\n if (declaration.type === 'VariableDeclaration') {\n declaration.declarations.forEach((declarator) => {\n exportFromVariableDeclarator(declarator, state);\n });\n return;\n }\n\n if (declaration.type === 'TSEnumDeclaration') {\n addExport(state, declaration.id.name, declaration.id, declaration.id.name);\n return;\n }\n\n if (\n declaration.type === 'FunctionDeclaration' ||\n declaration.type === 'ClassDeclaration'\n ) {\n const { id } = declaration as OxcFunction | OxcClass;\n if (id) {\n addExport(state, id.name, id, id.name);\n }\n }\n};\n\nconst collectFromExportNamedDeclaration = (\n node: ExportNamedDeclaration,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyExport(node)) {\n return;\n }\n\n const source = node.source?.value;\n node.specifiers.forEach((specifier) => {\n if (isTypeOnlyExport(specifier)) {\n return;\n }\n\n const exported = nameFromModuleExport(specifier.exported);\n const imported = nameFromModuleExport(specifier.local);\n\n if (source) {\n addReexport(state, {\n exported,\n imported,\n local: specifier,\n source,\n });\n return;\n }\n\n addExport(state, exported, specifier.local, imported);\n });\n\n collectExportedDeclaration(node.declaration, state);\n};\n\nconst collectFromExportAllDeclaration = (\n node: ExportAllDeclaration,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyExport(node)) {\n return;\n }\n\n addReexport(state, {\n exported: node.exported ? nameFromModuleExport(node.exported) : '*',\n imported: '*',\n local: node,\n source: node.source.value,\n });\n};\n\nconst collectFromExportDefaultDeclaration = (\n node: ExportDefaultDeclaration,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyExport(node)) {\n return;\n }\n\n addExport(state, 'default', node.declaration);\n};\n\nconst exportFromVariableDeclarator = (\n node: VariableDeclarator,\n state: AnalyzerState\n): void => {\n if (node.id.type === 'Identifier') {\n addExport(state, node.id.name, node.init ?? node.id, node.id.name);\n return;\n }\n\n if (node.id.type === 'ObjectPattern') {\n collectDestructed(node.id).forEach((destructed) => {\n if (destructed.as.type === 'Identifier') {\n addExport(\n state,\n destructed.as.name,\n node.init ?? destructed.as,\n destructed.as.name\n );\n }\n });\n return;\n }\n\n if (node.id.type === 'ArrayPattern') {\n collectBindingNames(node.id).forEach((name) =>\n addExport(state, name, node.init ?? node.id, name)\n );\n }\n};\n\nconst collectFromImportExpression = (\n node: ImportExpression,\n parent: Node | null,\n state: AnalyzerState\n): void => {\n const source = getStringConstant(node.source);\n if (!source) {\n return;\n }\n\n let container = parent;\n let awaited = false;\n if (container?.type === 'AwaitExpression') {\n awaited = true;\n container = findParentContainer(container);\n }\n\n if (container?.type === 'VariableDeclarator') {\n importFromVariableDeclarator(container, awaited, source, 'dynamic', state);\n }\n};\n\nconst collectFromWywDynamicImport = (\n node: CallExpression,\n parent: Node | null,\n state: AnalyzerState\n): void => {\n if (\n node.callee.type !== 'Identifier' ||\n node.callee.name !== '__wyw_dynamic_import'\n ) {\n return;\n }\n\n const [sourceArg] = node.arguments;\n if (!sourceArg || sourceArg.type === 'SpreadElement') {\n return;\n }\n\n const source = getStringConstant(sourceArg);\n if (!source) {\n return;\n }\n\n let container = parent;\n let awaited = false;\n if (container?.type === 'AwaitExpression') {\n awaited = true;\n container = findParentContainer(container);\n }\n\n if (container?.type === 'VariableDeclarator') {\n importFromVariableDeclarator(container, awaited, source, 'dynamic', state);\n return;\n }\n\n addImport(state, {\n imported: '*',\n local: node,\n source,\n type: 'dynamic',\n });\n};\n\nconst parentContainers = new WeakMap<Node, Node | null>();\n\nconst findParentContainer = (node: Node): Node | null =>\n parentContainers.get(node) ?? null;\n\nconst importFromVariableDeclarator = (\n node: VariableDeclarator,\n isSync: boolean,\n source: string,\n type: ImportKind,\n state: AnalyzerState\n): void => {\n if (node.id.type === 'Identifier') {\n addImport(state, {\n imported: '*',\n local: node.id,\n name: node.id.name,\n source,\n type,\n });\n return;\n }\n\n if (!isSync || node.id.type !== 'ObjectPattern') {\n return;\n }\n\n collectDestructed(node.id).forEach((destructed) => {\n addImport(state, {\n imported: destructed.what,\n local: destructed.as,\n source,\n type,\n });\n });\n};\n\nconst collectFromRequireDeclarator = (\n node: VariableDeclarator,\n scope: Scope,\n state: AnalyzerState\n): boolean => {\n if (!node.init) {\n return false;\n }\n\n const memberImport = sourceFromImportedMember(node.init, scope, state);\n if (memberImport) {\n if (node.id.type === 'Identifier') {\n defineBinding(scope, { kind: 'local', name: node.id.name });\n addImport(state, {\n imported: memberImport.imported,\n local: node.id,\n name: node.id.name,\n source: memberImport.source,\n type: 'cjs',\n });\n }\n return true;\n }\n\n const source = sourceFromRequireLike(node.init, scope, state);\n if (!source) {\n return false;\n }\n\n if (node.id.type === 'Identifier') {\n state.requireSources.set(node.id.name, source);\n const binding: NamespaceBinding = {\n kind: 'namespace',\n local: localFromNode(node.id, state.code, node.id.name),\n name: node.id.name,\n source,\n type: 'cjs',\n used: false,\n };\n defineBinding(scope, binding);\n state.namespaces.push(binding);\n return true;\n }\n\n if (node.id.type === 'ObjectPattern') {\n collectDestructed(node.id).forEach((destructed) => {\n addImport(state, {\n imported: destructed.what,\n local: destructed.as,\n source,\n type: 'cjs',\n });\n });\n return true;\n }\n\n return false;\n};\n\nconst collectFromNamespaceReference = (\n node: OxcIdentifier,\n parent: Node | null,\n ctx: VisitContext,\n state: AnalyzerState\n): void => {\n const binding = lookupBinding(ctx.scope, node.name);\n if (!binding || binding.kind !== 'namespace') {\n return;\n }\n\n if (isBindingPosition(node, parent, ctx.key)) {\n return;\n }\n\n if (isTypeNode(parent)) {\n return;\n }\n\n binding.used = true;\n\n if (parent?.type === 'MemberExpression' && parent.object === node) {\n const imported = nameFromMemberProperty(parent);\n addImport(state, {\n imported: imported ?? '*',\n local: imported ? parent : node,\n source: binding.source,\n type: binding.type,\n });\n return;\n }\n\n if (parent?.type === 'VariableDeclarator' && parent.init === node) {\n if (parent.id.type === 'ObjectPattern') {\n collectDestructed(parent.id).forEach((destructed) => {\n addImport(state, {\n imported: destructed.what,\n local: destructed.as,\n source: binding.source,\n type: binding.type,\n });\n });\n return;\n }\n }\n\n addImport(state, {\n imported: '*',\n local: node,\n name: node.name,\n source: binding.source,\n type: binding.type,\n });\n};\n\nconst isBindingPosition = (\n node: Node,\n parent: Node | null,\n key: string\n): boolean => {\n if (!parent) {\n return false;\n }\n\n if (parent.type === 'ImportNamespaceSpecifier' && key === 'local') {\n return true;\n }\n\n if (\n (parent.type === 'ImportSpecifier' ||\n parent.type === 'ImportDefaultSpecifier') &&\n key === 'local'\n ) {\n return true;\n }\n\n if (parent.type === 'VariableDeclarator' && key === 'id') {\n return true;\n }\n\n if (parent.type === 'FunctionDeclaration' && key === 'id') {\n return true;\n }\n\n if (parent.type === 'ClassDeclaration' && key === 'id') {\n return true;\n }\n\n if (\n parent.type === 'Property' &&\n parent.value === node &&\n parent.key !== node\n ) {\n return true;\n }\n\n return false;\n};\n\nconst isTypeNode = (node: Node | null): boolean =>\n !!node && (node.type.startsWith('TS') || node.type.startsWith('JSDoc'));\n\nconst collectFromAssignmentExpression = (\n node: AssignmentExpression,\n ctx: VisitContext,\n state: AnalyzerState\n): void => {\n if (node.operator !== '=') {\n return;\n }\n\n const exported = getExportAssignmentName(node.left);\n if (!exported || exported === '__esModule') {\n return;\n }\n\n const imported = sourceFromImportedMember(node.right, ctx.scope, state);\n if (imported) {\n addReexport(state, {\n exported,\n imported: imported.imported,\n local: node,\n source: imported.source,\n });\n return;\n }\n\n const directRequireSource = sourceFromDirectRequireBinding(\n node.right,\n ctx.scope,\n state\n );\n if (directRequireSource) {\n addReexport(state, {\n exported,\n imported: '*',\n local: node,\n source: directRequireSource,\n });\n return;\n }\n\n addExport(state, exported, node.right);\n};\n\nconst collectFromRequireExpressionStatement = (\n node: ExpressionStatement,\n scope: Scope,\n state: AnalyzerState\n): void => {\n const source = sourceFromDirectRequireBinding(node.expression, scope, state);\n if (!source) {\n return;\n }\n\n addImport(state, {\n imported: 'side-effect',\n local: node.expression,\n source,\n type: 'cjs',\n });\n};\n\nconst collectFromDefineProperty = (\n node: CallExpression,\n ctx: VisitContext,\n state: AnalyzerState\n): boolean => {\n if (\n node.callee.type !== 'MemberExpression' ||\n node.callee.object.type !== 'Identifier' ||\n node.callee.object.name !== 'Object' ||\n nameFromMemberProperty(node.callee) !== 'defineProperty'\n ) {\n return false;\n }\n\n const [target, nameArg, descriptor] = node.arguments;\n if (\n !target ||\n !nameArg ||\n !descriptor ||\n target.type === 'SpreadElement' ||\n nameArg.type === 'SpreadElement' ||\n descriptor.type === 'SpreadElement' ||\n !isExportsObject(target)\n ) {\n return false;\n }\n\n const exported = getStringConstant(nameArg);\n if (!exported || exported === '__esModule') {\n return true;\n }\n\n if (descriptor.type === 'ObjectExpression') {\n const getter = getObjectProperty(descriptor, 'get');\n const returned = getter ? getReturnedExpression(getter) : null;\n const imported = returned\n ? sourceFromImportedMember(returned, ctx.scope, state)\n : null;\n\n if (imported) {\n addReexport(state, {\n exported,\n imported: imported.imported,\n local: node,\n source: imported.source,\n });\n return true;\n }\n\n const directRequireSource = returned\n ? sourceFromDirectRequireBinding(returned, ctx.scope, state)\n : null;\n if (directRequireSource) {\n addReexport(state, {\n exported,\n imported: '*',\n local: node,\n source: directRequireSource,\n });\n return true;\n }\n\n if (returned) {\n addExport(state, exported, returned);\n return true;\n }\n }\n\n addExport(state, exported, node);\n return true;\n};\n\nconst collectFromHelperCall = (\n node: CallExpression,\n ctx: VisitContext,\n state: AnalyzerState\n): void => {\n const callee = getCalleeName(node.callee);\n if (!callee) {\n return;\n }\n\n if (collectFromDefineProperty(node, ctx, state)) {\n return;\n }\n\n if (callee === 'forEach' && node.callee.type === 'MemberExpression') {\n const { object } = node.callee;\n if (\n object.type === 'CallExpression' &&\n object.callee.type === 'MemberExpression' &&\n object.callee.object.type === 'Identifier' &&\n object.callee.object.name === 'Object' &&\n nameFromMemberProperty(object.callee) === 'keys'\n ) {\n const [keysArg] = object.arguments;\n if (keysArg && keysArg.type !== 'SpreadElement') {\n const source = sourceFromRequireLike(keysArg, ctx.scope, state);\n if (source) {\n addReexport(state, {\n exported: '*',\n imported: '*',\n local: node,\n source,\n });\n }\n }\n }\n return;\n }\n\n if (\n /(?:^|_)exportStar$/i.test(callee) ||\n callee === '_export_star' ||\n callee === '__reExport'\n ) {\n for (const arg of node.arguments) {\n if (arg.type === 'SpreadElement') {\n continue;\n }\n\n const source = sourceFromRequireLike(arg, ctx.scope, state);\n if (source) {\n addReexport(state, {\n exported: '*',\n imported: '*',\n local: node,\n source,\n });\n return;\n }\n }\n }\n\n if (callee === '__export' || callee === '_export') {\n const [firstArg, secondArg] = node.arguments;\n if (firstArg && firstArg.type !== 'SpreadElement') {\n const source = sourceFromRequireLike(firstArg, ctx.scope, state);\n if (source) {\n addReexport(state, {\n exported: '*',\n imported: '*',\n local: node,\n source,\n });\n return;\n }\n }\n\n if (secondArg?.type === 'ObjectExpression') {\n secondArg.properties.forEach((property) => {\n if (property.type === 'SpreadElement') {\n return;\n }\n\n const exported = nameFromPropertyKey(property.key);\n const returned = getReturnedExpression(property.value);\n if (!exported || !returned) {\n return;\n }\n\n const imported = sourceFromImportedMember(returned, ctx.scope, state);\n if (imported) {\n addReexport(state, {\n exported,\n imported: imported.imported,\n local: property,\n source: imported.source,\n });\n return;\n }\n\n const directRequireSource = sourceFromDirectRequireBinding(\n returned,\n ctx.scope,\n state\n );\n if (directRequireSource) {\n addReexport(state, {\n exported,\n imported: '*',\n local: property,\n source: directRequireSource,\n });\n return;\n }\n\n addExport(state, exported, returned);\n });\n }\n }\n};\n\nconst visit = (\n node: Node,\n ctx: VisitContext,\n state: AnalyzerState,\n mode: VisitMode = 'all'\n): void => {\n if (mode === 'all') {\n parentContainers.set(node, ctx.parent);\n }\n\n let { scope } = ctx;\n if (\n node.type === 'Program' ||\n node.type === 'BlockStatement' ||\n node.type === 'FunctionDeclaration' ||\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n scope = createScope(ctx.scope);\n }\n\n if (\n node.type === 'FunctionDeclaration' ||\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n node.params.forEach((param) => declareLocalBindingLike(scope, param));\n }\n\n if (node.type === 'ImportDeclaration') {\n collectFromImportDeclaration(node, scope, state);\n } else if (mode === 'all' && node.type === 'ExportNamedDeclaration') {\n collectFromExportNamedDeclaration(node, state);\n } else if (mode === 'all' && node.type === 'ExportAllDeclaration') {\n collectFromExportAllDeclaration(node, state);\n } else if (mode === 'all' && node.type === 'ExportDefaultDeclaration') {\n collectFromExportDefaultDeclaration(node, state);\n } else if (node.type === 'VariableDeclarator') {\n if (!collectFromRequireDeclarator(node, scope, state)) {\n declareLocalPattern(scope, node.id);\n }\n } else if (mode === 'all' && node.type === 'ImportExpression') {\n collectFromImportExpression(node, ctx.parent, state);\n } else if (mode === 'all' && node.type === 'CallExpression') {\n collectFromWywDynamicImport(node, ctx.parent, state);\n collectFromHelperCall(node, { ...ctx, scope }, state);\n } else if (mode === 'all' && node.type === 'ExpressionStatement') {\n collectFromRequireExpressionStatement(node, scope, state);\n } else if (mode === 'all' && node.type === 'AssignmentExpression') {\n collectFromAssignmentExpression(node, { ...ctx, scope }, state);\n } else if (node.type === 'Identifier') {\n collectFromNamespaceReference(node, ctx.parent, { ...ctx, scope }, state);\n }\n\n for (const child of getChildren(node)) {\n visit(child.node, { key: child.key, parent: node, scope }, state, mode);\n }\n};\n\nconst getChildren = (node: Node): { key: string; node: Node }[] => {\n const result: { key: string; node: Node }[] = [];\n const record = node as AnyNode;\n\n Object.keys(record).forEach((key) => {\n if (key === 'type' || key === 'start' || key === 'end' || key === 'range') {\n return;\n }\n\n const value = record[key];\n if (isNode(value)) {\n result.push({ key, node: value });\n return;\n }\n\n if (Array.isArray(value)) {\n value.forEach((item) => {\n if (isNode(item)) {\n result.push({ key, node: item });\n }\n });\n }\n });\n\n return result;\n};\n\nconst precollectRequireSources = (node: Node, state: AnalyzerState): void => {\n if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier') {\n const source = node.init ? sourceFromRequireSyntax(node.init) : null;\n if (source) {\n state.requireSources.set(node.id.name, source);\n }\n }\n\n getChildren(node).forEach((child) =>\n precollectRequireSources(child.node, state)\n );\n};\n\nconst addUnusedNamespaceSideEffects = (state: AnalyzerState): void => {\n state.namespaces.forEach((binding) => {\n if (!binding.used) {\n state.result.imports.push({\n imported: 'side-effect',\n local: binding.local,\n source: binding.source,\n type: binding.type,\n });\n }\n });\n};\n\nexport function collectOxcExportsAndImportsFromProgram(\n program: Program,\n code: string,\n isEsModule: boolean\n): OxcCollectedState {\n const rootScope = createScope(null);\n const state: AnalyzerState = {\n code,\n namespaces: [],\n requireSources: new Map(),\n result: {\n deadExports: [],\n exports: {},\n imports: [],\n isEsModule,\n reexports: [],\n },\n };\n\n precollectRequireSources(program, state);\n visit(\n program,\n { key: 'program', parent: null, scope: rootScope },\n state,\n 'all'\n );\n addUnusedNamespaceSideEffects(state);\n\n return state.result;\n}\n\nexport function collectOxcProcessorImportsFromProgram(\n program: Program,\n code: string\n): OxcCollectedImport[] {\n const rootScope = createScope(null);\n const state: AnalyzerState = {\n code,\n namespaces: [],\n requireSources: new Map(),\n result: {\n deadExports: [],\n exports: {},\n imports: [],\n isEsModule: true,\n reexports: [],\n },\n };\n\n precollectRequireSources(program, state);\n visit(\n program,\n { key: 'program', parent: null, scope: rootScope },\n state,\n 'importsOnly'\n );\n\n return state.result.imports;\n}\n\nexport function collectOxcExportsAndImports(\n code: string,\n filename: string\n): OxcCollectedState {\n const parsed = parseOxcCached(filename, code, 'unambiguous');\n\n return collectOxcExportsAndImportsFromProgram(\n parsed.program,\n code,\n parsed.module.hasModuleSyntax\n );\n}\n"],"file":"collectOxcExportsAndImports.js"}
|
|
1
|
+
{"mappings":"AA2BA,SAAS,sBAAsB;AAsF/B,MAAM,eAAe,YAAiC;CACpD,UAAU,IAAI,KAAK;CACnB;CACD;AAED,MAAM,UAAU,UACd,CAAC,CAAC,SACF,OAAO,UAAU,YACjB,UAAU,SACV,OAAQ,MAA6B,SAAS;AAEhD,MAAM,iBAAiB,MAAY,MAAc,UAA6B;CAC5E,MAAM,KAAK,MAAM,KAAK,OAAO,KAAK,IAAI;CACtC,KAAK,KAAK;CACV,MAAM,SAAS,KAAK,SAAS,eAAe,KAAK,OAAO;CACxD,OAAO,KAAK;CACb;AAED,MAAM,wBAAwB,SAC5B,KAAK,SAAS,YAAY,KAAK,QAAQ,KAAK;AAE9C,MAAM,uBAAuB,QAAoC;AAC/D,KAAI,IAAI,SAAS,cAAc;AAC7B,SAAO,IAAI;;AAGb,KAAI,IAAI,SAAS,aAAa,OAAO,IAAI,UAAU,UAAU;AAC3D,SAAO,IAAI;;AAGb,QAAO;;AAGT,MAAM,0BAA0B,SAA0C;AACxE,KAAI,KAAK,UAAU;AACjB,SAAO,KAAK,SAAS,SAAS,aAC5B,OAAO,KAAK,SAAS,UAAU,WAC7B,KAAK,SAAS,QACd;;AAGN,QAAO,KAAK,SAAS,SAAS,eAAe,KAAK,SAAS,OAAO;;AAGpE,MAAM,iBAAiB,OAAc,YAA2B;AAC9D,OAAM,SAAS,IAAI,QAAQ,MAAM,QAAQ;;AAG3C,MAAM,iBAAiB,OAAc,SAAiC;CACpE,IAAI,UAAwB;AAC5B,QAAO,SAAS;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AAW,QAAQ;EAAM,OAAO;EAAW,EAClD,OACA,cACD;AAED,QAAO,MAAM,OAAO;;AAGtB,OAAO,SAAS,4BACd,MACA,UACmB;CACnB,MAAM,SAAS,eAAe,UAAU,MAAM,cAAc;AAE5D,QAAO,uCACL,OAAO,SACP,MACA,OAAO,OAAO,gBACf","names":[],"sources":["../../src/utils/collectOxcExportsAndImports.ts"],"version":3,"sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define,no-restricted-syntax,no-continue */\n\nimport type {\n AssignmentExpression,\n BindingPattern,\n CallExpression,\n Class as OxcClass,\n ExportAllDeclaration,\n ExportDefaultDeclaration,\n ExportNamedDeclaration,\n ExportSpecifier,\n Expression,\n ExpressionStatement,\n Function as OxcFunction,\n ImportDeclaration,\n ImportExpression,\n ImportSpecifier,\n MemberExpression,\n ModuleExportName,\n Node,\n ObjectExpression,\n ObjectPattern,\n Program,\n PropertyKey,\n VariableDeclarator,\n} from 'oxc-parser';\n\nimport { parseOxcCached } from './parseOxc';\n\ntype ImportKind = 'cjs' | 'dynamic' | 'esm';\n\nexport type OxcLocal = {\n code: string;\n end: number;\n name?: string;\n start: number;\n};\n\nexport type OxcCollectedImport = {\n imported: string | 'default' | '*' | 'side-effect';\n local: OxcLocal;\n source: string;\n type: ImportKind;\n};\n\nexport type OxcCollectedExport = {\n exported: string | 'default' | '*';\n local: OxcLocal;\n};\n\nexport type OxcCollectedReexport = {\n exported: string | 'default' | '*';\n imported: string | 'default' | '*';\n local: OxcLocal;\n source: string;\n};\n\nexport type OxcCollectedState = {\n deadExports: string[];\n exports: Record<string | 'default' | '*', OxcLocal>;\n imports: OxcCollectedImport[];\n isEsModule: boolean;\n reexports: OxcCollectedReexport[];\n};\n\ntype VisitMode = 'all' | 'importsOnly';\n\ntype NamespaceBinding = {\n kind: 'namespace';\n local: OxcLocal;\n name: string;\n source: string;\n type: ImportKind;\n used: boolean;\n};\n\ntype LocalBinding = {\n kind: 'local';\n name: string;\n};\n\ntype Binding = LocalBinding | NamespaceBinding;\n\ntype Scope = {\n bindings: Map<string, Binding>;\n parent: Scope | null;\n};\n\ntype ChildContext = {\n key: string;\n parent: Node | null;\n};\n\ntype VisitContext = ChildContext & {\n scope: Scope;\n};\n\ntype AnyNode = Node & Record<string, unknown>;\n\ntype OxcIdentifier = Node & { name: string; type: 'Identifier' };\n\ntype Destructed = {\n as: Node;\n what: string | '*';\n};\n\ntype AnalyzerState = {\n code: string;\n namespaces: NamespaceBinding[];\n requireSources: Map<string, string>;\n result: OxcCollectedState;\n};\n\nconst createScope = (parent: Scope | null): Scope => ({\n bindings: new Map(),\n parent,\n});\n\nconst isNode = (value: unknown): value is Node =>\n !!value &&\n typeof value === 'object' &&\n 'type' in value &&\n typeof (value as { type?: unknown }).type === 'string';\n\nconst localFromNode = (node: Node, code: string, name?: string): OxcLocal => ({\n code: code.slice(node.start, node.end),\n end: node.end,\n name: name ?? (node.type === 'Identifier' ? node.name : undefined),\n start: node.start,\n});\n\nconst nameFromModuleExport = (node: ModuleExportName): string =>\n node.type === 'Literal' ? node.value : node.name;\n\nconst nameFromPropertyKey = (key: PropertyKey): string | null => {\n if (key.type === 'Identifier') {\n return key.name;\n }\n\n if (key.type === 'Literal' && typeof key.value === 'string') {\n return key.value;\n }\n\n return null;\n};\n\nconst nameFromMemberProperty = (node: MemberExpression): string | null => {\n if (node.computed) {\n return node.property.type === 'Literal' &&\n typeof node.property.value === 'string'\n ? node.property.value\n : null;\n }\n\n return node.property.type === 'Identifier' ? node.property.name : null;\n};\n\nconst defineBinding = (scope: Scope, binding: Binding): void => {\n scope.bindings.set(binding.name, binding);\n};\n\nconst lookupBinding = (scope: Scope, name: string): Binding | null => {\n let current: Scope | null = scope;\n while (current) {\n const binding = current.bindings.get(name);\n if (binding) {\n return binding;\n }\n\n current = current.parent;\n }\n\n return null;\n};\n\nconst collectBindingNames = (pattern: BindingPattern): string[] => {\n if (pattern.type === 'Identifier') {\n return [pattern.name];\n }\n\n if (pattern.type === 'AssignmentPattern') {\n return collectBindingNames(pattern.left);\n }\n\n if (pattern.type === 'ObjectPattern') {\n return pattern.properties.flatMap((property) =>\n property.type === 'RestElement'\n ? collectBindingNames(property.argument)\n : collectBindingNames(property.value)\n );\n }\n\n if (pattern.type === 'ArrayPattern') {\n return pattern.elements.flatMap((element) =>\n element ? collectBindingLikeNames(element) : []\n );\n }\n\n return [];\n};\n\nconst collectBindingLikeNames = (node: Node): string[] => {\n if (node.type === 'RestElement') {\n return collectBindingLikeNames(node.argument);\n }\n\n if (node.type === 'TSParameterProperty') {\n return collectBindingLikeNames(node.parameter);\n }\n\n return collectBindingNames(node as BindingPattern);\n};\n\nconst declareLocalPattern = (scope: Scope, pattern: BindingPattern): void => {\n collectBindingNames(pattern).forEach((name) =>\n defineBinding(scope, { kind: 'local', name })\n );\n};\n\nconst declareLocalBindingLike = (scope: Scope, node: Node): void => {\n collectBindingLikeNames(node).forEach((name) =>\n defineBinding(scope, { kind: 'local', name })\n );\n};\n\nconst isTypeOnlyImport = (\n declaration: ImportDeclaration,\n specifier?: ImportSpecifier\n): boolean =>\n declaration.importKind === 'type' || specifier?.importKind === 'type';\n\nconst isTypeOnlyExport = (\n declaration:\n | ExportAllDeclaration\n | ExportDefaultDeclaration\n | ExportNamedDeclaration\n | ExportSpecifier\n): boolean => 'exportKind' in declaration && declaration.exportKind === 'type';\n\nconst addImport = (\n state: AnalyzerState,\n item: Omit<OxcCollectedImport, 'local'> & { local: Node; name?: string }\n): void => {\n state.result.imports.push({\n imported: item.imported,\n local: localFromNode(item.local, state.code, item.name),\n source: item.source,\n type: item.type,\n });\n};\n\nconst addExport = (\n state: AnalyzerState,\n exported: string | 'default' | '*',\n local: Node,\n name?: string\n): void => {\n const { result } = state;\n result.exports[exported] = localFromNode(local, state.code, name);\n};\n\nconst addReexport = (\n state: AnalyzerState,\n item: Omit<OxcCollectedReexport, 'local'> & { local: Node }\n): void => {\n state.result.reexports.push({\n exported: item.exported,\n imported: item.imported,\n local: localFromNode(item.local, state.code),\n source: item.source,\n });\n};\n\nconst collectDestructed = (pattern: ObjectPattern): Destructed[] =>\n pattern.properties.flatMap((property) => {\n if (property.type === 'RestElement') {\n return collectBindingNames(property.argument).map(() => ({\n as: property.argument,\n what: '*' as const,\n }));\n }\n\n const firstKey = nameFromPropertyKey(property.key);\n if (!firstKey) {\n return [];\n }\n\n if (property.value.type === 'ObjectPattern') {\n return collectBindingNames(property.value).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n }\n\n if (property.value.type === 'ArrayPattern') {\n return collectBindingNames(property.value).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n }\n\n if (property.value.type === 'AssignmentPattern') {\n return collectBindingNames(property.value.left).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n }\n\n return collectBindingNames(property.value).map(() => ({\n as: property.value,\n what: firstKey,\n }));\n });\n\nconst getStringConstant = (expression: Expression): string | null => {\n if (expression.type === 'Literal' && typeof expression.value === 'string') {\n return expression.value;\n }\n\n if (\n expression.type === 'TemplateLiteral' &&\n expression.expressions.length === 0\n ) {\n return expression.quasis[0]?.value.cooked ?? null;\n }\n\n if (expression.type === 'BinaryExpression' && expression.operator === '+') {\n const left = getStringConstant(expression.left);\n const right = getStringConstant(expression.right);\n return left === null || right === null ? null : left + right;\n }\n\n if (\n expression.type === 'CallExpression' &&\n expression.callee.type === 'MemberExpression' &&\n nameFromMemberProperty(expression.callee) === 'concat'\n ) {\n const base = getStringConstant(expression.callee.object);\n if (base === null) {\n return null;\n }\n\n const parts = expression.arguments.map((arg) =>\n arg.type === 'SpreadElement' ? null : getStringConstant(arg)\n );\n if (parts.some((part) => part === null)) {\n return null;\n }\n\n return [base, ...(parts as string[])].join('');\n }\n\n if (\n expression.type === 'TSAsExpression' ||\n expression.type === 'TSSatisfiesExpression' ||\n expression.type === 'TSNonNullExpression' ||\n expression.type === 'TSTypeAssertion' ||\n expression.type === 'ParenthesizedExpression'\n ) {\n return getStringConstant(expression.expression);\n }\n\n return null;\n};\n\nconst isRequireCall = (node: Node, scope: Scope): boolean =>\n node.type === 'CallExpression' &&\n node.callee.type === 'Identifier' &&\n node.callee.name === 'require' &&\n lookupBinding(scope, 'require') === null;\n\nconst sourceFromRequireLike = (\n node: Node,\n scope: Scope,\n state: AnalyzerState\n): string | null => {\n if (isRequireCall(node, scope)) {\n const call = node as CallExpression;\n const [sourceArg] = call.arguments;\n if (!sourceArg || sourceArg.type === 'SpreadElement') {\n return null;\n }\n\n return getStringConstant(sourceArg);\n }\n\n if (node.type === 'Identifier') {\n return state.requireSources.get(node.name) ?? null;\n }\n\n if (node.type === 'CallExpression') {\n for (const arg of node.arguments) {\n if (arg.type === 'SpreadElement') {\n continue;\n }\n\n const source = sourceFromRequireLike(arg, scope, state);\n if (source) {\n return source;\n }\n }\n }\n\n return null;\n};\n\nconst sourceFromDirectRequireBinding = (\n node: Node,\n scope: Scope,\n state: AnalyzerState\n): string | null => {\n if (isRequireCall(node, scope)) {\n const call = node as CallExpression;\n const [sourceArg] = call.arguments;\n if (!sourceArg || sourceArg.type === 'SpreadElement') {\n return null;\n }\n\n return getStringConstant(sourceArg);\n }\n\n if (node.type === 'Identifier') {\n return state.requireSources.get(node.name) ?? null;\n }\n\n return null;\n};\n\nconst sourceFromRequireSyntax = (node: Node): string | null => {\n if (\n node.type === 'CallExpression' &&\n node.callee.type === 'Identifier' &&\n node.callee.name === 'require'\n ) {\n const [sourceArg] = node.arguments;\n return sourceArg && sourceArg.type !== 'SpreadElement'\n ? getStringConstant(sourceArg)\n : null;\n }\n\n if (node.type === 'CallExpression') {\n for (const arg of node.arguments) {\n if (arg.type === 'SpreadElement') {\n continue;\n }\n\n const source = sourceFromRequireSyntax(arg);\n if (source) {\n return source;\n }\n }\n }\n\n return null;\n};\n\nconst sourceFromImportedMember = (\n node: Node,\n scope: Scope,\n state: AnalyzerState\n): { imported: string | '*' | 'default'; source: string } | null => {\n if (node.type !== 'MemberExpression') {\n return null;\n }\n\n const source = sourceFromRequireLike(node.object, scope, state);\n const imported = nameFromMemberProperty(node);\n if (!source || !imported) {\n return null;\n }\n\n return { imported, source };\n};\n\nconst isExportsObject = (node: Node): boolean =>\n node.type === 'Identifier' && node.name === 'exports';\n\nconst getExportAssignmentName = (node: Node): string | 'default' | null => {\n if (node.type !== 'MemberExpression') {\n return null;\n }\n\n if (isExportsObject(node.object)) {\n return nameFromMemberProperty(node);\n }\n\n if (\n node.object.type === 'Identifier' &&\n node.object.name === 'module' &&\n nameFromMemberProperty(node) === 'exports'\n ) {\n return 'default';\n }\n\n return null;\n};\n\nconst getCalleeName = (node: Expression): string | null => {\n if (node.type === 'Identifier') {\n return node.name;\n }\n\n if (node.type === 'MemberExpression') {\n return nameFromMemberProperty(node);\n }\n\n return null;\n};\n\nconst getObjectProperty = (\n objectExpression: ObjectExpression,\n name: string\n): Node | null => {\n for (const property of objectExpression.properties) {\n if (property.type === 'SpreadElement') {\n continue;\n }\n\n if (nameFromPropertyKey(property.key) === name) {\n return property.value;\n }\n }\n\n return null;\n};\n\nconst getReturnedExpression = (node: Node): Expression | null => {\n if (node.type === 'ArrowFunctionExpression') {\n return node.body.type === 'BlockStatement'\n ? getReturnedExpression(node.body)\n : node.body;\n }\n\n if (\n node.type === 'FunctionExpression' ||\n node.type === 'FunctionDeclaration'\n ) {\n return node.body ? getReturnedExpression(node.body) : null;\n }\n\n if (node.type === 'BlockStatement') {\n const returned = node.body.find(\n (statement) => statement.type === 'ReturnStatement'\n );\n return returned?.type === 'ReturnStatement' ? returned.argument : null;\n }\n\n return null;\n};\n\nconst collectFromImportDeclaration = (\n node: ImportDeclaration,\n scope: Scope,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyImport(node)) {\n return;\n }\n\n const source = node.source.value;\n if (node.specifiers.length === 0) {\n addImport(state, {\n imported: 'side-effect',\n local: node,\n source,\n type: 'esm',\n });\n return;\n }\n\n node.specifiers.forEach((specifier) => {\n if (\n specifier.type === 'ImportSpecifier' &&\n isTypeOnlyImport(node, specifier)\n ) {\n return;\n }\n\n if (specifier.type === 'ImportNamespaceSpecifier') {\n const binding: NamespaceBinding = {\n kind: 'namespace',\n local: localFromNode(specifier.local, state.code, specifier.local.name),\n name: specifier.local.name,\n source,\n type: 'esm',\n used: false,\n };\n defineBinding(scope, binding);\n state.namespaces.push(binding);\n return;\n }\n\n if (specifier.type === 'ImportDefaultSpecifier') {\n defineBinding(scope, { kind: 'local', name: specifier.local.name });\n addImport(state, {\n imported: 'default',\n local: specifier.local,\n name: specifier.local.name,\n source,\n type: 'esm',\n });\n return;\n }\n\n defineBinding(scope, { kind: 'local', name: specifier.local.name });\n addImport(state, {\n imported: nameFromModuleExport(specifier.imported),\n local: specifier.local,\n name: specifier.local.name,\n source,\n type: 'esm',\n });\n });\n};\n\nconst collectExportedDeclaration = (\n declaration: ExportNamedDeclaration['declaration'],\n state: AnalyzerState\n): void => {\n if (!declaration) {\n return;\n }\n\n if (declaration.type === 'VariableDeclaration') {\n declaration.declarations.forEach((declarator) => {\n exportFromVariableDeclarator(declarator, state);\n });\n return;\n }\n\n if (declaration.type === 'TSEnumDeclaration') {\n addExport(state, declaration.id.name, declaration.id, declaration.id.name);\n return;\n }\n\n if (\n declaration.type === 'FunctionDeclaration' ||\n declaration.type === 'ClassDeclaration'\n ) {\n const { id } = declaration as OxcFunction | OxcClass;\n if (id) {\n addExport(state, id.name, id, id.name);\n }\n }\n};\n\nconst collectFromExportNamedDeclaration = (\n node: ExportNamedDeclaration,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyExport(node)) {\n return;\n }\n\n const source = node.source?.value;\n node.specifiers.forEach((specifier) => {\n if (isTypeOnlyExport(specifier)) {\n return;\n }\n\n const exported = nameFromModuleExport(specifier.exported);\n const imported = nameFromModuleExport(specifier.local);\n\n if (source) {\n addReexport(state, {\n exported,\n imported,\n local: specifier,\n source,\n });\n return;\n }\n\n addExport(state, exported, specifier.local, imported);\n });\n\n collectExportedDeclaration(node.declaration, state);\n};\n\nconst collectFromExportAllDeclaration = (\n node: ExportAllDeclaration,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyExport(node)) {\n return;\n }\n\n addReexport(state, {\n exported: node.exported ? nameFromModuleExport(node.exported) : '*',\n imported: '*',\n local: node,\n source: node.source.value,\n });\n};\n\nconst collectFromExportDefaultDeclaration = (\n node: ExportDefaultDeclaration,\n state: AnalyzerState\n): void => {\n if (isTypeOnlyExport(node)) {\n return;\n }\n\n addExport(state, 'default', node.declaration);\n};\n\nconst exportFromVariableDeclarator = (\n node: VariableDeclarator,\n state: AnalyzerState\n): void => {\n if (node.id.type === 'Identifier') {\n addExport(state, node.id.name, node.init ?? node.id, node.id.name);\n return;\n }\n\n if (node.id.type === 'ObjectPattern') {\n collectDestructed(node.id).forEach((destructed) => {\n if (destructed.as.type === 'Identifier') {\n addExport(\n state,\n destructed.as.name,\n node.init ?? destructed.as,\n destructed.as.name\n );\n }\n });\n return;\n }\n\n if (node.id.type === 'ArrayPattern') {\n collectBindingNames(node.id).forEach((name) =>\n addExport(state, name, node.init ?? node.id, name)\n );\n }\n};\n\nconst collectFromImportExpression = (\n node: ImportExpression,\n parent: Node | null,\n state: AnalyzerState\n): void => {\n const source = getStringConstant(node.source);\n if (!source) {\n return;\n }\n\n let container = parent;\n let awaited = false;\n if (container?.type === 'AwaitExpression') {\n awaited = true;\n container = findParentContainer(container);\n }\n\n if (container?.type === 'VariableDeclarator') {\n importFromVariableDeclarator(container, awaited, source, 'dynamic', state);\n }\n};\n\nconst collectFromWywDynamicImport = (\n node: CallExpression,\n parent: Node | null,\n state: AnalyzerState\n): void => {\n if (\n node.callee.type !== 'Identifier' ||\n node.callee.name !== '__wyw_dynamic_import'\n ) {\n return;\n }\n\n const [sourceArg] = node.arguments;\n if (!sourceArg || sourceArg.type === 'SpreadElement') {\n return;\n }\n\n const source = getStringConstant(sourceArg);\n if (!source) {\n return;\n }\n\n let container = parent;\n let awaited = false;\n if (container?.type === 'AwaitExpression') {\n awaited = true;\n container = findParentContainer(container);\n }\n\n if (container?.type === 'VariableDeclarator') {\n importFromVariableDeclarator(container, awaited, source, 'dynamic', state);\n return;\n }\n\n addImport(state, {\n imported: '*',\n local: node,\n source,\n type: 'dynamic',\n });\n};\n\nconst parentContainers = new WeakMap<Node, Node | null>();\n\nconst findParentContainer = (node: Node): Node | null =>\n parentContainers.get(node) ?? null;\n\nconst importFromVariableDeclarator = (\n node: VariableDeclarator,\n isSync: boolean,\n source: string,\n type: ImportKind,\n state: AnalyzerState\n): void => {\n if (node.id.type === 'Identifier') {\n addImport(state, {\n imported: '*',\n local: node.id,\n name: node.id.name,\n source,\n type,\n });\n return;\n }\n\n if (!isSync || node.id.type !== 'ObjectPattern') {\n return;\n }\n\n collectDestructed(node.id).forEach((destructed) => {\n addImport(state, {\n imported: destructed.what,\n local: destructed.as,\n source,\n type,\n });\n });\n};\n\nconst collectFromRequireDeclarator = (\n node: VariableDeclarator,\n scope: Scope,\n state: AnalyzerState\n): boolean => {\n if (!node.init) {\n return false;\n }\n\n const memberImport = sourceFromImportedMember(node.init, scope, state);\n if (memberImport) {\n if (node.id.type === 'Identifier') {\n defineBinding(scope, { kind: 'local', name: node.id.name });\n addImport(state, {\n imported: memberImport.imported,\n local: node.id,\n name: node.id.name,\n source: memberImport.source,\n type: 'cjs',\n });\n }\n return true;\n }\n\n const source = sourceFromRequireLike(node.init, scope, state);\n if (!source) {\n return false;\n }\n\n if (node.id.type === 'Identifier') {\n state.requireSources.set(node.id.name, source);\n const binding: NamespaceBinding = {\n kind: 'namespace',\n local: localFromNode(node.id, state.code, node.id.name),\n name: node.id.name,\n source,\n type: 'cjs',\n used: false,\n };\n defineBinding(scope, binding);\n state.namespaces.push(binding);\n return true;\n }\n\n if (node.id.type === 'ObjectPattern') {\n collectDestructed(node.id).forEach((destructed) => {\n addImport(state, {\n imported: destructed.what,\n local: destructed.as,\n source,\n type: 'cjs',\n });\n });\n return true;\n }\n\n return false;\n};\n\nconst collectFromNamespaceReference = (\n node: OxcIdentifier,\n parent: Node | null,\n ctx: VisitContext,\n state: AnalyzerState\n): void => {\n const binding = lookupBinding(ctx.scope, node.name);\n if (!binding || binding.kind !== 'namespace') {\n return;\n }\n\n if (isBindingPosition(node, parent, ctx.key)) {\n return;\n }\n\n if (isTypeNode(parent)) {\n return;\n }\n\n binding.used = true;\n\n if (parent?.type === 'MemberExpression' && parent.object === node) {\n const imported = nameFromMemberProperty(parent);\n addImport(state, {\n imported: imported ?? '*',\n local: imported ? parent : node,\n source: binding.source,\n type: binding.type,\n });\n return;\n }\n\n if (parent?.type === 'VariableDeclarator' && parent.init === node) {\n if (parent.id.type === 'ObjectPattern') {\n collectDestructed(parent.id).forEach((destructed) => {\n addImport(state, {\n imported: destructed.what,\n local: destructed.as,\n source: binding.source,\n type: binding.type,\n });\n });\n return;\n }\n }\n\n addImport(state, {\n imported: '*',\n local: node,\n name: node.name,\n source: binding.source,\n type: binding.type,\n });\n};\n\nconst isBindingPosition = (\n node: Node,\n parent: Node | null,\n key: string\n): boolean => {\n if (!parent) {\n return false;\n }\n\n if (parent.type === 'ImportNamespaceSpecifier' && key === 'local') {\n return true;\n }\n\n if (\n (parent.type === 'ImportSpecifier' ||\n parent.type === 'ImportDefaultSpecifier') &&\n key === 'local'\n ) {\n return true;\n }\n\n if (parent.type === 'VariableDeclarator' && key === 'id') {\n return true;\n }\n\n if (parent.type === 'FunctionDeclaration' && key === 'id') {\n return true;\n }\n\n if (parent.type === 'ClassDeclaration' && key === 'id') {\n return true;\n }\n\n if (\n parent.type === 'Property' &&\n parent.value === node &&\n parent.key !== node\n ) {\n return true;\n }\n\n return false;\n};\n\nconst isTypeNode = (node: Node | null): boolean =>\n !!node && (node.type.startsWith('TS') || node.type.startsWith('JSDoc'));\n\nconst collectFromAssignmentExpression = (\n node: AssignmentExpression,\n ctx: VisitContext,\n state: AnalyzerState\n): void => {\n if (node.operator !== '=') {\n return;\n }\n\n const exported = getExportAssignmentName(node.left);\n if (!exported || exported === '__esModule') {\n return;\n }\n\n const imported = sourceFromImportedMember(node.right, ctx.scope, state);\n if (imported) {\n addReexport(state, {\n exported,\n imported: imported.imported,\n local: node,\n source: imported.source,\n });\n return;\n }\n\n const directRequireSource = sourceFromDirectRequireBinding(\n node.right,\n ctx.scope,\n state\n );\n if (directRequireSource) {\n addReexport(state, {\n exported,\n imported: '*',\n local: node,\n source: directRequireSource,\n });\n return;\n }\n\n addExport(state, exported, node.right);\n};\n\nconst collectFromRequireExpressionStatement = (\n node: ExpressionStatement,\n scope: Scope,\n state: AnalyzerState\n): void => {\n const source = sourceFromDirectRequireBinding(node.expression, scope, state);\n if (!source) {\n return;\n }\n\n addImport(state, {\n imported: 'side-effect',\n local: node.expression,\n source,\n type: 'cjs',\n });\n};\n\nconst collectFromDefineProperty = (\n node: CallExpression,\n ctx: VisitContext,\n state: AnalyzerState\n): boolean => {\n if (\n node.callee.type !== 'MemberExpression' ||\n node.callee.object.type !== 'Identifier' ||\n node.callee.object.name !== 'Object' ||\n nameFromMemberProperty(node.callee) !== 'defineProperty'\n ) {\n return false;\n }\n\n const [target, nameArg, descriptor] = node.arguments;\n if (\n !target ||\n !nameArg ||\n !descriptor ||\n target.type === 'SpreadElement' ||\n nameArg.type === 'SpreadElement' ||\n descriptor.type === 'SpreadElement' ||\n !isExportsObject(target)\n ) {\n return false;\n }\n\n const exported = getStringConstant(nameArg);\n if (!exported || exported === '__esModule') {\n return true;\n }\n\n if (descriptor.type === 'ObjectExpression') {\n const getter = getObjectProperty(descriptor, 'get');\n const returned = getter ? getReturnedExpression(getter) : null;\n const imported = returned\n ? sourceFromImportedMember(returned, ctx.scope, state)\n : null;\n\n if (imported) {\n addReexport(state, {\n exported,\n imported: imported.imported,\n local: node,\n source: imported.source,\n });\n return true;\n }\n\n const directRequireSource = returned\n ? sourceFromDirectRequireBinding(returned, ctx.scope, state)\n : null;\n if (directRequireSource) {\n addReexport(state, {\n exported,\n imported: '*',\n local: node,\n source: directRequireSource,\n });\n return true;\n }\n\n if (returned) {\n addExport(state, exported, returned);\n return true;\n }\n }\n\n addExport(state, exported, node);\n return true;\n};\n\nconst collectFromHelperCall = (\n node: CallExpression,\n ctx: VisitContext,\n state: AnalyzerState\n): void => {\n const callee = getCalleeName(node.callee);\n if (!callee) {\n return;\n }\n\n if (collectFromDefineProperty(node, ctx, state)) {\n return;\n }\n\n if (callee === 'forEach' && node.callee.type === 'MemberExpression') {\n const { object } = node.callee;\n if (\n object.type === 'CallExpression' &&\n object.callee.type === 'MemberExpression' &&\n object.callee.object.type === 'Identifier' &&\n object.callee.object.name === 'Object' &&\n nameFromMemberProperty(object.callee) === 'keys'\n ) {\n const [keysArg] = object.arguments;\n if (keysArg && keysArg.type !== 'SpreadElement') {\n const source = sourceFromRequireLike(keysArg, ctx.scope, state);\n if (source) {\n addReexport(state, {\n exported: '*',\n imported: '*',\n local: node,\n source,\n });\n }\n }\n }\n return;\n }\n\n if (\n /(?:^|_)exportStar$/i.test(callee) ||\n callee === '_export_star' ||\n callee === '__reExport'\n ) {\n for (const arg of node.arguments) {\n if (arg.type === 'SpreadElement') {\n continue;\n }\n\n const source = sourceFromRequireLike(arg, ctx.scope, state);\n if (source) {\n addReexport(state, {\n exported: '*',\n imported: '*',\n local: node,\n source,\n });\n return;\n }\n }\n }\n\n if (callee === '__export' || callee === '_export') {\n const [firstArg, secondArg] = node.arguments;\n if (firstArg && firstArg.type !== 'SpreadElement') {\n const source = sourceFromRequireLike(firstArg, ctx.scope, state);\n if (source) {\n addReexport(state, {\n exported: '*',\n imported: '*',\n local: node,\n source,\n });\n return;\n }\n }\n\n if (secondArg?.type === 'ObjectExpression') {\n secondArg.properties.forEach((property) => {\n if (property.type === 'SpreadElement') {\n return;\n }\n\n const exported = nameFromPropertyKey(property.key);\n const returned = getReturnedExpression(property.value);\n if (!exported || !returned) {\n return;\n }\n\n const imported = sourceFromImportedMember(returned, ctx.scope, state);\n if (imported) {\n addReexport(state, {\n exported,\n imported: imported.imported,\n local: property,\n source: imported.source,\n });\n return;\n }\n\n const directRequireSource = sourceFromDirectRequireBinding(\n returned,\n ctx.scope,\n state\n );\n if (directRequireSource) {\n addReexport(state, {\n exported,\n imported: '*',\n local: property,\n source: directRequireSource,\n });\n return;\n }\n\n addExport(state, exported, returned);\n });\n }\n }\n};\n\nconst visit = (\n node: Node,\n ctx: VisitContext,\n state: AnalyzerState,\n mode: VisitMode = 'all'\n): void => {\n if (mode === 'all') {\n parentContainers.set(node, ctx.parent);\n }\n\n let { scope } = ctx;\n if (\n node.type === 'Program' ||\n node.type === 'BlockStatement' ||\n node.type === 'FunctionDeclaration' ||\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n scope = createScope(ctx.scope);\n }\n\n if (\n node.type === 'FunctionDeclaration' ||\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n node.params.forEach((param) => declareLocalBindingLike(scope, param));\n }\n\n if (node.type === 'ImportDeclaration') {\n collectFromImportDeclaration(node, scope, state);\n } else if (mode === 'all' && node.type === 'ExportNamedDeclaration') {\n collectFromExportNamedDeclaration(node, state);\n } else if (mode === 'all' && node.type === 'ExportAllDeclaration') {\n collectFromExportAllDeclaration(node, state);\n } else if (mode === 'all' && node.type === 'ExportDefaultDeclaration') {\n collectFromExportDefaultDeclaration(node, state);\n } else if (node.type === 'VariableDeclarator') {\n if (!collectFromRequireDeclarator(node, scope, state)) {\n declareLocalPattern(scope, node.id);\n }\n } else if (mode === 'all' && node.type === 'ImportExpression') {\n collectFromImportExpression(node, ctx.parent, state);\n } else if (mode === 'all' && node.type === 'CallExpression') {\n collectFromWywDynamicImport(node, ctx.parent, state);\n collectFromHelperCall(node, { ...ctx, scope }, state);\n } else if (mode === 'all' && node.type === 'ExpressionStatement') {\n collectFromRequireExpressionStatement(node, scope, state);\n } else if (mode === 'all' && node.type === 'AssignmentExpression') {\n collectFromAssignmentExpression(node, { ...ctx, scope }, state);\n } else if (node.type === 'Identifier') {\n collectFromNamespaceReference(node, ctx.parent, { ...ctx, scope }, state);\n }\n\n for (const child of getChildren(node)) {\n visit(child.node, { key: child.key, parent: node, scope }, state, mode);\n }\n};\n\nconst getChildren = (node: Node): { key: string; node: Node }[] => {\n const result: { key: string; node: Node }[] = [];\n const record = node as AnyNode;\n\n Object.keys(record).forEach((key) => {\n if (key === 'type' || key === 'start' || key === 'end' || key === 'range') {\n return;\n }\n\n const value = record[key];\n if (isNode(value)) {\n result.push({ key, node: value });\n return;\n }\n\n if (Array.isArray(value)) {\n value.forEach((item) => {\n if (isNode(item)) {\n result.push({ key, node: item });\n }\n });\n }\n });\n\n return result;\n};\n\nconst precollectRequireSources = (node: Node, state: AnalyzerState): void => {\n // Cheap text precheck at the Program entry: if the file body has no\n // 'require(' substring there can be no CommonJS require() init to collect.\n // Skip the full AST walk — meaningful saving on ESM-only modules in large\n // monorepos. Done at Program-level only so nested recursion doesn't pay\n // the indexOf cost per node.\n if (node.type === 'Program' && state.code.indexOf('require(') === -1) {\n return;\n }\n\n if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier') {\n const source = node.init ? sourceFromRequireSyntax(node.init) : null;\n if (source) {\n state.requireSources.set(node.id.name, source);\n }\n }\n\n getChildren(node).forEach((child) =>\n precollectRequireSources(child.node, state)\n );\n};\n\nconst addUnusedNamespaceSideEffects = (state: AnalyzerState): void => {\n state.namespaces.forEach((binding) => {\n if (!binding.used) {\n state.result.imports.push({\n imported: 'side-effect',\n local: binding.local,\n source: binding.source,\n type: binding.type,\n });\n }\n });\n};\n\nexport function collectOxcExportsAndImportsFromProgram(\n program: Program,\n code: string,\n isEsModule: boolean\n): OxcCollectedState {\n const rootScope = createScope(null);\n const state: AnalyzerState = {\n code,\n namespaces: [],\n requireSources: new Map(),\n result: {\n deadExports: [],\n exports: {},\n imports: [],\n isEsModule,\n reexports: [],\n },\n };\n\n precollectRequireSources(program, state);\n visit(\n program,\n { key: 'program', parent: null, scope: rootScope },\n state,\n 'all'\n );\n addUnusedNamespaceSideEffects(state);\n\n return state.result;\n}\n\nexport function collectOxcProcessorImportsFromProgram(\n program: Program,\n code: string\n): OxcCollectedImport[] {\n const rootScope = createScope(null);\n const state: AnalyzerState = {\n code,\n namespaces: [],\n requireSources: new Map(),\n result: {\n deadExports: [],\n exports: {},\n imports: [],\n isEsModule: true,\n reexports: [],\n },\n };\n\n precollectRequireSources(program, state);\n visit(\n program,\n { key: 'program', parent: null, scope: rootScope },\n state,\n 'importsOnly'\n );\n\n return state.result.imports;\n}\n\nexport function collectOxcExportsAndImports(\n code: string,\n filename: string\n): OxcCollectedState {\n const parsed = parseOxcCached(filename, code, 'unambiguous');\n\n return collectOxcExportsAndImportsFromProgram(\n parsed.program,\n code,\n parsed.module.hasModuleSyntax\n );\n}\n"],"file":"collectOxcExportsAndImports.js"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"","names":[],"sources":["../../../src/utils/collectOxcRuntime/types.ts"],"version":3,"sourcesContent":["import type { StrictOptions } from '@wyw-in-js/shared';\nimport type { RawSourceMap } from 'source-map';\n\nimport type { WYWTransformMetadata } from '../TransformMetadata';\n\nexport type OxcCollectOptions = Pick<\n StrictOptions,\n | 'classNameSlug'\n | 'displayName'\n | 'eval'\n | 'extensions'\n | 'tagResolver'\n | 'variableNameConfig'\n> & {\n preserveSideEffectImportLocals?: Set<string>;\n};\n\nexport type OxcCollectResult = {\n code: string;\n map: RawSourceMap;\n metadata: WYWTransformMetadata | null;\n};\n\nexport type RuntimeReplacement = {\n end: number;\n start: number;\n value: string;\n};\n"],"file":"types.js"}
|
|
1
|
+
{"mappings":"","names":[],"sources":["../../../src/utils/collectOxcRuntime/types.ts"],"version":3,"sourcesContent":["import type { StrictOptions } from '@wyw-in-js/shared';\nimport type { RawSourceMap } from 'source-map';\n\nimport type { WYWTransformMetadata } from '../TransformMetadata';\n\nexport type OxcCollectOptions = Pick<\n StrictOptions,\n | 'classNameSlug'\n | 'displayName'\n | 'eval'\n | 'extensions'\n | 'tagResolver'\n | 'variableNameConfig'\n> & {\n preserveSideEffectImportOrderLocals?: Set<string>;\n preserveSideEffectImportLocals?: Set<string>;\n};\n\nexport type OxcCollectResult = {\n code: string;\n map: RawSourceMap;\n metadata: WYWTransformMetadata | null;\n};\n\nexport type RuntimeReplacement = {\n end: number;\n start: number;\n value: string;\n};\n"],"file":"types.js"}
|