@wyw-in-js/transform 2.0.0-alpha.0 → 2.0.0-alpha.1
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/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 +186 -0
- package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +183 -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 +57 -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 +42 -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 +431 -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/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 +28 -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 +12 -1
- package/esm/utils/oxcPreevalTransforms.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 +4 -3
- 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 +3 -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 +220 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.d.ts +2 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +197 -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 +63 -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 +44 -9
- package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +8 -0
- package/types/utils/applyOxcProcessors/applyOxcProcessors.js +167 -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 +482 -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/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 +16 -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 +37 -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 +14 -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,540 @@
|
|
|
1
|
+
import { lookupStaticBinding } from "./staticBindings.js";
|
|
2
|
+
import { resolveBindingAt } from "./scopeAnalysis.js";
|
|
3
|
+
export const literalCode = (value) => {
|
|
4
|
+
if (typeof value === "number") {
|
|
5
|
+
return Number.isFinite(value) ? JSON.stringify(value) : null;
|
|
6
|
+
}
|
|
7
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
8
|
+
return JSON.stringify(value);
|
|
9
|
+
}
|
|
10
|
+
if (Array.isArray(value)) {
|
|
11
|
+
return JSON.stringify(value);
|
|
12
|
+
}
|
|
13
|
+
if (typeof value === "object" && value !== null) {
|
|
14
|
+
return `(${JSON.stringify(value)})`;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
};
|
|
18
|
+
export const isStaticSerializableValue = (value) => literalCode(value) !== null;
|
|
19
|
+
export const cloneStaticValue = (value) => {
|
|
20
|
+
if (Array.isArray(value)) {
|
|
21
|
+
return value.map((item) => cloneStaticValue(item));
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === "object" && value !== null) {
|
|
24
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cloneStaticValue(item)]));
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
};
|
|
28
|
+
const INT32_SIZE = 2 ** 32;
|
|
29
|
+
const INT32_SIGN_BIT = 2 ** 31;
|
|
30
|
+
const toInt32 = (value) => {
|
|
31
|
+
if (!Number.isFinite(value) || value === 0) {
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
const integer = Math.sign(value) * Math.floor(Math.abs(value));
|
|
35
|
+
const int32bit = (integer % INT32_SIZE + INT32_SIZE) % INT32_SIZE;
|
|
36
|
+
return int32bit >= INT32_SIGN_BIT ? int32bit - INT32_SIZE : int32bit;
|
|
37
|
+
};
|
|
38
|
+
const bitwiseNot = (value) => -toInt32(value) - 1;
|
|
39
|
+
const getObjectMember = (objectValue, property) => {
|
|
40
|
+
if (objectValue === null || objectValue === undefined || typeof objectValue !== "object" && typeof objectValue !== "string" && typeof objectValue !== "number" && typeof objectValue !== "boolean") {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
return objectValue[property];
|
|
44
|
+
};
|
|
45
|
+
const oxcStaticCallableValue = Symbol("wyw.oxc.staticCallableValue");
|
|
46
|
+
const isOxcStaticCallableValue = (value) => typeof value === "object" && value !== null && oxcStaticCallableValue in value;
|
|
47
|
+
const unwrapOxcStaticCallableValue = (value) => isOxcStaticCallableValue(value) ? value[oxcStaticCallableValue] : value;
|
|
48
|
+
export const createOxcStaticCallableValue = (value) => ({ [oxcStaticCallableValue]: value });
|
|
49
|
+
const assignPatternValue = (pattern, value, ctx, env, stack) => {
|
|
50
|
+
if (pattern.type === "Identifier") {
|
|
51
|
+
env.set(pattern.name, value);
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
if (pattern.type === "AssignmentPattern") {
|
|
55
|
+
return assignPatternValue(pattern.left, value === undefined ? evaluateStatic(pattern.right, ctx, env, stack) : value, ctx, env, stack);
|
|
56
|
+
}
|
|
57
|
+
if (pattern.type === "ObjectPattern") {
|
|
58
|
+
if (typeof value !== "object" || value === null) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return pattern.properties.every((property) => {
|
|
62
|
+
if (property.type === "RestElement") {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
let key;
|
|
66
|
+
if (property.computed) {
|
|
67
|
+
key = evaluateStatic(property.key, ctx, env, stack);
|
|
68
|
+
} else if (property.key.type === "Identifier") {
|
|
69
|
+
key = property.key.name;
|
|
70
|
+
} else if (property.key.type === "Literal") {
|
|
71
|
+
key = property.key.value;
|
|
72
|
+
}
|
|
73
|
+
if (key === undefined || key === null) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return assignPatternValue(property.value, getObjectMember(value, key), ctx, env, stack);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (pattern.type === "ArrayPattern") {
|
|
80
|
+
if (!Array.isArray(value)) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return pattern.elements.every((element, index) => element ? assignPatternValue(element, value[index], ctx, env, stack) : true);
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
};
|
|
87
|
+
const applyRootMutation = (bindingName, baseValue, mutation, ctx, env, stack) => {
|
|
88
|
+
const resolvePath = (node) => {
|
|
89
|
+
if (node.type === "Identifier") {
|
|
90
|
+
return node.name === bindingName ? { path: [] } : null;
|
|
91
|
+
}
|
|
92
|
+
if (node.type !== "MemberExpression") {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
const parent = resolvePath(node.object);
|
|
96
|
+
if (!parent) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
let key;
|
|
100
|
+
if (node.computed) {
|
|
101
|
+
key = evaluateStatic(node.property, ctx, env, stack);
|
|
102
|
+
} else if (node.property.type === "Identifier") {
|
|
103
|
+
key = node.property.name;
|
|
104
|
+
}
|
|
105
|
+
if (key === undefined || key === null || typeof key !== "string" && typeof key !== "number") {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
return { path: [...parent.path, key] };
|
|
109
|
+
};
|
|
110
|
+
const pathInfo = resolvePath(mutation.type === "AssignmentExpression" ? mutation.left : mutation.argument);
|
|
111
|
+
if (!pathInfo) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const cloned = cloneStaticValue(baseValue);
|
|
115
|
+
if (pathInfo.path.length === 0) {
|
|
116
|
+
if (mutation.type !== "AssignmentExpression") {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
return evaluateStatic(mutation.right, ctx, env, stack);
|
|
120
|
+
}
|
|
121
|
+
let target = cloned;
|
|
122
|
+
for (let idx = 0; idx < pathInfo.path.length - 1; idx += 1) {
|
|
123
|
+
const key = pathInfo.path[idx];
|
|
124
|
+
const next = target?.[key];
|
|
125
|
+
if (typeof next !== "object" || next === null) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
target = next;
|
|
129
|
+
}
|
|
130
|
+
const lastKey = pathInfo.path[pathInfo.path.length - 1];
|
|
131
|
+
if (mutation.type === "AssignmentExpression") {
|
|
132
|
+
const nextValue = evaluateStatic(mutation.right, ctx, env, stack);
|
|
133
|
+
if (nextValue === undefined) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
target[lastKey] = nextValue;
|
|
137
|
+
return cloned;
|
|
138
|
+
}
|
|
139
|
+
const currentValue = target[lastKey];
|
|
140
|
+
if (typeof currentValue !== "number") {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
target[lastKey] = mutation.operator === "++" ? currentValue + 1 : currentValue - 1;
|
|
144
|
+
return cloned;
|
|
145
|
+
};
|
|
146
|
+
const evaluateFunctionCall = (fn, args, ctx, env, stack) => {
|
|
147
|
+
if (fn.async || !fn.body) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
const localEnv = new Map(env);
|
|
151
|
+
for (let idx = 0; idx < fn.params.length; idx += 1) {
|
|
152
|
+
if (!assignPatternValue(fn.params[idx], args[idx], ctx, localEnv, stack)) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (fn.body.type !== "BlockStatement") {
|
|
157
|
+
return evaluateStatic(fn.body, ctx, localEnv, stack);
|
|
158
|
+
}
|
|
159
|
+
for (const statement of fn.body.body) {
|
|
160
|
+
if (statement.type === "VariableDeclaration") {
|
|
161
|
+
for (const declarator of statement.declarations) {
|
|
162
|
+
const value = declarator.init ? evaluateStatic(declarator.init, ctx, localEnv, stack) : undefined;
|
|
163
|
+
if (!assignPatternValue(declarator.id, value, ctx, localEnv, stack)) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (statement.type === "ReturnStatement") {
|
|
170
|
+
if (!statement.argument) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
return evaluateStatic(statement.argument, ctx, localEnv, stack);
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
return undefined;
|
|
178
|
+
};
|
|
179
|
+
const isProcessEnvMember = (node) => {
|
|
180
|
+
if (node.type !== "MemberExpression" || node.computed) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (node.property.type !== "Identifier" || node.property.name !== "env") {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return node.object.type === "Identifier" && node.object.name === "process";
|
|
187
|
+
};
|
|
188
|
+
const isProcessEnvValueAccess = (expression, env) => expression.type === "MemberExpression" && isProcessEnvMember(expression.object) && !env.has("process");
|
|
189
|
+
const isDeterministicUndefinedExpression = (expression, ctx, env) => {
|
|
190
|
+
if (isProcessEnvValueAccess(expression, env)) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
if (expression.type === "UnaryExpression" && expression.operator === "void") {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
return expression.type === "Identifier" && expression.name === "undefined" && !resolveBindingAt(ctx, expression.name, expression.start);
|
|
197
|
+
};
|
|
198
|
+
const evaluateBinary = (expression, ctx, env = new Map(), stack = []) => {
|
|
199
|
+
if (expression.type !== "BinaryExpression") {
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
const left = evaluateStatic(expression.left, ctx, env, stack);
|
|
203
|
+
const right = evaluateStatic(expression.right, ctx, env, stack);
|
|
204
|
+
const leftIsDeterministicUndefined = left === undefined && isDeterministicUndefinedExpression(expression.left, ctx, env);
|
|
205
|
+
const rightIsDeterministicUndefined = right === undefined && isDeterministicUndefinedExpression(expression.right, ctx, env);
|
|
206
|
+
if (left === undefined && !leftIsDeterministicUndefined || right === undefined && !rightIsDeterministicUndefined) {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
switch (expression.operator) {
|
|
210
|
+
case "===": return left === right;
|
|
211
|
+
case "!==": return left !== right;
|
|
212
|
+
case "==":
|
|
213
|
+
// eslint-disable-next-line eqeqeq
|
|
214
|
+
return left == right;
|
|
215
|
+
case "!=":
|
|
216
|
+
// eslint-disable-next-line eqeqeq
|
|
217
|
+
return left != right;
|
|
218
|
+
default: break;
|
|
219
|
+
}
|
|
220
|
+
if (expression.operator === "+") {
|
|
221
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
222
|
+
return left + right;
|
|
223
|
+
}
|
|
224
|
+
if ((typeof left === "string" || typeof left === "number") && (typeof right === "string" || typeof right === "number")) {
|
|
225
|
+
return `${left}${right}`;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
229
|
+
switch (expression.operator) {
|
|
230
|
+
case "<": return left < right;
|
|
231
|
+
case "<=": return left <= right;
|
|
232
|
+
case ">": return left > right;
|
|
233
|
+
case ">=": return left >= right;
|
|
234
|
+
case "-": return left - right;
|
|
235
|
+
case "*": return left * right;
|
|
236
|
+
case "/": return left / right;
|
|
237
|
+
case "%": return left % right;
|
|
238
|
+
case "**": return left ** right;
|
|
239
|
+
default: break;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return undefined;
|
|
243
|
+
};
|
|
244
|
+
export const evaluateStatic = (expression, ctx, env = new Map(), stack = []) => {
|
|
245
|
+
if (expression.type === "TSAsExpression" || expression.type === "TSSatisfiesExpression" || expression.type === "TSNonNullExpression" || expression.type === "TSInstantiationExpression" || expression.type === "TSTypeAssertion" || expression.type === "ParenthesizedExpression") {
|
|
246
|
+
return evaluateStatic(expression.expression, ctx, env, stack);
|
|
247
|
+
}
|
|
248
|
+
if (expression.type === "Literal") {
|
|
249
|
+
return expression.value;
|
|
250
|
+
}
|
|
251
|
+
if (expression.type === "UnaryExpression") {
|
|
252
|
+
if (expression.operator === "typeof") {
|
|
253
|
+
const argIsProcessEnvAccess = isProcessEnvValueAccess(expression.argument, env);
|
|
254
|
+
// `typeof someIdentifier` is the canonical undeclared-global
|
|
255
|
+
// probe — it returns 'undefined' regardless of whether the
|
|
256
|
+
// symbol is declared. Only fold truly unbound identifiers: declared
|
|
257
|
+
// but dynamic locals still have runtime values we cannot infer.
|
|
258
|
+
const argIsUnboundBareIdentifier = expression.argument.type === "Identifier" && !resolveBindingAt(ctx, expression.argument.name, expression.argument.start);
|
|
259
|
+
const arg = evaluateStatic(expression.argument, ctx, env, stack);
|
|
260
|
+
if (arg === undefined) {
|
|
261
|
+
return argIsProcessEnvAccess || argIsUnboundBareIdentifier ? "undefined" : undefined;
|
|
262
|
+
}
|
|
263
|
+
return typeof arg;
|
|
264
|
+
}
|
|
265
|
+
const arg = evaluateStatic(expression.argument, ctx, env, stack);
|
|
266
|
+
if (arg === undefined) {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
switch (expression.operator) {
|
|
270
|
+
case "-": return typeof arg === "number" ? -arg : undefined;
|
|
271
|
+
case "+": return typeof arg === "number" ? +arg : undefined;
|
|
272
|
+
case "!": return !arg;
|
|
273
|
+
case "~": return typeof arg === "number" ? bitwiseNot(arg) : undefined;
|
|
274
|
+
case "void": return undefined;
|
|
275
|
+
default: return undefined;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (expression.type === "LogicalExpression") {
|
|
279
|
+
const left = evaluateStatic(expression.left, ctx, env, stack);
|
|
280
|
+
// process.env.X access is the only source we trust as "deterministically
|
|
281
|
+
// undefined" — it's a build-time lookup we control. For everything else,
|
|
282
|
+
// undefined means "couldn't evaluate" and we must bail to avoid inlining
|
|
283
|
+
// a wrong fallback when the runtime value isn't actually nullish.
|
|
284
|
+
const leftIsProcessEnvAccess = isProcessEnvValueAccess(expression.left, env);
|
|
285
|
+
if (left === undefined && !leftIsProcessEnvAccess) {
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
if (expression.operator === "||") {
|
|
289
|
+
return left || evaluateStatic(expression.right, ctx, env, stack);
|
|
290
|
+
}
|
|
291
|
+
if (expression.operator === "??") {
|
|
292
|
+
return left ?? evaluateStatic(expression.right, ctx, env, stack);
|
|
293
|
+
}
|
|
294
|
+
if (expression.operator === "&&") {
|
|
295
|
+
return left && evaluateStatic(expression.right, ctx, env, stack);
|
|
296
|
+
}
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
if (expression.type === "ConditionalExpression") {
|
|
300
|
+
const test = evaluateStatic(expression.test, ctx, env, stack);
|
|
301
|
+
if (test === undefined) {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
return evaluateStatic(test ? expression.consequent : expression.alternate, ctx, env, stack);
|
|
305
|
+
}
|
|
306
|
+
if (expression.type === "TemplateLiteral") {
|
|
307
|
+
let result = "";
|
|
308
|
+
for (let idx = 0; idx < expression.quasis.length; idx += 1) {
|
|
309
|
+
result += expression.quasis[idx]?.value.cooked ?? "";
|
|
310
|
+
const nextExpression = expression.expressions[idx];
|
|
311
|
+
if (!nextExpression) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const value = evaluateStatic(nextExpression, ctx, env, stack);
|
|
315
|
+
if (value === undefined || typeof value !== "string" && typeof value !== "number") {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
result += String(value);
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
if (expression.type === "Identifier") {
|
|
323
|
+
if (env.has(expression.name)) {
|
|
324
|
+
return unwrapOxcStaticCallableValue(env.get(expression.name));
|
|
325
|
+
}
|
|
326
|
+
const binding = resolveBindingAt(ctx, expression.name, expression.start);
|
|
327
|
+
if (binding?.importedFrom) {
|
|
328
|
+
// staticBindings can supply a literal value for an imported name,
|
|
329
|
+
// bypassing whatever the source module would otherwise resolve to.
|
|
330
|
+
// Function values are deferred to the CallExpression branch.
|
|
331
|
+
const override = lookupStaticBinding(ctx.staticBindings, binding.importedFrom, binding.imported);
|
|
332
|
+
if (override.found && typeof override.value !== "function") {
|
|
333
|
+
return override.value;
|
|
334
|
+
}
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
if (!binding) {
|
|
338
|
+
return undefined;
|
|
339
|
+
}
|
|
340
|
+
if (binding.kind === "param") {
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
if (stack.includes(binding.name)) {
|
|
344
|
+
return undefined;
|
|
345
|
+
}
|
|
346
|
+
let value;
|
|
347
|
+
const { declarator } = binding;
|
|
348
|
+
const init = declarator?.init;
|
|
349
|
+
if (init) {
|
|
350
|
+
if (declarator.id.type !== "Identifier") {
|
|
351
|
+
return undefined;
|
|
352
|
+
}
|
|
353
|
+
value = evaluateStatic(init, ctx, env, [...stack, binding.name]);
|
|
354
|
+
} else if (binding.functionNode) {
|
|
355
|
+
value = binding.functionNode;
|
|
356
|
+
}
|
|
357
|
+
if (value !== undefined && binding.isRoot && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
358
|
+
const mutations = ctx.rootMutationsByBinding.get(binding.name) ?? [];
|
|
359
|
+
let nextValue = cloneStaticValue(value);
|
|
360
|
+
for (const mutation of mutations) {
|
|
361
|
+
if (mutation.start >= ctx.currentExpressionStart) {
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
const applied = applyRootMutation(binding.name, nextValue, mutation, ctx, env, [...stack, binding.name]);
|
|
365
|
+
if (applied === undefined) {
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
nextValue = applied;
|
|
369
|
+
}
|
|
370
|
+
return nextValue;
|
|
371
|
+
}
|
|
372
|
+
return value;
|
|
373
|
+
}
|
|
374
|
+
if (expression.type === "ObjectExpression") {
|
|
375
|
+
const result = {};
|
|
376
|
+
for (const property of expression.properties) {
|
|
377
|
+
if (property.type === "SpreadElement") {
|
|
378
|
+
const spreadValue = evaluateStatic(property.argument, ctx, env, stack);
|
|
379
|
+
if (typeof spreadValue !== "object" || spreadValue === null) {
|
|
380
|
+
return undefined;
|
|
381
|
+
}
|
|
382
|
+
Object.assign(result, spreadValue);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
let key;
|
|
386
|
+
if (property.computed) {
|
|
387
|
+
key = evaluateStatic(property.key, ctx, env, stack);
|
|
388
|
+
} else if (property.key.type === "Identifier") {
|
|
389
|
+
key = property.key.name;
|
|
390
|
+
} else if (property.key.type === "Literal") {
|
|
391
|
+
key = property.key.value;
|
|
392
|
+
}
|
|
393
|
+
if (key === undefined || key === null || typeof key !== "string" && typeof key !== "number") {
|
|
394
|
+
return undefined;
|
|
395
|
+
}
|
|
396
|
+
const value = evaluateStatic(property.value, ctx, env, stack);
|
|
397
|
+
if (value === undefined) {
|
|
398
|
+
return undefined;
|
|
399
|
+
}
|
|
400
|
+
result[key] = value;
|
|
401
|
+
}
|
|
402
|
+
return result;
|
|
403
|
+
}
|
|
404
|
+
if (expression.type === "ArrayExpression") {
|
|
405
|
+
const result = [];
|
|
406
|
+
for (const element of expression.elements) {
|
|
407
|
+
if (!element) {
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
if (element.type === "SpreadElement") {
|
|
411
|
+
const spreadValue = evaluateStatic(element.argument, ctx, env, stack);
|
|
412
|
+
if (!Array.isArray(spreadValue)) {
|
|
413
|
+
return undefined;
|
|
414
|
+
}
|
|
415
|
+
result.push(...spreadValue);
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
const value = evaluateStatic(element, ctx, env, stack);
|
|
419
|
+
if (value === undefined) {
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
result.push(value);
|
|
423
|
+
}
|
|
424
|
+
return result;
|
|
425
|
+
}
|
|
426
|
+
if (expression.type === "MemberExpression") {
|
|
427
|
+
let key;
|
|
428
|
+
if (expression.computed) {
|
|
429
|
+
key = evaluateStatic(expression.property, ctx, env, stack);
|
|
430
|
+
} else if (expression.property.type === "Identifier") {
|
|
431
|
+
key = expression.property.name;
|
|
432
|
+
}
|
|
433
|
+
if (key === undefined || key === null || typeof key !== "string" && typeof key !== "number") {
|
|
434
|
+
return undefined;
|
|
435
|
+
}
|
|
436
|
+
if (isProcessEnvValueAccess(expression, env) && typeof key === "string") {
|
|
437
|
+
// Treat process.env.X as deterministically undefined at build time.
|
|
438
|
+
// Reading from real process.env would couple the bundle to whatever
|
|
439
|
+
// happens to be set on the build machine; falling back to the
|
|
440
|
+
// ?? / || branch (or a runtime read) is more predictable.
|
|
441
|
+
return undefined;
|
|
442
|
+
}
|
|
443
|
+
const objectValue = evaluateStatic(expression.object, ctx, env, stack);
|
|
444
|
+
if (objectValue === undefined) {
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
return getObjectMember(objectValue, key);
|
|
448
|
+
}
|
|
449
|
+
if (expression.type === "NewExpression") {
|
|
450
|
+
if (expression.callee.type !== "Identifier" || expression.arguments.length !== 1) {
|
|
451
|
+
return undefined;
|
|
452
|
+
}
|
|
453
|
+
const [argument] = expression.arguments;
|
|
454
|
+
if (!argument || argument.type === "SpreadElement") {
|
|
455
|
+
return undefined;
|
|
456
|
+
}
|
|
457
|
+
const value = evaluateStatic(argument, ctx, env, stack);
|
|
458
|
+
if (value === undefined) {
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
if (expression.callee.name === "String") {
|
|
462
|
+
return String(value);
|
|
463
|
+
}
|
|
464
|
+
if (expression.callee.name === "Number") {
|
|
465
|
+
return Number(value);
|
|
466
|
+
}
|
|
467
|
+
if (expression.callee.name === "Boolean") {
|
|
468
|
+
return Boolean(value);
|
|
469
|
+
}
|
|
470
|
+
return undefined;
|
|
471
|
+
}
|
|
472
|
+
if (expression.type === "CallExpression") {
|
|
473
|
+
if (expression.callee.type === "Identifier") {
|
|
474
|
+
const args = expression.arguments.map((arg) => arg.type === "SpreadElement" ? undefined : evaluateStatic(arg, ctx, env, stack));
|
|
475
|
+
if (args.some((value) => value === undefined)) {
|
|
476
|
+
return undefined;
|
|
477
|
+
}
|
|
478
|
+
const staticCallable = env.get(expression.callee.name);
|
|
479
|
+
if (isOxcStaticCallableValue(staticCallable) && expression.arguments.length === 0) {
|
|
480
|
+
return unwrapOxcStaticCallableValue(staticCallable);
|
|
481
|
+
}
|
|
482
|
+
// Plain function in env (e.g. supplied via staticBindings as a
|
|
483
|
+
// pure helper). Invoke with already-evaluated args.
|
|
484
|
+
if (typeof staticCallable === "function") {
|
|
485
|
+
try {
|
|
486
|
+
return staticCallable(...args);
|
|
487
|
+
} catch {
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
if (expression.callee.name === "String" && args.length === 1) {
|
|
492
|
+
return String(args[0]);
|
|
493
|
+
}
|
|
494
|
+
if (expression.callee.name === "Number" && args.length === 1) {
|
|
495
|
+
return Number(args[0]);
|
|
496
|
+
}
|
|
497
|
+
if (expression.callee.name === "Boolean" && args.length === 1) {
|
|
498
|
+
return Boolean(args[0]);
|
|
499
|
+
}
|
|
500
|
+
const binding = resolveBindingAt(ctx, expression.callee.name, expression.callee.start);
|
|
501
|
+
// staticBindings can register a pure helper for an imported name
|
|
502
|
+
// (e.g. linaria's `cx` from '@linaria/core'). When the callee
|
|
503
|
+
// resolves to such an import and every arg evaluated, invoke the
|
|
504
|
+
// helper and return its result as a static value.
|
|
505
|
+
if (binding?.importedFrom) {
|
|
506
|
+
const override = lookupStaticBinding(ctx.staticBindings, binding.importedFrom, binding.imported);
|
|
507
|
+
if (override.found && typeof override.value === "function") {
|
|
508
|
+
try {
|
|
509
|
+
return override.value(...args);
|
|
510
|
+
} catch {
|
|
511
|
+
return undefined;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
const fn = binding?.functionNode ?? binding?.declarator?.init;
|
|
516
|
+
if (fn && (fn.type === "ArrowFunctionExpression" || fn.type === "FunctionDeclaration" || fn.type === "FunctionExpression")) {
|
|
517
|
+
return evaluateFunctionCall(fn, args, ctx, env, [...stack, expression.callee.name]);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (expression.callee.type === "MemberExpression") {
|
|
521
|
+
const objectValue = evaluateStatic(expression.callee.object, ctx, env, stack);
|
|
522
|
+
let key;
|
|
523
|
+
if (expression.callee.computed) {
|
|
524
|
+
key = evaluateStatic(expression.callee.property, ctx, env, stack);
|
|
525
|
+
} else if (expression.callee.property.type === "Identifier") {
|
|
526
|
+
key = expression.callee.property.name;
|
|
527
|
+
}
|
|
528
|
+
if (typeof objectValue === "string") {
|
|
529
|
+
if (key === "toLowerCase" && expression.arguments.length === 0) {
|
|
530
|
+
return objectValue.toLowerCase();
|
|
531
|
+
}
|
|
532
|
+
if (key === "toUpperCase" && expression.arguments.length === 0) {
|
|
533
|
+
return objectValue.to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;AAGlC,QAAI,QAAQ,iBAAiB,WAAW,UAAU,WAAW,GAAG;AAC9D,YAAO,YAAY,aAAa;;;;;AAMxC,QAAO,eAAe,YAAY,KAAK,KAAK,MAAM","names":[],"sources":["../../../src/utils/collectOxcTemplateDependencies/staticEvaluator.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax,no-continue,@typescript-eslint/no-use-before-define */\n\nimport type {\n AssignmentExpression,\n Expression,\n Node,\n UpdateExpression,\n} from 'oxc-parser';\n\nimport { lookupStaticBinding } from './staticBindings';\nimport { resolveBindingAt } from './scopeAnalysis';\nimport type { ExtractionContext, OxcFunctionLikeNode } from './types';\n\nexport const literalCode = (value: unknown): string | null => {\n if (typeof value === 'number') {\n return Number.isFinite(value) ? JSON.stringify(value) : null;\n }\n\n if (\n value === null ||\n typeof value === 'string' ||\n typeof value === 'boolean'\n ) {\n return JSON.stringify(value);\n }\n\n if (Array.isArray(value)) {\n return JSON.stringify(value);\n }\n\n if (typeof value === 'object' && value !== null) {\n return `(${JSON.stringify(value)})`;\n }\n\n return null;\n};\n\nexport const isStaticSerializableValue = (value: unknown): boolean =>\n literalCode(value) !== null;\n\nexport const cloneStaticValue = (value: unknown): unknown => {\n if (Array.isArray(value)) {\n return value.map((item) => cloneStaticValue(item));\n }\n\n if (typeof value === 'object' && value !== null) {\n return Object.fromEntries(\n Object.entries(value).map(([key, item]) => [key, cloneStaticValue(item)])\n );\n }\n\n return value;\n};\n\nconst INT32_SIZE = 2 ** 32;\nconst INT32_SIGN_BIT = 2 ** 31;\n\nconst toInt32 = (value: number): number => {\n if (!Number.isFinite(value) || value === 0) {\n return 0;\n }\n\n const integer = Math.sign(value) * Math.floor(Math.abs(value));\n const int32bit = ((integer % INT32_SIZE) + INT32_SIZE) % INT32_SIZE;\n\n return int32bit >= INT32_SIGN_BIT ? int32bit - INT32_SIZE : int32bit;\n};\n\nconst bitwiseNot = (value: number): number => -toInt32(value) - 1;\n\nconst getObjectMember = (\n objectValue: unknown,\n property: string | number\n): unknown | undefined => {\n if (\n objectValue === null ||\n objectValue === undefined ||\n (typeof objectValue !== 'object' &&\n typeof objectValue !== 'string' &&\n typeof objectValue !== 'number' &&\n typeof objectValue !== 'boolean')\n ) {\n return undefined;\n }\n\n return (objectValue as Record<string | number, unknown>)[property];\n};\n\ntype EvalEnv = Map<string, unknown>;\n\nconst oxcStaticCallableValue = Symbol('wyw.oxc.staticCallableValue');\n\ntype OxcStaticCallableValue = {\n [oxcStaticCallableValue]: unknown;\n};\n\nconst isOxcStaticCallableValue = (\n value: unknown\n): value is OxcStaticCallableValue =>\n typeof value === 'object' &&\n value !== null &&\n oxcStaticCallableValue in value;\n\nconst unwrapOxcStaticCallableValue = (value: unknown): unknown =>\n isOxcStaticCallableValue(value) ? value[oxcStaticCallableValue] : value;\n\nexport const createOxcStaticCallableValue = (\n value: unknown\n): OxcStaticCallableValue => ({\n [oxcStaticCallableValue]: value,\n});\n\nconst assignPatternValue = (\n pattern: Node,\n value: unknown,\n ctx: ExtractionContext,\n env: EvalEnv,\n stack: string[]\n): boolean => {\n if (pattern.type === 'Identifier') {\n env.set(pattern.name, value);\n return true;\n }\n\n if (pattern.type === 'AssignmentPattern') {\n return assignPatternValue(\n pattern.left,\n value === undefined\n ? evaluateStatic(pattern.right, ctx, env, stack)\n : value,\n ctx,\n env,\n stack\n );\n }\n\n if (pattern.type === 'ObjectPattern') {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n return pattern.properties.every((property) => {\n if (property.type === 'RestElement') {\n return false;\n }\n\n let key: unknown;\n if (property.computed) {\n key = evaluateStatic(property.key as Expression, ctx, env, stack);\n } else if (property.key.type === 'Identifier') {\n key = property.key.name;\n } else if (property.key.type === 'Literal') {\n key = property.key.value;\n }\n if (key === undefined || key === null) {\n return false;\n }\n\n return assignPatternValue(\n property.value,\n getObjectMember(value, key as string | number),\n ctx,\n env,\n stack\n );\n });\n }\n\n if (pattern.type === 'ArrayPattern') {\n if (!Array.isArray(value)) {\n return false;\n }\n\n return pattern.elements.every((element, index) =>\n element\n ? assignPatternValue(element, value[index], ctx, env, stack)\n : true\n );\n }\n\n return false;\n};\n\nconst applyRootMutation = (\n bindingName: string,\n baseValue: unknown,\n mutation: AssignmentExpression | UpdateExpression,\n ctx: ExtractionContext,\n env: EvalEnv,\n stack: string[]\n): unknown | undefined => {\n const resolvePath = (node: Node): { path: Array<string | number> } | null => {\n if (node.type === 'Identifier') {\n return node.name === bindingName ? { path: [] } : null;\n }\n\n if (node.type !== 'MemberExpression') {\n return null;\n }\n\n const parent = resolvePath(node.object);\n if (!parent) {\n return null;\n }\n\n let key: unknown;\n if (node.computed) {\n key = evaluateStatic(node.property as Expression, ctx, env, stack);\n } else if (node.property.type === 'Identifier') {\n key = node.property.name;\n }\n if (\n key === undefined ||\n key === null ||\n (typeof key !== 'string' && typeof key !== 'number')\n ) {\n return null;\n }\n\n return {\n path: [...parent.path, key],\n };\n };\n\n const pathInfo = resolvePath(\n mutation.type === 'AssignmentExpression' ? mutation.left : mutation.argument\n );\n if (!pathInfo) {\n return undefined;\n }\n\n const cloned = cloneStaticValue(baseValue);\n if (pathInfo.path.length === 0) {\n if (mutation.type !== 'AssignmentExpression') {\n return undefined;\n }\n\n return evaluateStatic(mutation.right, ctx, env, stack);\n }\n\n let target = cloned as Record<string | number, unknown>;\n for (let idx = 0; idx < pathInfo.path.length - 1; idx += 1) {\n const key = pathInfo.path[idx];\n const next = target?.[key];\n if (typeof next !== 'object' || next === null) {\n return undefined;\n }\n\n target = next as Record<string | number, unknown>;\n }\n\n const lastKey = pathInfo.path[pathInfo.path.length - 1]!;\n if (mutation.type === 'AssignmentExpression') {\n const nextValue = evaluateStatic(mutation.right, ctx, env, stack);\n if (nextValue === undefined) {\n return undefined;\n }\n\n target[lastKey] = nextValue;\n return cloned;\n }\n\n const currentValue = target[lastKey];\n if (typeof currentValue !== 'number') {\n return undefined;\n }\n\n target[lastKey] =\n mutation.operator === '++' ? currentValue + 1 : currentValue - 1;\n return cloned;\n};\n\nconst evaluateFunctionCall = (\n fn: OxcFunctionLikeNode,\n args: unknown[],\n ctx: ExtractionContext,\n env: EvalEnv,\n stack: string[]\n): unknown | undefined => {\n if (fn.async || !fn.body) {\n return undefined;\n }\n\n const localEnv = new Map(env);\n for (let idx = 0; idx < fn.params.length; idx += 1) {\n if (!assignPatternValue(fn.params[idx], args[idx], ctx, localEnv, stack)) {\n return undefined;\n }\n }\n\n if (fn.body.type !== 'BlockStatement') {\n return evaluateStatic(fn.body as Expression, ctx, localEnv, stack);\n }\n\n for (const statement of fn.body.body) {\n if (statement.type === 'VariableDeclaration') {\n for (const declarator of statement.declarations) {\n const value = declarator.init\n ? evaluateStatic(declarator.init, ctx, localEnv, stack)\n : undefined;\n if (!assignPatternValue(declarator.id, value, ctx, localEnv, stack)) {\n return undefined;\n }\n }\n continue;\n }\n\n if (statement.type === 'ReturnStatement') {\n if (!statement.argument) {\n return undefined;\n }\n\n return evaluateStatic(statement.argument, ctx, localEnv, stack);\n }\n\n return undefined;\n }\n\n return undefined;\n};\n\nconst isProcessEnvMember = (node: Node): boolean => {\n if (node.type !== 'MemberExpression' || node.computed) {\n return false;\n }\n\n if (node.property.type !== 'Identifier' || node.property.name !== 'env') {\n return false;\n }\n\n return node.object.type === 'Identifier' && node.object.name === 'process';\n};\n\nconst isProcessEnvValueAccess = (\n expression: Expression,\n env: EvalEnv\n): boolean =>\n expression.type === 'MemberExpression' &&\n isProcessEnvMember(expression.object) &&\n !env.has('process');\n\nconst isDeterministicUndefinedExpression = (\n expression: Expression,\n ctx: ExtractionContext,\n env: EvalEnv\n): boolean => {\n if (isProcessEnvValueAccess(expression, env)) {\n return true;\n }\n\n if (expression.type === 'UnaryExpression' && expression.operator === 'void') {\n return true;\n }\n\n return (\n expression.type === 'Identifier' &&\n expression.name === 'undefined' &&\n !resolveBindingAt(ctx, expression.name, expression.start)\n );\n};\n\nconst evaluateBinary = (\n expression: Expression,\n ctx: ExtractionContext,\n env: EvalEnv = new Map(),\n stack: string[] = []\n): unknown | undefined => {\n if (expression.type !== 'BinaryExpression') {\n return undefined;\n }\n\n const left = evaluateStatic(expression.left as Expression, ctx, env, stack);\n const right = evaluateStatic(expression.right as Expression, ctx, env, stack);\n\n const leftIsDeterministicUndefined =\n left === undefined &&\n isDeterministicUndefinedExpression(expression.left as Expression, ctx, env);\n const rightIsDeterministicUndefined =\n right === undefined &&\n isDeterministicUndefinedExpression(\n expression.right as Expression,\n ctx,\n env\n );\n\n if (\n (left === undefined && !leftIsDeterministicUndefined) ||\n (right === undefined && !rightIsDeterministicUndefined)\n ) {\n return undefined;\n }\n\n switch (expression.operator) {\n case '===':\n return left === right;\n case '!==':\n return left !== right;\n case '==':\n // eslint-disable-next-line eqeqeq\n return left == right;\n case '!=':\n // eslint-disable-next-line eqeqeq\n return left != right;\n default:\n break;\n }\n\n if (expression.operator === '+') {\n if (typeof left === 'number' && typeof right === 'number') {\n return left + right;\n }\n\n if (\n (typeof left === 'string' || typeof left === 'number') &&\n (typeof right === 'string' || typeof right === 'number')\n ) {\n return `${left}${right}`;\n }\n }\n\n if (typeof left === 'number' && typeof right === 'number') {\n switch (expression.operator) {\n case '<':\n return left < right;\n case '<=':\n return left <= right;\n case '>':\n return left > right;\n case '>=':\n return left >= right;\n case '-':\n return left - right;\n case '*':\n return left * right;\n case '/':\n return left / right;\n case '%':\n return left % right;\n case '**':\n return left ** right;\n default:\n break;\n }\n }\n\n return undefined;\n};\n\nexport const evaluateStatic = (\n expression: Expression,\n ctx: ExtractionContext,\n env: EvalEnv = new Map(),\n stack: string[] = []\n): unknown | undefined => {\n if (\n expression.type === 'TSAsExpression' ||\n expression.type === 'TSSatisfiesExpression' ||\n expression.type === 'TSNonNullExpression' ||\n expression.type === 'TSInstantiationExpression' ||\n expression.type === 'TSTypeAssertion' ||\n expression.type === 'ParenthesizedExpression'\n ) {\n return evaluateStatic(expression.expression as Expression, ctx, env, stack);\n }\n\n if (expression.type === 'Literal') {\n return expression.value;\n }\n\n if (expression.type === 'UnaryExpression') {\n if (expression.operator === 'typeof') {\n const argIsProcessEnvAccess = isProcessEnvValueAccess(\n expression.argument as Expression,\n env\n );\n // `typeof someIdentifier` is the canonical undeclared-global\n // probe — it returns 'undefined' regardless of whether the\n // symbol is declared. Only fold truly unbound identifiers: declared\n // but dynamic locals still have runtime values we cannot infer.\n const argIsUnboundBareIdentifier =\n expression.argument.type === 'Identifier' &&\n !resolveBindingAt(\n ctx,\n expression.argument.name,\n expression.argument.start\n );\n const arg = evaluateStatic(\n expression.argument as Expression,\n ctx,\n env,\n stack\n );\n if (arg === undefined) {\n return argIsProcessEnvAccess || argIsUnboundBareIdentifier\n ? 'undefined'\n : undefined;\n }\n\n return typeof arg;\n }\n\n const arg = evaluateStatic(\n expression.argument as Expression,\n ctx,\n env,\n stack\n );\n if (arg === undefined) {\n return undefined;\n }\n\n switch (expression.operator) {\n case '-':\n return typeof arg === 'number' ? -arg : undefined;\n case '+':\n return typeof arg === 'number' ? +arg : undefined;\n case '!':\n return !arg;\n case '~':\n return typeof arg === 'number' ? bitwiseNot(arg) : undefined;\n case 'void':\n return undefined;\n default:\n return undefined;\n }\n }\n\n if (expression.type === 'LogicalExpression') {\n const left = evaluateStatic(expression.left, ctx, env, stack);\n // process.env.X access is the only source we trust as \"deterministically\n // undefined\" — it's a build-time lookup we control. For everything else,\n // undefined means \"couldn't evaluate\" and we must bail to avoid inlining\n // a wrong fallback when the runtime value isn't actually nullish.\n const leftIsProcessEnvAccess = isProcessEnvValueAccess(\n expression.left,\n env\n );\n\n if (left === undefined && !leftIsProcessEnvAccess) {\n return undefined;\n }\n\n if (expression.operator === '||') {\n return left || evaluateStatic(expression.right, ctx, env, stack);\n }\n\n if (expression.operator === '??') {\n return left ?? evaluateStatic(expression.right, ctx, env, stack);\n }\n\n if (expression.operator === '&&') {\n return left && evaluateStatic(expression.right, ctx, env, stack);\n }\n\n return undefined;\n }\n\n if (expression.type === 'ConditionalExpression') {\n const test = evaluateStatic(expression.test, ctx, env, stack);\n if (test === undefined) {\n return undefined;\n }\n\n return evaluateStatic(\n test ? expression.consequent : expression.alternate,\n ctx,\n env,\n stack\n );\n }\n\n if (expression.type === 'TemplateLiteral') {\n let result = '';\n\n for (let idx = 0; idx < expression.quasis.length; idx += 1) {\n result += expression.quasis[idx]?.value.cooked ?? '';\n\n const nextExpression = expression.expressions[idx];\n if (!nextExpression) {\n continue;\n }\n\n const value = evaluateStatic(nextExpression, ctx, env, stack);\n if (\n value === undefined ||\n (typeof value !== 'string' && typeof value !== 'number')\n ) {\n return undefined;\n }\n\n result += String(value);\n }\n\n return result;\n }\n\n if (expression.type === 'Identifier') {\n if (env.has(expression.name)) {\n return unwrapOxcStaticCallableValue(env.get(expression.name));\n }\n\n const binding = resolveBindingAt(ctx, expression.name, expression.start);\n if (binding?.importedFrom) {\n // staticBindings can supply a literal value for an imported name,\n // bypassing whatever the source module would otherwise resolve to.\n // Function values are deferred to the CallExpression branch.\n const override = lookupStaticBinding(\n ctx.staticBindings,\n binding.importedFrom,\n binding.imported\n );\n if (override.found && typeof override.value !== 'function') {\n return override.value;\n }\n return undefined;\n }\n if (!binding) {\n return undefined;\n }\n\n if (binding.kind === 'param') {\n return undefined;\n }\n\n if (stack.includes(binding.name)) {\n return undefined;\n }\n\n let value: unknown | undefined;\n const { declarator } = binding;\n const init = declarator?.init;\n if (init) {\n if (declarator.id.type !== 'Identifier') {\n return undefined;\n }\n\n value = evaluateStatic(init, ctx, env, [...stack, binding.name]);\n } else if (binding.functionNode) {\n value = binding.functionNode;\n }\n\n if (\n value !== undefined &&\n binding.isRoot &&\n typeof value === 'object' &&\n value !== null &&\n !Array.isArray(value)\n ) {\n const mutations = ctx.rootMutationsByBinding.get(binding.name) ?? [];\n let nextValue = cloneStaticValue(value);\n for (const mutation of mutations) {\n if (mutation.start >= ctx.currentExpressionStart) {\n break;\n }\n\n const applied = applyRootMutation(\n binding.name,\n nextValue,\n mutation,\n ctx,\n env,\n [...stack, binding.name]\n );\n if (applied === undefined) {\n return undefined;\n }\n\n nextValue = applied;\n }\n\n return nextValue;\n }\n\n return value;\n }\n\n if (expression.type === 'ObjectExpression') {\n const result: Record<string, unknown> = {};\n\n for (const property of expression.properties) {\n if (property.type === 'SpreadElement') {\n const spreadValue = evaluateStatic(property.argument, ctx, env, stack);\n if (typeof spreadValue !== 'object' || spreadValue === null) {\n return undefined;\n }\n\n Object.assign(result, spreadValue);\n continue;\n }\n\n let key: unknown;\n if (property.computed) {\n key = evaluateStatic(property.key as Expression, ctx, env, stack);\n } else if (property.key.type === 'Identifier') {\n key = property.key.name;\n } else if (property.key.type === 'Literal') {\n key = property.key.value;\n }\n if (\n key === undefined ||\n key === null ||\n (typeof key !== 'string' && typeof key !== 'number')\n ) {\n return undefined;\n }\n\n const value = evaluateStatic(property.value, ctx, env, stack);\n if (value === undefined) {\n return undefined;\n }\n\n result[key] = value;\n }\n\n return result;\n }\n\n if (expression.type === 'ArrayExpression') {\n const result: unknown[] = [];\n\n for (const element of expression.elements) {\n if (!element) {\n return undefined;\n }\n\n if (element.type === 'SpreadElement') {\n const spreadValue = evaluateStatic(element.argument, ctx, env, stack);\n if (!Array.isArray(spreadValue)) {\n return undefined;\n }\n\n result.push(...spreadValue);\n continue;\n }\n\n const value = evaluateStatic(element, ctx, env, stack);\n if (value === undefined) {\n return undefined;\n }\n\n result.push(value);\n }\n\n return result;\n }\n\n if (expression.type === 'MemberExpression') {\n let key: unknown;\n if (expression.computed) {\n key = evaluateStatic(expression.property as Expression, ctx, env, stack);\n } else if (expression.property.type === 'Identifier') {\n key = expression.property.name;\n }\n if (\n key === undefined ||\n key === null ||\n (typeof key !== 'string' && typeof key !== 'number')\n ) {\n return undefined;\n }\n\n if (isProcessEnvValueAccess(expression, env) && typeof key === 'string') {\n // Treat process.env.X as deterministically undefined at build time.\n // Reading from real process.env would couple the bundle to whatever\n // happens to be set on the build machine; falling back to the\n // ?? / || branch (or a runtime read) is more predictable.\n return undefined;\n }\n\n const objectValue = evaluateStatic(expression.object, ctx, env, stack);\n if (objectValue === undefined) {\n return undefined;\n }\n\n return getObjectMember(objectValue, key);\n }\n\n if (expression.type === 'NewExpression') {\n if (\n expression.callee.type !== 'Identifier' ||\n expression.arguments.length !== 1\n ) {\n return undefined;\n }\n\n const [argument] = expression.arguments;\n if (!argument || argument.type === 'SpreadElement') {\n return undefined;\n }\n\n const value = evaluateStatic(argument, ctx, env, stack);\n if (value === undefined) {\n return undefined;\n }\n\n if (expression.callee.name === 'String') {\n return String(value);\n }\n\n if (expression.callee.name === 'Number') {\n return Number(value);\n }\n\n if (expression.callee.name === 'Boolean') {\n return Boolean(value);\n }\n\n return undefined;\n }\n\n if (expression.type === 'CallExpression') {\n if (expression.callee.type === 'Identifier') {\n const args = expression.arguments.map((arg) =>\n arg.type === 'SpreadElement'\n ? undefined\n : evaluateStatic(arg, ctx, env, stack)\n );\n if (args.some((value) => value === undefined)) {\n return undefined;\n }\n\n const staticCallable = env.get(expression.callee.name);\n if (\n isOxcStaticCallableValue(staticCallable) &&\n expression.arguments.length === 0\n ) {\n return unwrapOxcStaticCallableValue(staticCallable);\n }\n\n // Plain function in env (e.g. supplied via staticBindings as a\n // pure helper). Invoke with already-evaluated args.\n if (typeof staticCallable === 'function') {\n try {\n return (staticCallable as (...a: unknown[]) => unknown)(...args);\n } catch {\n return undefined;\n }\n }\n\n if (expression.callee.name === 'String' && args.length === 1) {\n return String(args[0]);\n }\n\n if (expression.callee.name === 'Number' && args.length === 1) {\n return Number(args[0]);\n }\n\n if (expression.callee.name === 'Boolean' && args.length === 1) {\n return Boolean(args[0]);\n }\n\n const binding = resolveBindingAt(\n ctx,\n expression.callee.name,\n expression.callee.start\n );\n\n // staticBindings can register a pure helper for an imported name\n // (e.g. linaria's `cx` from '@linaria/core'). When the callee\n // resolves to such an import and every arg evaluated, invoke the\n // helper and return its result as a static value.\n if (binding?.importedFrom) {\n const override = lookupStaticBinding(\n ctx.staticBindings,\n binding.importedFrom,\n binding.imported\n );\n if (override.found && typeof override.value === 'function') {\n try {\n return (override.value as (...a: unknown[]) => unknown)(...args);\n } catch {\n return undefined;\n }\n }\n }\n\n const fn = binding?.functionNode ?? binding?.declarator?.init;\n if (\n fn &&\n (fn.type === 'ArrowFunctionExpression' ||\n fn.type === 'FunctionDeclaration' ||\n fn.type === 'FunctionExpression')\n ) {\n return evaluateFunctionCall(fn, args, ctx, env, [\n ...stack,\n expression.callee.name,\n ]);\n }\n }\n\n if (expression.callee.type === 'MemberExpression') {\n const objectValue = evaluateStatic(\n expression.callee.object,\n ctx,\n env,\n stack\n );\n let key: unknown;\n if (expression.callee.computed) {\n key = evaluateStatic(\n expression.callee.property as Expression,\n ctx,\n env,\n stack\n );\n } else if (expression.callee.property.type === 'Identifier') {\n key = expression.callee.property.name;\n }\n if (typeof objectValue === 'string') {\n if (key === 'toLowerCase' && expression.arguments.length === 0) {\n return objectValue.toLowerCase();\n }\n\n if (key === 'toUpperCase' && expression.arguments.length === 0) {\n return objectValue.toUpperCase();\n }\n }\n }\n }\n\n return evaluateBinary(expression, ctx, env, stack);\n};\n"],"file":"staticEvaluator.js"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"","names":[],"sources":["../../../src/utils/collectOxcTemplateDependencies/types.ts"],"version":3,"sourcesContent":["import type { ExpressionValue, ValueType } from '@wyw-in-js/shared';\nimport type {\n AssignmentExpression,\n Expression,\n Node,\n TemplateLiteral,\n UpdateExpression,\n VariableDeclaration,\n VariableDeclarator,\n} from 'oxc-parser';\n\nimport type { OxcValueReplacement } from '../oxc/replacements';\nimport type { OxcLocationLookup } from '../oxc/sourceLocations';\n\nexport type OxcFunctionLikeNode = Node & {\n async: boolean;\n body: Node | null;\n id?: { name: string } | null;\n params: Node[];\n};\nexport type BindingKind = 'function' | 'import' | 'param' | 'variable';\nexport type ScopedDeclarationKind = 'const' | 'let' | 'var';\n\nexport type Binding = {\n declarationKind?: ScopedDeclarationKind;\n declaredAt: number;\n declaration: VariableDeclaration | null;\n declarator: VariableDeclarator | null;\n functionNode?: OxcFunctionLikeNode | null;\n imported?: 'default' | '*' | string;\n importedFrom?: string;\n isRoot: boolean;\n kind: BindingKind;\n name: string;\n scope: Scope;\n};\n\nexport type Replacement = OxcValueReplacement;\n\nexport type SpanLookup = Set<string> | null;\n\nexport type LocationLookup = OxcLocationLookup;\n\nexport type ExpressionSpan = {\n end: number;\n start: number;\n};\n\nexport type Scope = {\n bindings: Map<string, Binding>;\n depth: number;\n end: number;\n functionBoundary: boolean;\n params: Set<string>;\n parent: Scope | null;\n root: boolean;\n start: number;\n};\n\nexport type ReferenceIdentifier = {\n end: number;\n name: string;\n start: number;\n};\n\nexport type OxcStaticImportReference = {\n imported: 'default' | string;\n importLocal?: string;\n local: string;\n source: string;\n};\n\nexport type OxcStaticValue = {\n name: string;\n value: unknown;\n};\n\nexport type OxcStaticValueCandidate = {\n imports: OxcStaticImportReference[];\n inlineConstants?: Record<string, unknown>;\n name: string;\n source: string;\n};\n\nexport type TemplateExtractionResult = {\n code: string;\n dependencyNames: string[];\n expressionValues: Omit<ExpressionValue, 'buildCodeFrameError'>[];\n staticValueCandidates: OxcStaticValueCandidate[];\n staticValues: OxcStaticValue[];\n};\n\nexport type StaticBindings = Record<string, Record<string, unknown>>;\n\nexport type ExtractedExpression = {\n expressionCode: string;\n hasInlinableLocalReference?: boolean;\n importedFrom: string[];\n kind: ValueType.FUNCTION | ValueType.LAZY;\n staticExpressionCode?: string;\n staticImports: OxcStaticImportReference[];\n staticValue?: unknown;\n};\n\nexport type StaticLocalExpression = {\n importedFrom: string[];\n imports: OxcStaticImportReference[];\n source: string;\n};\n\nexport type ProgramAnalysis = {\n bindingsByName: Map<string, Binding[]>;\n rootMutationsByBinding: Map<\n string,\n Array<AssignmentExpression | UpdateExpression>\n >;\n targetExpressions: Expression[];\n templateLiterals: TemplateLiteral[];\n usedNames: Set<string>;\n};\n\nexport type ExtractionContext = {\n bindingResolutionCache: Map<string, Map<number, Binding | null>>;\n bindingsByName: Map<string, Binding[]>;\n code: string;\n currentInsertionPoint: number;\n currentExpressionStart: number;\n dependencyNames: Set<string>;\n expressionValues: Omit<ExpressionValue, 'buildCodeFrameError'>[];\n filename: string;\n hoistedBindingNames: Map<string, string>;\n hoistedDeclarations: Map<string, string>;\n hoistedDeclarationsByInsertionPoint: Map<number, string[]>;\n loc: LocationLookup;\n referencesByNode: WeakMap<Node, ReferenceIdentifier[]>;\n replacements: Replacement[];\n rootMutationsByBinding: Map<\n string,\n Array<AssignmentExpression | UpdateExpression>\n >;\n staticBindings?: StaticBindings;\n staticImportAliases: Map<string, string>;\n staticValueCandidates: OxcStaticValueCandidate[];\n staticValues: OxcStaticValue[];\n usedNames: Set<string>;\n};\n"],"file":"types.js"}
|