@wyw-in-js/transform 1.0.8 → 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 -28
- 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 -79
- package/esm/transform/generators/workflow.js.map +1 -1
- package/esm/transform/helpers/loadWywOptions.js +154 -73
- 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 +10 -0
- package/esm/utils/TransformDiagnostics.js.map +1 -0
- package/esm/utils/TransformMetadata.js +28 -10
- 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 +7 -8
- package/types/index.js +24 -77
- 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 +6 -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 -37
- package/types/transform/helpers/loadWywOptions.js +95 -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 +4 -24
- package/types/types.js +1 -2
- package/types/utils/EventEmitter.js +3 -9
- package/types/utils/ShakerMetadata.js +1 -5
- package/types/utils/TransformDiagnostics.d.ts +9 -0
- package/types/utils/TransformDiagnostics.js +11 -0
- package/types/utils/TransformMetadata.d.ts +22 -4
- package/types/utils/TransformMetadata.js +23 -8
- 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 -257
- 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 -90
- package/lib/transform/generators/workflow.js.map +0 -1
- package/lib/transform/helpers/loadWywOptions.js +0 -87
- 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/TransformMetadata.js +0 -19
- 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 -20
- 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,1391 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
import { basename, dirname } from 'path';
|
|
3
|
+
import { BaseProcessor, expressionToCode } from '@wyw-in-js/processor-utils';
|
|
4
|
+
import { ValueType } from '@wyw-in-js/shared';
|
|
5
|
+
import { collectOxcProcessorImportsFromProgram } from './collectOxcExportsAndImports';
|
|
6
|
+
import { EventEmitter } from './EventEmitter';
|
|
7
|
+
import { collectOxcExpressionDependencies, } from './collectOxcTemplateDependencies';
|
|
8
|
+
import { isNotNull } from './isNotNull';
|
|
9
|
+
import { createOxcAstService, printOxcAstServiceImport, } from './oxcAstService';
|
|
10
|
+
import { parseOxcProgramCached } from './parseOxc';
|
|
11
|
+
import { getProcessorForImport } from './processorLookup';
|
|
12
|
+
let didWarnSkipSymbolMismatch = false;
|
|
13
|
+
const GENERATED_HELPER_NAME_RE = /^_exp\d*$/;
|
|
14
|
+
const isNode = (value) => !!value &&
|
|
15
|
+
typeof value === 'object' &&
|
|
16
|
+
'type' in value &&
|
|
17
|
+
typeof value.type === 'string';
|
|
18
|
+
const getChildren = (node) => {
|
|
19
|
+
const result = [];
|
|
20
|
+
const record = node;
|
|
21
|
+
Object.keys(record).forEach((key) => {
|
|
22
|
+
if (key === 'type' || key === 'start' || key === 'end' || key === 'range') {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const value = record[key];
|
|
26
|
+
if (isNode(value)) {
|
|
27
|
+
result.push(value);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(value)) {
|
|
31
|
+
value.forEach((item) => {
|
|
32
|
+
if (isNode(item)) {
|
|
33
|
+
result.push(item);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
const parseOxc = (code, filename) => {
|
|
41
|
+
return parseOxcProgramCached(filename, code, 'module');
|
|
42
|
+
};
|
|
43
|
+
const visit = (node, enter, parent = null) => {
|
|
44
|
+
enter(node, parent);
|
|
45
|
+
getChildren(node).forEach((child) => visit(child, enter, node));
|
|
46
|
+
};
|
|
47
|
+
const applyReplacements = (code, replacements) => {
|
|
48
|
+
let result = code;
|
|
49
|
+
replacements
|
|
50
|
+
.sort((a, b) => b.start - a.start)
|
|
51
|
+
.forEach((replacement) => {
|
|
52
|
+
result =
|
|
53
|
+
result.slice(0, replacement.start) +
|
|
54
|
+
replacement.value +
|
|
55
|
+
result.slice(replacement.end);
|
|
56
|
+
});
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
const insertAddedImports = (code, program, addedImports) => {
|
|
60
|
+
if (addedImports.length === 0) {
|
|
61
|
+
return code;
|
|
62
|
+
}
|
|
63
|
+
const uniqueImports = [
|
|
64
|
+
...new Map(addedImports.map((item) => [
|
|
65
|
+
`${item.source}\0${item.imported}\0${item.local}`,
|
|
66
|
+
item,
|
|
67
|
+
])).values(),
|
|
68
|
+
];
|
|
69
|
+
const importBlock = uniqueImports.map(printOxcAstServiceImport).join('\n');
|
|
70
|
+
const lastImport = [...program.body]
|
|
71
|
+
.reverse()
|
|
72
|
+
.find((statement) => statement.type === 'ImportDeclaration');
|
|
73
|
+
const hashbangEnd = code.startsWith('#!')
|
|
74
|
+
? (() => {
|
|
75
|
+
const newline = code.indexOf('\n');
|
|
76
|
+
return newline === -1 ? code.length : newline + 1;
|
|
77
|
+
})()
|
|
78
|
+
: 0;
|
|
79
|
+
const insertionPoint = lastImport?.end ?? hashbangEnd;
|
|
80
|
+
const prefix = code.slice(0, insertionPoint);
|
|
81
|
+
const suffix = code.slice(insertionPoint);
|
|
82
|
+
const leadingBreak = prefix.length > 0 && !prefix.endsWith('\n') ? '\n' : '';
|
|
83
|
+
const trailingBreak = suffix.length > 0 && !suffix.startsWith('\n') ? '\n' : '';
|
|
84
|
+
return `${prefix}${leadingBreak}${importBlock}${trailingBreak}${suffix}`;
|
|
85
|
+
};
|
|
86
|
+
const createLocationLookup = (code) => {
|
|
87
|
+
const lineStarts = [0];
|
|
88
|
+
for (let idx = 0; idx < code.length; idx += 1) {
|
|
89
|
+
if (code[idx] === '\n') {
|
|
90
|
+
lineStarts.push(idx + 1);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return (offset) => {
|
|
94
|
+
let low = 0;
|
|
95
|
+
let high = lineStarts.length - 1;
|
|
96
|
+
while (low <= high) {
|
|
97
|
+
const mid = Math.floor((low + high) / 2);
|
|
98
|
+
const next = lineStarts[mid + 1] ?? Infinity;
|
|
99
|
+
if (lineStarts[mid] <= offset && offset < next) {
|
|
100
|
+
return {
|
|
101
|
+
column: offset - lineStarts[mid],
|
|
102
|
+
line: mid + 1,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (offset < lineStarts[mid]) {
|
|
106
|
+
high = mid - 1;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
low = mid + 1;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const lastLine = lineStarts.length - 1;
|
|
113
|
+
return {
|
|
114
|
+
column: Math.max(0, offset - lineStarts[lastLine]),
|
|
115
|
+
line: lastLine + 1,
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
const getSourceLocation = (start, end, loc, filename) => ({
|
|
120
|
+
end: loc(end),
|
|
121
|
+
filename: filename ?? undefined,
|
|
122
|
+
start: loc(start),
|
|
123
|
+
});
|
|
124
|
+
const buildCodeFrameError = (code, location, message) => {
|
|
125
|
+
const lines = code.split('\n');
|
|
126
|
+
const startLine = location.start.line;
|
|
127
|
+
const endLine = location.end.line;
|
|
128
|
+
const frameStart = Math.max(1, startLine - 2);
|
|
129
|
+
const frameEnd = Math.min(lines.length, endLine + 2);
|
|
130
|
+
const lineNoWidth = String(frameEnd).length;
|
|
131
|
+
const frame = [];
|
|
132
|
+
for (let lineNo = frameStart; lineNo <= frameEnd; lineNo += 1) {
|
|
133
|
+
const marker = lineNo === startLine ? '>' : ' ';
|
|
134
|
+
const line = lines[lineNo - 1] ?? '';
|
|
135
|
+
frame.push(line.length > 0
|
|
136
|
+
? `${marker} ${String(lineNo).padStart(lineNoWidth)} | ${line}`
|
|
137
|
+
: `${marker} ${String(lineNo).padStart(lineNoWidth)} |`);
|
|
138
|
+
if (lineNo === startLine) {
|
|
139
|
+
const pointerLength = startLine === endLine
|
|
140
|
+
? Math.max(1, location.end.column - location.start.column)
|
|
141
|
+
: 1;
|
|
142
|
+
frame.push(` ${' '.repeat(lineNoWidth)} | ${' '.repeat(location.start.column)}${'^'.repeat(pointerLength)}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const prefix = location.filename ? `${location.filename}: ` : '';
|
|
146
|
+
return new Error(`${prefix}${message}\n${frame.join('\n')}`);
|
|
147
|
+
};
|
|
148
|
+
const collectUsedNames = (program) => {
|
|
149
|
+
const names = new Set();
|
|
150
|
+
visit(program, (node) => {
|
|
151
|
+
if (node.type === 'Identifier') {
|
|
152
|
+
names.add(node.name);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
return names;
|
|
156
|
+
};
|
|
157
|
+
const isNodeReference = (node, parent) => {
|
|
158
|
+
if (node.type === 'Identifier') {
|
|
159
|
+
const parentRecord = parent;
|
|
160
|
+
if (!parentRecord) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
if (parent?.type === 'ImportDeclaration' ||
|
|
164
|
+
parent?.type === 'ImportSpecifier' ||
|
|
165
|
+
parent?.type === 'ImportDefaultSpecifier' ||
|
|
166
|
+
parent?.type === 'ImportNamespaceSpecifier') {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
if (parent?.type === 'MemberExpression' &&
|
|
170
|
+
parentRecord.property === node &&
|
|
171
|
+
!parentRecord.computed) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
if ((parent?.type === 'VariableDeclarator' ||
|
|
175
|
+
parent?.type === 'FunctionDeclaration' ||
|
|
176
|
+
parent?.type === 'ClassDeclaration' ||
|
|
177
|
+
parent?.type === 'ClassExpression') &&
|
|
178
|
+
parentRecord.id === node) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
if ((parent?.type === 'PropertyDefinition' ||
|
|
182
|
+
parent?.type === 'MethodDefinition') &&
|
|
183
|
+
parentRecord.key === node &&
|
|
184
|
+
!parentRecord.computed) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
if (parent?.type === 'Property' &&
|
|
188
|
+
parentRecord.key === node &&
|
|
189
|
+
parentRecord.value !== node &&
|
|
190
|
+
!parentRecord.computed) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (node.type === 'JSXIdentifier') {
|
|
196
|
+
const parentRecord = parent;
|
|
197
|
+
if (parent?.type === 'JSXAttribute' && parentRecord?.name === node) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
return false;
|
|
203
|
+
};
|
|
204
|
+
const collectReferencedNames = (root) => {
|
|
205
|
+
const names = new Set();
|
|
206
|
+
const walk = (node, parent = null) => {
|
|
207
|
+
if (node.type === 'ImportDeclaration') {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (isNodeReference(node, parent) &&
|
|
211
|
+
'name' in node &&
|
|
212
|
+
typeof node.name === 'string') {
|
|
213
|
+
names.add(node.name);
|
|
214
|
+
}
|
|
215
|
+
getChildren(node).forEach((child) => walk(child, node));
|
|
216
|
+
};
|
|
217
|
+
walk(root);
|
|
218
|
+
return names;
|
|
219
|
+
};
|
|
220
|
+
const collectImportLocalNames = (node) => {
|
|
221
|
+
if (node.type !== 'ImportDeclaration') {
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
const { specifiers } = node;
|
|
225
|
+
if (!Array.isArray(specifiers)) {
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
return specifiers
|
|
229
|
+
.map((specifier) => {
|
|
230
|
+
const { local } = specifier;
|
|
231
|
+
return isNode(local) && 'name' in local && typeof local.name === 'string'
|
|
232
|
+
? local.name
|
|
233
|
+
: null;
|
|
234
|
+
})
|
|
235
|
+
.filter(isNotNull);
|
|
236
|
+
};
|
|
237
|
+
const getImportSpecifierLocalName = (node) => {
|
|
238
|
+
const { local } = node;
|
|
239
|
+
return isNode(local) && 'name' in local && typeof local.name === 'string'
|
|
240
|
+
? local.name
|
|
241
|
+
: null;
|
|
242
|
+
};
|
|
243
|
+
const collectDeclaredNames = (node) => {
|
|
244
|
+
if (node.type === 'Identifier') {
|
|
245
|
+
return [node.name];
|
|
246
|
+
}
|
|
247
|
+
if (node.type === 'RestElement') {
|
|
248
|
+
return collectDeclaredNames(node.argument);
|
|
249
|
+
}
|
|
250
|
+
if (node.type === 'AssignmentPattern') {
|
|
251
|
+
return collectDeclaredNames(node.left);
|
|
252
|
+
}
|
|
253
|
+
if (node.type === 'ObjectPattern') {
|
|
254
|
+
return node.properties.flatMap((property) => property.type === 'RestElement'
|
|
255
|
+
? collectDeclaredNames(property.argument)
|
|
256
|
+
: collectDeclaredNames(property.value));
|
|
257
|
+
}
|
|
258
|
+
if (node.type === 'ArrayPattern') {
|
|
259
|
+
return node.elements.flatMap((element) => element ? collectDeclaredNames(element) : []);
|
|
260
|
+
}
|
|
261
|
+
if (node.type === 'TSParameterProperty') {
|
|
262
|
+
return collectDeclaredNames(node.parameter);
|
|
263
|
+
}
|
|
264
|
+
return [];
|
|
265
|
+
};
|
|
266
|
+
const collectTopLevelBindings = (statement) => {
|
|
267
|
+
const bindings = new Set();
|
|
268
|
+
if (statement.type === 'ImportDeclaration') {
|
|
269
|
+
collectImportLocalNames(statement).forEach((name) => bindings.add(name));
|
|
270
|
+
return bindings;
|
|
271
|
+
}
|
|
272
|
+
if (statement.type === 'VariableDeclaration') {
|
|
273
|
+
const { declarations } = statement;
|
|
274
|
+
if (!Array.isArray(declarations)) {
|
|
275
|
+
return bindings;
|
|
276
|
+
}
|
|
277
|
+
declarations.forEach((declarator) => {
|
|
278
|
+
const { id } = declarator;
|
|
279
|
+
if (isNode(id)) {
|
|
280
|
+
collectDeclaredNames(id).forEach((name) => bindings.add(name));
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
return bindings;
|
|
284
|
+
}
|
|
285
|
+
if ((statement.type === 'FunctionDeclaration' ||
|
|
286
|
+
statement.type === 'ClassDeclaration' ||
|
|
287
|
+
statement.type === 'TSEnumDeclaration') &&
|
|
288
|
+
'id' in statement) {
|
|
289
|
+
const { id } = statement;
|
|
290
|
+
if (isNode(id) && id.type === 'Identifier') {
|
|
291
|
+
bindings.add(id.name);
|
|
292
|
+
}
|
|
293
|
+
return bindings;
|
|
294
|
+
}
|
|
295
|
+
if (statement.type === 'ExportNamedDeclaration') {
|
|
296
|
+
const { declaration } = statement;
|
|
297
|
+
return isNode(declaration)
|
|
298
|
+
? collectTopLevelBindings(declaration)
|
|
299
|
+
: bindings;
|
|
300
|
+
}
|
|
301
|
+
return bindings;
|
|
302
|
+
};
|
|
303
|
+
const collectTopLevelStatementInfos = (program) => program.body.map((statement) => ({
|
|
304
|
+
bindings: collectTopLevelBindings(statement),
|
|
305
|
+
node: statement,
|
|
306
|
+
references: collectReferencedNames(statement),
|
|
307
|
+
}));
|
|
308
|
+
const collectTopLevelBindingsFromStatements = (statements) => new Set(statements.flatMap((statement) => [...statement.bindings]));
|
|
309
|
+
const collectRemovableNamesFromStatements = (statements, initialNames) => {
|
|
310
|
+
const removable = new Set(initialNames);
|
|
311
|
+
const bindingToStatement = new Map();
|
|
312
|
+
statements.forEach((statement) => {
|
|
313
|
+
statement.bindings.forEach((name) => {
|
|
314
|
+
bindingToStatement.set(name, statement);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
const queue = [...removable];
|
|
318
|
+
while (queue.length > 0) {
|
|
319
|
+
const name = queue.shift();
|
|
320
|
+
const statement = bindingToStatement.get(name);
|
|
321
|
+
if (statement) {
|
|
322
|
+
statement.references.forEach((reference) => {
|
|
323
|
+
if (bindingToStatement.has(reference) && !removable.has(reference)) {
|
|
324
|
+
removable.add(reference);
|
|
325
|
+
queue.push(reference);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return removable;
|
|
331
|
+
};
|
|
332
|
+
const createScopedCleanupScope = (parent) => ({
|
|
333
|
+
bindings: new Map(),
|
|
334
|
+
parent,
|
|
335
|
+
});
|
|
336
|
+
const resolveScopedBinding = (scope, name) => {
|
|
337
|
+
let current = scope;
|
|
338
|
+
while (current) {
|
|
339
|
+
const bindingId = current.bindings.get(name);
|
|
340
|
+
if (bindingId) {
|
|
341
|
+
return bindingId;
|
|
342
|
+
}
|
|
343
|
+
current = current.parent;
|
|
344
|
+
}
|
|
345
|
+
return null;
|
|
346
|
+
};
|
|
347
|
+
const collectScopedBindingInfos = (program) => {
|
|
348
|
+
const bindings = new Map();
|
|
349
|
+
let sequence = 0;
|
|
350
|
+
const addBinding = (scope, name, kind, declaration) => {
|
|
351
|
+
const id = `${kind}:${name}:${declaration.start}:${sequence}`;
|
|
352
|
+
sequence += 1;
|
|
353
|
+
bindings.set(id, {
|
|
354
|
+
declaration,
|
|
355
|
+
dependencies: new Set(),
|
|
356
|
+
externalReferences: 0,
|
|
357
|
+
id,
|
|
358
|
+
incomingFromBindings: new Set(),
|
|
359
|
+
kind,
|
|
360
|
+
name,
|
|
361
|
+
});
|
|
362
|
+
scope.bindings.set(name, id);
|
|
363
|
+
return id;
|
|
364
|
+
};
|
|
365
|
+
const addPatternBindings = (scope, pattern, kind, declaration) => {
|
|
366
|
+
collectDeclaredNames(pattern).forEach((name) => {
|
|
367
|
+
addBinding(scope, name, kind, declaration);
|
|
368
|
+
});
|
|
369
|
+
};
|
|
370
|
+
const recordReference = (scope, name, ownerBindingId) => {
|
|
371
|
+
const targetId = resolveScopedBinding(scope, name);
|
|
372
|
+
if (!targetId) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const target = bindings.get(targetId);
|
|
376
|
+
if (!target) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (ownerBindingId && ownerBindingId !== targetId) {
|
|
380
|
+
target.incomingFromBindings.add(ownerBindingId);
|
|
381
|
+
bindings.get(ownerBindingId)?.dependencies.add(targetId);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
target.externalReferences += 1;
|
|
385
|
+
};
|
|
386
|
+
let walk;
|
|
387
|
+
function walkPatternReferenceSubexpressions(node, scope, ownerBindingId) {
|
|
388
|
+
if (node.type === 'Identifier') {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (node.type === 'TSParameterProperty') {
|
|
392
|
+
walkPatternReferenceSubexpressions(node.parameter, scope, ownerBindingId);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (node.type === 'RestElement') {
|
|
396
|
+
walkPatternReferenceSubexpressions(node.argument, scope, ownerBindingId);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (node.type === 'AssignmentPattern') {
|
|
400
|
+
walkPatternReferenceSubexpressions(node.left, scope, ownerBindingId);
|
|
401
|
+
walk(node.right, scope, node, ownerBindingId);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (node.type === 'ObjectPattern') {
|
|
405
|
+
node.properties.forEach((property) => {
|
|
406
|
+
if (property.type === 'RestElement') {
|
|
407
|
+
walkPatternReferenceSubexpressions(property.argument, scope, ownerBindingId);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (property.computed && isNode(property.key)) {
|
|
411
|
+
walk(property.key, scope, property, ownerBindingId);
|
|
412
|
+
}
|
|
413
|
+
walkPatternReferenceSubexpressions(property.value, scope, ownerBindingId);
|
|
414
|
+
});
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (node.type === 'ArrayPattern') {
|
|
418
|
+
node.elements.forEach((element) => {
|
|
419
|
+
if (element && isNode(element)) {
|
|
420
|
+
walkPatternReferenceSubexpressions(element, scope, ownerBindingId);
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
walk = (node, scope, parent = null, ownerBindingId = null) => {
|
|
426
|
+
if (node.type === 'ImportDeclaration') {
|
|
427
|
+
const { specifiers } = node;
|
|
428
|
+
if (Array.isArray(specifiers)) {
|
|
429
|
+
specifiers.forEach((specifier) => {
|
|
430
|
+
const { local } = specifier;
|
|
431
|
+
if (isNode(local) &&
|
|
432
|
+
local.type === 'Identifier' &&
|
|
433
|
+
typeof local.name === 'string') {
|
|
434
|
+
addBinding(scope, local.name, 'import', node);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
|
|
441
|
+
walk(node.declaration, scope, node, ownerBindingId);
|
|
442
|
+
collectTopLevelBindings(node).forEach((name) => {
|
|
443
|
+
recordReference(scope, name, ownerBindingId);
|
|
444
|
+
});
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
if (node.type === 'ExportDefaultDeclaration') {
|
|
448
|
+
const { declaration } = node;
|
|
449
|
+
if (isNode(declaration)) {
|
|
450
|
+
walk(declaration, scope, node, ownerBindingId);
|
|
451
|
+
if ((declaration.type === 'FunctionDeclaration' ||
|
|
452
|
+
declaration.type === 'ClassDeclaration') &&
|
|
453
|
+
declaration.id) {
|
|
454
|
+
recordReference(scope, declaration.id.name, ownerBindingId);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
if (node.type === 'VariableDeclaration') {
|
|
460
|
+
const { declarations } = node;
|
|
461
|
+
if (!Array.isArray(declarations)) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
declarations.forEach((declarator) => {
|
|
465
|
+
const { id } = declarator;
|
|
466
|
+
if (isNode(id)) {
|
|
467
|
+
addPatternBindings(scope, id, 'variable', node);
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
declarations.forEach((declarator) => {
|
|
471
|
+
const { id } = declarator;
|
|
472
|
+
const { init } = declarator;
|
|
473
|
+
if (!isNode(id) || !isNode(init)) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
const ownerName = collectDeclaredNames(id)[0] ?? null;
|
|
477
|
+
const nextOwner = ownerName !== null ? resolveScopedBinding(scope, ownerName) : null;
|
|
478
|
+
walk(init, scope, declarator, nextOwner);
|
|
479
|
+
});
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (node.type === 'FunctionDeclaration' && node.id) {
|
|
483
|
+
const functionBindingId = addBinding(scope, node.id.name, 'function', node);
|
|
484
|
+
const fnScope = createScopedCleanupScope(scope);
|
|
485
|
+
node.params.forEach((param) => {
|
|
486
|
+
addPatternBindings(fnScope, param, 'param', param);
|
|
487
|
+
walkPatternReferenceSubexpressions(param, fnScope, functionBindingId);
|
|
488
|
+
});
|
|
489
|
+
if (node.body) {
|
|
490
|
+
walk(node.body, fnScope, node, functionBindingId);
|
|
491
|
+
}
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
if (node.type === 'FunctionExpression' ||
|
|
495
|
+
node.type === 'ArrowFunctionExpression') {
|
|
496
|
+
const fnScope = createScopedCleanupScope(scope);
|
|
497
|
+
if (node.type === 'FunctionExpression' && node.id) {
|
|
498
|
+
addBinding(fnScope, node.id.name, 'function', node);
|
|
499
|
+
}
|
|
500
|
+
node.params.forEach((param) => {
|
|
501
|
+
addPatternBindings(fnScope, param, 'param', param);
|
|
502
|
+
walkPatternReferenceSubexpressions(param, fnScope, ownerBindingId);
|
|
503
|
+
});
|
|
504
|
+
if (node.body) {
|
|
505
|
+
walk(node.body, fnScope, node, ownerBindingId);
|
|
506
|
+
}
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (node.type === 'BlockStatement') {
|
|
510
|
+
const blockScope = createScopedCleanupScope(scope);
|
|
511
|
+
getChildren(node).forEach((child) => walk(child, blockScope, node, ownerBindingId));
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (isNodeReference(node, parent) &&
|
|
515
|
+
'name' in node &&
|
|
516
|
+
typeof node.name === 'string') {
|
|
517
|
+
recordReference(scope, node.name, ownerBindingId);
|
|
518
|
+
}
|
|
519
|
+
getChildren(node).forEach((child) => walk(child, scope, node, ownerBindingId));
|
|
520
|
+
};
|
|
521
|
+
walk(program, createScopedCleanupScope(null));
|
|
522
|
+
return bindings;
|
|
523
|
+
};
|
|
524
|
+
const collectScopedRemovableBindingIds = (bindings, initialNames) => {
|
|
525
|
+
const removable = new Set();
|
|
526
|
+
let changed = true;
|
|
527
|
+
while (changed) {
|
|
528
|
+
changed = false;
|
|
529
|
+
for (const binding of bindings.values()) {
|
|
530
|
+
if (!removable.has(binding.id) &&
|
|
531
|
+
binding.kind !== 'import' &&
|
|
532
|
+
binding.kind !== 'param' &&
|
|
533
|
+
binding.externalReferences === 0) {
|
|
534
|
+
const seededByName = initialNames.has(binding.name) ||
|
|
535
|
+
(GENERATED_HELPER_NAME_RE.test(binding.name) &&
|
|
536
|
+
binding.incomingFromBindings.size === 0);
|
|
537
|
+
const allIncomingRemovable = binding.incomingFromBindings.size > 0 &&
|
|
538
|
+
[...binding.incomingFromBindings].every((sourceId) => removable.has(sourceId));
|
|
539
|
+
if ((seededByName && binding.incomingFromBindings.size === 0) ||
|
|
540
|
+
allIncomingRemovable) {
|
|
541
|
+
removable.add(binding.id);
|
|
542
|
+
changed = true;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return removable;
|
|
548
|
+
};
|
|
549
|
+
function expandImportRemovalRange(code, start, end) {
|
|
550
|
+
let removalStart = start;
|
|
551
|
+
while (removalStart > 0 &&
|
|
552
|
+
(code[removalStart - 1] === ' ' || code[removalStart - 1] === '\t')) {
|
|
553
|
+
removalStart -= 1;
|
|
554
|
+
}
|
|
555
|
+
let removalEnd = end;
|
|
556
|
+
if (code[removalEnd] === ';') {
|
|
557
|
+
removalEnd += 1;
|
|
558
|
+
}
|
|
559
|
+
while (removalEnd < code.length &&
|
|
560
|
+
(code[removalEnd] === ' ' || code[removalEnd] === '\t')) {
|
|
561
|
+
removalEnd += 1;
|
|
562
|
+
}
|
|
563
|
+
if (code[removalEnd] === '\r' && code[removalEnd + 1] === '\n') {
|
|
564
|
+
removalEnd += 2;
|
|
565
|
+
}
|
|
566
|
+
else if (code[removalEnd] === '\n') {
|
|
567
|
+
removalEnd += 1;
|
|
568
|
+
}
|
|
569
|
+
return {
|
|
570
|
+
end: removalEnd,
|
|
571
|
+
start: removalStart,
|
|
572
|
+
value: '',
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
const collectUnusedScopedDeclarationRemovals = (code, bindings, initialRemovableNames) => {
|
|
576
|
+
const removableBindingIds = collectScopedRemovableBindingIds(bindings, initialRemovableNames);
|
|
577
|
+
const removals = new Map();
|
|
578
|
+
bindings.forEach((binding) => {
|
|
579
|
+
if (!removableBindingIds.has(binding.id) ||
|
|
580
|
+
binding.kind === 'import' ||
|
|
581
|
+
binding.kind === 'param' ||
|
|
582
|
+
binding.externalReferences > 0 ||
|
|
583
|
+
[...binding.incomingFromBindings].some((sourceId) => !removableBindingIds.has(sourceId))) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (binding.kind === 'function' &&
|
|
587
|
+
binding.declaration.type === 'FunctionDeclaration') {
|
|
588
|
+
const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
|
|
589
|
+
removals.set(`${range.start}:${range.end}`, range);
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
if (binding.declaration.type !== 'VariableDeclaration') {
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
const { declarations } = binding.declaration;
|
|
596
|
+
if (!Array.isArray(declarations) || declarations.length !== 1) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
|
|
600
|
+
removals.set(`${range.start}:${range.end}`, range);
|
|
601
|
+
});
|
|
602
|
+
return [...removals.values()];
|
|
603
|
+
};
|
|
604
|
+
const expandImportSpecifierRemovalRange = (code, start, end) => {
|
|
605
|
+
let removalStart = start;
|
|
606
|
+
let removalEnd = end;
|
|
607
|
+
let whitespaceStart = removalStart;
|
|
608
|
+
while (whitespaceStart > 0 &&
|
|
609
|
+
(code[whitespaceStart - 1] === ' ' || code[whitespaceStart - 1] === '\t')) {
|
|
610
|
+
whitespaceStart -= 1;
|
|
611
|
+
}
|
|
612
|
+
if (code[whitespaceStart - 1] !== '{') {
|
|
613
|
+
removalStart = whitespaceStart;
|
|
614
|
+
}
|
|
615
|
+
while (removalEnd < code.length &&
|
|
616
|
+
(code[removalEnd] === ' ' || code[removalEnd] === '\t')) {
|
|
617
|
+
removalEnd += 1;
|
|
618
|
+
}
|
|
619
|
+
if (code[removalEnd] === ',') {
|
|
620
|
+
removalEnd += 1;
|
|
621
|
+
while (removalEnd < code.length &&
|
|
622
|
+
(code[removalEnd] === ' ' || code[removalEnd] === '\t')) {
|
|
623
|
+
removalEnd += 1;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
while (removalStart > 0 &&
|
|
628
|
+
(code[removalStart - 1] === ' ' || code[removalStart - 1] === '\t')) {
|
|
629
|
+
removalStart -= 1;
|
|
630
|
+
}
|
|
631
|
+
if (code[removalStart - 1] === ',') {
|
|
632
|
+
removalStart -= 1;
|
|
633
|
+
while (removalStart > 0 &&
|
|
634
|
+
(code[removalStart - 1] === ' ' || code[removalStart - 1] === '\t')) {
|
|
635
|
+
removalStart -= 1;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return {
|
|
640
|
+
end: removalEnd,
|
|
641
|
+
start: removalStart,
|
|
642
|
+
value: '',
|
|
643
|
+
};
|
|
644
|
+
};
|
|
645
|
+
const mergeEmptyRemovalRanges = (removals) => {
|
|
646
|
+
if (removals.length <= 1) {
|
|
647
|
+
return removals;
|
|
648
|
+
}
|
|
649
|
+
const sorted = [...removals].sort((a, b) => a.start - b.start);
|
|
650
|
+
const merged = [];
|
|
651
|
+
sorted.forEach((removal) => {
|
|
652
|
+
const previous = merged[merged.length - 1];
|
|
653
|
+
if (previous &&
|
|
654
|
+
previous.value === '' &&
|
|
655
|
+
removal.value === '' &&
|
|
656
|
+
removal.start <= previous.end) {
|
|
657
|
+
previous.end = Math.max(previous.end, removal.end);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
merged.push({ ...removal });
|
|
661
|
+
});
|
|
662
|
+
return merged;
|
|
663
|
+
};
|
|
664
|
+
const collectUnusedImportRemovals = (code, program, referencedNames, removableNames, preserveSideEffectImportLocals) => {
|
|
665
|
+
const removals = [];
|
|
666
|
+
program.body.forEach((statement) => {
|
|
667
|
+
if (statement.type !== 'ImportDeclaration') {
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
const localNames = collectImportLocalNames(statement);
|
|
671
|
+
const removableLocalNames = localNames.filter((localName) => removableNames.has(localName));
|
|
672
|
+
if (removableLocalNames.length > 0 &&
|
|
673
|
+
removableLocalNames.length === localNames.length &&
|
|
674
|
+
removableLocalNames.every((localName) => !referencedNames.has(localName))) {
|
|
675
|
+
if (removableLocalNames.some((localName) => preserveSideEffectImportLocals.has(localName))) {
|
|
676
|
+
removals.push({
|
|
677
|
+
end: statement.end,
|
|
678
|
+
start: statement.start,
|
|
679
|
+
value: `import ${code.slice(statement.source.start, statement.source.end)};`,
|
|
680
|
+
});
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
removals.push(expandImportRemovalRange(code, statement.start, statement.end));
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
const { specifiers } = statement;
|
|
687
|
+
if (!Array.isArray(specifiers) || specifiers.length <= 1) {
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
specifiers.forEach((specifier) => {
|
|
691
|
+
if (!isNode(specifier)) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const localName = getImportSpecifierLocalName(specifier);
|
|
695
|
+
if (localName &&
|
|
696
|
+
removableNames.has(localName) &&
|
|
697
|
+
!referencedNames.has(localName)) {
|
|
698
|
+
removals.push(expandImportSpecifierRemovalRange(code, specifier.start, specifier.end));
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
});
|
|
702
|
+
return removals;
|
|
703
|
+
};
|
|
704
|
+
const collectUnusedTopLevelDeclarationRemovals = (code, program, referencedNames, removableNames) => {
|
|
705
|
+
const removals = [];
|
|
706
|
+
program.body.forEach((statement) => {
|
|
707
|
+
if (statement.type !== 'VariableDeclaration') {
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
const localNames = [...collectTopLevelBindings(statement)];
|
|
711
|
+
if (localNames.length > 0 &&
|
|
712
|
+
localNames.every((localName) => removableNames.has(localName)) &&
|
|
713
|
+
localNames.every((localName) => !referencedNames.has(localName))) {
|
|
714
|
+
removals.push(expandImportRemovalRange(code, statement.start, statement.end));
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
return removals;
|
|
718
|
+
};
|
|
719
|
+
const collectUnusedGeneratedHelperDeclarationRemovals = (code, program, referencedNames) => {
|
|
720
|
+
const removals = [];
|
|
721
|
+
program.body.forEach((statement) => {
|
|
722
|
+
if (statement.type !== 'VariableDeclaration') {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const localNames = [...collectTopLevelBindings(statement)];
|
|
726
|
+
if (localNames.length > 0 &&
|
|
727
|
+
localNames.every((localName) => GENERATED_HELPER_NAME_RE.test(localName)) &&
|
|
728
|
+
localNames.every((localName) => !referencedNames.has(localName))) {
|
|
729
|
+
removals.push(expandImportRemovalRange(code, statement.start, statement.end));
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
return removals;
|
|
733
|
+
};
|
|
734
|
+
const collectTopLevelExpressionStatementRemovals = (code, statements, topLevelBindings, removableExpressionRefs) => {
|
|
735
|
+
const removals = [];
|
|
736
|
+
statements.forEach((statement) => {
|
|
737
|
+
if (statement.node.type !== 'ExpressionStatement') {
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
const { expression } = statement.node;
|
|
741
|
+
const isPureExpression = expression.type === 'Identifier' ||
|
|
742
|
+
expression.type === 'Literal' ||
|
|
743
|
+
expression.type === 'ObjectExpression' ||
|
|
744
|
+
expression.type === 'ArrayExpression' ||
|
|
745
|
+
expression.type === 'ArrowFunctionExpression' ||
|
|
746
|
+
expression.type === 'FunctionExpression' ||
|
|
747
|
+
(expression.type === 'TemplateLiteral' &&
|
|
748
|
+
expression.expressions.length === 0);
|
|
749
|
+
if (!isPureExpression) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
const localReferences = [...statement.references].filter((name) => topLevelBindings.has(name));
|
|
753
|
+
if (localReferences.length > 0 &&
|
|
754
|
+
localReferences.every((name) => removableExpressionRefs.has(name))) {
|
|
755
|
+
removals.push(expandImportRemovalRange(code, statement.node.start, statement.node.end));
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
return removals;
|
|
759
|
+
};
|
|
760
|
+
const collectEmptyTopLevelBlockRemovals = (code, program) => {
|
|
761
|
+
const removals = [];
|
|
762
|
+
program.body.forEach((statement) => {
|
|
763
|
+
if (statement.type !== 'BlockStatement' || statement.body.length > 0) {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
removals.push(expandImportRemovalRange(code, statement.start, statement.end));
|
|
767
|
+
});
|
|
768
|
+
return removals;
|
|
769
|
+
};
|
|
770
|
+
const removeUnusedAfterReplacement = (code, filename, initialRemovableNames, removableExpressionRefs, preserveSideEffectImportLocals) => {
|
|
771
|
+
let current = code;
|
|
772
|
+
const cumulativeRemovableNames = new Set(initialRemovableNames);
|
|
773
|
+
const applyIfParsable = (next) => {
|
|
774
|
+
try {
|
|
775
|
+
parseOxc(next, filename);
|
|
776
|
+
return next;
|
|
777
|
+
}
|
|
778
|
+
catch {
|
|
779
|
+
return current;
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
for (let idx = 0; idx < 5; idx += 1) {
|
|
783
|
+
const previous = current;
|
|
784
|
+
const program = parseOxc(current, filename);
|
|
785
|
+
const statements = collectTopLevelStatementInfos(program);
|
|
786
|
+
const removableNames = collectRemovableNamesFromStatements(statements, cumulativeRemovableNames);
|
|
787
|
+
removableNames.forEach((name) => cumulativeRemovableNames.add(name));
|
|
788
|
+
const referencedNames = collectReferencedNames(program);
|
|
789
|
+
const topLevelBindings = collectTopLevelBindingsFromStatements(statements);
|
|
790
|
+
const scopedBindings = collectScopedBindingInfos(program);
|
|
791
|
+
const removals = mergeEmptyRemovalRanges([
|
|
792
|
+
...collectUnusedScopedDeclarationRemovals(current, scopedBindings, cumulativeRemovableNames),
|
|
793
|
+
...collectUnusedTopLevelDeclarationRemovals(current, program, referencedNames, cumulativeRemovableNames),
|
|
794
|
+
...collectUnusedGeneratedHelperDeclarationRemovals(current, program, referencedNames),
|
|
795
|
+
...collectUnusedImportRemovals(current, program, referencedNames, cumulativeRemovableNames, preserveSideEffectImportLocals),
|
|
796
|
+
...collectTopLevelExpressionStatementRemovals(current, statements, topLevelBindings, removableExpressionRefs),
|
|
797
|
+
...collectEmptyTopLevelBlockRemovals(current, program),
|
|
798
|
+
]);
|
|
799
|
+
current =
|
|
800
|
+
removals.length > 0
|
|
801
|
+
? applyIfParsable(applyReplacements(current, removals))
|
|
802
|
+
: current;
|
|
803
|
+
if (current === previous) {
|
|
804
|
+
return current;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
return current;
|
|
808
|
+
};
|
|
809
|
+
const getMemberName = (node) => {
|
|
810
|
+
if (node.computed) {
|
|
811
|
+
return node.property.type === 'Literal' &&
|
|
812
|
+
typeof node.property.value === 'string'
|
|
813
|
+
? node.property.value
|
|
814
|
+
: null;
|
|
815
|
+
}
|
|
816
|
+
return node.property.type === 'Identifier' ? node.property.name : null;
|
|
817
|
+
};
|
|
818
|
+
const unwrapQualifiedExpression = (node) => {
|
|
819
|
+
if (node.type === 'TSAsExpression' ||
|
|
820
|
+
node.type === 'TSSatisfiesExpression' ||
|
|
821
|
+
node.type === 'TSNonNullExpression' ||
|
|
822
|
+
node.type === 'TSTypeAssertion' ||
|
|
823
|
+
node.type === 'ParenthesizedExpression') {
|
|
824
|
+
return unwrapQualifiedExpression(node.expression);
|
|
825
|
+
}
|
|
826
|
+
if (node.type === 'SequenceExpression') {
|
|
827
|
+
const sequence = node;
|
|
828
|
+
return unwrapQualifiedExpression(sequence.expressions[sequence.expressions.length - 1] ?? node);
|
|
829
|
+
}
|
|
830
|
+
return node;
|
|
831
|
+
};
|
|
832
|
+
const getRootIdentifier = (node) => {
|
|
833
|
+
const expression = unwrapQualifiedExpression(node);
|
|
834
|
+
if (expression.type === 'Identifier') {
|
|
835
|
+
return expression;
|
|
836
|
+
}
|
|
837
|
+
if (expression.type === 'MemberExpression') {
|
|
838
|
+
return getRootIdentifier(expression.object);
|
|
839
|
+
}
|
|
840
|
+
if (expression.type === 'CallExpression') {
|
|
841
|
+
return getRootIdentifier(expression.callee);
|
|
842
|
+
}
|
|
843
|
+
return null;
|
|
844
|
+
};
|
|
845
|
+
const getQualifiedName = (node) => {
|
|
846
|
+
const expression = unwrapQualifiedExpression(node);
|
|
847
|
+
if (expression.type === 'Identifier') {
|
|
848
|
+
return expression.name;
|
|
849
|
+
}
|
|
850
|
+
if (expression.type === 'MemberExpression') {
|
|
851
|
+
const object = getQualifiedName(expression.object);
|
|
852
|
+
const member = getMemberName(expression);
|
|
853
|
+
return object && member ? `${object}.${member}` : null;
|
|
854
|
+
}
|
|
855
|
+
if (expression.type === 'CallExpression') {
|
|
856
|
+
return getQualifiedName(expression.callee);
|
|
857
|
+
}
|
|
858
|
+
return null;
|
|
859
|
+
};
|
|
860
|
+
const resolveDefinedProcessor = (callee, definedProcessors) => {
|
|
861
|
+
const qualified = getQualifiedName(callee);
|
|
862
|
+
if (qualified) {
|
|
863
|
+
const definedProcessor = definedProcessors.get(qualified);
|
|
864
|
+
if (definedProcessor) {
|
|
865
|
+
return {
|
|
866
|
+
collapseQualifiedCallee: qualified.includes('.'),
|
|
867
|
+
definedProcessor,
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
const root = getRootIdentifier(callee);
|
|
872
|
+
if (!root) {
|
|
873
|
+
return null;
|
|
874
|
+
}
|
|
875
|
+
const definedProcessor = definedProcessors.get(root.name);
|
|
876
|
+
return definedProcessor
|
|
877
|
+
? {
|
|
878
|
+
collapseQualifiedCallee: false,
|
|
879
|
+
definedProcessor,
|
|
880
|
+
}
|
|
881
|
+
: null;
|
|
882
|
+
};
|
|
883
|
+
const isCallTagOfTaggedTemplate = (node, parent) => parent?.type === 'TaggedTemplateExpression' && parent.tag === node;
|
|
884
|
+
const expandReplacementTarget = (target, ancestors) => {
|
|
885
|
+
let current = target;
|
|
886
|
+
for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
|
|
887
|
+
const ancestor = ancestors[idx];
|
|
888
|
+
if (ancestor.type === 'SequenceExpression' &&
|
|
889
|
+
ancestor.expressions[ancestor.expressions.length - 1] === current) {
|
|
890
|
+
current = ancestor;
|
|
891
|
+
}
|
|
892
|
+
else if (ancestor.type === 'ParenthesizedExpression' &&
|
|
893
|
+
ancestor.expression === current) {
|
|
894
|
+
current = ancestor;
|
|
895
|
+
}
|
|
896
|
+
else {
|
|
897
|
+
break;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return current;
|
|
901
|
+
};
|
|
902
|
+
const collectProcessorUsages = (program, definedProcessors) => {
|
|
903
|
+
const usages = [];
|
|
904
|
+
const walk = (node, ancestors, parent = null) => {
|
|
905
|
+
if (node.type === 'TaggedTemplateExpression') {
|
|
906
|
+
const callee = node.tag;
|
|
907
|
+
const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
|
|
908
|
+
if (resolvedProcessor) {
|
|
909
|
+
usages.push({
|
|
910
|
+
ancestors,
|
|
911
|
+
callee,
|
|
912
|
+
collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
|
|
913
|
+
definedProcessor: resolvedProcessor.definedProcessor,
|
|
914
|
+
kind: 'template',
|
|
915
|
+
replacementTarget: expandReplacementTarget(node, ancestors),
|
|
916
|
+
target: node,
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
else if (node.type === 'CallExpression' &&
|
|
921
|
+
!isCallTagOfTaggedTemplate(node, parent)) {
|
|
922
|
+
const { callee } = node;
|
|
923
|
+
const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
|
|
924
|
+
if (resolvedProcessor) {
|
|
925
|
+
usages.push({
|
|
926
|
+
ancestors,
|
|
927
|
+
callee,
|
|
928
|
+
collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
|
|
929
|
+
definedProcessor: resolvedProcessor.definedProcessor,
|
|
930
|
+
kind: 'call',
|
|
931
|
+
replacementTarget: expandReplacementTarget(node, ancestors),
|
|
932
|
+
target: node,
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
getChildren(node).forEach((child) => walk(child, [...ancestors, node], node));
|
|
937
|
+
};
|
|
938
|
+
walk(program, []);
|
|
939
|
+
return usages.sort((a, b) => a.target.start - b.target.start);
|
|
940
|
+
};
|
|
941
|
+
const expressionSpan = (expression) => ({
|
|
942
|
+
end: expression.end,
|
|
943
|
+
start: expression.start,
|
|
944
|
+
});
|
|
945
|
+
const collectCallArgumentSpans = (node) => {
|
|
946
|
+
const expression = unwrapQualifiedExpression(node);
|
|
947
|
+
if (expression.type === 'CallExpression') {
|
|
948
|
+
const call = expression;
|
|
949
|
+
const calleeSpans = collectCallArgumentSpans(call.callee);
|
|
950
|
+
const argumentSpans = call.arguments.flatMap((arg) => arg.type === 'SpreadElement' ? [] : [expressionSpan(arg)]);
|
|
951
|
+
return [...calleeSpans, ...argumentSpans];
|
|
952
|
+
}
|
|
953
|
+
if (expression.type === 'MemberExpression') {
|
|
954
|
+
return collectCallArgumentSpans(expression.object);
|
|
955
|
+
}
|
|
956
|
+
return [];
|
|
957
|
+
};
|
|
958
|
+
const collectUsageExpressionSpans = (usage) => {
|
|
959
|
+
const calleeSpans = collectCallArgumentSpans(usage.callee);
|
|
960
|
+
if (usage.kind === 'template') {
|
|
961
|
+
return [
|
|
962
|
+
...calleeSpans,
|
|
963
|
+
...usage.target.quasi.expressions.map((expression) => expressionSpan(expression)),
|
|
964
|
+
];
|
|
965
|
+
}
|
|
966
|
+
return [
|
|
967
|
+
...calleeSpans,
|
|
968
|
+
...usage.target.arguments.flatMap((arg) => arg.type === 'SpreadElement' ? [] : [expressionSpan(arg)]),
|
|
969
|
+
];
|
|
970
|
+
};
|
|
971
|
+
const literalExpressionValue = (expression, code, source, location) => {
|
|
972
|
+
if (expression.type !== 'Literal') {
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
if (expression.value === null ||
|
|
976
|
+
typeof expression.value === 'string' ||
|
|
977
|
+
typeof expression.value === 'number' ||
|
|
978
|
+
typeof expression.value === 'boolean') {
|
|
979
|
+
let type;
|
|
980
|
+
if (expression.value === null) {
|
|
981
|
+
type = 'NullLiteral';
|
|
982
|
+
}
|
|
983
|
+
else if (typeof expression.value === 'string') {
|
|
984
|
+
type = 'StringLiteral';
|
|
985
|
+
}
|
|
986
|
+
else if (typeof expression.value === 'number') {
|
|
987
|
+
type = 'NumericLiteral';
|
|
988
|
+
}
|
|
989
|
+
else {
|
|
990
|
+
type = 'BooleanLiteral';
|
|
991
|
+
}
|
|
992
|
+
const ex = expression.value === null
|
|
993
|
+
? { loc: location, type }
|
|
994
|
+
: {
|
|
995
|
+
loc: location,
|
|
996
|
+
type,
|
|
997
|
+
value: expression.value,
|
|
998
|
+
};
|
|
999
|
+
return {
|
|
1000
|
+
buildCodeFrameError: (message) => buildCodeFrameError(code, location, message),
|
|
1001
|
+
ex,
|
|
1002
|
+
kind: ValueType.CONST,
|
|
1003
|
+
source,
|
|
1004
|
+
value: expression.value,
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
return null;
|
|
1008
|
+
};
|
|
1009
|
+
const expressionValue = (expression, code, loc, filename) => {
|
|
1010
|
+
const source = code.slice(expression.start, expression.end);
|
|
1011
|
+
const location = getSourceLocation(expression.start, expression.end, loc, filename);
|
|
1012
|
+
const literal = literalExpressionValue(expression, code, source, location);
|
|
1013
|
+
if (literal) {
|
|
1014
|
+
return literal;
|
|
1015
|
+
}
|
|
1016
|
+
const helperCallName = expression.type === 'CallExpression' &&
|
|
1017
|
+
expression.arguments.length === 0 &&
|
|
1018
|
+
expression.callee.type === 'Identifier' &&
|
|
1019
|
+
GENERATED_HELPER_NAME_RE.test(expression.callee.name)
|
|
1020
|
+
? expression.callee.name
|
|
1021
|
+
: null;
|
|
1022
|
+
let ex;
|
|
1023
|
+
if (expression.type === 'Identifier') {
|
|
1024
|
+
ex = { loc: location, name: expression.name, type: 'Identifier' };
|
|
1025
|
+
}
|
|
1026
|
+
else if (helperCallName) {
|
|
1027
|
+
ex = { loc: location, name: helperCallName, type: 'Identifier' };
|
|
1028
|
+
}
|
|
1029
|
+
else {
|
|
1030
|
+
ex = {
|
|
1031
|
+
loc: location,
|
|
1032
|
+
name: code.slice(expression.start, expression.end),
|
|
1033
|
+
type: 'Identifier',
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
1036
|
+
return {
|
|
1037
|
+
buildCodeFrameError: (message) => buildCodeFrameError(code, location, message),
|
|
1038
|
+
ex,
|
|
1039
|
+
kind: expression.type === 'ArrowFunctionExpression' ||
|
|
1040
|
+
expression.type === 'FunctionExpression'
|
|
1041
|
+
? ValueType.FUNCTION
|
|
1042
|
+
: ValueType.LAZY,
|
|
1043
|
+
source,
|
|
1044
|
+
};
|
|
1045
|
+
};
|
|
1046
|
+
const withCurrentExpressionLocation = (value, expression, loc, filename) => {
|
|
1047
|
+
const location = getSourceLocation(expression.start, expression.end, loc, filename);
|
|
1048
|
+
if (value.kind === ValueType.CONST) {
|
|
1049
|
+
return {
|
|
1050
|
+
...value,
|
|
1051
|
+
ex: {
|
|
1052
|
+
...value.ex,
|
|
1053
|
+
loc: location,
|
|
1054
|
+
},
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
1057
|
+
if (value.kind === ValueType.FUNCTION) {
|
|
1058
|
+
return {
|
|
1059
|
+
...value,
|
|
1060
|
+
ex: {
|
|
1061
|
+
...value.ex,
|
|
1062
|
+
loc: location,
|
|
1063
|
+
},
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
return {
|
|
1067
|
+
...value,
|
|
1068
|
+
ex: {
|
|
1069
|
+
...value.ex,
|
|
1070
|
+
loc: location,
|
|
1071
|
+
},
|
|
1072
|
+
};
|
|
1073
|
+
};
|
|
1074
|
+
const shiftExpressionValue = (expressionValues, expression, code, loc, filename) => expressionValues.length > 0
|
|
1075
|
+
? withCurrentExpressionLocation(expressionValues.shift(), expression, loc, filename)
|
|
1076
|
+
: expressionValue(expression, code, loc, filename);
|
|
1077
|
+
const zipTemplate = (template, code, loc, filename, expressionValues) => {
|
|
1078
|
+
const parts = template.quasi.quasis.flatMap((quasi, idx) => {
|
|
1079
|
+
const expression = template.quasi.expressions[idx];
|
|
1080
|
+
const templateElement = {
|
|
1081
|
+
...quasi,
|
|
1082
|
+
loc: getSourceLocation(quasi.start, quasi.end, loc, filename),
|
|
1083
|
+
};
|
|
1084
|
+
return [
|
|
1085
|
+
templateElement,
|
|
1086
|
+
expression
|
|
1087
|
+
? shiftExpressionValue(expressionValues, expression, code, loc, filename)
|
|
1088
|
+
: null,
|
|
1089
|
+
].filter(isNotNull);
|
|
1090
|
+
});
|
|
1091
|
+
return ['template', parts];
|
|
1092
|
+
};
|
|
1093
|
+
const buildCalleeParams = (node, code, loc, filename, expressionValues, collapseQualifiedCallee = false) => {
|
|
1094
|
+
const expression = unwrapQualifiedExpression(node);
|
|
1095
|
+
if (collapseQualifiedCallee &&
|
|
1096
|
+
(expression.type === 'Identifier' || expression.type === 'MemberExpression')) {
|
|
1097
|
+
return [['callee', expression]];
|
|
1098
|
+
}
|
|
1099
|
+
if (expression.type === 'Identifier') {
|
|
1100
|
+
return [['callee', { name: expression.name, type: 'Identifier' }]];
|
|
1101
|
+
}
|
|
1102
|
+
if (expression.type === 'MemberExpression') {
|
|
1103
|
+
const params = buildCalleeParams(expression.object, code, loc, filename, expressionValues, collapseQualifiedCallee);
|
|
1104
|
+
const member = getMemberName(expression);
|
|
1105
|
+
return params && member ? [...params, ['member', member]] : null;
|
|
1106
|
+
}
|
|
1107
|
+
if (expression.type === 'CallExpression') {
|
|
1108
|
+
const call = expression;
|
|
1109
|
+
const params = buildCalleeParams(call.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
|
|
1110
|
+
if (!params) {
|
|
1111
|
+
return null;
|
|
1112
|
+
}
|
|
1113
|
+
const callValues = call.arguments
|
|
1114
|
+
.filter((arg) => arg.type !== 'SpreadElement')
|
|
1115
|
+
.map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
|
|
1116
|
+
return [...params, ['call', ...callValues]];
|
|
1117
|
+
}
|
|
1118
|
+
return null;
|
|
1119
|
+
};
|
|
1120
|
+
const buildParams = (usage, code, loc, filename, expressionValues, collapseQualifiedCallee) => {
|
|
1121
|
+
const params = buildCalleeParams(usage.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
|
|
1122
|
+
if (!params) {
|
|
1123
|
+
return null;
|
|
1124
|
+
}
|
|
1125
|
+
if (usage.kind === 'template') {
|
|
1126
|
+
return [
|
|
1127
|
+
...params,
|
|
1128
|
+
zipTemplate(usage.target, code, loc, filename, expressionValues),
|
|
1129
|
+
];
|
|
1130
|
+
}
|
|
1131
|
+
const callValues = usage.target.arguments
|
|
1132
|
+
.filter((arg) => arg.type !== 'SpreadElement')
|
|
1133
|
+
.map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
|
|
1134
|
+
return [...params, ['call', ...callValues]];
|
|
1135
|
+
};
|
|
1136
|
+
const getPropertyKeyName = (property, code) => {
|
|
1137
|
+
const { key } = property;
|
|
1138
|
+
if (!isNode(key)) {
|
|
1139
|
+
return null;
|
|
1140
|
+
}
|
|
1141
|
+
if (key.type === 'Identifier') {
|
|
1142
|
+
return key.name;
|
|
1143
|
+
}
|
|
1144
|
+
if (key.type === 'Literal') {
|
|
1145
|
+
return String(key.value);
|
|
1146
|
+
}
|
|
1147
|
+
return typeof key.start === 'number' && typeof key.end === 'number'
|
|
1148
|
+
? code.slice(key.start, key.end)
|
|
1149
|
+
: null;
|
|
1150
|
+
};
|
|
1151
|
+
const getDisplayName = (ancestors, idx, code, filename) => {
|
|
1152
|
+
const owner = [...ancestors].reverse().find((node) => {
|
|
1153
|
+
return (node.type === 'Property' ||
|
|
1154
|
+
node.type === 'JSXOpeningElement' ||
|
|
1155
|
+
node.type === 'VariableDeclarator');
|
|
1156
|
+
});
|
|
1157
|
+
if (owner?.type === 'Property') {
|
|
1158
|
+
const keyName = getPropertyKeyName(owner, code);
|
|
1159
|
+
if (keyName) {
|
|
1160
|
+
return keyName;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
else if (owner?.type === 'JSXOpeningElement') {
|
|
1164
|
+
const { name } = owner;
|
|
1165
|
+
if (isNode(name) && name.type === 'JSXIdentifier') {
|
|
1166
|
+
return name.name;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
else if (owner?.type === 'VariableDeclarator') {
|
|
1170
|
+
const { id } = owner;
|
|
1171
|
+
if (isNode(id) && id.type === 'Identifier') {
|
|
1172
|
+
return id.name;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
let displayName = basename(filename ?? 'unknown').replace(/\.[a-z\d]+$/, '');
|
|
1176
|
+
if (filename && /^index\.[a-z\d]+$/.test(basename(filename))) {
|
|
1177
|
+
displayName = basename(dirname(filename));
|
|
1178
|
+
}
|
|
1179
|
+
if (!displayName) {
|
|
1180
|
+
throw new Error("Couldn't determine a name for the component. Ensure that it's either:\n" +
|
|
1181
|
+
'- Assigned to a variable\n' +
|
|
1182
|
+
'- Is an object property\n' +
|
|
1183
|
+
'- Is a prop in a JSX element\n');
|
|
1184
|
+
}
|
|
1185
|
+
return `${displayName}${idx}`;
|
|
1186
|
+
};
|
|
1187
|
+
const getTagOwner = (ancestors) => {
|
|
1188
|
+
const owner = [...ancestors]
|
|
1189
|
+
.reverse()
|
|
1190
|
+
.find((node) => node.type === 'Property' ||
|
|
1191
|
+
node.type === 'JSXOpeningElement' ||
|
|
1192
|
+
node.type === 'VariableDeclarator');
|
|
1193
|
+
return owner ?? null;
|
|
1194
|
+
};
|
|
1195
|
+
const isTagReferenced = (program, ancestors) => {
|
|
1196
|
+
const owner = getTagOwner(ancestors);
|
|
1197
|
+
if (owner?.type !== 'VariableDeclarator') {
|
|
1198
|
+
return true;
|
|
1199
|
+
}
|
|
1200
|
+
const { id } = owner;
|
|
1201
|
+
if (!isNode(id) || id.type !== 'Identifier') {
|
|
1202
|
+
return true;
|
|
1203
|
+
}
|
|
1204
|
+
if (ancestors.some((node) => node.type === 'ExportNamedDeclaration')) {
|
|
1205
|
+
return true;
|
|
1206
|
+
}
|
|
1207
|
+
let referenced = false;
|
|
1208
|
+
visit(program, (node, parent) => {
|
|
1209
|
+
const referenceName = node.type === 'Identifier' || node.type === 'JSXIdentifier'
|
|
1210
|
+
? node.name
|
|
1211
|
+
: null;
|
|
1212
|
+
if (referenced ||
|
|
1213
|
+
referenceName !== id.name ||
|
|
1214
|
+
(node.type === 'Identifier' &&
|
|
1215
|
+
node.start === id.start &&
|
|
1216
|
+
node.end === id.end)) {
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
referenced = isNodeReference(node, parent);
|
|
1220
|
+
});
|
|
1221
|
+
return referenced;
|
|
1222
|
+
};
|
|
1223
|
+
const isReplacementPure = (replacement) => replacement.type === 'CallExpression';
|
|
1224
|
+
const createProcessor = (definedProcessor, params, target, replacementTarget, ancestors, fileContext, options, code, loc, idx, isReferenced, usedNames, replacements) => {
|
|
1225
|
+
const [Processor, tagSource] = definedProcessor;
|
|
1226
|
+
const astService = createOxcAstService(usedNames);
|
|
1227
|
+
const replacer = (replacement, isPure) => {
|
|
1228
|
+
const next = typeof replacement === 'function' ? replacement(target) : replacement;
|
|
1229
|
+
const replacementCode = expressionToCode(next);
|
|
1230
|
+
replacements.push({
|
|
1231
|
+
start: replacementTarget.start,
|
|
1232
|
+
end: replacementTarget.end,
|
|
1233
|
+
value: isPure && isReplacementPure(next)
|
|
1234
|
+
? `/*#__PURE__*/${replacementCode}`
|
|
1235
|
+
: replacementCode,
|
|
1236
|
+
});
|
|
1237
|
+
};
|
|
1238
|
+
try {
|
|
1239
|
+
let displayName;
|
|
1240
|
+
try {
|
|
1241
|
+
displayName = getDisplayName(ancestors, idx, code, fileContext.filename);
|
|
1242
|
+
}
|
|
1243
|
+
catch (error) {
|
|
1244
|
+
if (error instanceof Error &&
|
|
1245
|
+
error.message.startsWith("Couldn't determine a name for the component")) {
|
|
1246
|
+
let displayNameNode = target;
|
|
1247
|
+
if (target.type === 'TaggedTemplateExpression') {
|
|
1248
|
+
displayNameNode = target.tag;
|
|
1249
|
+
}
|
|
1250
|
+
else if (target.type === 'CallExpression') {
|
|
1251
|
+
displayNameNode = target.callee;
|
|
1252
|
+
}
|
|
1253
|
+
const pointerNode = displayNameNode.type === 'MemberExpression'
|
|
1254
|
+
? getRootIdentifier(displayNameNode) ?? displayNameNode
|
|
1255
|
+
: displayNameNode;
|
|
1256
|
+
throw buildCodeFrameError(code, getSourceLocation(pointerNode.start, pointerNode.end, loc, fileContext.filename), error.message);
|
|
1257
|
+
}
|
|
1258
|
+
throw error;
|
|
1259
|
+
}
|
|
1260
|
+
return {
|
|
1261
|
+
astService,
|
|
1262
|
+
processor: new Processor(params, tagSource, astService, getSourceLocation(target.start, target.end, loc, fileContext.filename), replacer, displayName, isReferenced, idx, options, fileContext),
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
catch (e) {
|
|
1266
|
+
if (e === BaseProcessor.SKIP) {
|
|
1267
|
+
return null;
|
|
1268
|
+
}
|
|
1269
|
+
if (typeof e === 'symbol' &&
|
|
1270
|
+
e.description === BaseProcessor.SKIP.description) {
|
|
1271
|
+
if (!didWarnSkipSymbolMismatch) {
|
|
1272
|
+
didWarnSkipSymbolMismatch = true;
|
|
1273
|
+
// eslint-disable-next-line no-console
|
|
1274
|
+
console.warn([
|
|
1275
|
+
"[wyw-in-js] Processor threw Symbol('skip') that does not match BaseProcessor.SKIP identity.",
|
|
1276
|
+
'This usually means duplicate copies of @wyw-in-js/processor-utils (or the processor) are bundled/installed.',
|
|
1277
|
+
'Consider deduping dependencies to avoid subtle issues (instanceof checks, sentinels, etc).',
|
|
1278
|
+
].join('\n'));
|
|
1279
|
+
}
|
|
1280
|
+
return null;
|
|
1281
|
+
}
|
|
1282
|
+
throw e;
|
|
1283
|
+
}
|
|
1284
|
+
};
|
|
1285
|
+
export const applyOxcProcessors = (code, fileContext, options, callback, cleanupUnused = false) => {
|
|
1286
|
+
const filename = fileContext.filename ?? 'unknown.js';
|
|
1287
|
+
const eventEmitter = options.eventEmitter ?? EventEmitter.dummy;
|
|
1288
|
+
let workingCode = code;
|
|
1289
|
+
let program = parseOxc(workingCode, filename);
|
|
1290
|
+
const definedProcessors = new Map();
|
|
1291
|
+
const removableImportLocals = new Set();
|
|
1292
|
+
const removableExpressionRefs = new Set();
|
|
1293
|
+
eventEmitter.perf('transform:preeval:processTemplate:imports', () => {
|
|
1294
|
+
const imports = eventEmitter.perf('transform:preeval:processTemplate:imports:analysis', () => collectOxcProcessorImportsFromProgram(program, workingCode));
|
|
1295
|
+
eventEmitter.perf('transform:preeval:processTemplate:imports:lookup', () => {
|
|
1296
|
+
imports.forEach((item) => {
|
|
1297
|
+
const localName = item.local.name ?? item.local.code;
|
|
1298
|
+
if (item.imported === 'side-effect' || !localName) {
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
const [processor, tagSource] = getProcessorForImport({
|
|
1302
|
+
imported: item.imported,
|
|
1303
|
+
source: item.source,
|
|
1304
|
+
}, filename, options);
|
|
1305
|
+
if (processor) {
|
|
1306
|
+
definedProcessors.set(localName, [processor, tagSource]);
|
|
1307
|
+
removableImportLocals.add(localName);
|
|
1308
|
+
const rootLocalName = localName.split('.')[0];
|
|
1309
|
+
if (rootLocalName) {
|
|
1310
|
+
removableImportLocals.add(rootLocalName);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
});
|
|
1314
|
+
});
|
|
1315
|
+
});
|
|
1316
|
+
if (definedProcessors.size === 0) {
|
|
1317
|
+
return {
|
|
1318
|
+
code: workingCode,
|
|
1319
|
+
processors: [],
|
|
1320
|
+
staticValueCandidates: [],
|
|
1321
|
+
staticValues: [],
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
let processorUsages = eventEmitter.perf('transform:preeval:processTemplate:usages', () => collectProcessorUsages(program, definedProcessors));
|
|
1325
|
+
if (processorUsages.length === 0) {
|
|
1326
|
+
return {
|
|
1327
|
+
code: workingCode,
|
|
1328
|
+
processors: [],
|
|
1329
|
+
staticValueCandidates: [],
|
|
1330
|
+
staticValues: [],
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
const targetExpressionSpans = processorUsages.flatMap(collectUsageExpressionSpans);
|
|
1334
|
+
const extracted = targetExpressionSpans.length > 0
|
|
1335
|
+
? eventEmitter.perf('transform:preeval:processTemplate:deps', () => collectOxcExpressionDependencies(workingCode, filename, options.evaluate, targetExpressionSpans))
|
|
1336
|
+
: {
|
|
1337
|
+
code: workingCode,
|
|
1338
|
+
dependencyNames: [],
|
|
1339
|
+
expressionValues: [],
|
|
1340
|
+
staticValueCandidates: [],
|
|
1341
|
+
staticValues: [],
|
|
1342
|
+
};
|
|
1343
|
+
if (extracted.code !== workingCode) {
|
|
1344
|
+
workingCode = extracted.code;
|
|
1345
|
+
program = eventEmitter.perf('transform:preeval:processTemplate:reparse', () => parseOxc(workingCode, filename));
|
|
1346
|
+
processorUsages = eventEmitter.perf('transform:preeval:processTemplate:usages', () => collectProcessorUsages(program, definedProcessors));
|
|
1347
|
+
}
|
|
1348
|
+
const templateExpressionValues = extracted.expressionValues.map((value) => ({
|
|
1349
|
+
...value,
|
|
1350
|
+
buildCodeFrameError: (message) => buildCodeFrameError(code, value.ex.loc, message),
|
|
1351
|
+
}));
|
|
1352
|
+
const loc = createLocationLookup(workingCode);
|
|
1353
|
+
const usedNames = eventEmitter.perf('transform:preeval:processTemplate:usedNames', () => collectUsedNames(program));
|
|
1354
|
+
const addedImports = [];
|
|
1355
|
+
const replacements = [];
|
|
1356
|
+
const processors = [];
|
|
1357
|
+
extracted.dependencyNames.forEach((name) => removableImportLocals.add(name));
|
|
1358
|
+
eventEmitter.perf('transform:preeval:processTemplate:processors', () => {
|
|
1359
|
+
processorUsages.forEach((usage, idx) => {
|
|
1360
|
+
const params = buildParams(usage, workingCode, loc, filename, templateExpressionValues, usage.collapseQualifiedCallee);
|
|
1361
|
+
if (!params) {
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
const created = createProcessor(usage.definedProcessor, params, usage.target, usage.replacementTarget, usage.ancestors, fileContext, options, workingCode, loc, idx, isTagReferenced(program, usage.ancestors), usedNames, replacements);
|
|
1365
|
+
if (!created) {
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
const { astService, processor } = created;
|
|
1369
|
+
const owner = getTagOwner(usage.ancestors);
|
|
1370
|
+
if (owner?.type === 'VariableDeclarator') {
|
|
1371
|
+
const { id } = owner;
|
|
1372
|
+
if (isNode(id) && id.type === 'Identifier') {
|
|
1373
|
+
removableExpressionRefs.add(id.name);
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
processors.push(processor);
|
|
1377
|
+
callback(processor);
|
|
1378
|
+
addedImports.push(...astService.getAddedImports());
|
|
1379
|
+
});
|
|
1380
|
+
});
|
|
1381
|
+
const replacedCode = applyReplacements(workingCode, replacements);
|
|
1382
|
+
const codeWithAddedImports = insertAddedImports(replacedCode, program, addedImports);
|
|
1383
|
+
return {
|
|
1384
|
+
code: cleanupUnused
|
|
1385
|
+
? eventEmitter.perf('transform:preeval:processTemplate:cleanup', () => removeUnusedAfterReplacement(codeWithAddedImports, filename, removableImportLocals, new Set([...removableExpressionRefs, ...extracted.dependencyNames]), options.preserveSideEffectImportLocals ?? new Set()))
|
|
1386
|
+
: codeWithAddedImports,
|
|
1387
|
+
processors,
|
|
1388
|
+
staticValueCandidates: extracted.staticValueCandidates,
|
|
1389
|
+
staticValues: extracted.staticValues,
|
|
1390
|
+
};
|
|
1391
|
+
};
|