@wyw-in-js/transform 1.1.0 → 2.0.0-alpha.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 +426 -289
- package/esm/cache.js.map +1 -1
- package/esm/debug/fileReporter.js +137 -134
- package/esm/debug/fileReporter.js.map +1 -1
- package/esm/eval/broker.js +2134 -0
- package/esm/eval/broker.js.map +1 -0
- package/esm/eval/lru.js +36 -0
- package/esm/eval/lru.js.map +1 -0
- package/esm/eval/prepareModuleOnDemand.js +21 -0
- package/esm/eval/prepareModuleOnDemand.js.map +1 -0
- package/esm/eval/protocol.js +2 -0
- package/esm/eval/protocol.js.map +1 -0
- package/esm/eval/resolverStrategy.js +51 -0
- package/esm/eval/resolverStrategy.js.map +1 -0
- package/esm/eval/runner.js +1759 -0
- package/esm/eval/runner.js.map +1 -0
- package/esm/eval/serialize.js +333 -0
- package/esm/eval/serialize.js.map +1 -0
- package/esm/eval/writeQueue.js +81 -0
- package/esm/eval/writeQueue.js.map +1 -0
- package/esm/evaluators/index.js +11 -12
- package/esm/evaluators/index.js.map +1 -1
- package/esm/index.js +25 -29
- package/esm/index.js.map +1 -1
- package/esm/module.js +896 -520
- package/esm/module.js.map +1 -1
- package/esm/shaker.js +14 -80
- package/esm/shaker.js.map +1 -1
- package/esm/transform/BaseEntrypoint.js +162 -164
- package/esm/transform/BaseEntrypoint.js.map +1 -1
- package/esm/transform/Entrypoint.helpers.js +96 -253
- package/esm/transform/Entrypoint.helpers.js.map +1 -1
- package/esm/transform/Entrypoint.js +336 -270
- package/esm/transform/Entrypoint.js.map +1 -1
- package/esm/transform/Entrypoint.types.js +1 -1
- package/esm/transform/Entrypoint.types.js.map +1 -1
- package/esm/transform/EvaluatedEntrypoint.js +10 -4
- package/esm/transform/EvaluatedEntrypoint.js.map +1 -1
- package/esm/transform/actions/AbortError.js +6 -6
- package/esm/transform/actions/AbortError.js.map +1 -1
- package/esm/transform/actions/BaseAction.js +140 -136
- package/esm/transform/actions/BaseAction.js.map +1 -1
- package/esm/transform/actions/UnprocessedEntrypointError.js +6 -6
- package/esm/transform/actions/UnprocessedEntrypointError.js.map +1 -1
- package/esm/transform/actions/actionRunner.js +63 -66
- package/esm/transform/actions/actionRunner.js.map +1 -1
- package/esm/transform/actions/types.js +1 -1
- package/esm/transform/actions/types.js.map +1 -1
- package/esm/transform/barrelManifest.types.js +2 -0
- package/esm/transform/barrelManifest.types.js.map +1 -0
- package/esm/transform/generators/baseProcessingHandlers.js +15 -17
- package/esm/transform/generators/baseProcessingHandlers.js.map +1 -1
- package/esm/transform/generators/collect.js +27 -55
- package/esm/transform/generators/collect.js.map +1 -1
- package/esm/transform/generators/createStylisPreprocessor.js +353 -321
- package/esm/transform/generators/createStylisPreprocessor.js.map +1 -1
- package/esm/transform/generators/evalFile.js +48 -47
- package/esm/transform/generators/evalFile.js.map +1 -1
- package/esm/transform/generators/extract.js +78 -90
- package/esm/transform/generators/extract.js.map +1 -1
- package/esm/transform/generators/getExports.js +57 -74
- package/esm/transform/generators/getExports.js.map +1 -1
- package/esm/transform/generators/index.js +11 -11
- package/esm/transform/generators/index.js.map +1 -1
- package/esm/transform/generators/processEntrypoint.js +78 -67
- package/esm/transform/generators/processEntrypoint.js.map +1 -1
- package/esm/transform/generators/processImports.js +102 -67
- package/esm/transform/generators/processImports.js.map +1 -1
- package/esm/transform/generators/resolveImports.js +165 -196
- package/esm/transform/generators/resolveImports.js.map +1 -1
- package/esm/transform/generators/resolveStaticOxcValues.js +2911 -0
- package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -0
- package/esm/transform/generators/rewriteOxcBarrelImports.js +822 -0
- package/esm/transform/generators/rewriteOxcBarrelImports.js.map +1 -0
- package/esm/transform/generators/transform.js +239 -248
- package/esm/transform/generators/transform.js.map +1 -1
- package/esm/transform/generators/workflow.js +87 -90
- package/esm/transform/generators/workflow.js.map +1 -1
- package/esm/transform/helpers/loadWywOptions.js +154 -74
- package/esm/transform/helpers/loadWywOptions.js.map +1 -1
- package/esm/transform/helpers/withDefaultServices.js +13 -22
- package/esm/transform/helpers/withDefaultServices.js.map +1 -1
- package/esm/transform/isStaticallyEvaluatableModule.js +140 -152
- package/esm/transform/isStaticallyEvaluatableModule.js.map +1 -1
- package/esm/transform/oxcBarrelManifest.js +349 -0
- package/esm/transform/oxcBarrelManifest.js.map +1 -0
- package/esm/transform/rootLog.js +3 -3
- package/esm/transform/rootLog.js.map +1 -1
- package/esm/transform/syntax.js +2 -0
- package/esm/transform/syntax.js.map +1 -0
- package/esm/transform/types.js +2 -2
- package/esm/transform/types.js.map +1 -1
- package/esm/transform.js +123 -147
- package/esm/transform.js.map +1 -1
- package/esm/types.js +4 -1
- package/esm/types.js.map +1 -1
- package/esm/utils/EventEmitter.js +46 -48
- package/esm/utils/EventEmitter.js.map +1 -1
- package/esm/utils/ShakerMetadata.js +2 -2
- package/esm/utils/ShakerMetadata.js.map +1 -1
- package/esm/utils/TransformDiagnostics.js +9 -9
- package/esm/utils/TransformDiagnostics.js.map +1 -1
- package/esm/utils/TransformMetadata.js +24 -26
- package/esm/utils/TransformMetadata.js.map +1 -1
- package/esm/utils/applyOxcProcessors.js +1217 -0
- package/esm/utils/applyOxcProcessors.js.map +1 -0
- package/esm/utils/collectOxcExportsAndImports.js +934 -0
- package/esm/utils/collectOxcExportsAndImports.js.map +1 -0
- package/esm/utils/collectOxcRuntime.js +220 -0
- package/esm/utils/collectOxcRuntime.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies.js +1398 -0
- package/esm/utils/collectOxcTemplateDependencies.js.map +1 -0
- package/esm/utils/dispose-polyfill.js +3 -4
- package/esm/utils/dispose-polyfill.js.map +1 -1
- package/esm/utils/getFileIdx.js +6 -6
- package/esm/utils/getFileIdx.js.map +1 -1
- package/esm/utils/getPluginKey.js +12 -12
- package/esm/utils/getPluginKey.js.map +1 -1
- package/esm/utils/getVisitorKeys.js +9 -3
- package/esm/utils/getVisitorKeys.js.map +1 -1
- package/esm/utils/hasCachedWywPrevalExport.js +23 -0
- package/esm/utils/hasCachedWywPrevalExport.js.map +1 -0
- package/esm/utils/hasWywPreval.js +5 -5
- package/esm/utils/hasWywPreval.js.map +1 -1
- package/esm/utils/importOverrides.js +75 -90
- package/esm/utils/importOverrides.js.map +1 -1
- package/esm/utils/isNode.js +2 -2
- package/esm/utils/isNode.js.map +1 -1
- package/esm/utils/isNotNull.js +2 -2
- package/esm/utils/isNotNull.js.map +1 -1
- package/esm/utils/isSerializable.js +11 -11
- package/esm/utils/isSerializable.js.map +1 -1
- package/esm/utils/oxcAstService.js +121 -0
- package/esm/utils/oxcAstService.js.map +1 -0
- package/esm/utils/oxcEmit.js +447 -0
- package/esm/utils/oxcEmit.js.map +1 -0
- package/esm/utils/oxcPreevalStage.js +97 -0
- package/esm/utils/oxcPreevalStage.js.map +1 -0
- package/esm/utils/oxcPreevalTransforms.js +1054 -0
- package/esm/utils/oxcPreevalTransforms.js.map +1 -0
- package/esm/utils/oxcShaker.js +662 -0
- package/esm/utils/oxcShaker.js.map +1 -0
- package/esm/utils/parseOxc.js +37 -0
- package/esm/utils/parseOxc.js.map +1 -0
- package/esm/utils/parseRequest.js +27 -27
- package/esm/utils/parseRequest.js.map +1 -1
- package/esm/utils/peek.js +1 -1
- package/esm/utils/peek.js.map +1 -1
- package/esm/utils/processorLookup.js +125 -0
- package/esm/utils/processorLookup.js.map +1 -0
- package/esm/utils/resolveWithConditions.js +99 -0
- package/esm/utils/resolveWithConditions.js.map +1 -0
- package/esm/vm/createVmContext.js +140 -141
- package/esm/vm/createVmContext.js.map +1 -1
- package/esm/vm/process.js +11 -13
- package/esm/vm/process.js.map +1 -1
- package/package.json +18 -26
- package/types/cache.d.ts +17 -8
- package/types/cache.js +237 -95
- package/types/debug/fileReporter.js +22 -22
- package/types/eval/broker.d.ts +88 -0
- package/types/eval/broker.js +2262 -0
- package/types/eval/lru.d.ts +10 -0
- package/types/eval/lru.js +36 -0
- package/types/eval/prepareModuleOnDemand.d.ts +7 -0
- package/types/eval/prepareModuleOnDemand.js +24 -0
- package/types/eval/protocol.d.ts +97 -0
- package/types/eval/protocol.js +1 -0
- package/types/eval/resolverStrategy.d.ts +13 -0
- package/types/eval/resolverStrategy.js +46 -0
- package/types/eval/serialize.d.ts +78 -0
- package/types/eval/serialize.js +357 -0
- package/types/eval/writeQueue.d.ts +13 -0
- package/types/eval/writeQueue.js +80 -0
- package/types/evaluators/index.d.ts +2 -2
- package/types/evaluators/index.js +6 -9
- package/types/index.d.ts +3 -6
- package/types/index.js +24 -82
- package/types/module.d.ts +35 -7
- package/types/module.js +535 -163
- package/types/shaker.d.ts +2 -10
- package/types/shaker.js +10 -100
- package/types/transform/BaseEntrypoint.js +6 -11
- package/types/transform/Entrypoint.d.ts +15 -15
- package/types/transform/Entrypoint.helpers.d.ts +2 -5
- package/types/transform/Entrypoint.helpers.js +43 -203
- package/types/transform/Entrypoint.js +130 -53
- package/types/transform/Entrypoint.types.d.ts +28 -6
- package/types/transform/Entrypoint.types.js +1 -2
- package/types/transform/EvaluatedEntrypoint.d.ts +13 -2
- package/types/transform/EvaluatedEntrypoint.js +7 -6
- package/types/transform/actions/AbortError.js +2 -7
- package/types/transform/actions/BaseAction.js +4 -8
- package/types/transform/actions/UnprocessedEntrypointError.js +2 -7
- package/types/transform/actions/actionRunner.js +8 -12
- package/types/transform/actions/types.d.ts +2 -2
- package/types/transform/actions/types.js +1 -2
- package/types/transform/{barrelManifest.d.ts → barrelManifest.types.d.ts} +0 -2
- package/types/transform/barrelManifest.types.js +1 -0
- package/types/transform/generators/baseProcessingHandlers.d.ts +4 -5
- package/types/transform/generators/baseProcessingHandlers.js +10 -14
- package/types/transform/generators/collect.js +13 -39
- package/types/transform/generators/createStylisPreprocessor.js +19 -60
- package/types/transform/generators/evalFile.d.ts +2 -2
- package/types/transform/generators/evalFile.js +26 -28
- package/types/transform/generators/extract.js +5 -8
- package/types/transform/generators/getExports.js +23 -30
- package/types/transform/generators/index.d.ts +2 -2
- package/types/transform/generators/index.js +11 -14
- package/types/transform/generators/processEntrypoint.d.ts +2 -2
- package/types/transform/generators/processEntrypoint.js +60 -26
- package/types/transform/generators/processImports.d.ts +0 -3
- package/types/transform/generators/processImports.js +60 -20
- package/types/transform/generators/resolveImports.js +18 -22
- package/types/transform/generators/resolveStaticOxcValues.d.ts +2 -0
- package/types/transform/generators/resolveStaticOxcValues.js +3235 -0
- package/types/transform/generators/{rewriteBarrelImports.d.ts → rewriteOxcBarrelImports.d.ts} +2 -3
- package/types/transform/generators/{rewriteBarrelImports.js → rewriteOxcBarrelImports.js} +282 -225
- package/types/transform/generators/transform.d.ts +3 -7
- package/types/transform/generators/transform.js +203 -199
- package/types/transform/generators/workflow.js +62 -45
- package/types/transform/helpers/loadWywOptions.js +94 -20
- package/types/transform/helpers/withDefaultServices.d.ts +1 -1
- package/types/transform/helpers/withDefaultServices.js +6 -44
- package/types/transform/isStaticallyEvaluatableModule.d.ts +1 -2
- package/types/transform/isStaticallyEvaluatableModule.js +125 -126
- package/types/transform/oxcBarrelManifest.d.ts +2 -0
- package/types/transform/{barrelManifest.js → oxcBarrelManifest.js} +156 -97
- package/types/transform/rootLog.js +2 -5
- package/types/transform/syntax.d.ts +38 -0
- package/types/transform/syntax.js +1 -0
- package/types/transform/types.d.ts +9 -6
- package/types/transform/types.js +1 -4
- package/types/transform.d.ts +2 -2
- package/types/transform.js +88 -101
- package/types/types.d.ts +0 -23
- package/types/types.js +1 -2
- package/types/utils/EventEmitter.js +3 -9
- package/types/utils/ShakerMetadata.js +1 -5
- package/types/utils/TransformDiagnostics.js +3 -7
- package/types/utils/TransformMetadata.js +8 -16
- package/types/utils/applyOxcProcessors.d.ts +16 -0
- package/types/utils/applyOxcProcessors.js +1391 -0
- package/types/utils/collectOxcExportsAndImports.d.ts +35 -0
- package/types/utils/collectOxcExportsAndImports.js +957 -0
- package/types/utils/collectOxcRuntime.d.ts +14 -0
- package/types/utils/collectOxcRuntime.js +250 -0
- package/types/utils/collectOxcTemplateDependencies.d.ts +38 -0
- package/types/utils/collectOxcTemplateDependencies.js +1580 -0
- package/types/utils/getFileIdx.js +1 -4
- package/types/utils/getPluginKey.d.ts +5 -2
- package/types/utils/getPluginKey.js +2 -6
- package/types/utils/getVisitorKeys.d.ts +4 -4
- package/types/utils/getVisitorKeys.js +9 -6
- package/types/utils/hasCachedWywPrevalExport.d.ts +14 -0
- package/types/utils/hasCachedWywPrevalExport.js +30 -0
- package/types/utils/hasWywPreval.js +1 -4
- package/types/utils/importOverrides.js +17 -27
- package/types/utils/isNode.d.ts +2 -2
- package/types/utils/isNode.js +2 -6
- package/types/utils/isNotNull.js +1 -4
- package/types/utils/isSerializable.js +3 -6
- package/types/utils/oxcAstService.d.ts +11 -0
- package/types/utils/oxcAstService.js +79 -0
- package/types/utils/oxcEmit.d.ts +19 -0
- package/types/utils/oxcEmit.js +506 -0
- package/types/utils/oxcPreevalStage.d.ts +20 -0
- package/types/utils/oxcPreevalStage.js +102 -0
- package/types/utils/oxcPreevalTransforms.d.ts +13 -0
- package/types/utils/oxcPreevalTransforms.js +1179 -0
- package/types/utils/oxcShaker.d.ts +13 -0
- package/types/utils/oxcShaker.js +751 -0
- package/types/utils/parseOxc.d.ts +11 -0
- package/types/utils/parseOxc.js +38 -0
- package/types/utils/parseRequest.js +2 -7
- package/types/utils/peek.js +1 -5
- package/types/utils/processorLookup.d.ts +8 -0
- package/types/utils/processorLookup.js +135 -0
- package/types/utils/resolveWithConditions.d.ts +12 -0
- package/types/utils/resolveWithConditions.js +103 -0
- package/types/vm/createVmContext.d.ts +2 -2
- package/types/vm/createVmContext.js +25 -62
- package/types/vm/process.js +20 -26
- package/esm/babel.js +0 -2
- package/esm/babel.js.map +0 -1
- package/esm/options/buildOptions.js +0 -168
- package/esm/options/buildOptions.js.map +0 -1
- package/esm/options/buildOptions.test.js +0 -138
- package/esm/options/buildOptions.test.js.map +0 -1
- package/esm/options/loadBabelOptions.js +0 -24
- package/esm/options/loadBabelOptions.js.map +0 -1
- package/esm/plugins/babel-transform.js +0 -53
- package/esm/plugins/babel-transform.js.map +0 -1
- package/esm/plugins/collector.js +0 -60
- package/esm/plugins/collector.js.map +0 -1
- package/esm/plugins/dynamic-import.js +0 -56
- package/esm/plugins/dynamic-import.js.map +0 -1
- package/esm/plugins/preeval.js +0 -73
- package/esm/plugins/preeval.js.map +0 -1
- package/esm/plugins/shaker.js +0 -680
- package/esm/plugins/shaker.js.map +0 -1
- package/esm/transform/barrelManifest.js +0 -291
- package/esm/transform/barrelManifest.js.map +0 -1
- package/esm/transform/generators/explodeReexports.js +0 -64
- package/esm/transform/generators/explodeReexports.js.map +0 -1
- package/esm/transform/generators/rewriteBarrelImports.js +0 -733
- package/esm/transform/generators/rewriteBarrelImports.js.map +0 -1
- package/esm/utils/addIdentifierToWywPreval.js +0 -68
- package/esm/utils/addIdentifierToWywPreval.js.map +0 -1
- package/esm/utils/collectExportsAndImports.js +0 -1157
- package/esm/utils/collectExportsAndImports.js.map +0 -1
- package/esm/utils/collectTemplateDependencies.js +0 -228
- package/esm/utils/collectTemplateDependencies.js.map +0 -1
- package/esm/utils/createId.js +0 -6
- package/esm/utils/createId.js.map +0 -1
- package/esm/utils/findIdentifiers.js +0 -62
- package/esm/utils/findIdentifiers.js.map +0 -1
- package/esm/utils/getConstantStringValue.js +0 -58
- package/esm/utils/getConstantStringValue.js.map +0 -1
- package/esm/utils/getMemberExpressionPropertyName.js +0 -11
- package/esm/utils/getMemberExpressionPropertyName.js.map +0 -1
- package/esm/utils/getScope.js +0 -6
- package/esm/utils/getScope.js.map +0 -1
- package/esm/utils/getSource.js +0 -15
- package/esm/utils/getSource.js.map +0 -1
- package/esm/utils/getTagProcessor.js +0 -404
- package/esm/utils/getTagProcessor.js.map +0 -1
- package/esm/utils/isExports.js +0 -22
- package/esm/utils/isExports.js.map +0 -1
- package/esm/utils/isGlobal.js +0 -6
- package/esm/utils/isGlobal.js.map +0 -1
- package/esm/utils/isNodePath.js +0 -4
- package/esm/utils/isNodePath.js.map +0 -1
- package/esm/utils/isRemoved.js +0 -46
- package/esm/utils/isRemoved.js.map +0 -1
- package/esm/utils/isRequire.js +0 -13
- package/esm/utils/isRequire.js.map +0 -1
- package/esm/utils/isTypedNode.js +0 -6
- package/esm/utils/isTypedNode.js.map +0 -1
- package/esm/utils/isUnnecessaryReactCall.js +0 -72
- package/esm/utils/isUnnecessaryReactCall.js.map +0 -1
- package/esm/utils/removeDangerousCode.js +0 -276
- package/esm/utils/removeDangerousCode.js.map +0 -1
- package/esm/utils/replaceImportMetaEnv.js +0 -44
- package/esm/utils/replaceImportMetaEnv.js.map +0 -1
- package/esm/utils/scopeHelpers.js +0 -527
- package/esm/utils/scopeHelpers.js.map +0 -1
- package/esm/utils/traversalCache.js +0 -23
- package/esm/utils/traversalCache.js.map +0 -1
- package/esm/utils/unwrapExpression.js +0 -18
- package/esm/utils/unwrapExpression.js.map +0 -1
- package/esm/utils/unwrapSequence.js +0 -14
- package/esm/utils/unwrapSequence.js.map +0 -1
- package/esm/utils/valueToLiteral.js +0 -59
- package/esm/utils/valueToLiteral.js.map +0 -1
- package/esm/utils/visitors/JSXElementsRemover.js +0 -51
- package/esm/utils/visitors/JSXElementsRemover.js.map +0 -1
- package/lib/babel.js +0 -2
- package/lib/babel.js.map +0 -1
- package/lib/cache.js +0 -308
- package/lib/cache.js.map +0 -1
- package/lib/debug/fileReporter.js +0 -153
- package/lib/debug/fileReporter.js.map +0 -1
- package/lib/evaluators/index.js +0 -20
- package/lib/evaluators/index.js.map +0 -1
- package/lib/index.js +0 -286
- package/lib/index.js.map +0 -1
- package/lib/module.js +0 -552
- package/lib/module.js.map +0 -1
- package/lib/options/buildOptions.js +0 -176
- package/lib/options/buildOptions.js.map +0 -1
- package/lib/options/buildOptions.test.js +0 -141
- package/lib/options/buildOptions.test.js.map +0 -1
- package/lib/options/loadBabelOptions.js +0 -31
- package/lib/options/loadBabelOptions.js.map +0 -1
- package/lib/plugins/babel-transform.js +0 -60
- package/lib/plugins/babel-transform.js.map +0 -1
- package/lib/plugins/collector.js +0 -70
- package/lib/plugins/collector.js.map +0 -1
- package/lib/plugins/dynamic-import.js +0 -61
- package/lib/plugins/dynamic-import.js.map +0 -1
- package/lib/plugins/preeval.js +0 -81
- package/lib/plugins/preeval.js.map +0 -1
- package/lib/plugins/shaker.js +0 -691
- package/lib/plugins/shaker.js.map +0 -1
- package/lib/shaker.js +0 -95
- package/lib/shaker.js.map +0 -1
- package/lib/transform/BaseEntrypoint.js +0 -179
- package/lib/transform/BaseEntrypoint.js.map +0 -1
- package/lib/transform/Entrypoint.helpers.js +0 -279
- package/lib/transform/Entrypoint.helpers.js.map +0 -1
- package/lib/transform/Entrypoint.js +0 -289
- package/lib/transform/Entrypoint.js.map +0 -1
- package/lib/transform/Entrypoint.types.js +0 -2
- package/lib/transform/Entrypoint.types.js.map +0 -1
- package/lib/transform/EvaluatedEntrypoint.js +0 -13
- package/lib/transform/EvaluatedEntrypoint.js.map +0 -1
- package/lib/transform/actions/AbortError.js +0 -16
- package/lib/transform/actions/AbortError.js.map +0 -1
- package/lib/transform/actions/BaseAction.js +0 -150
- package/lib/transform/actions/BaseAction.js.map +0 -1
- package/lib/transform/actions/UnprocessedEntrypointError.js +0 -16
- package/lib/transform/actions/UnprocessedEntrypointError.js.map +0 -1
- package/lib/transform/actions/actionRunner.js +0 -82
- package/lib/transform/actions/actionRunner.js.map +0 -1
- package/lib/transform/actions/types.js +0 -2
- package/lib/transform/actions/types.js.map +0 -1
- package/lib/transform/barrelManifest.js +0 -300
- package/lib/transform/barrelManifest.js.map +0 -1
- package/lib/transform/generators/baseProcessingHandlers.js +0 -27
- package/lib/transform/generators/baseProcessingHandlers.js.map +0 -1
- package/lib/transform/generators/collect.js +0 -66
- package/lib/transform/generators/collect.js.map +0 -1
- package/lib/transform/generators/createStylisPreprocessor.js +0 -372
- package/lib/transform/generators/createStylisPreprocessor.js.map +0 -1
- package/lib/transform/generators/evalFile.js +0 -57
- package/lib/transform/generators/evalFile.js.map +0 -1
- package/lib/transform/generators/explodeReexports.js +0 -71
- package/lib/transform/generators/explodeReexports.js.map +0 -1
- package/lib/transform/generators/extract.js +0 -102
- package/lib/transform/generators/extract.js.map +0 -1
- package/lib/transform/generators/getExports.js +0 -85
- package/lib/transform/generators/getExports.js.map +0 -1
- package/lib/transform/generators/index.js +0 -19
- package/lib/transform/generators/index.js.map +0 -1
- package/lib/transform/generators/processEntrypoint.js +0 -76
- package/lib/transform/generators/processEntrypoint.js.map +0 -1
- package/lib/transform/generators/processImports.js +0 -82
- package/lib/transform/generators/processImports.js.map +0 -1
- package/lib/transform/generators/resolveImports.js +0 -221
- package/lib/transform/generators/resolveImports.js.map +0 -1
- package/lib/transform/generators/rewriteBarrelImports.js +0 -743
- package/lib/transform/generators/rewriteBarrelImports.js.map +0 -1
- package/lib/transform/generators/transform.js +0 -272
- package/lib/transform/generators/transform.js.map +0 -1
- package/lib/transform/generators/workflow.js +0 -100
- package/lib/transform/generators/workflow.js.map +0 -1
- package/lib/transform/helpers/loadWywOptions.js +0 -88
- package/lib/transform/helpers/loadWywOptions.js.map +0 -1
- package/lib/transform/helpers/withDefaultServices.js +0 -31
- package/lib/transform/helpers/withDefaultServices.js.map +0 -1
- package/lib/transform/isStaticallyEvaluatableModule.js +0 -159
- package/lib/transform/isStaticallyEvaluatableModule.js.map +0 -1
- package/lib/transform/rootLog.js +0 -9
- package/lib/transform/rootLog.js.map +0 -1
- package/lib/transform/types.js +0 -8
- package/lib/transform/types.js.map +0 -1
- package/lib/transform.js +0 -160
- package/lib/transform.js.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/lib/utils/EventEmitter.js +0 -61
- package/lib/utils/EventEmitter.js.map +0 -1
- package/lib/utils/ShakerMetadata.js +0 -9
- package/lib/utils/ShakerMetadata.js.map +0 -1
- package/lib/utils/TransformDiagnostics.js +0 -20
- package/lib/utils/TransformDiagnostics.js.map +0 -1
- package/lib/utils/TransformMetadata.js +0 -45
- package/lib/utils/TransformMetadata.js.map +0 -1
- package/lib/utils/addIdentifierToWywPreval.js +0 -75
- package/lib/utils/addIdentifierToWywPreval.js.map +0 -1
- package/lib/utils/collectExportsAndImports.js +0 -1173
- package/lib/utils/collectExportsAndImports.js.map +0 -1
- package/lib/utils/collectTemplateDependencies.js +0 -242
- package/lib/utils/collectTemplateDependencies.js.map +0 -1
- package/lib/utils/createId.js +0 -13
- package/lib/utils/createId.js.map +0 -1
- package/lib/utils/dispose-polyfill.js +0 -9
- package/lib/utils/dispose-polyfill.js.map +0 -1
- package/lib/utils/findIdentifiers.js +0 -73
- package/lib/utils/findIdentifiers.js.map +0 -1
- package/lib/utils/getConstantStringValue.js +0 -66
- package/lib/utils/getConstantStringValue.js.map +0 -1
- package/lib/utils/getFileIdx.js +0 -16
- package/lib/utils/getFileIdx.js.map +0 -1
- package/lib/utils/getMemberExpressionPropertyName.js +0 -18
- package/lib/utils/getMemberExpressionPropertyName.js.map +0 -1
- package/lib/utils/getPluginKey.js +0 -21
- package/lib/utils/getPluginKey.js.map +0 -1
- package/lib/utils/getScope.js +0 -12
- package/lib/utils/getScope.js.map +0 -1
- package/lib/utils/getSource.js +0 -24
- package/lib/utils/getSource.js.map +0 -1
- package/lib/utils/getTagProcessor.js +0 -424
- package/lib/utils/getTagProcessor.js.map +0 -1
- package/lib/utils/getVisitorKeys.js +0 -11
- package/lib/utils/getVisitorKeys.js.map +0 -1
- package/lib/utils/hasWywPreval.js +0 -13
- package/lib/utils/hasWywPreval.js.map +0 -1
- package/lib/utils/importOverrides.js +0 -119
- package/lib/utils/importOverrides.js.map +0 -1
- package/lib/utils/isExports.js +0 -27
- package/lib/utils/isExports.js.map +0 -1
- package/lib/utils/isGlobal.js +0 -13
- package/lib/utils/isGlobal.js.map +0 -1
- package/lib/utils/isNode.js +0 -9
- package/lib/utils/isNode.js.map +0 -1
- package/lib/utils/isNodePath.js +0 -10
- package/lib/utils/isNodePath.js.map +0 -1
- package/lib/utils/isNotNull.js +0 -10
- package/lib/utils/isNotNull.js.map +0 -1
- package/lib/utils/isRemoved.js +0 -52
- package/lib/utils/isRemoved.js.map +0 -1
- package/lib/utils/isRequire.js +0 -18
- package/lib/utils/isRequire.js.map +0 -1
- package/lib/utils/isSerializable.js +0 -19
- package/lib/utils/isSerializable.js.map +0 -1
- package/lib/utils/isTypedNode.js +0 -13
- package/lib/utils/isTypedNode.js.map +0 -1
- package/lib/utils/isUnnecessaryReactCall.js +0 -81
- package/lib/utils/isUnnecessaryReactCall.js.map +0 -1
- package/lib/utils/parseRequest.js +0 -37
- package/lib/utils/parseRequest.js.map +0 -1
- package/lib/utils/peek.js +0 -9
- package/lib/utils/peek.js.map +0 -1
- package/lib/utils/removeDangerousCode.js +0 -284
- package/lib/utils/removeDangerousCode.js.map +0 -1
- package/lib/utils/replaceImportMetaEnv.js +0 -50
- package/lib/utils/replaceImportMetaEnv.js.map +0 -1
- package/lib/utils/scopeHelpers.js +0 -557
- package/lib/utils/scopeHelpers.js.map +0 -1
- package/lib/utils/traversalCache.js +0 -31
- package/lib/utils/traversalCache.js.map +0 -1
- package/lib/utils/unwrapExpression.js +0 -25
- package/lib/utils/unwrapExpression.js.map +0 -1
- package/lib/utils/unwrapSequence.js +0 -20
- package/lib/utils/unwrapSequence.js.map +0 -1
- package/lib/utils/valueToLiteral.js +0 -65
- package/lib/utils/valueToLiteral.js.map +0 -1
- package/lib/utils/visitors/JSXElementsRemover.js +0 -57
- package/lib/utils/visitors/JSXElementsRemover.js.map +0 -1
- package/lib/vm/createVmContext.js +0 -166
- package/lib/vm/createVmContext.js.map +0 -1
- package/lib/vm/process.js +0 -38
- package/lib/vm/process.js.map +0 -1
- package/types/babel.d.ts +0 -2
- package/types/babel.js +0 -2
- package/types/options/buildOptions.d.ts +0 -6
- package/types/options/buildOptions.js +0 -178
- package/types/options/loadBabelOptions.d.ts +0 -3
- package/types/options/loadBabelOptions.js +0 -26
- package/types/plugins/babel-transform.d.ts +0 -4
- package/types/plugins/babel-transform.js +0 -49
- package/types/plugins/collector.d.ts +0 -23
- package/types/plugins/collector.js +0 -62
- package/types/plugins/dynamic-import.d.ts +0 -6
- package/types/plugins/dynamic-import.js +0 -60
- package/types/plugins/preeval.d.ts +0 -16
- package/types/plugins/preeval.js +0 -69
- package/types/plugins/shaker.d.ts +0 -14
- package/types/plugins/shaker.js +0 -724
- package/types/transform/generators/explodeReexports.d.ts +0 -7
- package/types/transform/generators/explodeReexports.js +0 -65
- package/types/utils/addIdentifierToWywPreval.d.ts +0 -4
- package/types/utils/addIdentifierToWywPreval.js +0 -74
- package/types/utils/collectExportsAndImports.d.ts +0 -31
- package/types/utils/collectExportsAndImports.js +0 -1147
- package/types/utils/collectTemplateDependencies.d.ts +0 -17
- package/types/utils/collectTemplateDependencies.js +0 -220
- package/types/utils/createId.d.ts +0 -2
- package/types/utils/createId.js +0 -9
- package/types/utils/findIdentifiers.d.ts +0 -6
- package/types/utils/findIdentifiers.js +0 -67
- package/types/utils/getConstantStringValue.d.ts +0 -2
- package/types/utils/getConstantStringValue.js +0 -94
- package/types/utils/getMemberExpressionPropertyName.d.ts +0 -2
- package/types/utils/getMemberExpressionPropertyName.js +0 -46
- package/types/utils/getScope.d.ts +0 -2
- package/types/utils/getScope.js +0 -10
- package/types/utils/getSource.d.ts +0 -2
- package/types/utils/getSource.js +0 -22
- package/types/utils/getTagProcessor.d.ts +0 -13
- package/types/utils/getTagProcessor.js +0 -411
- package/types/utils/isExports.d.ts +0 -6
- package/types/utils/isExports.js +0 -19
- package/types/utils/isGlobal.d.ts +0 -2
- package/types/utils/isGlobal.js +0 -9
- package/types/utils/isNodePath.d.ts +0 -3
- package/types/utils/isNodePath.js +0 -6
- package/types/utils/isRemoved.d.ts +0 -5
- package/types/utils/isRemoved.js +0 -41
- package/types/utils/isRequire.d.ts +0 -6
- package/types/utils/isRequire.js +0 -14
- package/types/utils/isTypedNode.d.ts +0 -5
- package/types/utils/isTypedNode.js +0 -9
- package/types/utils/isUnnecessaryReactCall.d.ts +0 -3
- package/types/utils/isUnnecessaryReactCall.js +0 -75
- package/types/utils/removeDangerousCode.d.ts +0 -4
- package/types/utils/removeDangerousCode.js +0 -326
- package/types/utils/replaceImportMetaEnv.d.ts +0 -3
- package/types/utils/replaceImportMetaEnv.js +0 -39
- package/types/utils/scopeHelpers.d.ts +0 -12
- package/types/utils/scopeHelpers.js +0 -580
- package/types/utils/traversalCache.d.ts +0 -4
- package/types/utils/traversalCache.js +0 -27
- package/types/utils/unwrapExpression.d.ts +0 -2
- package/types/utils/unwrapExpression.js +0 -57
- package/types/utils/unwrapSequence.d.ts +0 -8
- package/types/utils/unwrapSequence.js +0 -16
- package/types/utils/valueToLiteral.d.ts +0 -3
- package/types/utils/valueToLiteral.js +0 -63
- package/types/utils/visitors/JSXElementsRemover.d.ts +0 -3
- package/types/utils/visitors/JSXElementsRemover.js +0 -51
|
@@ -0,0 +1,1398 @@
|
|
|
1
|
+
import { ValueType } from "@wyw-in-js/shared";
|
|
2
|
+
import { parseOxcProgramCached } from "./parseOxc.js";
|
|
3
|
+
const isNode = (value) => !!value && typeof value === "object" && "type" in value && typeof value.type === "string";
|
|
4
|
+
const getChildren = (node) => {
|
|
5
|
+
const result = [];
|
|
6
|
+
const record = node;
|
|
7
|
+
Object.keys(record).forEach((key) => {
|
|
8
|
+
if (key === "type" || key === "start" || key === "end" || key === "range") {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const value = record[key];
|
|
12
|
+
if (isNode(value)) {
|
|
13
|
+
result.push(value);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
value.forEach((item) => {
|
|
18
|
+
if (isNode(item)) {
|
|
19
|
+
result.push(item);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
const parseOxc = (code, filename) => {
|
|
27
|
+
return parseOxcProgramCached(filename, code, "unambiguous");
|
|
28
|
+
};
|
|
29
|
+
const toSpanKey = (start, end) => `${start}:${end}`;
|
|
30
|
+
const createSpanLookup = (spans) => {
|
|
31
|
+
if (!spans || spans.length === 0) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return new Set(spans.map((span) => toSpanKey(span.start, span.end)));
|
|
35
|
+
};
|
|
36
|
+
const matchesSpanLookup = (node, spanLookup) => !spanLookup || spanLookup.has(toSpanKey(node.start, node.end));
|
|
37
|
+
const createLocationLookup = (code) => {
|
|
38
|
+
const lineStarts = [0];
|
|
39
|
+
for (let idx = 0; idx < code.length; idx += 1) {
|
|
40
|
+
if (code[idx] === "\n") {
|
|
41
|
+
lineStarts.push(idx + 1);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return (offset) => {
|
|
45
|
+
let low = 0;
|
|
46
|
+
let high = lineStarts.length - 1;
|
|
47
|
+
while (low <= high) {
|
|
48
|
+
const mid = Math.floor((low + high) / 2);
|
|
49
|
+
const next = lineStarts[mid + 1] ?? Infinity;
|
|
50
|
+
if (lineStarts[mid] <= offset && offset < next) {
|
|
51
|
+
return {
|
|
52
|
+
column: offset - lineStarts[mid],
|
|
53
|
+
line: mid + 1
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (offset < lineStarts[mid]) {
|
|
57
|
+
high = mid - 1;
|
|
58
|
+
} else {
|
|
59
|
+
low = mid + 1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const lastLine = lineStarts.length - 1;
|
|
63
|
+
return {
|
|
64
|
+
column: Math.max(0, offset - lineStarts[lastLine]),
|
|
65
|
+
line: lastLine + 1
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
const getSourceLocation = (start, end, ctx) => ({
|
|
70
|
+
end: ctx.loc(end),
|
|
71
|
+
filename: ctx.filename,
|
|
72
|
+
identifierName: undefined,
|
|
73
|
+
start: ctx.loc(start)
|
|
74
|
+
});
|
|
75
|
+
const createScope = (parent, node, root = false, functionBoundary = false) => ({
|
|
76
|
+
bindings: new Map(),
|
|
77
|
+
depth: parent ? parent.depth + 1 : 0,
|
|
78
|
+
end: node.end,
|
|
79
|
+
functionBoundary,
|
|
80
|
+
params: new Set(),
|
|
81
|
+
parent,
|
|
82
|
+
root,
|
|
83
|
+
start: node.start
|
|
84
|
+
});
|
|
85
|
+
const normalizeDeclarationKind = (declarationKind) => {
|
|
86
|
+
if (declarationKind === "var") {
|
|
87
|
+
return "var";
|
|
88
|
+
}
|
|
89
|
+
if (declarationKind === "let") {
|
|
90
|
+
return "let";
|
|
91
|
+
}
|
|
92
|
+
return "const";
|
|
93
|
+
};
|
|
94
|
+
const moduleExportName = (node) => node.type === "Literal" ? String(node.value) : node.name;
|
|
95
|
+
const getImportSpecifierInfo = (statement, specifier) => {
|
|
96
|
+
const local = specifier.local?.name;
|
|
97
|
+
if (!local) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
if (specifier.type === "ImportDefaultSpecifier") {
|
|
101
|
+
return {
|
|
102
|
+
imported: "default",
|
|
103
|
+
local
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
107
|
+
return {
|
|
108
|
+
imported: "*",
|
|
109
|
+
local
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if (statement.importKind === "type" || specifier.importKind === "type") {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
imported: moduleExportName(specifier.imported),
|
|
117
|
+
local
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const getDeclarationScope = (scope, declarationKind) => {
|
|
121
|
+
if (declarationKind !== "var") {
|
|
122
|
+
return scope;
|
|
123
|
+
}
|
|
124
|
+
let current = scope;
|
|
125
|
+
while (current && !current.functionBoundary) {
|
|
126
|
+
current = current.parent;
|
|
127
|
+
}
|
|
128
|
+
return current ?? scope;
|
|
129
|
+
};
|
|
130
|
+
const collectBindingNames = (node) => {
|
|
131
|
+
if (node.type === "Identifier") {
|
|
132
|
+
return [node.name];
|
|
133
|
+
}
|
|
134
|
+
if (node.type === "RestElement") {
|
|
135
|
+
return collectBindingNames(node.argument);
|
|
136
|
+
}
|
|
137
|
+
if (node.type === "AssignmentPattern") {
|
|
138
|
+
return collectBindingNames(node.left);
|
|
139
|
+
}
|
|
140
|
+
if (node.type === "ObjectPattern") {
|
|
141
|
+
return node.properties.flatMap((property) => property.type === "RestElement" ? collectBindingNames(property.argument) : collectBindingNames(property.value));
|
|
142
|
+
}
|
|
143
|
+
if (node.type === "ArrayPattern") {
|
|
144
|
+
return node.elements.flatMap((element) => element ? collectBindingNames(element) : []);
|
|
145
|
+
}
|
|
146
|
+
if (node.type === "TSParameterProperty") {
|
|
147
|
+
return collectBindingNames(node.parameter);
|
|
148
|
+
}
|
|
149
|
+
return [];
|
|
150
|
+
};
|
|
151
|
+
const isInTypeContext = (ancestors) => ancestors.some((ancestor) => ancestor.type.startsWith("TS") || ancestor.type.startsWith("JSDoc"));
|
|
152
|
+
const isPropertyOnlyIdentifier = (node, parent) => !!parent && parent.type === "MemberExpression" && parent.property === node && !parent.computed;
|
|
153
|
+
const isObjectPropertyKey = (node, parent) => !!parent && parent.type === "Property" && parent.key === node && !parent.computed && parent.value !== node;
|
|
154
|
+
const isBindingPosition = (node, parent) => {
|
|
155
|
+
if (!parent) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
if (parent.type === "VariableDeclarator" && parent.id === node) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id === node) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if ((parent.type === "ImportSpecifier" || parent.type === "ImportDefaultSpecifier" || parent.type === "ImportNamespaceSpecifier") && "local" in parent && parent.local === node) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
};
|
|
169
|
+
const visit = (node, scope, enter, parent = null, ancestors = []) => {
|
|
170
|
+
const visitNode = (currentNode, currentScope, currentParent) => {
|
|
171
|
+
let nextScope;
|
|
172
|
+
if (currentNode.type === "Program") {
|
|
173
|
+
nextScope = createScope(null, currentNode, true, true);
|
|
174
|
+
} else if (currentNode.type === "BlockStatement" || currentNode.type === "FunctionDeclaration" || currentNode.type === "FunctionExpression" || currentNode.type === "ArrowFunctionExpression") {
|
|
175
|
+
nextScope = createScope(currentScope, currentNode, false, currentNode.type !== "BlockStatement");
|
|
176
|
+
} else if (currentScope) {
|
|
177
|
+
nextScope = currentScope;
|
|
178
|
+
} else {
|
|
179
|
+
nextScope = createScope(null, currentNode, false, true);
|
|
180
|
+
}
|
|
181
|
+
if (currentNode.type === "FunctionDeclaration" || currentNode.type === "FunctionExpression" || currentNode.type === "ArrowFunctionExpression") {
|
|
182
|
+
currentNode.params.forEach((param) => {
|
|
183
|
+
collectBindingNames(param).forEach((name) => {
|
|
184
|
+
nextScope.params.add(name);
|
|
185
|
+
nextScope.bindings.set(name, {
|
|
186
|
+
declaredAt: param.start,
|
|
187
|
+
declaration: null,
|
|
188
|
+
declarator: null,
|
|
189
|
+
functionNode: null,
|
|
190
|
+
isRoot: false,
|
|
191
|
+
kind: "param",
|
|
192
|
+
name,
|
|
193
|
+
scope: nextScope
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
enter(currentNode, nextScope, currentParent, ancestors);
|
|
199
|
+
ancestors.push(currentNode);
|
|
200
|
+
getChildren(currentNode).forEach((child) => visitNode(child, nextScope, currentNode));
|
|
201
|
+
ancestors.pop();
|
|
202
|
+
};
|
|
203
|
+
visitNode(node, scope, parent);
|
|
204
|
+
};
|
|
205
|
+
const analyzeProgram = (program, { collectTargetExpressions = false, collectTemplateLiterals = false, expressionSpanLookup = null, templateSpanLookup = null } = {}) => {
|
|
206
|
+
const bindings = new Map();
|
|
207
|
+
const usedNames = new Set();
|
|
208
|
+
const templateLiterals = [];
|
|
209
|
+
const targetExpressions = [];
|
|
210
|
+
const addBinding = (scope, binding) => {
|
|
211
|
+
scope.bindings.set(binding.name, binding);
|
|
212
|
+
const existing = bindings.get(binding.name) ?? [];
|
|
213
|
+
existing.push(binding);
|
|
214
|
+
bindings.set(binding.name, existing);
|
|
215
|
+
};
|
|
216
|
+
const collectTargets = (node, ancestors) => {
|
|
217
|
+
if (collectTemplateLiterals && node.type === "TemplateLiteral" && node.expressions.length > 0 && !ancestors.some((ancestor) => ancestor.type === "TemplateLiteral") && matchesSpanLookup(node, templateSpanLookup)) {
|
|
218
|
+
templateLiterals.push(node);
|
|
219
|
+
}
|
|
220
|
+
if (collectTargetExpressions && expressionSpanLookup && matchesSpanLookup(node, expressionSpanLookup)) {
|
|
221
|
+
targetExpressions.push(node);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
visit(program, null, (node, scope, _parent, ancestors) => {
|
|
225
|
+
collectTargets(node, ancestors);
|
|
226
|
+
if (node.type === "Identifier") {
|
|
227
|
+
usedNames.add(node.name);
|
|
228
|
+
}
|
|
229
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
230
|
+
node.params.forEach((param) => {
|
|
231
|
+
collectBindingNames(param).forEach((name) => {
|
|
232
|
+
const binding = scope.bindings.get(name);
|
|
233
|
+
if (binding) {
|
|
234
|
+
addBinding(scope, binding);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
if (node.type !== "FunctionDeclaration") {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (node.type === "ImportDeclaration") {
|
|
243
|
+
const source = node.source.value;
|
|
244
|
+
node.specifiers.forEach((specifier) => {
|
|
245
|
+
const importInfo = getImportSpecifierInfo(node, specifier);
|
|
246
|
+
if (!importInfo) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
addBinding(scope, {
|
|
250
|
+
declaredAt: specifier.start,
|
|
251
|
+
declaration: null,
|
|
252
|
+
declarator: null,
|
|
253
|
+
functionNode: null,
|
|
254
|
+
imported: importInfo.imported,
|
|
255
|
+
importedFrom: source,
|
|
256
|
+
isRoot: scope.root,
|
|
257
|
+
kind: "import",
|
|
258
|
+
name: importInfo.local,
|
|
259
|
+
scope
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (node.type !== "VariableDeclaration") {
|
|
265
|
+
if (node.type === "FunctionDeclaration" && node.id) {
|
|
266
|
+
const declarationScope = scope.parent ?? scope;
|
|
267
|
+
const binding = {
|
|
268
|
+
declaredAt: node.start,
|
|
269
|
+
declaration: null,
|
|
270
|
+
declarator: null,
|
|
271
|
+
functionNode: node,
|
|
272
|
+
isRoot: declarationScope.root,
|
|
273
|
+
kind: "function",
|
|
274
|
+
name: node.id.name,
|
|
275
|
+
scope: declarationScope
|
|
276
|
+
};
|
|
277
|
+
addBinding(declarationScope, binding);
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
node.declarations.forEach((declarator) => {
|
|
282
|
+
collectBindingNames(declarator.id).forEach((name) => {
|
|
283
|
+
const declarationKind = normalizeDeclarationKind(node.kind);
|
|
284
|
+
const declarationScope = getDeclarationScope(scope, declarationKind);
|
|
285
|
+
const binding = {
|
|
286
|
+
declarationKind,
|
|
287
|
+
declaredAt: declarator.start,
|
|
288
|
+
declaration: node,
|
|
289
|
+
declarator,
|
|
290
|
+
functionNode: null,
|
|
291
|
+
isRoot: declarationScope.root,
|
|
292
|
+
kind: "variable",
|
|
293
|
+
name,
|
|
294
|
+
scope: declarationScope
|
|
295
|
+
};
|
|
296
|
+
addBinding(declarationScope, binding);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
return {
|
|
301
|
+
bindingsByName: bindings,
|
|
302
|
+
rootMutationsByBinding: collectRootMutations(program),
|
|
303
|
+
targetExpressions: targetExpressions.sort((a, b) => a.start - b.start),
|
|
304
|
+
templateLiterals,
|
|
305
|
+
usedNames
|
|
306
|
+
};
|
|
307
|
+
};
|
|
308
|
+
const resolveBindingAt = (ctx, name, referenceStart) => {
|
|
309
|
+
const cachedBindings = ctx.bindingResolutionCache.get(name);
|
|
310
|
+
if (cachedBindings?.has(referenceStart)) {
|
|
311
|
+
return cachedBindings.get(referenceStart) ?? undefined;
|
|
312
|
+
}
|
|
313
|
+
const bindings = ctx.bindingsByName.get(name);
|
|
314
|
+
const bindingCache = cachedBindings ?? new Map();
|
|
315
|
+
if (!cachedBindings) {
|
|
316
|
+
ctx.bindingResolutionCache.set(name, bindingCache);
|
|
317
|
+
}
|
|
318
|
+
if (!bindings || bindings.length === 0) {
|
|
319
|
+
bindingCache.set(referenceStart, null);
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
let binding;
|
|
323
|
+
bindings.forEach((candidate) => {
|
|
324
|
+
if (candidate.scope.start > referenceStart || referenceStart >= candidate.scope.end) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (!binding || candidate.scope.depth > binding.scope.depth || candidate.scope.depth === binding.scope.depth && candidate.declaredAt > binding.declaredAt) {
|
|
328
|
+
binding = candidate;
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
bindingCache.set(referenceStart, binding ?? null);
|
|
332
|
+
return binding;
|
|
333
|
+
};
|
|
334
|
+
const collectRootMutations = (program) => {
|
|
335
|
+
const mutations = new Map();
|
|
336
|
+
const getRootMutationTarget = (node) => {
|
|
337
|
+
if (node.type === "Identifier") {
|
|
338
|
+
return {
|
|
339
|
+
binding: node.name,
|
|
340
|
+
path: []
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
if (node.type !== "MemberExpression") {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
const parent = getRootMutationTarget(node.object);
|
|
347
|
+
if (!parent) {
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
let key = null;
|
|
351
|
+
if (node.computed && node.property.type === "Literal" && (typeof node.property.value === "string" || typeof node.property.value === "number")) {
|
|
352
|
+
key = node.property.value;
|
|
353
|
+
} else if (!node.computed && node.property.type === "Identifier") {
|
|
354
|
+
key = node.property.name;
|
|
355
|
+
}
|
|
356
|
+
if (key === null) {
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
binding: parent.binding,
|
|
361
|
+
path: [...parent.path, key]
|
|
362
|
+
};
|
|
363
|
+
};
|
|
364
|
+
program.body.forEach((statement) => {
|
|
365
|
+
if (statement.type !== "ExpressionStatement") {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const { expression } = statement;
|
|
369
|
+
if (expression.type === "AssignmentExpression") {
|
|
370
|
+
const target = getRootMutationTarget(expression.left);
|
|
371
|
+
if (!target || target.path.length === 0) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const bucket = mutations.get(target.binding) ?? [];
|
|
375
|
+
bucket.push(expression);
|
|
376
|
+
mutations.set(target.binding, bucket);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (expression.type === "UpdateExpression") {
|
|
380
|
+
const target = getRootMutationTarget(expression.argument);
|
|
381
|
+
if (!target || target.path.length === 0) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const bucket = mutations.get(target.binding) ?? [];
|
|
385
|
+
bucket.push(expression);
|
|
386
|
+
mutations.set(target.binding, bucket);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
return mutations;
|
|
390
|
+
};
|
|
391
|
+
const hasLocalBinding = (scope, name) => {
|
|
392
|
+
let current = scope;
|
|
393
|
+
while (current) {
|
|
394
|
+
if (current.bindings.has(name)) {
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
current = current.parent;
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
};
|
|
401
|
+
const hasLocalBindingCached = (scope, name, cache) => {
|
|
402
|
+
const scopeCache = cache.get(scope);
|
|
403
|
+
if (scopeCache?.has(name)) {
|
|
404
|
+
return scopeCache.get(name);
|
|
405
|
+
}
|
|
406
|
+
const result = hasLocalBinding(scope, name);
|
|
407
|
+
const nextScopeCache = scopeCache ?? new Map();
|
|
408
|
+
nextScopeCache.set(name, result);
|
|
409
|
+
if (!scopeCache) {
|
|
410
|
+
cache.set(scope, nextScopeCache);
|
|
411
|
+
}
|
|
412
|
+
return result;
|
|
413
|
+
};
|
|
414
|
+
const findReferences = (node, referenceCache) => {
|
|
415
|
+
const cachedReferences = referenceCache?.get(node);
|
|
416
|
+
if (cachedReferences) {
|
|
417
|
+
return cachedReferences;
|
|
418
|
+
}
|
|
419
|
+
const refs = new Map();
|
|
420
|
+
const localBindingCache = new WeakMap();
|
|
421
|
+
visit(node, null, (current, scope, parent, ancestors) => {
|
|
422
|
+
if (current.type !== "Identifier" || isInTypeContext(ancestors) || isBindingPosition(current, parent) || isPropertyOnlyIdentifier(current, parent) || isObjectPropertyKey(current, parent) || hasLocalBindingCached(scope, current.name, localBindingCache)) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
const key = `${current.start}:${current.end}:${current.name}`;
|
|
426
|
+
refs.set(key, {
|
|
427
|
+
end: current.end,
|
|
428
|
+
name: current.name,
|
|
429
|
+
start: current.start
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
const resolvedReferences = [...refs.values()];
|
|
433
|
+
referenceCache?.set(node, resolvedReferences);
|
|
434
|
+
return resolvedReferences;
|
|
435
|
+
};
|
|
436
|
+
const isBindingDeclaredWithin = (binding, container) => container.start <= binding.declaredAt && binding.declaredAt < container.end;
|
|
437
|
+
const literalCode = (value) => {
|
|
438
|
+
if (typeof value === "number") {
|
|
439
|
+
return Number.isFinite(value) ? JSON.stringify(value) : null;
|
|
440
|
+
}
|
|
441
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
442
|
+
return JSON.stringify(value);
|
|
443
|
+
}
|
|
444
|
+
if (Array.isArray(value)) {
|
|
445
|
+
return JSON.stringify(value);
|
|
446
|
+
}
|
|
447
|
+
if (typeof value === "object" && value !== null) {
|
|
448
|
+
return `(${JSON.stringify(value)})`;
|
|
449
|
+
}
|
|
450
|
+
return null;
|
|
451
|
+
};
|
|
452
|
+
const isStaticSerializableValue = (value) => literalCode(value) !== null;
|
|
453
|
+
const cloneStaticValue = (value) => {
|
|
454
|
+
if (Array.isArray(value)) {
|
|
455
|
+
return value.map((item) => cloneStaticValue(item));
|
|
456
|
+
}
|
|
457
|
+
if (typeof value === "object" && value !== null) {
|
|
458
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cloneStaticValue(item)]));
|
|
459
|
+
}
|
|
460
|
+
return value;
|
|
461
|
+
};
|
|
462
|
+
const getObjectMember = (objectValue, property) => {
|
|
463
|
+
if (objectValue === null || objectValue === undefined || typeof objectValue !== "object" && typeof objectValue !== "string" && typeof objectValue !== "number" && typeof objectValue !== "boolean") {
|
|
464
|
+
return undefined;
|
|
465
|
+
}
|
|
466
|
+
return objectValue[property];
|
|
467
|
+
};
|
|
468
|
+
const oxcStaticCallableValue = Symbol("wyw.oxc.staticCallableValue");
|
|
469
|
+
const isOxcStaticCallableValue = (value) => typeof value === "object" && value !== null && oxcStaticCallableValue in value;
|
|
470
|
+
const unwrapOxcStaticCallableValue = (value) => isOxcStaticCallableValue(value) ? value[oxcStaticCallableValue] : value;
|
|
471
|
+
export const createOxcStaticCallableValue = (value) => ({ [oxcStaticCallableValue]: value });
|
|
472
|
+
const assignPatternValue = (pattern, value, ctx, env, stack) => {
|
|
473
|
+
if (pattern.type === "Identifier") {
|
|
474
|
+
env.set(pattern.name, value);
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
if (pattern.type === "AssignmentPattern") {
|
|
478
|
+
return assignPatternValue(pattern.left, value === undefined ? evaluateStatic(pattern.right, ctx, env, stack) : value, ctx, env, stack);
|
|
479
|
+
}
|
|
480
|
+
if (pattern.type === "ObjectPattern") {
|
|
481
|
+
if (typeof value !== "object" || value === null) {
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
return pattern.properties.every((property) => {
|
|
485
|
+
if (property.type === "RestElement") {
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
let key;
|
|
489
|
+
if (property.computed) {
|
|
490
|
+
key = evaluateStatic(property.key, ctx, env, stack);
|
|
491
|
+
} else if (property.key.type === "Identifier") {
|
|
492
|
+
key = property.key.name;
|
|
493
|
+
} else if (property.key.type === "Literal") {
|
|
494
|
+
key = property.key.value;
|
|
495
|
+
}
|
|
496
|
+
if (key === undefined || key === null) {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
return assignPatternValue(property.value, getObjectMember(value, key), ctx, env, stack);
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
if (pattern.type === "ArrayPattern") {
|
|
503
|
+
if (!Array.isArray(value)) {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
return pattern.elements.every((element, index) => element ? assignPatternValue(element, value[index], ctx, env, stack) : true);
|
|
507
|
+
}
|
|
508
|
+
return false;
|
|
509
|
+
};
|
|
510
|
+
const applyRootMutation = (bindingName, baseValue, mutation, ctx, env, stack) => {
|
|
511
|
+
const resolvePath = (node) => {
|
|
512
|
+
if (node.type === "Identifier") {
|
|
513
|
+
return node.name === bindingName ? { path: [] } : null;
|
|
514
|
+
}
|
|
515
|
+
if (node.type !== "MemberExpression") {
|
|
516
|
+
return null;
|
|
517
|
+
}
|
|
518
|
+
const parent = resolvePath(node.object);
|
|
519
|
+
if (!parent) {
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
let key;
|
|
523
|
+
if (node.computed) {
|
|
524
|
+
key = evaluateStatic(node.property, ctx, env, stack);
|
|
525
|
+
} else if (node.property.type === "Identifier") {
|
|
526
|
+
key = node.property.name;
|
|
527
|
+
}
|
|
528
|
+
if (key === undefined || key === null || typeof key !== "string" && typeof key !== "number") {
|
|
529
|
+
return null;
|
|
530
|
+
}
|
|
531
|
+
return { path: [...parent.path, key] };
|
|
532
|
+
};
|
|
533
|
+
const pathInfo = resolvePath(mutation.type === "AssignmentExpression" ? mutation.left : mutation.argument);
|
|
534
|
+
if (!pathInfo) {
|
|
535
|
+
return undefined;
|
|
536
|
+
}
|
|
537
|
+
const cloned = cloneStaticValue(baseValue);
|
|
538
|
+
if (pathInfo.path.length === 0) {
|
|
539
|
+
if (mutation.type !== "AssignmentExpression") {
|
|
540
|
+
return undefined;
|
|
541
|
+
}
|
|
542
|
+
return evaluateStatic(mutation.right, ctx, env, stack);
|
|
543
|
+
}
|
|
544
|
+
let target = cloned;
|
|
545
|
+
for (let idx = 0; idx < pathInfo.path.length - 1; idx += 1) {
|
|
546
|
+
const key = pathInfo.path[idx];
|
|
547
|
+
const next = target?.[key];
|
|
548
|
+
if (typeof next !== "object" || next === null) {
|
|
549
|
+
return undefined;
|
|
550
|
+
}
|
|
551
|
+
target = next;
|
|
552
|
+
}
|
|
553
|
+
const lastKey = pathInfo.path[pathInfo.path.length - 1];
|
|
554
|
+
if (mutation.type === "AssignmentExpression") {
|
|
555
|
+
const nextValue = evaluateStatic(mutation.right, ctx, env, stack);
|
|
556
|
+
if (nextValue === undefined) {
|
|
557
|
+
return undefined;
|
|
558
|
+
}
|
|
559
|
+
target[lastKey] = nextValue;
|
|
560
|
+
return cloned;
|
|
561
|
+
}
|
|
562
|
+
const currentValue = target[lastKey];
|
|
563
|
+
if (typeof currentValue !== "number") {
|
|
564
|
+
return undefined;
|
|
565
|
+
}
|
|
566
|
+
target[lastKey] = mutation.operator === "++" ? currentValue + 1 : currentValue - 1;
|
|
567
|
+
return cloned;
|
|
568
|
+
};
|
|
569
|
+
const evaluateFunctionCall = (fn, args, ctx, env, stack) => {
|
|
570
|
+
if (fn.async || !fn.body) {
|
|
571
|
+
return undefined;
|
|
572
|
+
}
|
|
573
|
+
const localEnv = new Map(env);
|
|
574
|
+
for (let idx = 0; idx < fn.params.length; idx += 1) {
|
|
575
|
+
if (!assignPatternValue(fn.params[idx], args[idx], ctx, localEnv, stack)) {
|
|
576
|
+
return undefined;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
if (fn.body.type !== "BlockStatement") {
|
|
580
|
+
return evaluateStatic(fn.body, ctx, localEnv, stack);
|
|
581
|
+
}
|
|
582
|
+
for (const statement of fn.body.body) {
|
|
583
|
+
if (statement.type === "VariableDeclaration") {
|
|
584
|
+
for (const declarator of statement.declarations) {
|
|
585
|
+
const value = declarator.init ? evaluateStatic(declarator.init, ctx, localEnv, stack) : undefined;
|
|
586
|
+
if (!assignPatternValue(declarator.id, value, ctx, localEnv, stack)) {
|
|
587
|
+
return undefined;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
if (statement.type === "ReturnStatement") {
|
|
593
|
+
if (!statement.argument) {
|
|
594
|
+
return undefined;
|
|
595
|
+
}
|
|
596
|
+
return evaluateStatic(statement.argument, ctx, localEnv, stack);
|
|
597
|
+
}
|
|
598
|
+
return undefined;
|
|
599
|
+
}
|
|
600
|
+
return undefined;
|
|
601
|
+
};
|
|
602
|
+
const getConstantReplacement = (binding, ctx) => {
|
|
603
|
+
const init = binding?.declarator?.init;
|
|
604
|
+
if (!init) {
|
|
605
|
+
return null;
|
|
606
|
+
}
|
|
607
|
+
if (init.type === "Literal") {
|
|
608
|
+
return literalCode(init.value);
|
|
609
|
+
}
|
|
610
|
+
if (init.type === "ObjectExpression" && binding?.isRoot && binding.declarator?.id.type === "Identifier") {
|
|
611
|
+
const evaluated = evaluateStatic(binding.declarator.id, ctx);
|
|
612
|
+
return literalCode(evaluated);
|
|
613
|
+
}
|
|
614
|
+
return null;
|
|
615
|
+
};
|
|
616
|
+
const replaceIdentifierReferences = (expression, replacements, code) => {
|
|
617
|
+
const localReplacements = [];
|
|
618
|
+
const ancestors = [];
|
|
619
|
+
const walk = (current, parent) => {
|
|
620
|
+
if (current.type === "Identifier" && replacements.has(current.name) && !isInTypeContext(ancestors) && !isBindingPosition(current, parent) && !isPropertyOnlyIdentifier(current, parent) && !isObjectPropertyKey(current, parent)) {
|
|
621
|
+
const replacement = replacements.get(current.name);
|
|
622
|
+
// Shorthand property `{ width }` → `{ width: 500 }` when the
|
|
623
|
+
// identifier is the value side of a shorthand ObjectProperty.
|
|
624
|
+
const isShorthandValue = !!parent && parent.type === "Property" && parent.shorthand && parent.value === current;
|
|
625
|
+
localReplacements.push({
|
|
626
|
+
start: isShorthandValue ? parent.start : current.start,
|
|
627
|
+
end: current.end,
|
|
628
|
+
value: isShorthandValue ? `${current.name}: ${replacement}` : replacement
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
ancestors.push(current);
|
|
632
|
+
getChildren(current).forEach((child) => walk(child, current));
|
|
633
|
+
ancestors.pop();
|
|
634
|
+
};
|
|
635
|
+
walk(expression, null);
|
|
636
|
+
let result = code.slice(expression.start, expression.end);
|
|
637
|
+
localReplacements.sort((a, b) => b.start - a.start).forEach((replacement) => {
|
|
638
|
+
const start = replacement.start - expression.start;
|
|
639
|
+
const end = replacement.end - expression.start;
|
|
640
|
+
result = result.slice(0, start) + replacement.value + result.slice(end);
|
|
641
|
+
});
|
|
642
|
+
return result;
|
|
643
|
+
};
|
|
644
|
+
const applyExpressionReplacements = (expression, replacements, code) => {
|
|
645
|
+
let result = code.slice(expression.start, expression.end);
|
|
646
|
+
replacements.sort((a, b) => b.start - a.start).forEach((replacement) => {
|
|
647
|
+
const start = replacement.start - expression.start;
|
|
648
|
+
const end = replacement.end - expression.start;
|
|
649
|
+
result = result.slice(0, start) + replacement.value + result.slice(end);
|
|
650
|
+
});
|
|
651
|
+
return result;
|
|
652
|
+
};
|
|
653
|
+
const staticImportAliasPart = (value) => value.replace(/[^A-Za-z0-9_$]/g, "_") || "value";
|
|
654
|
+
const allocateStaticImportAlias = (binding, imported, ctx) => {
|
|
655
|
+
const key = `${binding.importedFrom ?? ""}\0${binding.name}\0${imported}`;
|
|
656
|
+
const existing = ctx.staticImportAliases.get(key);
|
|
657
|
+
if (existing) {
|
|
658
|
+
return existing;
|
|
659
|
+
}
|
|
660
|
+
const namespacePart = staticImportAliasPart(binding.name);
|
|
661
|
+
const importedPart = staticImportAliasPart(imported);
|
|
662
|
+
let alias = `_wyw_static_${namespacePart}_${importedPart}`;
|
|
663
|
+
let idx = 1;
|
|
664
|
+
while (ctx.usedNames.has(alias)) {
|
|
665
|
+
idx += 1;
|
|
666
|
+
alias = `_wyw_static_${namespacePart}_${importedPart}_${idx}`;
|
|
667
|
+
}
|
|
668
|
+
ctx.usedNames.add(alias);
|
|
669
|
+
ctx.staticImportAliases.set(key, alias);
|
|
670
|
+
return alias;
|
|
671
|
+
};
|
|
672
|
+
const staticMemberPropertyName = (expression) => {
|
|
673
|
+
if (!expression.computed && expression.property.type === "Identifier") {
|
|
674
|
+
return expression.property.name;
|
|
675
|
+
}
|
|
676
|
+
if (expression.computed && expression.property.type === "Literal" && typeof expression.property.value === "string") {
|
|
677
|
+
return expression.property.value;
|
|
678
|
+
}
|
|
679
|
+
return null;
|
|
680
|
+
};
|
|
681
|
+
const collectStaticNamespaceMemberReferences = (expression, ctx) => {
|
|
682
|
+
const coveredReferenceStarts = new Set();
|
|
683
|
+
const imports = new Map();
|
|
684
|
+
const replacements = [];
|
|
685
|
+
const walk = (node) => {
|
|
686
|
+
if (node.type === "MemberExpression" && node.object.type === "Identifier") {
|
|
687
|
+
const binding = resolveBindingAt(ctx, node.object.name, node.object.start);
|
|
688
|
+
const imported = staticMemberPropertyName(node);
|
|
689
|
+
if (binding?.importedFrom && binding.imported === "*" && imported !== null) {
|
|
690
|
+
const alias = allocateStaticImportAlias(binding, imported, ctx);
|
|
691
|
+
imports.set(`${binding.importedFrom}\0${imported}\0${alias}`, {
|
|
692
|
+
imported,
|
|
693
|
+
importLocal: binding.name,
|
|
694
|
+
local: alias,
|
|
695
|
+
source: binding.importedFrom
|
|
696
|
+
});
|
|
697
|
+
replacements.push({
|
|
698
|
+
end: node.end,
|
|
699
|
+
start: node.start,
|
|
700
|
+
value: alias
|
|
701
|
+
});
|
|
702
|
+
coveredReferenceStarts.add(node.object.start);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
getChildren(node).forEach(walk);
|
|
706
|
+
};
|
|
707
|
+
walk(expression);
|
|
708
|
+
return {
|
|
709
|
+
coveredReferenceStarts,
|
|
710
|
+
imports: [...imports.values()],
|
|
711
|
+
replacements
|
|
712
|
+
};
|
|
713
|
+
};
|
|
714
|
+
const isProcessEnvMember = (node) => {
|
|
715
|
+
if (node.type !== "MemberExpression" || node.computed) {
|
|
716
|
+
return false;
|
|
717
|
+
}
|
|
718
|
+
if (node.property.type !== "Identifier" || node.property.name !== "env") {
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
return node.object.type === "Identifier" && node.object.name === "process";
|
|
722
|
+
};
|
|
723
|
+
const evaluateBinary = (expression, ctx, env = new Map(), stack = []) => {
|
|
724
|
+
if (expression.type !== "BinaryExpression") {
|
|
725
|
+
return undefined;
|
|
726
|
+
}
|
|
727
|
+
const left = evaluateStatic(expression.left, ctx, env, stack);
|
|
728
|
+
const right = evaluateStatic(expression.right, ctx, env, stack);
|
|
729
|
+
if (left === undefined || right === undefined) {
|
|
730
|
+
return undefined;
|
|
731
|
+
}
|
|
732
|
+
if (expression.operator === "+") {
|
|
733
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
734
|
+
return left + right;
|
|
735
|
+
}
|
|
736
|
+
if ((typeof left === "string" || typeof left === "number") && (typeof right === "string" || typeof right === "number")) {
|
|
737
|
+
return `${left}${right}`;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
741
|
+
switch (expression.operator) {
|
|
742
|
+
case "-": return left - right;
|
|
743
|
+
case "*": return left * right;
|
|
744
|
+
case "/": return left / right;
|
|
745
|
+
case "%": return left % right;
|
|
746
|
+
case "**": return left ** right;
|
|
747
|
+
default: break;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return undefined;
|
|
751
|
+
};
|
|
752
|
+
const evaluateStatic = (expression, ctx, env = new Map(), stack = []) => {
|
|
753
|
+
if (expression.type === "TSAsExpression" || expression.type === "TSSatisfiesExpression" || expression.type === "TSNonNullExpression" || expression.type === "TSInstantiationExpression" || expression.type === "TSTypeAssertion" || expression.type === "ParenthesizedExpression") {
|
|
754
|
+
return evaluateStatic(expression.expression, ctx, env, stack);
|
|
755
|
+
}
|
|
756
|
+
if (expression.type === "Literal") {
|
|
757
|
+
return expression.value;
|
|
758
|
+
}
|
|
759
|
+
if (expression.type === "UnaryExpression") {
|
|
760
|
+
if (expression.operator === "typeof") {
|
|
761
|
+
const argIsProcessEnvAccess = expression.argument.type === "MemberExpression" && isProcessEnvMember(expression.argument.object);
|
|
762
|
+
const arg = evaluateStatic(expression.argument, ctx, env, stack);
|
|
763
|
+
if (arg === undefined) {
|
|
764
|
+
return argIsProcessEnvAccess ? "undefined" : undefined;
|
|
765
|
+
}
|
|
766
|
+
return typeof arg;
|
|
767
|
+
}
|
|
768
|
+
const arg = evaluateStatic(expression.argument, ctx, env, stack);
|
|
769
|
+
if (arg === undefined) {
|
|
770
|
+
return undefined;
|
|
771
|
+
}
|
|
772
|
+
switch (expression.operator) {
|
|
773
|
+
case "-": return typeof arg === "number" ? -arg : undefined;
|
|
774
|
+
case "+": return typeof arg === "number" ? +arg : undefined;
|
|
775
|
+
case "!": return !arg;
|
|
776
|
+
case "~": return typeof arg === "number" ? ~arg : undefined;
|
|
777
|
+
case "void": return undefined;
|
|
778
|
+
default: return undefined;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
if (expression.type === "LogicalExpression") {
|
|
782
|
+
const left = evaluateStatic(expression.left, ctx, env, stack);
|
|
783
|
+
// process.env.X access is the only source we trust as "deterministically
|
|
784
|
+
// undefined" — it's a build-time lookup we control. For everything else,
|
|
785
|
+
// undefined means "couldn't evaluate" and we must bail to avoid inlining
|
|
786
|
+
// a wrong fallback when the runtime value isn't actually nullish.
|
|
787
|
+
const leftIsProcessEnvAccess = expression.left.type === "MemberExpression" && isProcessEnvMember(expression.left.object);
|
|
788
|
+
if (left === undefined && !leftIsProcessEnvAccess) {
|
|
789
|
+
return undefined;
|
|
790
|
+
}
|
|
791
|
+
if (expression.operator === "||") {
|
|
792
|
+
return left || evaluateStatic(expression.right, ctx, env, stack);
|
|
793
|
+
}
|
|
794
|
+
if (expression.operator === "??") {
|
|
795
|
+
return left ?? evaluateStatic(expression.right, ctx, env, stack);
|
|
796
|
+
}
|
|
797
|
+
if (expression.operator === "&&") {
|
|
798
|
+
return left && evaluateStatic(expression.right, ctx, env, stack);
|
|
799
|
+
}
|
|
800
|
+
return undefined;
|
|
801
|
+
}
|
|
802
|
+
if (expression.type === "ConditionalExpression") {
|
|
803
|
+
const test = evaluateStatic(expression.test, ctx, env, stack);
|
|
804
|
+
if (test === undefined) {
|
|
805
|
+
return undefined;
|
|
806
|
+
}
|
|
807
|
+
return evaluateStatic(test ? expression.consequent : expression.alternate, ctx, env, stack);
|
|
808
|
+
}
|
|
809
|
+
if (expression.type === "TemplateLiteral") {
|
|
810
|
+
let result = "";
|
|
811
|
+
for (let idx = 0; idx < expression.quasis.length; idx += 1) {
|
|
812
|
+
result += expression.quasis[idx]?.value.cooked ?? "";
|
|
813
|
+
const nextExpression = expression.expressions[idx];
|
|
814
|
+
if (!nextExpression) {
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
const value = evaluateStatic(nextExpression, ctx, env, stack);
|
|
818
|
+
if (value === undefined || typeof value !== "string" && typeof value !== "number") {
|
|
819
|
+
return undefined;
|
|
820
|
+
}
|
|
821
|
+
result += String(value);
|
|
822
|
+
}
|
|
823
|
+
return result;
|
|
824
|
+
}
|
|
825
|
+
if (expression.type === "Identifier") {
|
|
826
|
+
if (env.has(expression.name)) {
|
|
827
|
+
return unwrapOxcStaticCallableValue(env.get(expression.name));
|
|
828
|
+
}
|
|
829
|
+
const binding = resolveBindingAt(ctx, expression.name, expression.start);
|
|
830
|
+
if (!binding || binding.importedFrom) {
|
|
831
|
+
return undefined;
|
|
832
|
+
}
|
|
833
|
+
if (binding.kind === "param") {
|
|
834
|
+
return undefined;
|
|
835
|
+
}
|
|
836
|
+
if (stack.includes(binding.name)) {
|
|
837
|
+
return undefined;
|
|
838
|
+
}
|
|
839
|
+
let value;
|
|
840
|
+
const { declarator } = binding;
|
|
841
|
+
const init = declarator?.init;
|
|
842
|
+
if (init) {
|
|
843
|
+
if (declarator.id.type !== "Identifier") {
|
|
844
|
+
return undefined;
|
|
845
|
+
}
|
|
846
|
+
value = evaluateStatic(init, ctx, env, [...stack, binding.name]);
|
|
847
|
+
} else if (binding.functionNode) {
|
|
848
|
+
value = binding.functionNode;
|
|
849
|
+
}
|
|
850
|
+
if (value !== undefined && binding.isRoot && typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
851
|
+
const mutations = ctx.rootMutationsByBinding.get(binding.name) ?? [];
|
|
852
|
+
let nextValue = cloneStaticValue(value);
|
|
853
|
+
for (const mutation of mutations) {
|
|
854
|
+
if (mutation.start >= ctx.currentExpressionStart) {
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
const applied = applyRootMutation(binding.name, nextValue, mutation, ctx, env, [...stack, binding.name]);
|
|
858
|
+
if (applied === undefined) {
|
|
859
|
+
return undefined;
|
|
860
|
+
}
|
|
861
|
+
nextValue = applied;
|
|
862
|
+
}
|
|
863
|
+
return nextValue;
|
|
864
|
+
}
|
|
865
|
+
return value;
|
|
866
|
+
}
|
|
867
|
+
if (expression.type === "ObjectExpression") {
|
|
868
|
+
const result = {};
|
|
869
|
+
for (const property of expression.properties) {
|
|
870
|
+
if (property.type === "SpreadElement") {
|
|
871
|
+
const spreadValue = evaluateStatic(property.argument, ctx, env, stack);
|
|
872
|
+
if (typeof spreadValue !== "object" || spreadValue === null) {
|
|
873
|
+
return undefined;
|
|
874
|
+
}
|
|
875
|
+
Object.assign(result, spreadValue);
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
let key;
|
|
879
|
+
if (property.computed) {
|
|
880
|
+
key = evaluateStatic(property.key, ctx, env, stack);
|
|
881
|
+
} else if (property.key.type === "Identifier") {
|
|
882
|
+
key = property.key.name;
|
|
883
|
+
} else if (property.key.type === "Literal") {
|
|
884
|
+
key = property.key.value;
|
|
885
|
+
}
|
|
886
|
+
if (key === undefined || key === null || typeof key !== "string" && typeof key !== "number") {
|
|
887
|
+
return undefined;
|
|
888
|
+
}
|
|
889
|
+
const value = evaluateStatic(property.value, ctx, env, stack);
|
|
890
|
+
if (value === undefined) {
|
|
891
|
+
return undefined;
|
|
892
|
+
}
|
|
893
|
+
result[key] = value;
|
|
894
|
+
}
|
|
895
|
+
return result;
|
|
896
|
+
}
|
|
897
|
+
if (expression.type === "ArrayExpression") {
|
|
898
|
+
const result = [];
|
|
899
|
+
for (const element of expression.elements) {
|
|
900
|
+
if (!element) {
|
|
901
|
+
return undefined;
|
|
902
|
+
}
|
|
903
|
+
if (element.type === "SpreadElement") {
|
|
904
|
+
const spreadValue = evaluateStatic(element.argument, ctx, env, stack);
|
|
905
|
+
if (!Array.isArray(spreadValue)) {
|
|
906
|
+
return undefined;
|
|
907
|
+
}
|
|
908
|
+
result.push(...spreadValue);
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
const value = evaluateStatic(element, ctx, env, stack);
|
|
912
|
+
if (value === undefined) {
|
|
913
|
+
return undefined;
|
|
914
|
+
}
|
|
915
|
+
result.push(value);
|
|
916
|
+
}
|
|
917
|
+
return result;
|
|
918
|
+
}
|
|
919
|
+
if (expression.type === "MemberExpression") {
|
|
920
|
+
let key;
|
|
921
|
+
if (expression.computed) {
|
|
922
|
+
key = evaluateStatic(expression.property, ctx, env, stack);
|
|
923
|
+
} else if (expression.property.type === "Identifier") {
|
|
924
|
+
key = expression.property.name;
|
|
925
|
+
}
|
|
926
|
+
if (key === undefined || key === null || typeof key !== "string" && typeof key !== "number") {
|
|
927
|
+
return undefined;
|
|
928
|
+
}
|
|
929
|
+
if (isProcessEnvMember(expression.object) && typeof key === "string" && !env.has("process")) {
|
|
930
|
+
// Treat process.env.X as deterministically undefined at build time.
|
|
931
|
+
// Reading from real process.env would couple the bundle to whatever
|
|
932
|
+
// happens to be set on the build machine; falling back to the
|
|
933
|
+
// ?? / || branch (or a runtime read) is more predictable.
|
|
934
|
+
return undefined;
|
|
935
|
+
}
|
|
936
|
+
const objectValue = evaluateStatic(expression.object, ctx, env, stack);
|
|
937
|
+
if (objectValue === undefined) {
|
|
938
|
+
return undefined;
|
|
939
|
+
}
|
|
940
|
+
return getObjectMember(objectValue, key);
|
|
941
|
+
}
|
|
942
|
+
if (expression.type === "NewExpression") {
|
|
943
|
+
if (expression.callee.type !== "Identifier" || expression.arguments.length !== 1) {
|
|
944
|
+
return undefined;
|
|
945
|
+
}
|
|
946
|
+
const [argument] = expression.arguments;
|
|
947
|
+
if (!argument || argument.type === "SpreadElement") {
|
|
948
|
+
return undefined;
|
|
949
|
+
}
|
|
950
|
+
const value = evaluateStatic(argument, ctx, env, stack);
|
|
951
|
+
if (value === undefined) {
|
|
952
|
+
return undefined;
|
|
953
|
+
}
|
|
954
|
+
if (expression.callee.name === "String") {
|
|
955
|
+
return String(value);
|
|
956
|
+
}
|
|
957
|
+
if (expression.callee.name === "Number") {
|
|
958
|
+
return Number(value);
|
|
959
|
+
}
|
|
960
|
+
if (expression.callee.name === "Boolean") {
|
|
961
|
+
return Boolean(value);
|
|
962
|
+
}
|
|
963
|
+
return undefined;
|
|
964
|
+
}
|
|
965
|
+
if (expression.type === "CallExpression") {
|
|
966
|
+
if (expression.callee.type === "Identifier") {
|
|
967
|
+
const args = expression.arguments.map((arg) => arg.type === "SpreadElement" ? undefined : evaluateStatic(arg, ctx, env, stack));
|
|
968
|
+
if (args.some((value) => value === undefined)) {
|
|
969
|
+
return undefined;
|
|
970
|
+
}
|
|
971
|
+
const staticCallable = env.get(expression.callee.name);
|
|
972
|
+
if (isOxcStaticCallableValue(staticCallable) && expression.arguments.length === 0) {
|
|
973
|
+
return unwrapOxcStaticCallableValue(staticCallable);
|
|
974
|
+
}
|
|
975
|
+
if (expression.callee.name === "String" && args.length === 1) {
|
|
976
|
+
return String(args[0]);
|
|
977
|
+
}
|
|
978
|
+
if (expression.callee.name === "Number" && args.length === 1) {
|
|
979
|
+
return Number(args[0]);
|
|
980
|
+
}
|
|
981
|
+
if (expression.callee.name === "Boolean" && args.length === 1) {
|
|
982
|
+
return Boolean(args[0]);
|
|
983
|
+
}
|
|
984
|
+
const binding = resolveBindingAt(ctx, expression.callee.name, expression.callee.start);
|
|
985
|
+
const fn = binding?.functionNode ?? binding?.declarator?.init;
|
|
986
|
+
if (fn && (fn.type === "ArrowFunctionExpression" || fn.type === "FunctionDeclaration" || fn.type === "FunctionExpression")) {
|
|
987
|
+
return evaluateFunctionCall(fn, args, ctx, env, [...stack, expression.callee.name]);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
if (expression.callee.type === "MemberExpression") {
|
|
991
|
+
const objectValue = evaluateStatic(expression.callee.object, ctx, env, stack);
|
|
992
|
+
let key;
|
|
993
|
+
if (expression.callee.computed) {
|
|
994
|
+
key = evaluateStatic(expression.callee.property, ctx, env, stack);
|
|
995
|
+
} else if (expression.callee.property.type === "Identifier") {
|
|
996
|
+
key = expression.callee.property.name;
|
|
997
|
+
}
|
|
998
|
+
if (typeof objectValue === "string") {
|
|
999
|
+
if (key === "toLowerCase" && expression.arguments.length === 0) {
|
|
1000
|
+
return objectValue.toLowerCase();
|
|
1001
|
+
}
|
|
1002
|
+
if (key === "toUpperCase" && expression.arguments.length === 0) {
|
|
1003
|
+
return objectValue.toUpperCase();
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
return evaluateBinary(expression, ctx, env, stack);
|
|
1009
|
+
};
|
|
1010
|
+
const allocateExpressionName = (ctx) => {
|
|
1011
|
+
let base = "_exp";
|
|
1012
|
+
let idx = 1;
|
|
1013
|
+
while (ctx.usedNames.has(base)) {
|
|
1014
|
+
idx += 1;
|
|
1015
|
+
base = `_exp${idx}`;
|
|
1016
|
+
}
|
|
1017
|
+
ctx.usedNames.add(base);
|
|
1018
|
+
return base;
|
|
1019
|
+
};
|
|
1020
|
+
const hoistedBindingKey = (binding) => `${binding.scope.start}:${binding.scope.end}:${binding.declaredAt}:${binding.name}`;
|
|
1021
|
+
const allocateHoistedBindingName = (originalName, ctx) => {
|
|
1022
|
+
const sanitized = originalName.replace(/[^A-Za-z0-9_$]/g, "_") || "hoisted";
|
|
1023
|
+
const base = /^[A-Za-z_$]/.test(sanitized) ? `_${sanitized}` : "_hoisted";
|
|
1024
|
+
let candidate = base;
|
|
1025
|
+
let idx = 2;
|
|
1026
|
+
while (ctx.usedNames.has(candidate)) {
|
|
1027
|
+
candidate = `${base}${idx}`;
|
|
1028
|
+
idx += 1;
|
|
1029
|
+
}
|
|
1030
|
+
ctx.usedNames.add(candidate);
|
|
1031
|
+
return candidate;
|
|
1032
|
+
};
|
|
1033
|
+
const getHoistedBindingName = (binding, ctx) => {
|
|
1034
|
+
const key = hoistedBindingKey(binding);
|
|
1035
|
+
const existing = ctx.hoistedBindingNames.get(key);
|
|
1036
|
+
if (existing) {
|
|
1037
|
+
return existing;
|
|
1038
|
+
}
|
|
1039
|
+
const next = allocateHoistedBindingName(binding.name, ctx);
|
|
1040
|
+
ctx.hoistedBindingNames.set(key, next);
|
|
1041
|
+
return next;
|
|
1042
|
+
};
|
|
1043
|
+
const declarationInitCode = (init, ctx) => {
|
|
1044
|
+
const renamedDependencies = new Map();
|
|
1045
|
+
findReferences(init, ctx.referencesByNode).forEach(({ name, start }) => {
|
|
1046
|
+
const dependency = resolveBindingAt(ctx, name, start);
|
|
1047
|
+
if (!dependency || dependency.importedFrom || dependency.isRoot || dependency.declarator?.id.type !== "Identifier") {
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
renamedDependencies.set(name, getHoistedBindingName(dependency, ctx));
|
|
1051
|
+
});
|
|
1052
|
+
return renamedDependencies.size > 0 ? replaceIdentifierReferences(init, renamedDependencies, ctx.code) : ctx.code.slice(init.start, init.end);
|
|
1053
|
+
};
|
|
1054
|
+
const addHoistedCode = (key, code, ctx) => {
|
|
1055
|
+
if (ctx.hoistedDeclarations.has(key)) {
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
ctx.hoistedDeclarations.set(key, code);
|
|
1059
|
+
const declarations = ctx.hoistedDeclarationsByInsertionPoint.get(ctx.currentInsertionPoint) ?? [];
|
|
1060
|
+
declarations.push(code);
|
|
1061
|
+
ctx.hoistedDeclarationsByInsertionPoint.set(ctx.currentInsertionPoint, declarations);
|
|
1062
|
+
};
|
|
1063
|
+
const declarationCode = (binding, ctx) => {
|
|
1064
|
+
const { declarator } = binding;
|
|
1065
|
+
if (!declarator) {
|
|
1066
|
+
return "";
|
|
1067
|
+
}
|
|
1068
|
+
const { id } = declarator;
|
|
1069
|
+
if (id.type !== "Identifier") {
|
|
1070
|
+
const idCode = ctx.code.slice(id.start, id.end);
|
|
1071
|
+
if (!declarator.init) {
|
|
1072
|
+
return `let ${idCode};`;
|
|
1073
|
+
}
|
|
1074
|
+
return `let ${idCode} = ${declarationInitCode(declarator.init, ctx)};`;
|
|
1075
|
+
}
|
|
1076
|
+
const hoistedName = getHoistedBindingName(binding, ctx);
|
|
1077
|
+
if (!declarator.init) {
|
|
1078
|
+
return `let ${hoistedName};`;
|
|
1079
|
+
}
|
|
1080
|
+
return `let ${hoistedName} = ${declarationInitCode(declarator.init, ctx)};`;
|
|
1081
|
+
};
|
|
1082
|
+
const assertHoistable = (binding, ctx, stack = []) => {
|
|
1083
|
+
if (!binding.declarator?.init || binding.importedFrom || binding.isRoot) {
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
if (stack.includes(binding.name)) {
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
const refs = findReferences(binding.declarator.init, ctx.referencesByNode);
|
|
1090
|
+
refs.forEach(({ name, start }) => {
|
|
1091
|
+
const nextBinding = resolveBindingAt(ctx, name, start);
|
|
1092
|
+
if (!nextBinding) {
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
if (nextBinding.kind === "param") {
|
|
1096
|
+
throw new Error(`This identifier cannot be used in the template, because it is a function parameter.`);
|
|
1097
|
+
}
|
|
1098
|
+
assertHoistable(nextBinding, ctx, [...stack, binding.name]);
|
|
1099
|
+
});
|
|
1100
|
+
};
|
|
1101
|
+
const addHoistedDeclaration = (binding, ctx, stack = []) => {
|
|
1102
|
+
if (!binding.declaration || !binding.declarator || binding.importedFrom || binding.isRoot || stack.includes(binding.name)) {
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
const hoistSource = binding.declarator.init ?? binding.declarator;
|
|
1106
|
+
findReferences(hoistSource, ctx.referencesByNode).forEach(({ name, start }) => {
|
|
1107
|
+
const dependency = resolveBindingAt(ctx, name, start);
|
|
1108
|
+
if (dependency) {
|
|
1109
|
+
addHoistedDeclaration(dependency, ctx, [...stack, binding.name]);
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
if (!ctx.hoistedDeclarations.has(binding.name)) {
|
|
1113
|
+
addHoistedCode(binding.name, declarationCode(binding, ctx), ctx);
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
const literalExpressionValue = (expression, ctx) => {
|
|
1117
|
+
if (expression.type !== "Literal") {
|
|
1118
|
+
return null;
|
|
1119
|
+
}
|
|
1120
|
+
if (expression.value !== null && typeof expression.value !== "string" && typeof expression.value !== "number" && typeof expression.value !== "boolean") {
|
|
1121
|
+
return null;
|
|
1122
|
+
}
|
|
1123
|
+
let type;
|
|
1124
|
+
if (expression.value === null) {
|
|
1125
|
+
type = "NullLiteral";
|
|
1126
|
+
} else if (typeof expression.value === "string") {
|
|
1127
|
+
type = "StringLiteral";
|
|
1128
|
+
} else if (typeof expression.value === "number") {
|
|
1129
|
+
type = "NumericLiteral";
|
|
1130
|
+
} else {
|
|
1131
|
+
type = "BooleanLiteral";
|
|
1132
|
+
}
|
|
1133
|
+
const loc = getSourceLocation(expression.start, expression.end, ctx);
|
|
1134
|
+
const ex = expression.value === null ? {
|
|
1135
|
+
loc,
|
|
1136
|
+
type
|
|
1137
|
+
} : {
|
|
1138
|
+
loc,
|
|
1139
|
+
type,
|
|
1140
|
+
value: expression.value
|
|
1141
|
+
};
|
|
1142
|
+
return {
|
|
1143
|
+
ex,
|
|
1144
|
+
kind: ValueType.CONST,
|
|
1145
|
+
source: ctx.code.slice(expression.start, expression.end),
|
|
1146
|
+
value: expression.value
|
|
1147
|
+
};
|
|
1148
|
+
};
|
|
1149
|
+
const extractExpression = (expression, ctx, evaluate) => {
|
|
1150
|
+
const source = ctx.code.slice(expression.start, expression.end);
|
|
1151
|
+
const isFunction = expression.type === "FunctionExpression" || expression.type === "ArrowFunctionExpression";
|
|
1152
|
+
if (evaluate) {
|
|
1153
|
+
const evaluated = evaluateStatic(expression, ctx);
|
|
1154
|
+
const literal = literalCode(evaluated);
|
|
1155
|
+
if (literal) {
|
|
1156
|
+
findReferences(expression, ctx.referencesByNode).forEach(({ name }) => ctx.dependencyNames.add(name));
|
|
1157
|
+
return {
|
|
1158
|
+
expressionCode: literal,
|
|
1159
|
+
importedFrom: [],
|
|
1160
|
+
kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,
|
|
1161
|
+
staticImports: [],
|
|
1162
|
+
staticValue: isStaticSerializableValue(evaluated) ? cloneStaticValue(evaluated) : undefined
|
|
1163
|
+
};
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
const identifierReplacements = new Map();
|
|
1167
|
+
const importedFrom = [];
|
|
1168
|
+
const namespaceStatic = collectStaticNamespaceMemberReferences(expression, ctx);
|
|
1169
|
+
const staticImports = [...namespaceStatic.imports];
|
|
1170
|
+
let hasNonStaticLocalReference = false;
|
|
1171
|
+
findReferences(expression, ctx.referencesByNode).forEach(({ name, start }) => {
|
|
1172
|
+
const binding = resolveBindingAt(ctx, name, start);
|
|
1173
|
+
if (!binding) {
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
if (isFunction && isBindingDeclaredWithin(binding, expression)) {
|
|
1177
|
+
return;
|
|
1178
|
+
}
|
|
1179
|
+
ctx.dependencyNames.add(name);
|
|
1180
|
+
if (binding.importedFrom) {
|
|
1181
|
+
importedFrom.push(binding.importedFrom);
|
|
1182
|
+
if (binding.imported && binding.imported !== "*") {
|
|
1183
|
+
staticImports.push({
|
|
1184
|
+
imported: binding.imported,
|
|
1185
|
+
local: binding.name,
|
|
1186
|
+
source: binding.importedFrom
|
|
1187
|
+
});
|
|
1188
|
+
} else if (binding.imported === "*" && namespaceStatic.coveredReferenceStarts.has(start)) {} else {
|
|
1189
|
+
hasNonStaticLocalReference = true;
|
|
1190
|
+
}
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
const replacement = getConstantReplacement(binding, ctx);
|
|
1194
|
+
if (evaluate && replacement) {
|
|
1195
|
+
identifierReplacements.set(name, replacement);
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
1198
|
+
hasNonStaticLocalReference = true;
|
|
1199
|
+
assertHoistable(binding, ctx);
|
|
1200
|
+
addHoistedDeclaration(binding, ctx);
|
|
1201
|
+
if (!binding.isRoot && binding.declarator?.id.type === "Identifier") {
|
|
1202
|
+
identifierReplacements.set(name, getHoistedBindingName(binding, ctx));
|
|
1203
|
+
}
|
|
1204
|
+
});
|
|
1205
|
+
return {
|
|
1206
|
+
expressionCode: identifierReplacements.size > 0 ? replaceIdentifierReferences(expression, identifierReplacements, ctx.code) : source,
|
|
1207
|
+
importedFrom,
|
|
1208
|
+
kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,
|
|
1209
|
+
staticExpressionCode: namespaceStatic.replacements.length > 0 ? applyExpressionReplacements(expression, namespaceStatic.replacements, ctx.code) : undefined,
|
|
1210
|
+
staticImports: hasNonStaticLocalReference ? [] : staticImports
|
|
1211
|
+
};
|
|
1212
|
+
};
|
|
1213
|
+
const getInsertionPoints = (program, expressions) => {
|
|
1214
|
+
if (expressions.length === 0) {
|
|
1215
|
+
return [];
|
|
1216
|
+
}
|
|
1217
|
+
if (program.body.length === 0) {
|
|
1218
|
+
return expressions.map(() => 0);
|
|
1219
|
+
}
|
|
1220
|
+
const insertionPoints = [];
|
|
1221
|
+
let ownerIndex = 0;
|
|
1222
|
+
expressions.forEach((expression) => {
|
|
1223
|
+
while (ownerIndex < program.body.length - 1 && program.body[ownerIndex].end < expression.start) {
|
|
1224
|
+
ownerIndex += 1;
|
|
1225
|
+
}
|
|
1226
|
+
let owner = program.body[ownerIndex];
|
|
1227
|
+
if (!owner || owner.start > expression.start || owner.end < expression.end) {
|
|
1228
|
+
owner = program.body.find((statement) => statement.start <= expression.start && statement.end >= expression.end);
|
|
1229
|
+
}
|
|
1230
|
+
insertionPoints.push(owner?.start ?? 0);
|
|
1231
|
+
});
|
|
1232
|
+
return insertionPoints;
|
|
1233
|
+
};
|
|
1234
|
+
const applyReplacements = (code, replacements) => {
|
|
1235
|
+
let result = code;
|
|
1236
|
+
replacements.sort((a, b) => b.start - a.start).forEach((replacement) => {
|
|
1237
|
+
result = result.slice(0, replacement.start) + replacement.value + result.slice(replacement.end);
|
|
1238
|
+
});
|
|
1239
|
+
return result;
|
|
1240
|
+
};
|
|
1241
|
+
const extractExpressions = (code, filename, evaluate, program, analysis, expressions) => {
|
|
1242
|
+
if (expressions.length === 0) {
|
|
1243
|
+
return {
|
|
1244
|
+
code,
|
|
1245
|
+
dependencyNames: [],
|
|
1246
|
+
expressionValues: [],
|
|
1247
|
+
staticValueCandidates: [],
|
|
1248
|
+
staticValues: []
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
const insertionPoints = getInsertionPoints(program, expressions);
|
|
1252
|
+
const ctx = {
|
|
1253
|
+
bindingResolutionCache: new Map(),
|
|
1254
|
+
bindingsByName: analysis.bindingsByName,
|
|
1255
|
+
code,
|
|
1256
|
+
currentInsertionPoint: insertionPoints[0] ?? 0,
|
|
1257
|
+
currentExpressionStart: expressions[0].start,
|
|
1258
|
+
dependencyNames: new Set(),
|
|
1259
|
+
expressionValues: [],
|
|
1260
|
+
filename,
|
|
1261
|
+
hoistedBindingNames: new Map(),
|
|
1262
|
+
hoistedDeclarations: new Map(),
|
|
1263
|
+
hoistedDeclarationsByInsertionPoint: new Map(),
|
|
1264
|
+
loc: createLocationLookup(code),
|
|
1265
|
+
referencesByNode: new WeakMap(),
|
|
1266
|
+
replacements: [],
|
|
1267
|
+
rootMutationsByBinding: analysis.rootMutationsByBinding,
|
|
1268
|
+
staticImportAliases: new Map(),
|
|
1269
|
+
staticValueCandidates: [],
|
|
1270
|
+
staticValues: [],
|
|
1271
|
+
usedNames: new Set(analysis.usedNames)
|
|
1272
|
+
};
|
|
1273
|
+
expressions.forEach((expression, index) => {
|
|
1274
|
+
ctx.currentInsertionPoint = insertionPoints[index] ?? 0;
|
|
1275
|
+
ctx.currentExpressionStart = expression.start;
|
|
1276
|
+
const literal = literalExpressionValue(expression, ctx);
|
|
1277
|
+
if (literal) {
|
|
1278
|
+
ctx.expressionValues.push(literal);
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
const { expressionCode, importedFrom, kind, staticExpressionCode, staticImports, staticValue } = extractExpression(expression, ctx, evaluate);
|
|
1282
|
+
const expName = allocateExpressionName(ctx);
|
|
1283
|
+
addHoistedCode(expName, `const ${expName} = () => (${expressionCode});`, ctx);
|
|
1284
|
+
if (staticValue !== undefined && kind !== ValueType.FUNCTION) {
|
|
1285
|
+
ctx.staticValues.push({
|
|
1286
|
+
name: expName,
|
|
1287
|
+
value: staticValue
|
|
1288
|
+
});
|
|
1289
|
+
} else if (staticImports.length > 0 && kind !== ValueType.FUNCTION) {
|
|
1290
|
+
const uniqueImports = new Map();
|
|
1291
|
+
staticImports.forEach((item) => {
|
|
1292
|
+
uniqueImports.set(`${item.local}\0${item.importLocal ?? ""}\0${item.source}\0${item.imported}`, item);
|
|
1293
|
+
});
|
|
1294
|
+
ctx.staticValueCandidates.push({
|
|
1295
|
+
imports: [...uniqueImports.values()],
|
|
1296
|
+
name: expName,
|
|
1297
|
+
source: staticExpressionCode ?? expressionCode
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
ctx.replacements.push({
|
|
1301
|
+
start: expression.start,
|
|
1302
|
+
end: expression.end,
|
|
1303
|
+
value: `${expName}()`
|
|
1304
|
+
});
|
|
1305
|
+
ctx.expressionValues.push({
|
|
1306
|
+
ex: {
|
|
1307
|
+
loc: getSourceLocation(expression.start, expression.end, ctx),
|
|
1308
|
+
name: expName,
|
|
1309
|
+
type: "Identifier"
|
|
1310
|
+
},
|
|
1311
|
+
importedFrom,
|
|
1312
|
+
kind,
|
|
1313
|
+
source: ctx.code.slice(expression.start, expression.end)
|
|
1314
|
+
});
|
|
1315
|
+
});
|
|
1316
|
+
ctx.hoistedDeclarationsByInsertionPoint.forEach((declarations, point) => {
|
|
1317
|
+
ctx.replacements.push({
|
|
1318
|
+
start: point,
|
|
1319
|
+
end: point,
|
|
1320
|
+
value: `${declarations.join("\n")}\n`
|
|
1321
|
+
});
|
|
1322
|
+
});
|
|
1323
|
+
return {
|
|
1324
|
+
code: applyReplacements(code, ctx.replacements),
|
|
1325
|
+
dependencyNames: [...ctx.dependencyNames],
|
|
1326
|
+
expressionValues: ctx.expressionValues,
|
|
1327
|
+
staticValueCandidates: ctx.staticValueCandidates,
|
|
1328
|
+
staticValues: ctx.staticValues
|
|
1329
|
+
};
|
|
1330
|
+
};
|
|
1331
|
+
export const isOxcStaticSerializableValue = (value) => isStaticSerializableValue(value);
|
|
1332
|
+
export const evaluateOxcStaticExpressionAt = (code, filename, expressionSpan, staticBindings = new Map()) => {
|
|
1333
|
+
const program = parseOxc(code, filename);
|
|
1334
|
+
const analysis = analyzeProgram(program, {
|
|
1335
|
+
collectTargetExpressions: true,
|
|
1336
|
+
expressionSpanLookup: createSpanLookup([expressionSpan])
|
|
1337
|
+
});
|
|
1338
|
+
const [expression] = analysis.targetExpressions;
|
|
1339
|
+
if (!expression) {
|
|
1340
|
+
return undefined;
|
|
1341
|
+
}
|
|
1342
|
+
const ctx = {
|
|
1343
|
+
bindingResolutionCache: new Map(),
|
|
1344
|
+
bindingsByName: analysis.bindingsByName,
|
|
1345
|
+
code,
|
|
1346
|
+
currentInsertionPoint: 0,
|
|
1347
|
+
currentExpressionStart: expression.start,
|
|
1348
|
+
dependencyNames: new Set(),
|
|
1349
|
+
expressionValues: [],
|
|
1350
|
+
filename,
|
|
1351
|
+
hoistedBindingNames: new Map(),
|
|
1352
|
+
hoistedDeclarations: new Map(),
|
|
1353
|
+
hoistedDeclarationsByInsertionPoint: new Map(),
|
|
1354
|
+
loc: createLocationLookup(code),
|
|
1355
|
+
referencesByNode: new WeakMap(),
|
|
1356
|
+
replacements: [],
|
|
1357
|
+
rootMutationsByBinding: analysis.rootMutationsByBinding,
|
|
1358
|
+
staticImportAliases: new Map(),
|
|
1359
|
+
staticValueCandidates: [],
|
|
1360
|
+
staticValues: [],
|
|
1361
|
+
usedNames: new Set(analysis.usedNames)
|
|
1362
|
+
};
|
|
1363
|
+
return evaluateStatic(expression, ctx, new Map(staticBindings));
|
|
1364
|
+
};
|
|
1365
|
+
export const evaluateOxcStaticExpression = (source, filename, staticBindings = new Map()) => {
|
|
1366
|
+
const code = `const __wyw_static_value = ${source};`;
|
|
1367
|
+
const program = parseOxc(code, filename);
|
|
1368
|
+
const declaration = program.body[0];
|
|
1369
|
+
if (declaration?.type !== "VariableDeclaration") {
|
|
1370
|
+
return undefined;
|
|
1371
|
+
}
|
|
1372
|
+
const [declarator] = declaration.declarations;
|
|
1373
|
+
if (!declarator?.init) {
|
|
1374
|
+
return undefined;
|
|
1375
|
+
}
|
|
1376
|
+
return evaluateOxcStaticExpressionAt(code, filename, {
|
|
1377
|
+
end: declarator.init.end,
|
|
1378
|
+
start: declarator.init.start
|
|
1379
|
+
}, staticBindings);
|
|
1380
|
+
};
|
|
1381
|
+
export const collectOxcExpressionDependencies = (code, filename, evaluate = false, targetExpressionSpans) => {
|
|
1382
|
+
const program = parseOxc(code, filename);
|
|
1383
|
+
const analysis = analyzeProgram(program, {
|
|
1384
|
+
collectTargetExpressions: true,
|
|
1385
|
+
expressionSpanLookup: createSpanLookup(targetExpressionSpans)
|
|
1386
|
+
});
|
|
1387
|
+
return extractExpressions(code, filename, evaluate, program, analysis, analysis.targetExpressions);
|
|
1388
|
+
};
|
|
1389
|
+
export const collectOxcTemplateDependencies = (code, filename, evaluate = false, targetTemplateSpans) => {
|
|
1390
|
+
const program = parseOxc(code, filename);
|
|
1391
|
+
const analysis = analyzeProgram(program, {
|
|
1392
|
+
collectTemplateLiterals: true,
|
|
1393
|
+
templateSpanLookup: createSpanLookup(targetTemplateSpans)
|
|
1394
|
+
});
|
|
1395
|
+
const expressions = analysis.templateLiterals.flatMap((template) => template.expressions);
|
|
1396
|
+
return extractExpressions(code, filename, evaluate, program, analysis, expressions);
|
|
1397
|
+
};
|
|
1398
|
+
//# sourceMappingURL=collectOxcTemplateDependencies.js.map
|