@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,157 @@
|
|
|
1
|
+
import { isNotNull } from "../isNotNull.js";
|
|
2
|
+
import { getOxcNodeChildren, isOxcNode, walkOxc } from "../oxc/ast.js";
|
|
3
|
+
export const collectUsedNames = (program) => {
|
|
4
|
+
const names = new Set();
|
|
5
|
+
walkOxc(program, (node) => {
|
|
6
|
+
if (node.type === "Identifier") {
|
|
7
|
+
names.add(node.name);
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
return names;
|
|
11
|
+
};
|
|
12
|
+
export const isNodeReference = (node, parent) => {
|
|
13
|
+
if (node.type === "Identifier") {
|
|
14
|
+
const parentRecord = parent;
|
|
15
|
+
if (!parentRecord) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
if (parent?.type === "ImportDeclaration" || parent?.type === "ImportSpecifier" || parent?.type === "ImportDefaultSpecifier" || parent?.type === "ImportNamespaceSpecifier") {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (parent?.type === "MemberExpression" && parentRecord.property === node && !parentRecord.computed) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
if ((parent?.type === "VariableDeclarator" || parent?.type === "FunctionDeclaration" || parent?.type === "ClassDeclaration" || parent?.type === "ClassExpression") && parentRecord.id === node) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if ((parent?.type === "PropertyDefinition" || parent?.type === "MethodDefinition") && parentRecord.key === node && !parentRecord.computed) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (parent?.type === "Property" && parentRecord.key === node && parentRecord.value !== node && !parentRecord.computed) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (node.type === "JSXIdentifier") {
|
|
36
|
+
const parentRecord = parent;
|
|
37
|
+
if (parent?.type === "JSXAttribute" && parentRecord?.name === node) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
};
|
|
44
|
+
export const collectReferencedNames = (root) => {
|
|
45
|
+
const names = new Set();
|
|
46
|
+
const walk = (node, parent = null) => {
|
|
47
|
+
if (node.type === "ImportDeclaration") {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (isNodeReference(node, parent) && "name" in node && typeof node.name === "string") {
|
|
51
|
+
names.add(node.name);
|
|
52
|
+
}
|
|
53
|
+
getOxcNodeChildren(node).forEach((child) => walk(child, node));
|
|
54
|
+
};
|
|
55
|
+
walk(root);
|
|
56
|
+
return names;
|
|
57
|
+
};
|
|
58
|
+
export const collectImportLocalNames = (node) => {
|
|
59
|
+
if (node.type !== "ImportDeclaration") {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
const { specifiers } = node;
|
|
63
|
+
if (!Array.isArray(specifiers)) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
return specifiers.map((specifier) => {
|
|
67
|
+
const { local } = specifier;
|
|
68
|
+
return isOxcNode(local) && "name" in local && typeof local.name === "string" ? local.name : null;
|
|
69
|
+
}).filter(isNotNull);
|
|
70
|
+
};
|
|
71
|
+
export const getImportSpecifierLocalName = (node) => {
|
|
72
|
+
const { local } = node;
|
|
73
|
+
return isOxcNode(local) && "name" in local && typeof local.name === "string" ? local.name : null;
|
|
74
|
+
};
|
|
75
|
+
export const collectDeclaredNames = (node) => {
|
|
76
|
+
if (node.type === "Identifier") {
|
|
77
|
+
return [node.name];
|
|
78
|
+
}
|
|
79
|
+
if (node.type === "RestElement") {
|
|
80
|
+
return collectDeclaredNames(node.argument);
|
|
81
|
+
}
|
|
82
|
+
if (node.type === "AssignmentPattern") {
|
|
83
|
+
return collectDeclaredNames(node.left);
|
|
84
|
+
}
|
|
85
|
+
if (node.type === "ObjectPattern") {
|
|
86
|
+
return node.properties.flatMap((property) => property.type === "RestElement" ? collectDeclaredNames(property.argument) : collectDeclaredNames(property.value));
|
|
87
|
+
}
|
|
88
|
+
if (node.type === "ArrayPattern") {
|
|
89
|
+
return node.elements.flatMap((element) => element ? collectDeclaredNames(element) : []);
|
|
90
|
+
}
|
|
91
|
+
if (node.type === "TSParameterProperty") {
|
|
92
|
+
return collectDeclaredNames(node.parameter);
|
|
93
|
+
}
|
|
94
|
+
return [];
|
|
95
|
+
};
|
|
96
|
+
export const collectTopLevelBindings = (statement) => {
|
|
97
|
+
const bindings = new Set();
|
|
98
|
+
if (statement.type === "ImportDeclaration") {
|
|
99
|
+
collectImportLocalNames(statement).forEach((name) => bindings.add(name));
|
|
100
|
+
return bindings;
|
|
101
|
+
}
|
|
102
|
+
if (statement.type === "VariableDeclaration") {
|
|
103
|
+
const { declarations } = statement;
|
|
104
|
+
if (!Array.isArray(declarations)) {
|
|
105
|
+
return bindings;
|
|
106
|
+
}
|
|
107
|
+
declarations.forEach((declarator) => {
|
|
108
|
+
const { id } = declarator;
|
|
109
|
+
if (isOxcNode(id)) {
|
|
110
|
+
collectDeclaredNames(id).forEach((name) => bindings.add(name));
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return bindings;
|
|
114
|
+
}
|
|
115
|
+
if ((statement.type === "FunctionDeclaration" || statement.type === "ClassDeclaration" || statement.type === "TSEnumDeclaration") && "id" in statement) {
|
|
116
|
+
const { id } = statement;
|
|
117
|
+
if (isOxcNode(id) && id.type === "Identifier") {
|
|
118
|
+
bindings.add(id.name);
|
|
119
|
+
}
|
|
120
|
+
return bindings;
|
|
121
|
+
}
|
|
122
|
+
if (statement.type === "ExportNamedDeclaration") {
|
|
123
|
+
const { declaration } = statement;
|
|
124
|
+
return isOxcNode(declaration) ? collectTopLevelBindings(declaration) : bindings;
|
|
125
|
+
}
|
|
126
|
+
return bindings;
|
|
127
|
+
};
|
|
128
|
+
export const collectTopLevelStatementInfos = (program) => program.body.map((statement) => ({
|
|
129
|
+
bindings: collectTopLevelBindings(statement),
|
|
130
|
+
node: statement,
|
|
131
|
+
references: collectReferencedNames(statement)
|
|
132
|
+
}));
|
|
133
|
+
export const collectTopLevelBindingsFromStatements = (statements) => new Set(statements.flatMap((statement) => [...statement.bindings]));
|
|
134
|
+
export const collectRemovableNamesFromStatements = (statements, initialNames) => {
|
|
135
|
+
const removable = new Set(initialNames);
|
|
136
|
+
const bindingToStatement = new Map();
|
|
137
|
+
statements.forEach((statement) => {
|
|
138
|
+
statement.bindings.forEach((name) => {
|
|
139
|
+
bindingToStatement.set(name, statement);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
const queue = [...removable];
|
|
143
|
+
while (queue.length > 0) {
|
|
144
|
+
const name = queue.shift();
|
|
145
|
+
const statement = bindingToStatement.get(name);
|
|
146
|
+
if (statement) {
|
|
147
|
+
statement.references.forEach((reference) => {
|
|
148
|
+
if (bindingToStatement.has(reference) && !removable.has(reference)) {
|
|
149
|
+
removable.add(reference);
|
|
150
|
+
queue.push(reference);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return removable;
|
|
156
|
+
};
|
|
157
|
+
//# sourceMappingURL=cleanupBindings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAIA,SAAS,iBAAiB;AAC1B,SAAS,oBAAoB,WAAW,eAAe;AAGvD,OAAO,MAAM,oBAAoB,YAAkC;CACjE,MAAM,QAAQ,IAAI,KAAa;AAC/B,SAAQ,UAAU,SAAS;AACzB,MAAI,KAAK,SAAS,cAAc;AAC9B,SAAM,IAAI,KAAK,KAAK;;GAEtB;AAEF,QAAO;;AAGT,OAAO,MAAM,mBAAmB,MAAY,WAAiC;AAC3E,KAAI,KAAK,SAAS,cAAc;EAC9B,MAAM,eAAe;AAErB,MAAI,CAAC,cAAc;AACjB,UAAO;;AAGT,MACE,QAAQ,SAAS,uBACjB,QAAQ,SAAS,qBACjB,QAAQ,SAAS,4BACjB,QAAQ,SAAS,4BACjB;AACA,UAAO;;AAGT,MACE,QAAQ,SAAS,sBACjB,aAAa,aAAa,QAC1B,CAAC,aAAa,UACd;AACA,UAAO;;AAGT,OACG,QAAQ,SAAS,wBAChB,QAAQ,SAAS,yBACjB,QAAQ,SAAS,sBACjB,QAAQ,SAAS,sBACnB,aAAa,OAAO,MACpB;AACA,UAAO;;AAGT,OACG,QAAQ,SAAS,wBAChB,QAAQ,SAAS,uBACnB,aAAa,QAAQ,QACrB,CAAC,aAAa,UACd;AACA,UAAO;;AA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/B,UAAU,wBAAwB,UAAU;CAC5C,MAAM;CACN,YAAY,uBAAuB,UAAU;CAC9C,EAAE;AAEL,OAAO,MAAM,yCACX,eAEA,IAAI,IAAI,WAAW,SAAS,cAAc,CAAC,GAAG,UAAU,SAAS,CAAC,CAAC;AAErE,OAAO,MAAM,uCACX,YACA,iBACgB;CAChB,MAAM,YAAY,IAAI,IAAI,aAAa;CACvC,MAAM,qBAAqB,IAAI,KAAoC;AAEnE,YAAW,SAAS,cAAc;AAChC,YAAU,SAAS,SAAS,SAAS;AACnC,sBAAmB,IAAI,MAAM,UAAU;IACvC;GACF;CAEF,MAAM,QAAQ,CAAC,GAAG,UAAU;AAC5B,QAAO,MAAM,SAAS,GAAG;EACvB,MAAM,OAAO,MAAM,OAAO;EAC1B,MAAM,YAAY,mBAAmB,IAAI,KAAK;AAC9C,MAAI,WAAW;AACb,aAAU,WAAW,SAAS,cAAc;AAC1C,QAAI,mBAAmB,IAAI,UAAU,IAAI,CAAC,UAAU,IAAI,UAAU,EAAE;AAClE,eAAU,IAAI,UAAU;AACxB,WAAM,KAAK,UAAU;;KAEvB;;;AAIN,QAAO","names":[],"sources":["../../../src/utils/applyOxcProcessors/cleanupBindings.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax */\n\nimport type { Node, Program } from 'oxc-parser';\n\nimport { isNotNull } from '../isNotNull';\nimport { getOxcNodeChildren, isOxcNode, walkOxc } from '../oxc/ast';\nimport type { AnyNode, TopLevelStatementInfo } from './types';\n\nexport const collectUsedNames = (program: Program): Set<string> => {\n const names = new Set<string>();\n walkOxc(program, (node) => {\n if (node.type === 'Identifier') {\n names.add(node.name);\n }\n });\n\n return names;\n};\n\nexport const isNodeReference = (node: Node, parent: Node | null): boolean => {\n if (node.type === 'Identifier') {\n const parentRecord = parent as AnyNode | null;\n\n if (!parentRecord) {\n return true;\n }\n\n if (\n parent?.type === 'ImportDeclaration' ||\n parent?.type === 'ImportSpecifier' ||\n parent?.type === 'ImportDefaultSpecifier' ||\n parent?.type === 'ImportNamespaceSpecifier'\n ) {\n return false;\n }\n\n if (\n parent?.type === 'MemberExpression' &&\n parentRecord.property === node &&\n !parentRecord.computed\n ) {\n return false;\n }\n\n if (\n (parent?.type === 'VariableDeclarator' ||\n parent?.type === 'FunctionDeclaration' ||\n parent?.type === 'ClassDeclaration' ||\n parent?.type === 'ClassExpression') &&\n parentRecord.id === node\n ) {\n return false;\n }\n\n if (\n (parent?.type === 'PropertyDefinition' ||\n parent?.type === 'MethodDefinition') &&\n parentRecord.key === node &&\n !parentRecord.computed\n ) {\n return false;\n }\n\n if (\n parent?.type === 'Property' &&\n parentRecord.key === node &&\n parentRecord.value !== node &&\n !parentRecord.computed\n ) {\n return false;\n }\n\n return true;\n }\n\n if (node.type === 'JSXIdentifier') {\n const parentRecord = parent as AnyNode | null;\n if (parent?.type === 'JSXAttribute' && parentRecord?.name === node) {\n return false;\n }\n\n return true;\n }\n\n return false;\n};\n\nexport const collectReferencedNames = (root: Node): Set<string> => {\n const names = new Set<string>();\n\n const walk = (node: Node, parent: Node | null = null): void => {\n if (node.type === 'ImportDeclaration') {\n return;\n }\n\n if (\n isNodeReference(node, parent) &&\n 'name' in node &&\n typeof node.name === 'string'\n ) {\n names.add(node.name);\n }\n\n getOxcNodeChildren(node).forEach((child) => walk(child, node));\n };\n\n walk(root);\n return names;\n};\n\nexport const collectImportLocalNames = (node: Node): string[] => {\n if (node.type !== 'ImportDeclaration') {\n return [];\n }\n\n const { specifiers } = node as AnyNode;\n if (!Array.isArray(specifiers)) {\n return [];\n }\n\n return specifiers\n .map((specifier) => {\n const { local } = specifier as AnyNode;\n return isOxcNode(local) &&\n 'name' in local &&\n typeof local.name === 'string'\n ? local.name\n : null;\n })\n .filter(isNotNull);\n};\n\nexport const getImportSpecifierLocalName = (node: Node): string | null => {\n const { local } = node as AnyNode;\n return isOxcNode(local) && 'name' in local && typeof local.name === 'string'\n ? local.name\n : null;\n};\n\nexport const collectDeclaredNames = (node: Node): string[] => {\n if (node.type === 'Identifier') {\n return [node.name];\n }\n\n if (node.type === 'RestElement') {\n return collectDeclaredNames(node.argument);\n }\n\n if (node.type === 'AssignmentPattern') {\n return collectDeclaredNames(node.left);\n }\n\n if (node.type === 'ObjectPattern') {\n return node.properties.flatMap((property) =>\n property.type === 'RestElement'\n ? collectDeclaredNames(property.argument)\n : collectDeclaredNames(property.value)\n );\n }\n\n if (node.type === 'ArrayPattern') {\n return node.elements.flatMap((element) =>\n element ? collectDeclaredNames(element) : []\n );\n }\n\n if (node.type === 'TSParameterProperty') {\n return collectDeclaredNames(node.parameter);\n }\n\n return [];\n};\n\nexport const collectTopLevelBindings = (statement: Node): Set<string> => {\n const bindings = new Set<string>();\n\n if (statement.type === 'ImportDeclaration') {\n collectImportLocalNames(statement).forEach((name) => bindings.add(name));\n return bindings;\n }\n\n if (statement.type === 'VariableDeclaration') {\n const { declarations } = statement as AnyNode;\n if (!Array.isArray(declarations)) {\n return bindings;\n }\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n if (isOxcNode(id)) {\n collectDeclaredNames(id).forEach((name) => bindings.add(name));\n }\n });\n return bindings;\n }\n\n if (\n (statement.type === 'FunctionDeclaration' ||\n statement.type === 'ClassDeclaration' ||\n statement.type === 'TSEnumDeclaration') &&\n 'id' in statement\n ) {\n const { id } = statement as AnyNode;\n if (isOxcNode(id) && id.type === 'Identifier') {\n bindings.add(id.name);\n }\n return bindings;\n }\n\n if (statement.type === 'ExportNamedDeclaration') {\n const { declaration } = statement as AnyNode;\n return isOxcNode(declaration)\n ? collectTopLevelBindings(declaration)\n : bindings;\n }\n\n return bindings;\n};\n\nexport const collectTopLevelStatementInfos = (\n program: Program\n): TopLevelStatementInfo[] =>\n program.body.map((statement) => ({\n bindings: collectTopLevelBindings(statement),\n node: statement,\n references: collectReferencedNames(statement),\n }));\n\nexport const collectTopLevelBindingsFromStatements = (\n statements: TopLevelStatementInfo[]\n): Set<string> =>\n new Set(statements.flatMap((statement) => [...statement.bindings]));\n\nexport const collectRemovableNamesFromStatements = (\n statements: TopLevelStatementInfo[],\n initialNames: Set<string>\n): Set<string> => {\n const removable = new Set(initialNames);\n const bindingToStatement = new Map<string, TopLevelStatementInfo>();\n\n statements.forEach((statement) => {\n statement.bindings.forEach((name) => {\n bindingToStatement.set(name, statement);\n });\n });\n\n const queue = [...removable];\n while (queue.length > 0) {\n const name = queue.shift()!;\n const statement = bindingToStatement.get(name);\n if (statement) {\n statement.references.forEach((reference) => {\n if (bindingToStatement.has(reference) && !removable.has(reference)) {\n removable.add(reference);\n queue.push(reference);\n }\n });\n }\n }\n\n return removable;\n};\n"],"file":"cleanupBindings.js"}
|