@wyw-in-js/transform 2.0.0-alpha.0 → 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/cache.js +7 -0
- package/esm/cache.js.map +1 -1
- package/esm/debug/fileReporter.js +35 -1
- package/esm/debug/fileReporter.js.map +1 -1
- package/esm/eval/broker.js +230 -89
- package/esm/eval/broker.js.map +1 -1
- package/esm/eval/protocol.js.map +1 -1
- package/esm/eval/runner.js +143 -24
- package/esm/eval/runner.js.map +1 -1
- package/esm/module.js +70 -43
- package/esm/module.js.map +1 -1
- 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/processEntrypoint.js +35 -20
- package/esm/transform/generators/processEntrypoint.js.map +1 -1
- package/esm/transform/generators/resolveImports.js +48 -2
- package/esm/transform/generators/resolveImports.js.map +1 -1
- package/esm/transform/generators/resolveStaticOxcValues/cache.js +182 -0
- package/esm/transform/generators/resolveStaticOxcValues/cache.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js +146 -0
- package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/dependencies.js +33 -0
- package/esm/transform/generators/resolveStaticOxcValues/dependencies.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/environment.js +29 -0
- package/esm/transform/generators/resolveStaticOxcValues/environment.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js +351 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js +253 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js +310 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +74 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +229 -0
- package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js +172 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js +476 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js +55 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js +123 -0
- package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/prune.js +272 -0
- package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +184 -0
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js +298 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +298 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/types.js +3 -0
- package/esm/transform/generators/resolveStaticOxcValues/types.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +59 -0
- package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues.js +1 -2910
- package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -1
- package/esm/transform/generators/transform.js +58 -45
- package/esm/transform/generators/transform.js.map +1 -1
- package/esm/transform/helpers/loadWywOptions.js +33 -4
- package/esm/transform/helpers/loadWywOptions.js.map +1 -1
- package/esm/transform.js +3 -1
- package/esm/transform.js.map +1 -1
- package/esm/utils/EventEmitter.js +55 -9
- package/esm/utils/EventEmitter.js.map +1 -1
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js +160 -0
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js.map +1 -0
- package/esm/utils/applyOxcProcessors/cleanupBindings.js +157 -0
- package/esm/utils/applyOxcProcessors/cleanupBindings.js.map +1 -0
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js +517 -0
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js.map +1 -0
- package/esm/utils/applyOxcProcessors/displayName.js +93 -0
- package/esm/utils/applyOxcProcessors/displayName.js.map +1 -0
- package/esm/utils/applyOxcProcessors/expressionValues.js +152 -0
- package/esm/utils/applyOxcProcessors/expressionValues.js.map +1 -0
- package/esm/utils/applyOxcProcessors/processorFactory.js +62 -0
- package/esm/utils/applyOxcProcessors/processorFactory.js.map +1 -0
- package/esm/utils/applyOxcProcessors/processorUsages.js +143 -0
- package/esm/utils/applyOxcProcessors/processorUsages.js.map +1 -0
- package/esm/utils/applyOxcProcessors/sameFileStaticValues.js +207 -0
- package/esm/utils/applyOxcProcessors/sameFileStaticValues.js.map +1 -0
- package/esm/utils/applyOxcProcessors/shared.js +29 -0
- package/esm/utils/applyOxcProcessors/shared.js.map +1 -0
- package/esm/utils/applyOxcProcessors/types.js +2 -0
- package/esm/utils/applyOxcProcessors/types.js.map +1 -0
- package/esm/utils/applyOxcProcessors.js +1 -1216
- package/esm/utils/applyOxcProcessors.js.map +1 -1
- package/esm/utils/collectOxcExportsAndImports.js +8 -0
- package/esm/utils/collectOxcExportsAndImports.js.map +1 -1
- package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js +157 -0
- package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js.map +1 -0
- package/esm/utils/collectOxcRuntime/sourceMap.js +36 -0
- package/esm/utils/collectOxcRuntime/sourceMap.js.map +1 -0
- package/esm/utils/collectOxcRuntime/types.js +2 -0
- package/esm/utils/collectOxcRuntime/types.js.map +1 -0
- package/esm/utils/collectOxcRuntime.js +5 -193
- package/esm/utils/collectOxcRuntime.js.map +1 -1
- package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js +496 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js +113 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js +387 -0
- package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/staticBindings.js +17 -0
- package/esm/utils/collectOxcTemplateDependencies/staticBindings.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js +540 -0
- package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/types.js +2 -0
- package/esm/utils/collectOxcTemplateDependencies/types.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies.js +3 -1397
- package/esm/utils/collectOxcTemplateDependencies.js.map +1 -1
- package/esm/utils/nativeResolver.js +93 -0
- package/esm/utils/nativeResolver.js.map +1 -0
- package/esm/utils/oxc/ast.js +58 -0
- package/esm/utils/oxc/ast.js.map +1 -0
- package/esm/utils/oxc/parse.js +3 -0
- package/esm/utils/oxc/parse.js.map +1 -0
- package/esm/utils/oxc/replacements.js +14 -0
- package/esm/utils/oxc/replacements.js.map +1 -0
- package/esm/utils/oxc/sourceLocations.js +59 -0
- package/esm/utils/oxc/sourceLocations.js.map +1 -0
- package/esm/utils/oxcPreevalStage/evalStrategy.js +3 -0
- package/esm/utils/oxcPreevalStage/evalStrategy.js.map +1 -0
- package/esm/utils/oxcPreevalStage/prepareCode.js +21 -0
- package/esm/utils/oxcPreevalStage/prepareCode.js.map +1 -0
- package/esm/utils/oxcPreevalStage/prevalExport.js +23 -0
- package/esm/utils/oxcPreevalStage/prevalExport.js.map +1 -0
- package/esm/utils/oxcPreevalStage/processors.js +17 -0
- package/esm/utils/oxcPreevalStage/processors.js.map +1 -0
- package/esm/utils/oxcPreevalStage/staticOverlay.js +18 -0
- package/esm/utils/oxcPreevalStage/staticOverlay.js.map +1 -0
- package/esm/utils/oxcPreevalStage/types.js +2 -0
- package/esm/utils/oxcPreevalStage/types.js.map +1 -0
- package/esm/utils/oxcPreevalStage.js +17 -73
- package/esm/utils/oxcPreevalStage.js.map +1 -1
- package/esm/utils/oxcPreevalTransforms.js +106 -66
- package/esm/utils/oxcPreevalTransforms.js.map +1 -1
- package/esm/utils/parseOxc.js +6 -1
- package/esm/utils/parseOxc.js.map +1 -1
- package/esm/utils/processorStaticSemantics.js +157 -0
- package/esm/utils/processorStaticSemantics.js.map +1 -0
- package/esm/utils/resolveWithConditions.js +3 -3
- package/esm/utils/resolveWithConditions.js.map +1 -1
- package/package.json +6 -5
- package/types/cache.js +8 -0
- package/types/debug/fileReporter.js +46 -1
- package/types/eval/broker.d.ts +6 -2
- package/types/eval/broker.js +244 -95
- package/types/eval/protocol.d.ts +15 -1
- package/types/module.d.ts +4 -1
- package/types/module.js +97 -48
- package/types/transform/Entrypoint.types.d.ts +4 -0
- package/types/transform/generators/collect.js +1 -0
- package/types/transform/generators/resolveImports.d.ts +3 -1
- package/types/transform/generators/resolveImports.js +49 -2
- package/types/transform/generators/resolveStaticOxcValues/cache.d.ts +25 -0
- package/types/transform/generators/resolveStaticOxcValues/cache.js +182 -0
- package/types/transform/generators/resolveStaticOxcValues/candidateResolver.d.ts +5 -0
- package/types/transform/generators/resolveStaticOxcValues/candidateResolver.js +149 -0
- package/types/transform/generators/resolveStaticOxcValues/dependencies.d.ts +6 -0
- package/types/transform/generators/resolveStaticOxcValues/dependencies.js +37 -0
- package/types/transform/generators/resolveStaticOxcValues/environment.d.ts +12 -0
- package/types/transform/generators/resolveStaticOxcValues/environment.js +32 -0
- package/types/transform/generators/resolveStaticOxcValues/exportResolver.d.ts +4 -0
- package/types/transform/generators/resolveStaticOxcValues/exportResolver.js +358 -0
- package/types/transform/generators/resolveStaticOxcValues/exportTargets.d.ts +20 -0
- package/types/transform/generators/resolveStaticOxcValues/exportTargets.js +285 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssign.d.ts +41 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssign.js +352 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.d.ts +6 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +88 -0
- package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.d.ts +13 -0
- package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +263 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.d.ts +4 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.js +191 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.d.ts +40 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.js +516 -0
- package/types/transform/generators/resolveStaticOxcValues/processorTarget.d.ts +5 -0
- package/types/transform/generators/resolveStaticOxcValues/processorTarget.js +62 -0
- package/types/transform/generators/resolveStaticOxcValues/programAnalysis.d.ts +11 -0
- package/types/transform/generators/resolveStaticOxcValues/programAnalysis.js +133 -0
- package/types/transform/generators/resolveStaticOxcValues/prune.d.ts +15 -0
- package/types/transform/generators/resolveStaticOxcValues/prune.js +312 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.d.ts +2 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +198 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpression.d.ts +25 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpression.js +391 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.d.ts +14 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +304 -0
- package/types/transform/generators/resolveStaticOxcValues/types.d.ts +99 -0
- package/types/transform/generators/resolveStaticOxcValues/types.js +2 -0
- package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.d.ts +7 -0
- package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +64 -0
- package/types/transform/generators/resolveStaticOxcValues.d.ts +1 -2
- package/types/transform/generators/resolveStaticOxcValues.js +1 -3235
- package/types/transform/generators/transform.js +64 -49
- package/types/transform/helpers/loadWywOptions.js +23 -3
- package/types/transform.js +3 -1
- package/types/utils/EventEmitter.d.ts +16 -1
- package/types/utils/EventEmitter.js +57 -9
- package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +9 -0
- package/types/utils/applyOxcProcessors/applyOxcProcessors.js +169 -0
- package/types/utils/applyOxcProcessors/cleanupBindings.d.ts +12 -0
- package/types/utils/applyOxcProcessors/cleanupBindings.js +189 -0
- package/types/utils/applyOxcProcessors/cleanupRemovals.d.ts +16 -0
- package/types/utils/applyOxcProcessors/cleanupRemovals.js +567 -0
- package/types/utils/applyOxcProcessors/displayName.d.ts +9 -0
- package/types/utils/applyOxcProcessors/displayName.js +113 -0
- package/types/utils/applyOxcProcessors/expressionValues.d.ts +11 -0
- package/types/utils/applyOxcProcessors/expressionValues.js +170 -0
- package/types/utils/applyOxcProcessors/processorFactory.d.ts +7 -0
- package/types/utils/applyOxcProcessors/processorFactory.js +70 -0
- package/types/utils/applyOxcProcessors/processorUsages.d.ts +16 -0
- package/types/utils/applyOxcProcessors/processorUsages.js +163 -0
- package/types/utils/applyOxcProcessors/sameFileStaticValues.d.ts +23 -0
- package/types/utils/applyOxcProcessors/sameFileStaticValues.js +211 -0
- package/types/utils/applyOxcProcessors/shared.d.ts +10 -0
- package/types/utils/applyOxcProcessors/shared.js +37 -0
- package/types/utils/applyOxcProcessors/types.d.ts +88 -0
- package/types/utils/applyOxcProcessors/types.js +1 -0
- package/types/utils/applyOxcProcessors.d.ts +1 -16
- package/types/utils/applyOxcProcessors.js +1 -1391
- package/types/utils/collectOxcExportsAndImports.js +8 -0
- package/types/utils/collectOxcRuntime/normalizeRuntimeCode.d.ts +1 -0
- package/types/utils/collectOxcRuntime/normalizeRuntimeCode.js +185 -0
- package/types/utils/collectOxcRuntime/sourceMap.d.ts +2 -0
- package/types/utils/collectOxcRuntime/sourceMap.js +37 -0
- package/types/utils/collectOxcRuntime/types.d.ts +17 -0
- package/types/utils/collectOxcRuntime/types.js +1 -0
- package/types/utils/collectOxcRuntime.d.ts +2 -12
- package/types/utils/collectOxcRuntime.js +5 -224
- package/types/utils/collectOxcTemplateDependencies/expressionExtraction.d.ts +6 -0
- package/types/utils/collectOxcTemplateDependencies/expressionExtraction.js +550 -0
- package/types/utils/collectOxcTemplateDependencies/expressionReplacements.d.ts +11 -0
- package/types/utils/collectOxcTemplateDependencies/expressionReplacements.js +131 -0
- package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.d.ts +20 -0
- package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.js +434 -0
- package/types/utils/collectOxcTemplateDependencies/staticBindings.d.ts +7 -0
- package/types/utils/collectOxcTemplateDependencies/staticBindings.js +13 -0
- package/types/utils/collectOxcTemplateDependencies/staticEvaluator.d.ts +13 -0
- package/types/utils/collectOxcTemplateDependencies/staticEvaluator.js +626 -0
- package/types/utils/collectOxcTemplateDependencies/types.d.ts +116 -0
- package/types/utils/collectOxcTemplateDependencies/types.js +1 -0
- package/types/utils/collectOxcTemplateDependencies.d.ts +4 -38
- package/types/utils/collectOxcTemplateDependencies.js +3 -1580
- package/types/utils/nativeResolver.d.ts +13 -0
- package/types/utils/nativeResolver.js +91 -0
- package/types/utils/oxc/ast.d.ts +4 -0
- package/types/utils/oxc/ast.js +61 -0
- package/types/utils/oxc/parse.d.ts +3 -0
- package/types/utils/oxc/parse.js +2 -0
- package/types/utils/oxc/replacements.d.ts +12 -0
- package/types/utils/oxc/replacements.js +18 -0
- package/types/utils/oxc/sourceLocations.d.ts +5 -0
- package/types/utils/oxc/sourceLocations.js +63 -0
- package/types/utils/oxcPreevalStage/evalStrategy.d.ts +3 -0
- package/types/utils/oxcPreevalStage/evalStrategy.js +2 -0
- package/types/utils/oxcPreevalStage/prepareCode.d.ts +3 -0
- package/types/utils/oxcPreevalStage/prepareCode.js +20 -0
- package/types/utils/oxcPreevalStage/prevalExport.d.ts +1 -0
- package/types/utils/oxcPreevalStage/prevalExport.js +22 -0
- package/types/utils/oxcPreevalStage/processors.d.ts +10 -0
- package/types/utils/oxcPreevalStage/processors.js +16 -0
- package/types/utils/oxcPreevalStage/staticOverlay.d.ts +3 -0
- package/types/utils/oxcPreevalStage/staticOverlay.js +23 -0
- package/types/utils/oxcPreevalStage/types.d.ts +22 -0
- package/types/utils/oxcPreevalStage/types.js +1 -0
- package/types/utils/oxcPreevalStage.d.ts +2 -18
- package/types/utils/oxcPreevalStage.js +17 -79
- package/types/utils/oxcPreevalTransforms.js +111 -75
- package/types/utils/parseOxc.js +6 -1
- package/types/utils/processorStaticSemantics.d.ts +13 -0
- package/types/utils/processorStaticSemantics.js +191 -0
- package/types/utils/resolveWithConditions.js +3 -3
- package/esm/eval/resolverStrategy.js +0 -51
- package/esm/eval/resolverStrategy.js.map +0 -1
- package/types/eval/resolverStrategy.d.ts +0 -13
- package/types/eval/resolverStrategy.js +0 -46
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
import { isNotNull } from '../isNotNull';
|
|
3
|
+
import { getOxcNodeChildren, isOxcNode, walkOxc } from '../oxc/ast';
|
|
4
|
+
export const collectUsedNames = (program) => {
|
|
5
|
+
const names = new Set();
|
|
6
|
+
walkOxc(program, (node) => {
|
|
7
|
+
if (node.type === 'Identifier') {
|
|
8
|
+
names.add(node.name);
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
return names;
|
|
12
|
+
};
|
|
13
|
+
export const isNodeReference = (node, parent) => {
|
|
14
|
+
if (node.type === 'Identifier') {
|
|
15
|
+
const parentRecord = parent;
|
|
16
|
+
if (!parentRecord) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
if (parent?.type === 'ImportDeclaration' ||
|
|
20
|
+
parent?.type === 'ImportSpecifier' ||
|
|
21
|
+
parent?.type === 'ImportDefaultSpecifier' ||
|
|
22
|
+
parent?.type === 'ImportNamespaceSpecifier') {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (parent?.type === 'MemberExpression' &&
|
|
26
|
+
parentRecord.property === node &&
|
|
27
|
+
!parentRecord.computed) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if ((parent?.type === 'VariableDeclarator' ||
|
|
31
|
+
parent?.type === 'FunctionDeclaration' ||
|
|
32
|
+
parent?.type === 'ClassDeclaration' ||
|
|
33
|
+
parent?.type === 'ClassExpression') &&
|
|
34
|
+
parentRecord.id === node) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if ((parent?.type === 'PropertyDefinition' ||
|
|
38
|
+
parent?.type === 'MethodDefinition') &&
|
|
39
|
+
parentRecord.key === node &&
|
|
40
|
+
!parentRecord.computed) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (parent?.type === 'Property' &&
|
|
44
|
+
parentRecord.key === node &&
|
|
45
|
+
parentRecord.value !== node &&
|
|
46
|
+
!parentRecord.computed) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
if (node.type === 'JSXIdentifier') {
|
|
52
|
+
const parentRecord = parent;
|
|
53
|
+
if (parent?.type === 'JSXAttribute' && parentRecord?.name === node) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
};
|
|
60
|
+
export const collectReferencedNames = (root) => {
|
|
61
|
+
const names = new Set();
|
|
62
|
+
const walk = (node, parent = null) => {
|
|
63
|
+
if (node.type === 'ImportDeclaration') {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (isNodeReference(node, parent) &&
|
|
67
|
+
'name' in node &&
|
|
68
|
+
typeof node.name === 'string') {
|
|
69
|
+
names.add(node.name);
|
|
70
|
+
}
|
|
71
|
+
getOxcNodeChildren(node).forEach((child) => walk(child, node));
|
|
72
|
+
};
|
|
73
|
+
walk(root);
|
|
74
|
+
return names;
|
|
75
|
+
};
|
|
76
|
+
export const collectImportLocalNames = (node) => {
|
|
77
|
+
if (node.type !== 'ImportDeclaration') {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
const { specifiers } = node;
|
|
81
|
+
if (!Array.isArray(specifiers)) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
return specifiers
|
|
85
|
+
.map((specifier) => {
|
|
86
|
+
const { local } = specifier;
|
|
87
|
+
return isOxcNode(local) &&
|
|
88
|
+
'name' in local &&
|
|
89
|
+
typeof local.name === 'string'
|
|
90
|
+
? local.name
|
|
91
|
+
: null;
|
|
92
|
+
})
|
|
93
|
+
.filter(isNotNull);
|
|
94
|
+
};
|
|
95
|
+
export const getImportSpecifierLocalName = (node) => {
|
|
96
|
+
const { local } = node;
|
|
97
|
+
return isOxcNode(local) && 'name' in local && typeof local.name === 'string'
|
|
98
|
+
? local.name
|
|
99
|
+
: null;
|
|
100
|
+
};
|
|
101
|
+
export const collectDeclaredNames = (node) => {
|
|
102
|
+
if (node.type === 'Identifier') {
|
|
103
|
+
return [node.name];
|
|
104
|
+
}
|
|
105
|
+
if (node.type === 'RestElement') {
|
|
106
|
+
return collectDeclaredNames(node.argument);
|
|
107
|
+
}
|
|
108
|
+
if (node.type === 'AssignmentPattern') {
|
|
109
|
+
return collectDeclaredNames(node.left);
|
|
110
|
+
}
|
|
111
|
+
if (node.type === 'ObjectPattern') {
|
|
112
|
+
return node.properties.flatMap((property) => property.type === 'RestElement'
|
|
113
|
+
? collectDeclaredNames(property.argument)
|
|
114
|
+
: collectDeclaredNames(property.value));
|
|
115
|
+
}
|
|
116
|
+
if (node.type === 'ArrayPattern') {
|
|
117
|
+
return node.elements.flatMap((element) => element ? collectDeclaredNames(element) : []);
|
|
118
|
+
}
|
|
119
|
+
if (node.type === 'TSParameterProperty') {
|
|
120
|
+
return collectDeclaredNames(node.parameter);
|
|
121
|
+
}
|
|
122
|
+
return [];
|
|
123
|
+
};
|
|
124
|
+
export const collectTopLevelBindings = (statement) => {
|
|
125
|
+
const bindings = new Set();
|
|
126
|
+
if (statement.type === 'ImportDeclaration') {
|
|
127
|
+
collectImportLocalNames(statement).forEach((name) => bindings.add(name));
|
|
128
|
+
return bindings;
|
|
129
|
+
}
|
|
130
|
+
if (statement.type === 'VariableDeclaration') {
|
|
131
|
+
const { declarations } = statement;
|
|
132
|
+
if (!Array.isArray(declarations)) {
|
|
133
|
+
return bindings;
|
|
134
|
+
}
|
|
135
|
+
declarations.forEach((declarator) => {
|
|
136
|
+
const { id } = declarator;
|
|
137
|
+
if (isOxcNode(id)) {
|
|
138
|
+
collectDeclaredNames(id).forEach((name) => bindings.add(name));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
return bindings;
|
|
142
|
+
}
|
|
143
|
+
if ((statement.type === 'FunctionDeclaration' ||
|
|
144
|
+
statement.type === 'ClassDeclaration' ||
|
|
145
|
+
statement.type === 'TSEnumDeclaration') &&
|
|
146
|
+
'id' in statement) {
|
|
147
|
+
const { id } = statement;
|
|
148
|
+
if (isOxcNode(id) && id.type === 'Identifier') {
|
|
149
|
+
bindings.add(id.name);
|
|
150
|
+
}
|
|
151
|
+
return bindings;
|
|
152
|
+
}
|
|
153
|
+
if (statement.type === 'ExportNamedDeclaration') {
|
|
154
|
+
const { declaration } = statement;
|
|
155
|
+
return isOxcNode(declaration)
|
|
156
|
+
? collectTopLevelBindings(declaration)
|
|
157
|
+
: bindings;
|
|
158
|
+
}
|
|
159
|
+
return bindings;
|
|
160
|
+
};
|
|
161
|
+
export const collectTopLevelStatementInfos = (program) => program.body.map((statement) => ({
|
|
162
|
+
bindings: collectTopLevelBindings(statement),
|
|
163
|
+
node: statement,
|
|
164
|
+
references: collectReferencedNames(statement),
|
|
165
|
+
}));
|
|
166
|
+
export const collectTopLevelBindingsFromStatements = (statements) => new Set(statements.flatMap((statement) => [...statement.bindings]));
|
|
167
|
+
export const collectRemovableNamesFromStatements = (statements, initialNames) => {
|
|
168
|
+
const removable = new Set(initialNames);
|
|
169
|
+
const bindingToStatement = new Map();
|
|
170
|
+
statements.forEach((statement) => {
|
|
171
|
+
statement.bindings.forEach((name) => {
|
|
172
|
+
bindingToStatement.set(name, statement);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
const queue = [...removable];
|
|
176
|
+
while (queue.length > 0) {
|
|
177
|
+
const name = queue.shift();
|
|
178
|
+
const statement = bindingToStatement.get(name);
|
|
179
|
+
if (statement) {
|
|
180
|
+
statement.references.forEach((reference) => {
|
|
181
|
+
if (bindingToStatement.has(reference) && !removable.has(reference)) {
|
|
182
|
+
removable.add(reference);
|
|
183
|
+
queue.push(reference);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return removable;
|
|
189
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Program } from 'oxc-parser';
|
|
2
|
+
import type { Replacement, ScopedBindingInfo, ScopedCleanupScope, TopLevelStatementInfo } from './types';
|
|
3
|
+
export declare const createScopedCleanupScope: (parent: ScopedCleanupScope | null) => ScopedCleanupScope;
|
|
4
|
+
export declare const resolveScopedBinding: (scope: ScopedCleanupScope, name: string) => string | null;
|
|
5
|
+
export declare const collectScopedBindingInfos: (program: Program) => Map<string, ScopedBindingInfo>;
|
|
6
|
+
export declare const collectScopedRemovableBindingIds: (bindings: Map<string, ScopedBindingInfo>, initialNames: Set<string>) => Set<string>;
|
|
7
|
+
export declare function expandImportRemovalRange(code: string, start: number, end: number): Replacement;
|
|
8
|
+
export declare const collectUnusedScopedDeclarationRemovals: (code: string, bindings: Map<string, ScopedBindingInfo>, initialRemovableNames: Set<string>) => Replacement[];
|
|
9
|
+
export declare const expandImportSpecifierRemovalRange: (code: string, start: number, end: number) => Replacement;
|
|
10
|
+
export declare const mergeEmptyRemovalRanges: (removals: Replacement[]) => Replacement[];
|
|
11
|
+
export declare const collectUnusedImportRemovals: (code: string, program: Program, referencedNames: Set<string>, removableNames: Set<string>, preserveSideEffectImportLocals: Set<string>, preserveSideEffectImportOrderLocals?: Set<string>) => Replacement[];
|
|
12
|
+
export declare const collectUnusedTopLevelDeclarationRemovals: (code: string, program: Program, referencedNames: Set<string>, removableNames: Set<string>) => Replacement[];
|
|
13
|
+
export declare const collectUnusedGeneratedHelperDeclarationRemovals: (code: string, program: Program, referencedNames: Set<string>) => Replacement[];
|
|
14
|
+
export declare const collectTopLevelExpressionStatementRemovals: (code: string, statements: TopLevelStatementInfo[], topLevelBindings: Set<string>, removableExpressionRefs: Set<string>) => Replacement[];
|
|
15
|
+
export declare const collectEmptyTopLevelBlockRemovals: (code: string, program: Program) => Replacement[];
|
|
16
|
+
export declare const removeUnusedAfterReplacement: (code: string, filename: string, initialRemovableNames: Set<string>, removableExpressionRefs: Set<string>, preserveSideEffectImportLocals: Set<string>, preserveSideEffectImportOrderLocals?: Set<string>) => string;
|