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