@wyw-in-js/transform 2.0.0-alpha.0 → 2.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/cache.js +7 -0
- package/esm/cache.js.map +1 -1
- package/esm/debug/fileReporter.js +35 -1
- package/esm/debug/fileReporter.js.map +1 -1
- package/esm/eval/broker.js +230 -89
- package/esm/eval/broker.js.map +1 -1
- package/esm/eval/protocol.js.map +1 -1
- package/esm/eval/runner.js +143 -24
- package/esm/eval/runner.js.map +1 -1
- package/esm/module.js +70 -43
- package/esm/module.js.map +1 -1
- package/esm/transform/Entrypoint.types.js.map +1 -1
- package/esm/transform/generators/processEntrypoint.js +35 -20
- package/esm/transform/generators/processEntrypoint.js.map +1 -1
- package/esm/transform/generators/resolveImports.js +48 -2
- package/esm/transform/generators/resolveImports.js.map +1 -1
- package/esm/transform/generators/resolveStaticOxcValues/cache.js +182 -0
- package/esm/transform/generators/resolveStaticOxcValues/cache.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js +146 -0
- package/esm/transform/generators/resolveStaticOxcValues/candidateResolver.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/dependencies.js +33 -0
- package/esm/transform/generators/resolveStaticOxcValues/dependencies.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/environment.js +29 -0
- package/esm/transform/generators/resolveStaticOxcValues/environment.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js +351 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportResolver.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js +253 -0
- package/esm/transform/generators/resolveStaticOxcValues/exportTargets.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js +310 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssign.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +74 -0
- package/esm/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +229 -0
- package/esm/transform/generators/resolveStaticOxcValues/opaqueRuntime.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js +172 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js +476 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorStaticModel.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js +55 -0
- package/esm/transform/generators/resolveStaticOxcValues/processorTarget.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js +123 -0
- package/esm/transform/generators/resolveStaticOxcValues/programAnalysis.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/prune.js +186 -0
- package/esm/transform/generators/resolveStaticOxcValues/prune.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +183 -0
- package/esm/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js +298 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpression.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +298 -0
- package/esm/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/types.js +3 -0
- package/esm/transform/generators/resolveStaticOxcValues/types.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +59 -0
- package/esm/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js.map +1 -0
- package/esm/transform/generators/resolveStaticOxcValues.js +1 -2910
- package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -1
- package/esm/transform/generators/transform.js +57 -45
- package/esm/transform/generators/transform.js.map +1 -1
- package/esm/transform/helpers/loadWywOptions.js +33 -4
- package/esm/transform/helpers/loadWywOptions.js.map +1 -1
- package/esm/transform.js +3 -1
- package/esm/transform.js.map +1 -1
- package/esm/utils/EventEmitter.js +42 -9
- package/esm/utils/EventEmitter.js.map +1 -1
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js +160 -0
- package/esm/utils/applyOxcProcessors/applyOxcProcessors.js.map +1 -0
- package/esm/utils/applyOxcProcessors/cleanupBindings.js +157 -0
- package/esm/utils/applyOxcProcessors/cleanupBindings.js.map +1 -0
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js +431 -0
- package/esm/utils/applyOxcProcessors/cleanupRemovals.js.map +1 -0
- package/esm/utils/applyOxcProcessors/displayName.js +93 -0
- package/esm/utils/applyOxcProcessors/displayName.js.map +1 -0
- package/esm/utils/applyOxcProcessors/expressionValues.js +152 -0
- package/esm/utils/applyOxcProcessors/expressionValues.js.map +1 -0
- package/esm/utils/applyOxcProcessors/processorFactory.js +62 -0
- package/esm/utils/applyOxcProcessors/processorFactory.js.map +1 -0
- package/esm/utils/applyOxcProcessors/processorUsages.js +143 -0
- package/esm/utils/applyOxcProcessors/processorUsages.js.map +1 -0
- package/esm/utils/applyOxcProcessors/sameFileStaticValues.js +207 -0
- package/esm/utils/applyOxcProcessors/sameFileStaticValues.js.map +1 -0
- package/esm/utils/applyOxcProcessors/shared.js +29 -0
- package/esm/utils/applyOxcProcessors/shared.js.map +1 -0
- package/esm/utils/applyOxcProcessors/types.js +2 -0
- package/esm/utils/applyOxcProcessors/types.js.map +1 -0
- package/esm/utils/applyOxcProcessors.js +1 -1216
- package/esm/utils/applyOxcProcessors.js.map +1 -1
- package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js +157 -0
- package/esm/utils/collectOxcRuntime/normalizeRuntimeCode.js.map +1 -0
- package/esm/utils/collectOxcRuntime/sourceMap.js +36 -0
- package/esm/utils/collectOxcRuntime/sourceMap.js.map +1 -0
- package/esm/utils/collectOxcRuntime/types.js +2 -0
- package/esm/utils/collectOxcRuntime/types.js.map +1 -0
- package/esm/utils/collectOxcRuntime.js +5 -193
- package/esm/utils/collectOxcRuntime.js.map +1 -1
- package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js +496 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionExtraction.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js +113 -0
- package/esm/utils/collectOxcTemplateDependencies/expressionReplacements.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js +387 -0
- package/esm/utils/collectOxcTemplateDependencies/scopeAnalysis.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/staticBindings.js +17 -0
- package/esm/utils/collectOxcTemplateDependencies/staticBindings.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js +540 -0
- package/esm/utils/collectOxcTemplateDependencies/staticEvaluator.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies/types.js +2 -0
- package/esm/utils/collectOxcTemplateDependencies/types.js.map +1 -0
- package/esm/utils/collectOxcTemplateDependencies.js +3 -1397
- package/esm/utils/collectOxcTemplateDependencies.js.map +1 -1
- package/esm/utils/nativeResolver.js +93 -0
- package/esm/utils/nativeResolver.js.map +1 -0
- package/esm/utils/oxc/ast.js +28 -0
- package/esm/utils/oxc/ast.js.map +1 -0
- package/esm/utils/oxc/parse.js +3 -0
- package/esm/utils/oxc/parse.js.map +1 -0
- package/esm/utils/oxc/replacements.js +14 -0
- package/esm/utils/oxc/replacements.js.map +1 -0
- package/esm/utils/oxc/sourceLocations.js +59 -0
- package/esm/utils/oxc/sourceLocations.js.map +1 -0
- package/esm/utils/oxcPreevalStage/evalStrategy.js +3 -0
- package/esm/utils/oxcPreevalStage/evalStrategy.js.map +1 -0
- package/esm/utils/oxcPreevalStage/prepareCode.js +21 -0
- package/esm/utils/oxcPreevalStage/prepareCode.js.map +1 -0
- package/esm/utils/oxcPreevalStage/prevalExport.js +23 -0
- package/esm/utils/oxcPreevalStage/prevalExport.js.map +1 -0
- package/esm/utils/oxcPreevalStage/processors.js +17 -0
- package/esm/utils/oxcPreevalStage/processors.js.map +1 -0
- package/esm/utils/oxcPreevalStage/staticOverlay.js +18 -0
- package/esm/utils/oxcPreevalStage/staticOverlay.js.map +1 -0
- package/esm/utils/oxcPreevalStage/types.js +2 -0
- package/esm/utils/oxcPreevalStage/types.js.map +1 -0
- package/esm/utils/oxcPreevalStage.js +17 -73
- package/esm/utils/oxcPreevalStage.js.map +1 -1
- package/esm/utils/oxcPreevalTransforms.js +12 -1
- package/esm/utils/oxcPreevalTransforms.js.map +1 -1
- package/esm/utils/processorStaticSemantics.js +157 -0
- package/esm/utils/processorStaticSemantics.js.map +1 -0
- package/esm/utils/resolveWithConditions.js +3 -3
- package/esm/utils/resolveWithConditions.js.map +1 -1
- package/package.json +4 -3
- package/types/cache.js +8 -0
- package/types/debug/fileReporter.js +46 -1
- package/types/eval/broker.d.ts +6 -2
- package/types/eval/broker.js +244 -95
- package/types/eval/protocol.d.ts +15 -1
- package/types/module.d.ts +4 -1
- package/types/module.js +97 -48
- package/types/transform/Entrypoint.types.d.ts +3 -0
- package/types/transform/generators/resolveImports.d.ts +3 -1
- package/types/transform/generators/resolveImports.js +49 -2
- package/types/transform/generators/resolveStaticOxcValues/cache.d.ts +25 -0
- package/types/transform/generators/resolveStaticOxcValues/cache.js +182 -0
- package/types/transform/generators/resolveStaticOxcValues/candidateResolver.d.ts +5 -0
- package/types/transform/generators/resolveStaticOxcValues/candidateResolver.js +149 -0
- package/types/transform/generators/resolveStaticOxcValues/dependencies.d.ts +6 -0
- package/types/transform/generators/resolveStaticOxcValues/dependencies.js +37 -0
- package/types/transform/generators/resolveStaticOxcValues/environment.d.ts +12 -0
- package/types/transform/generators/resolveStaticOxcValues/environment.js +32 -0
- package/types/transform/generators/resolveStaticOxcValues/exportResolver.d.ts +4 -0
- package/types/transform/generators/resolveStaticOxcValues/exportResolver.js +358 -0
- package/types/transform/generators/resolveStaticOxcValues/exportTargets.d.ts +20 -0
- package/types/transform/generators/resolveStaticOxcValues/exportTargets.js +285 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssign.d.ts +41 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssign.js +352 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.d.ts +6 -0
- package/types/transform/generators/resolveStaticOxcValues/objectAssignStaticExport.js +88 -0
- package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.d.ts +13 -0
- package/types/transform/generators/resolveStaticOxcValues/opaqueRuntime.js +263 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.d.ts +4 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticExport.js +191 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.d.ts +40 -0
- package/types/transform/generators/resolveStaticOxcValues/processorStaticModel.js +516 -0
- package/types/transform/generators/resolveStaticOxcValues/processorTarget.d.ts +5 -0
- package/types/transform/generators/resolveStaticOxcValues/processorTarget.js +62 -0
- package/types/transform/generators/resolveStaticOxcValues/programAnalysis.d.ts +11 -0
- package/types/transform/generators/resolveStaticOxcValues/programAnalysis.js +133 -0
- package/types/transform/generators/resolveStaticOxcValues/prune.d.ts +15 -0
- package/types/transform/generators/resolveStaticOxcValues/prune.js +220 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.d.ts +2 -0
- package/types/transform/generators/resolveStaticOxcValues/resolveStaticOxcPreevalValues.js +197 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpression.d.ts +25 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpression.js +391 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.d.ts +14 -0
- package/types/transform/generators/resolveStaticOxcValues/staticExpressionDependencies.js +304 -0
- package/types/transform/generators/resolveStaticOxcValues/types.d.ts +99 -0
- package/types/transform/generators/resolveStaticOxcValues/types.js +2 -0
- package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.d.ts +7 -0
- package/types/transform/generators/resolveStaticOxcValues/zeroArgFunctionStaticExport.js +64 -0
- package/types/transform/generators/resolveStaticOxcValues.d.ts +1 -2
- package/types/transform/generators/resolveStaticOxcValues.js +1 -3235
- package/types/transform/generators/transform.js +63 -49
- package/types/transform/helpers/loadWywOptions.js +23 -3
- package/types/transform.js +3 -1
- package/types/utils/EventEmitter.d.ts +16 -1
- package/types/utils/EventEmitter.js +44 -9
- package/types/utils/applyOxcProcessors/applyOxcProcessors.d.ts +8 -0
- package/types/utils/applyOxcProcessors/applyOxcProcessors.js +167 -0
- package/types/utils/applyOxcProcessors/cleanupBindings.d.ts +12 -0
- package/types/utils/applyOxcProcessors/cleanupBindings.js +189 -0
- package/types/utils/applyOxcProcessors/cleanupRemovals.d.ts +16 -0
- package/types/utils/applyOxcProcessors/cleanupRemovals.js +482 -0
- package/types/utils/applyOxcProcessors/displayName.d.ts +9 -0
- package/types/utils/applyOxcProcessors/displayName.js +113 -0
- package/types/utils/applyOxcProcessors/expressionValues.d.ts +11 -0
- package/types/utils/applyOxcProcessors/expressionValues.js +170 -0
- package/types/utils/applyOxcProcessors/processorFactory.d.ts +7 -0
- package/types/utils/applyOxcProcessors/processorFactory.js +70 -0
- package/types/utils/applyOxcProcessors/processorUsages.d.ts +16 -0
- package/types/utils/applyOxcProcessors/processorUsages.js +163 -0
- package/types/utils/applyOxcProcessors/sameFileStaticValues.d.ts +23 -0
- package/types/utils/applyOxcProcessors/sameFileStaticValues.js +211 -0
- package/types/utils/applyOxcProcessors/shared.d.ts +10 -0
- package/types/utils/applyOxcProcessors/shared.js +37 -0
- package/types/utils/applyOxcProcessors/types.d.ts +88 -0
- package/types/utils/applyOxcProcessors/types.js +1 -0
- package/types/utils/applyOxcProcessors.d.ts +1 -16
- package/types/utils/applyOxcProcessors.js +1 -1391
- package/types/utils/collectOxcRuntime/normalizeRuntimeCode.d.ts +1 -0
- package/types/utils/collectOxcRuntime/normalizeRuntimeCode.js +185 -0
- package/types/utils/collectOxcRuntime/sourceMap.d.ts +2 -0
- package/types/utils/collectOxcRuntime/sourceMap.js +37 -0
- package/types/utils/collectOxcRuntime/types.d.ts +16 -0
- package/types/utils/collectOxcRuntime/types.js +1 -0
- package/types/utils/collectOxcRuntime.d.ts +2 -12
- package/types/utils/collectOxcRuntime.js +5 -224
- package/types/utils/collectOxcTemplateDependencies/expressionExtraction.d.ts +6 -0
- package/types/utils/collectOxcTemplateDependencies/expressionExtraction.js +550 -0
- package/types/utils/collectOxcTemplateDependencies/expressionReplacements.d.ts +11 -0
- package/types/utils/collectOxcTemplateDependencies/expressionReplacements.js +131 -0
- package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.d.ts +20 -0
- package/types/utils/collectOxcTemplateDependencies/scopeAnalysis.js +434 -0
- package/types/utils/collectOxcTemplateDependencies/staticBindings.d.ts +7 -0
- package/types/utils/collectOxcTemplateDependencies/staticBindings.js +13 -0
- package/types/utils/collectOxcTemplateDependencies/staticEvaluator.d.ts +13 -0
- package/types/utils/collectOxcTemplateDependencies/staticEvaluator.js +626 -0
- package/types/utils/collectOxcTemplateDependencies/types.d.ts +116 -0
- package/types/utils/collectOxcTemplateDependencies/types.js +1 -0
- package/types/utils/collectOxcTemplateDependencies.d.ts +4 -38
- package/types/utils/collectOxcTemplateDependencies.js +3 -1580
- package/types/utils/nativeResolver.d.ts +13 -0
- package/types/utils/nativeResolver.js +91 -0
- package/types/utils/oxc/ast.d.ts +4 -0
- package/types/utils/oxc/ast.js +37 -0
- package/types/utils/oxc/parse.d.ts +3 -0
- package/types/utils/oxc/parse.js +2 -0
- package/types/utils/oxc/replacements.d.ts +12 -0
- package/types/utils/oxc/replacements.js +18 -0
- package/types/utils/oxc/sourceLocations.d.ts +5 -0
- package/types/utils/oxc/sourceLocations.js +63 -0
- package/types/utils/oxcPreevalStage/evalStrategy.d.ts +3 -0
- package/types/utils/oxcPreevalStage/evalStrategy.js +2 -0
- package/types/utils/oxcPreevalStage/prepareCode.d.ts +3 -0
- package/types/utils/oxcPreevalStage/prepareCode.js +20 -0
- package/types/utils/oxcPreevalStage/prevalExport.d.ts +1 -0
- package/types/utils/oxcPreevalStage/prevalExport.js +22 -0
- package/types/utils/oxcPreevalStage/processors.d.ts +10 -0
- package/types/utils/oxcPreevalStage/processors.js +16 -0
- package/types/utils/oxcPreevalStage/staticOverlay.d.ts +3 -0
- package/types/utils/oxcPreevalStage/staticOverlay.js +23 -0
- package/types/utils/oxcPreevalStage/types.d.ts +22 -0
- package/types/utils/oxcPreevalStage/types.js +1 -0
- package/types/utils/oxcPreevalStage.d.ts +2 -18
- package/types/utils/oxcPreevalStage.js +17 -79
- package/types/utils/oxcPreevalTransforms.js +14 -1
- package/types/utils/processorStaticSemantics.d.ts +13 -0
- package/types/utils/processorStaticSemantics.js +191 -0
- package/types/utils/resolveWithConditions.js +3 -3
- package/esm/eval/resolverStrategy.js +0 -51
- package/esm/eval/resolverStrategy.js.map +0 -1
- package/types/eval/resolverStrategy.d.ts +0 -13
- package/types/eval/resolverStrategy.js +0 -46
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAIA,SAAS,oBAAoB,iBAAiB;AAC9C,SAAS,4BAA4B;AACrC,SACE,sBACA,yBACA,wBACA,qCACA,yBACA,uCACA,+BACA,6BACA,uBACK;AACP,SAAS,0BAA0B,gBAAgB;AAUnD,OAAO,MAAM,4BACX,YACwB;CACxB,UAAU,IAAI,KAAK;CACnB;CACD;AAED,OAAO,MAAM,wBACX,OACA,SACkB;CAClB,IAAI,UAAqC;AACzC,QAAO,SAAS;EACd,MAAM,YAAY,QAAQ,SAAS,IAAI,KAAK;AAC5C,MAAI,WAAW;AACb,UAAO;;AAGT,YAAU,QAAQ;;AAGpB,QAAO;;AAGT,OAAO,MAAM,6BACX,YACmC;CACnC,MAAM,WAAW,IAAI,KAAgC;CACrD,IAAI,WAAW;CAEf,MAAM,cACJ,OACA,MACA,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;EACnC,MAAM,WAAW;EACjB,MAAM,UAAU,SAAS,SAAS,SAAS;EAC3C,MAAM,aAAa,8BAA8B,QAAQ;EACzD,MAAM,iBAAiB,oCACrB,YACA,yBACD;AACD,iBAAe,SAAS,SAAS,yBAAyB,IAAI,KAAK,CAAC;EACpE,MAAM,kBAAkB,uBAAuB,QAAQ;EACvD,MAAM,mBAAmB,sCAAsC,WAAW;EAC1E,MAAM,iBAAiB,0BAA0B,QAAQ;EACzD,MAAM,WAAW,wBAAwB;GACvC,GAAG,uCACD,SACA,gBACA,yBACD;GACD,GAAG,yCACD,SACA,SACA,iBACA,yBACD;GACD,GAAG,gDACD,SACA,SACA,gBACD;GACD,GAAG,4BACD,SACA,SACA,iBACA,0BACA,+BACD;GACD,GAAG,2CACD,SACA,YACA,kBACA,wBACD;GACD,GAAG,kCAAkC,SAAS,QAAQ;GACvD,CAAC;AACF,YACE,SAAS,SAAS,IACd,gBAAgB,qBAAqB,SAAS,SAAS,CAAC,GACxD;AAEN,MAAI,YAAY,UAAU;AACxB,UAAO;;;AAIX,QAAO","names":[],"sources":["../../../src/utils/applyOxcProcessors/cleanupRemovals.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax,no-continue */\n\nimport type { Node, Program } from 'oxc-parser';\n\nimport { getOxcNodeChildren, isOxcNode } from '../oxc/ast';\nimport { applyOxcReplacements } from '../oxc/replacements';\nimport {\n collectDeclaredNames,\n collectImportLocalNames,\n collectReferencedNames,\n collectRemovableNamesFromStatements,\n collectTopLevelBindings,\n collectTopLevelBindingsFromStatements,\n collectTopLevelStatementInfos,\n getImportSpecifierLocalName,\n isNodeReference,\n} from './cleanupBindings';\nimport { GENERATED_HELPER_NAME_RE, parseOxc } from './shared';\nimport type {\n AnyNode,\n Replacement,\n ScopedBindingInfo,\n ScopedBindingKind,\n ScopedCleanupScope,\n TopLevelStatementInfo,\n} from './types';\n\nexport const createScopedCleanupScope = (\n parent: ScopedCleanupScope | null\n): ScopedCleanupScope => ({\n bindings: new Map(),\n parent,\n});\n\nexport const resolveScopedBinding = (\n scope: ScopedCleanupScope,\n name: string\n): string | null => {\n let current: ScopedCleanupScope | null = scope;\n while (current) {\n const bindingId = current.bindings.get(name);\n if (bindingId) {\n return bindingId;\n }\n\n current = current.parent;\n }\n\n return null;\n};\n\nexport const collectScopedBindingInfos = (\n program: Program\n): Map<string, ScopedBindingInfo> => {\n const bindings = new Map<string, ScopedBindingInfo>();\n let sequence = 0;\n\n const addBinding = (\n scope: ScopedCleanupScope,\n name: string,\n kind: ScopedBindingKind,\n declaration: Node\n ): string => {\n const id = `${kind}:${name}:${declaration.start}:${sequence}`;\n sequence += 1;\n\n bindings.set(id, {\n declaration,\n dependencies: new Set(),\n externalReferences: 0,\n id,\n incomingFromBindings: new Set(),\n kind,\n name,\n });\n scope.bindings.set(name, id);\n return id;\n };\n\n const addPatternBindings = (\n scope: ScopedCleanupScope,\n pattern: Node,\n kind: ScopedBindingKind,\n declaration: Node\n ): void => {\n collectDeclaredNames(pattern).forEach((name) => {\n addBinding(scope, name, kind, declaration);\n });\n };\n\n const recordReference = (\n scope: ScopedCleanupScope,\n name: string,\n ownerBindingId: string | null\n ): void => {\n const targetId = resolveScopedBinding(scope, name);\n if (!targetId) {\n return;\n }\n\n const target = bindings.get(targetId);\n if (!target) {\n return;\n }\n\n if (ownerBindingId && ownerBindingId !== targetId) {\n target.incomingFromBindings.add(ownerBindingId);\n bindings.get(ownerBindingId)?.dependencies.add(targetId);\n return;\n }\n\n target.externalReferences += 1;\n };\n\n type ScopedWalk = (\n node: Node,\n scope: ScopedCleanupScope,\n parent?: Node | null,\n ownerBindingId?: string | null\n ) => void;\n\n let walk: ScopedWalk;\n\n function walkPatternReferenceSubexpressions(\n node: Node,\n scope: ScopedCleanupScope,\n ownerBindingId: string | null\n ): void {\n if (node.type === 'Identifier') {\n return;\n }\n\n if (node.type === 'TSParameterProperty') {\n walkPatternReferenceSubexpressions(node.parameter, scope, ownerBindingId);\n return;\n }\n\n if (node.type === 'RestElement') {\n walkPatternReferenceSubexpressions(node.argument, scope, ownerBindingId);\n return;\n }\n\n if (node.type === 'AssignmentPattern') {\n walkPatternReferenceSubexpressions(node.left, scope, ownerBindingId);\n walk(node.right, scope, node, ownerBindingId);\n return;\n }\n\n if (node.type === 'ObjectPattern') {\n node.properties.forEach((property) => {\n if (property.type === 'RestElement') {\n walkPatternReferenceSubexpressions(\n property.argument,\n scope,\n ownerBindingId\n );\n return;\n }\n\n if (property.computed && isOxcNode(property.key)) {\n walk(property.key, scope, property, ownerBindingId);\n }\n\n walkPatternReferenceSubexpressions(\n property.value,\n scope,\n ownerBindingId\n );\n });\n return;\n }\n\n if (node.type === 'ArrayPattern') {\n node.elements.forEach((element) => {\n if (element && isOxcNode(element)) {\n walkPatternReferenceSubexpressions(element, scope, ownerBindingId);\n }\n });\n }\n }\n\n walk = (\n node: Node,\n scope: ScopedCleanupScope,\n parent: Node | null = null,\n ownerBindingId: string | null = null\n ): void => {\n if (node.type === 'ImportDeclaration') {\n const { specifiers } = node as AnyNode;\n if (Array.isArray(specifiers)) {\n specifiers.forEach((specifier) => {\n const { local } = specifier as AnyNode;\n if (\n isOxcNode(local) &&\n local.type === 'Identifier' &&\n typeof local.name === 'string'\n ) {\n addBinding(scope, local.name, 'import', node);\n }\n });\n }\n return;\n }\n\n if (node.type === 'ExportNamedDeclaration' && node.declaration) {\n walk(node.declaration, scope, node, ownerBindingId);\n collectTopLevelBindings(node).forEach((name) => {\n recordReference(scope, name, ownerBindingId);\n });\n return;\n }\n\n if (node.type === 'ExportDefaultDeclaration') {\n const { declaration } = node as AnyNode;\n if (isOxcNode(declaration)) {\n walk(declaration, scope, node, ownerBindingId);\n if (\n (declaration.type === 'FunctionDeclaration' ||\n declaration.type === 'ClassDeclaration') &&\n declaration.id\n ) {\n recordReference(scope, declaration.id.name, ownerBindingId);\n }\n }\n return;\n }\n\n if (node.type === 'VariableDeclaration') {\n const { declarations } = node as AnyNode;\n if (!Array.isArray(declarations)) {\n return;\n }\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n if (isOxcNode(id)) {\n addPatternBindings(scope, id, 'variable', node);\n }\n });\n\n declarations.forEach((declarator) => {\n const { id } = declarator as AnyNode;\n const { init } = declarator as AnyNode;\n if (!isOxcNode(id) || !isOxcNode(init)) {\n return;\n }\n\n const ownerName = collectDeclaredNames(id)[0] ?? null;\n const nextOwner =\n ownerName !== null ? resolveScopedBinding(scope, ownerName) : null;\n walk(init, scope, declarator as Node, nextOwner);\n });\n return;\n }\n\n if (node.type === 'FunctionDeclaration' && node.id) {\n const functionBindingId = addBinding(\n scope,\n node.id.name,\n 'function',\n node\n );\n const fnScope = createScopedCleanupScope(scope);\n\n node.params.forEach((param) => {\n addPatternBindings(fnScope, param, 'param', param);\n walkPatternReferenceSubexpressions(param, fnScope, functionBindingId);\n });\n\n if (node.body) {\n walk(node.body, fnScope, node, functionBindingId);\n }\n return;\n }\n\n if (\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n ) {\n const fnScope = createScopedCleanupScope(scope);\n if (node.type === 'FunctionExpression' && node.id) {\n addBinding(fnScope, node.id.name, 'function', node);\n }\n\n node.params.forEach((param) => {\n addPatternBindings(fnScope, param, 'param', param);\n walkPatternReferenceSubexpressions(param, fnScope, ownerBindingId);\n });\n\n if (node.body) {\n walk(node.body, fnScope, node, ownerBindingId);\n }\n return;\n }\n\n if (node.type === 'BlockStatement') {\n const blockScope = createScopedCleanupScope(scope);\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, blockScope, node, ownerBindingId)\n );\n return;\n }\n\n if (\n isNodeReference(node, parent) &&\n 'name' in node &&\n typeof node.name === 'string'\n ) {\n recordReference(scope, node.name, ownerBindingId);\n }\n\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, scope, node, ownerBindingId)\n );\n };\n\n walk(program, createScopedCleanupScope(null));\n return bindings;\n};\n\nexport const collectScopedRemovableBindingIds = (\n bindings: Map<string, ScopedBindingInfo>,\n initialNames: Set<string>\n): Set<string> => {\n const removable = new Set<string>();\n let changed = true;\n\n while (changed) {\n changed = false;\n\n for (const binding of bindings.values()) {\n if (\n !removable.has(binding.id) &&\n binding.kind !== 'import' &&\n binding.kind !== 'param' &&\n binding.externalReferences === 0\n ) {\n const seededByName =\n initialNames.has(binding.name) ||\n (GENERATED_HELPER_NAME_RE.test(binding.name) &&\n binding.incomingFromBindings.size === 0);\n const allIncomingRemovable =\n binding.incomingFromBindings.size > 0 &&\n [...binding.incomingFromBindings].every((sourceId) =>\n removable.has(sourceId)\n );\n\n if (\n (seededByName && binding.incomingFromBindings.size === 0) ||\n allIncomingRemovable\n ) {\n removable.add(binding.id);\n changed = true;\n }\n }\n }\n }\n\n return removable;\n};\n\nexport function expandImportRemovalRange(\n code: string,\n start: number,\n end: number\n): Replacement {\n let removalStart = start;\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n\n let removalEnd = end;\n if (code[removalEnd] === ';') {\n removalEnd += 1;\n }\n\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n\n if (code[removalEnd] === '\\r' && code[removalEnd + 1] === '\\n') {\n removalEnd += 2;\n } else if (code[removalEnd] === '\\n') {\n removalEnd += 1;\n }\n\n return {\n end: removalEnd,\n start: removalStart,\n value: '',\n };\n}\n\nexport const collectUnusedScopedDeclarationRemovals = (\n code: string,\n bindings: Map<string, ScopedBindingInfo>,\n initialRemovableNames: Set<string>\n): Replacement[] => {\n const removableBindingIds = collectScopedRemovableBindingIds(\n bindings,\n initialRemovableNames\n );\n const removals = new Map<string, Replacement>();\n\n bindings.forEach((binding) => {\n if (\n !removableBindingIds.has(binding.id) ||\n binding.kind === 'import' ||\n binding.kind === 'param' ||\n binding.externalReferences > 0 ||\n [...binding.incomingFromBindings].some(\n (sourceId) => !removableBindingIds.has(sourceId)\n )\n ) {\n return;\n }\n\n if (\n binding.kind === 'function' &&\n binding.declaration.type === 'FunctionDeclaration'\n ) {\n const range = expandImportRemovalRange(\n code,\n binding.declaration.start,\n binding.declaration.end\n );\n removals.set(`${range.start}:${range.end}`, range);\n return;\n }\n\n if (binding.declaration.type !== 'VariableDeclaration') {\n return;\n }\n\n const { declarations } = binding.declaration as AnyNode;\n if (!Array.isArray(declarations) || declarations.length !== 1) {\n return;\n }\n\n const range = expandImportRemovalRange(\n code,\n binding.declaration.start,\n binding.declaration.end\n );\n removals.set(`${range.start}:${range.end}`, range);\n });\n\n return [...removals.values()];\n};\n\nexport const expandImportSpecifierRemovalRange = (\n code: string,\n start: number,\n end: number\n): Replacement => {\n let removalStart = start;\n let removalEnd = end;\n\n let whitespaceStart = removalStart;\n while (\n whitespaceStart > 0 &&\n (code[whitespaceStart - 1] === ' ' || code[whitespaceStart - 1] === '\\t')\n ) {\n whitespaceStart -= 1;\n }\n if (code[whitespaceStart - 1] !== '{') {\n removalStart = whitespaceStart;\n }\n\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n\n if (code[removalEnd] === ',') {\n removalEnd += 1;\n while (\n removalEnd < code.length &&\n (code[removalEnd] === ' ' || code[removalEnd] === '\\t')\n ) {\n removalEnd += 1;\n }\n } else {\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n\n if (code[removalStart - 1] === ',') {\n removalStart -= 1;\n while (\n removalStart > 0 &&\n (code[removalStart - 1] === ' ' || code[removalStart - 1] === '\\t')\n ) {\n removalStart -= 1;\n }\n }\n }\n\n return {\n end: removalEnd,\n start: removalStart,\n value: '',\n };\n};\n\nexport const mergeEmptyRemovalRanges = (\n removals: Replacement[]\n): Replacement[] => {\n if (removals.length <= 1) {\n return removals;\n }\n\n const sorted = [...removals].sort((a, b) => a.start - b.start);\n const merged: Replacement[] = [];\n\n sorted.forEach((removal) => {\n const previous = merged[merged.length - 1];\n if (\n previous &&\n previous.value === '' &&\n removal.value === '' &&\n removal.start <= previous.end\n ) {\n previous.end = Math.max(previous.end, removal.end);\n return;\n }\n\n merged.push({ ...removal });\n });\n\n return merged;\n};\n\nexport const collectUnusedImportRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>,\n removableNames: Set<string>,\n preserveSideEffectImportLocals: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'ImportDeclaration') {\n return;\n }\n\n const localNames = collectImportLocalNames(statement);\n const removableLocalNames = localNames.filter((localName) =>\n removableNames.has(localName)\n );\n if (\n removableLocalNames.length > 0 &&\n removableLocalNames.length === localNames.length &&\n removableLocalNames.every((localName) => !referencedNames.has(localName))\n ) {\n if (\n removableLocalNames.some((localName) =>\n preserveSideEffectImportLocals.has(localName)\n )\n ) {\n removals.push({\n end: statement.end,\n start: statement.start,\n value: `import ${code.slice(\n statement.source.start,\n statement.source.end\n )};`,\n });\n return;\n }\n\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n return;\n }\n\n const { specifiers } = statement as AnyNode;\n if (!Array.isArray(specifiers) || specifiers.length <= 1) {\n return;\n }\n\n specifiers.forEach((specifier) => {\n if (!isOxcNode(specifier)) {\n return;\n }\n\n const localName = getImportSpecifierLocalName(specifier);\n if (\n localName &&\n removableNames.has(localName) &&\n !referencedNames.has(localName)\n ) {\n removals.push(\n expandImportSpecifierRemovalRange(\n code,\n specifier.start,\n specifier.end\n )\n );\n }\n });\n });\n\n return removals;\n};\n\nexport const collectUnusedTopLevelDeclarationRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>,\n removableNames: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'VariableDeclaration') {\n return;\n }\n\n const localNames = [...collectTopLevelBindings(statement)];\n\n if (\n localNames.length > 0 &&\n localNames.every((localName) => removableNames.has(localName)) &&\n localNames.every((localName) => !referencedNames.has(localName))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectUnusedGeneratedHelperDeclarationRemovals = (\n code: string,\n program: Program,\n referencedNames: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'VariableDeclaration') {\n return;\n }\n\n const localNames = [...collectTopLevelBindings(statement)];\n if (\n localNames.length > 0 &&\n localNames.every((localName) =>\n GENERATED_HELPER_NAME_RE.test(localName)\n ) &&\n localNames.every((localName) => !referencedNames.has(localName))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectTopLevelExpressionStatementRemovals = (\n code: string,\n statements: TopLevelStatementInfo[],\n topLevelBindings: Set<string>,\n removableExpressionRefs: Set<string>\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n statements.forEach((statement) => {\n if (statement.node.type !== 'ExpressionStatement') {\n return;\n }\n\n const { expression } = statement.node;\n const isPureExpression =\n expression.type === 'Identifier' ||\n expression.type === 'Literal' ||\n expression.type === 'ObjectExpression' ||\n expression.type === 'ArrayExpression' ||\n expression.type === 'ArrowFunctionExpression' ||\n expression.type === 'FunctionExpression' ||\n (expression.type === 'TemplateLiteral' &&\n expression.expressions.length === 0);\n if (!isPureExpression) {\n return;\n }\n\n const localReferences = [...statement.references].filter((name) =>\n topLevelBindings.has(name)\n );\n\n if (\n localReferences.length > 0 &&\n localReferences.every((name) => removableExpressionRefs.has(name))\n ) {\n removals.push(\n expandImportRemovalRange(code, statement.node.start, statement.node.end)\n );\n }\n });\n\n return removals;\n};\n\nexport const collectEmptyTopLevelBlockRemovals = (\n code: string,\n program: Program\n): Replacement[] => {\n const removals: Replacement[] = [];\n\n program.body.forEach((statement) => {\n if (statement.type !== 'BlockStatement' || statement.body.length > 0) {\n return;\n }\n\n removals.push(\n expandImportRemovalRange(code, statement.start, statement.end)\n );\n });\n\n return removals;\n};\n\nexport const removeUnusedAfterReplacement = (\n code: string,\n filename: string,\n initialRemovableNames: Set<string>,\n removableExpressionRefs: Set<string>,\n preserveSideEffectImportLocals: Set<string>\n): string => {\n let current = code;\n const cumulativeRemovableNames = new Set(initialRemovableNames);\n const applyIfParsable = (next: string): string => {\n try {\n parseOxc(next, filename);\n return next;\n } catch {\n return current;\n }\n };\n\n for (let idx = 0; idx < 5; idx += 1) {\n const previous = current;\n const program = parseOxc(current, filename);\n const statements = collectTopLevelStatementInfos(program);\n const removableNames = collectRemovableNamesFromStatements(\n statements,\n cumulativeRemovableNames\n );\n removableNames.forEach((name) => cumulativeRemovableNames.add(name));\n const referencedNames = collectReferencedNames(program);\n const topLevelBindings = collectTopLevelBindingsFromStatements(statements);\n const scopedBindings = collectScopedBindingInfos(program);\n const removals = mergeEmptyRemovalRanges([\n ...collectUnusedScopedDeclarationRemovals(\n current,\n scopedBindings,\n cumulativeRemovableNames\n ),\n ...collectUnusedTopLevelDeclarationRemovals(\n current,\n program,\n referencedNames,\n cumulativeRemovableNames\n ),\n ...collectUnusedGeneratedHelperDeclarationRemovals(\n current,\n program,\n referencedNames\n ),\n ...collectUnusedImportRemovals(\n current,\n program,\n referencedNames,\n cumulativeRemovableNames,\n preserveSideEffectImportLocals\n ),\n ...collectTopLevelExpressionStatementRemovals(\n current,\n statements,\n topLevelBindings,\n removableExpressionRefs\n ),\n ...collectEmptyTopLevelBlockRemovals(current, program),\n ]);\n current =\n removals.length > 0\n ? applyIfParsable(applyOxcReplacements(current, removals))\n : current;\n\n if (current === previous) {\n return current;\n }\n }\n\n return current;\n};\n"],"file":"cleanupRemovals.js"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-syntax */
|
|
2
|
+
import { basename, dirname } from "path";
|
|
3
|
+
import { ValueType } from "@wyw-in-js/shared";
|
|
4
|
+
import { isOxcNode, walkOxc } from "../oxc/ast.js";
|
|
5
|
+
import { isNodeReference } from "./cleanupBindings.js";
|
|
6
|
+
export const getPropertyKeyName = (property, code) => {
|
|
7
|
+
const { key } = property;
|
|
8
|
+
if (!isOxcNode(key)) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
if (key.type === "Identifier") {
|
|
12
|
+
return key.name;
|
|
13
|
+
}
|
|
14
|
+
if (key.type === "Literal") {
|
|
15
|
+
return String(key.value);
|
|
16
|
+
}
|
|
17
|
+
return typeof key.start === "number" && typeof key.end === "number" ? code.slice(key.start, key.end) : null;
|
|
18
|
+
};
|
|
19
|
+
export const getDisplayName = (ancestors, idx, code, filename) => {
|
|
20
|
+
const owner = [...ancestors].reverse().find((node) => {
|
|
21
|
+
return node.type === "Property" || node.type === "JSXOpeningElement" || node.type === "VariableDeclarator";
|
|
22
|
+
});
|
|
23
|
+
if (owner?.type === "Property") {
|
|
24
|
+
const keyName = getPropertyKeyName(owner, code);
|
|
25
|
+
if (keyName) {
|
|
26
|
+
return keyName;
|
|
27
|
+
}
|
|
28
|
+
} else if (owner?.type === "JSXOpeningElement") {
|
|
29
|
+
const { name } = owner;
|
|
30
|
+
if (isOxcNode(name) && name.type === "JSXIdentifier") {
|
|
31
|
+
return name.name;
|
|
32
|
+
}
|
|
33
|
+
} else if (owner?.type === "VariableDeclarator") {
|
|
34
|
+
const { id } = owner;
|
|
35
|
+
if (isOxcNode(id) && id.type === "Identifier") {
|
|
36
|
+
return id.name;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
let displayName = basename(filename ?? "unknown").replace(/\.[a-z\d]+$/, "");
|
|
40
|
+
if (filename && /^index\.[a-z\d]+$/.test(basename(filename))) {
|
|
41
|
+
displayName = basename(dirname(filename));
|
|
42
|
+
}
|
|
43
|
+
if (!displayName) {
|
|
44
|
+
throw new Error("Couldn't determine a name for the component. Ensure that it's either:\n" + "- Assigned to a variable\n" + "- Is an object property\n" + "- Is a prop in a JSX element\n");
|
|
45
|
+
}
|
|
46
|
+
return `${displayName}${idx}`;
|
|
47
|
+
};
|
|
48
|
+
export const getTagOwner = (ancestors) => {
|
|
49
|
+
const owner = [...ancestors].reverse().find((node) => node.type === "Property" || node.type === "JSXOpeningElement" || node.type === "VariableDeclarator");
|
|
50
|
+
return owner ?? null;
|
|
51
|
+
};
|
|
52
|
+
export const isTagReferenced = (program, ancestors) => {
|
|
53
|
+
const owner = getTagOwner(ancestors);
|
|
54
|
+
if (owner?.type !== "VariableDeclarator") {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
const { id } = owner;
|
|
58
|
+
if (!isOxcNode(id) || id.type !== "Identifier") {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (ancestors.some((node) => node.type === "ExportNamedDeclaration")) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
let referenced = false;
|
|
65
|
+
walkOxc(program, (node, parent) => {
|
|
66
|
+
const referenceName = node.type === "Identifier" || node.type === "JSXIdentifier" ? node.name : null;
|
|
67
|
+
if (referenced || referenceName !== id.name || node.type === "Identifier" && node.start === id.start && node.end === id.end) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
referenced = isNodeReference(node, parent);
|
|
71
|
+
});
|
|
72
|
+
return referenced;
|
|
73
|
+
};
|
|
74
|
+
export const collectSameFileProcessorStaticValues = (expressionValues, processorStaticValuesByLocal) => {
|
|
75
|
+
const staticValues = [];
|
|
76
|
+
const seen = new Set();
|
|
77
|
+
expressionValues.forEach((value) => {
|
|
78
|
+
if (value.kind !== ValueType.LAZY) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const staticValue = processorStaticValuesByLocal.get(value.source);
|
|
82
|
+
if (staticValue === undefined || value.ex.type !== "Identifier" || seen.has(value.ex.name)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
seen.add(value.ex.name);
|
|
86
|
+
staticValues.push({
|
|
87
|
+
name: value.ex.name,
|
|
88
|
+
value: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,CAAC;GACF;AAEF,QAAO","names":[],"sources":["../../../src/utils/applyOxcProcessors/displayName.ts"],"version":3,"sourcesContent":["/* eslint-disable no-restricted-syntax */\n\nimport { basename, dirname } from 'path';\n\nimport type { ExpressionValue } from '@wyw-in-js/shared';\nimport { ValueType } from '@wyw-in-js/shared';\nimport type { Node, Program } from 'oxc-parser';\n\nimport type { OxcStaticValue } from '../collectOxcTemplateDependencies';\nimport { isOxcNode, walkOxc } from '../oxc/ast';\nimport { isNodeReference } from './cleanupBindings';\nimport type { AnyNode } from './types';\n\nexport const getPropertyKeyName = (\n property: AnyNode,\n code: string\n): string | null => {\n const { key } = property;\n if (!isOxcNode(key)) {\n return null;\n }\n\n if (key.type === 'Identifier') {\n return key.name;\n }\n\n if (key.type === 'Literal') {\n return String(key.value);\n }\n\n return typeof key.start === 'number' && typeof key.end === 'number'\n ? code.slice(key.start, key.end)\n : null;\n};\n\nexport const getDisplayName = (\n ancestors: Node[],\n idx: number,\n code: string,\n filename?: string | null\n): string => {\n const owner = [...ancestors].reverse().find((node) => {\n return (\n node.type === 'Property' ||\n node.type === 'JSXOpeningElement' ||\n node.type === 'VariableDeclarator'\n );\n }) as AnyNode | undefined;\n\n if (owner?.type === 'Property') {\n const keyName = getPropertyKeyName(owner, code);\n if (keyName) {\n return keyName;\n }\n } else if (owner?.type === 'JSXOpeningElement') {\n const { name } = owner;\n if (isOxcNode(name) && name.type === 'JSXIdentifier') {\n return name.name;\n }\n } else if (owner?.type === 'VariableDeclarator') {\n const { id } = owner;\n if (isOxcNode(id) && id.type === 'Identifier') {\n return id.name;\n }\n }\n\n let displayName = basename(filename ?? 'unknown').replace(/\\.[a-z\\d]+$/, '');\n if (filename && /^index\\.[a-z\\d]+$/.test(basename(filename))) {\n displayName = basename(dirname(filename));\n }\n\n if (!displayName) {\n throw new Error(\n \"Couldn't determine a name for the component. Ensure that it's either:\\n\" +\n '- Assigned to a variable\\n' +\n '- Is an object property\\n' +\n '- Is a prop in a JSX element\\n'\n );\n }\n\n return `${displayName}${idx}`;\n};\n\nexport const getTagOwner = (ancestors: Node[]): AnyNode | null => {\n const owner = [...ancestors]\n .reverse()\n .find(\n (node) =>\n node.type === 'Property' ||\n node.type === 'JSXOpeningElement' ||\n node.type === 'VariableDeclarator'\n ) as AnyNode | undefined;\n\n return owner ?? null;\n};\n\nexport const isTagReferenced = (\n program: Program,\n ancestors: Node[]\n): boolean => {\n const owner = getTagOwner(ancestors);\n if (owner?.type !== 'VariableDeclarator') {\n return true;\n }\n\n const { id } = owner;\n if (!isOxcNode(id) || id.type !== 'Identifier') {\n return true;\n }\n\n if (ancestors.some((node) => node.type === 'ExportNamedDeclaration')) {\n return true;\n }\n\n let referenced = false;\n walkOxc(program, (node, parent) => {\n const referenceName =\n node.type === 'Identifier' || node.type === 'JSXIdentifier'\n ? node.name\n : null;\n\n if (\n referenced ||\n referenceName !== id.name ||\n (node.type === 'Identifier' &&\n node.start === id.start &&\n node.end === id.end)\n ) {\n return;\n }\n\n referenced = isNodeReference(node, parent);\n });\n\n return referenced;\n};\n\nexport const collectSameFileProcessorStaticValues = (\n expressionValues: Omit<ExpressionValue, 'buildCodeFrameError'>[],\n processorStaticValuesByLocal: Map<string, unknown>\n): OxcStaticValue[] => {\n const staticValues: OxcStaticValue[] = [];\n const seen = new Set<string>();\n\n expressionValues.forEach((value) => {\n if (value.kind !== ValueType.LAZY) {\n return;\n }\n\n const staticValue = processorStaticValuesByLocal.get(value.source);\n if (\n staticValue === undefined ||\n value.ex.type !== 'Identifier' ||\n seen.has(value.ex.name)\n ) {\n return;\n }\n\n seen.add(value.ex.name);\n staticValues.push({\n name: value.ex.name,\n value: staticValue,\n });\n });\n\n return staticValues;\n};\n"],"file":"displayName.js"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { ValueType } from "@wyw-in-js/shared";
|
|
2
|
+
import { isNotNull } from "../isNotNull.js";
|
|
3
|
+
import { buildOxcCodeFrameError } from "../oxc/sourceLocations.js";
|
|
4
|
+
import { getMemberName, unwrapQualifiedExpression } from "./processorUsages.js";
|
|
5
|
+
import { GENERATED_HELPER_NAME_RE, getSourceLocation } from "./shared.js";
|
|
6
|
+
export const literalExpressionValue = (expression, code, source, location) => {
|
|
7
|
+
if (expression.type !== "Literal") {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
if (expression.value === null || typeof expression.value === "string" || typeof expression.value === "number" || typeof expression.value === "boolean") {
|
|
11
|
+
let type;
|
|
12
|
+
if (expression.value === null) {
|
|
13
|
+
type = "NullLiteral";
|
|
14
|
+
} else if (typeof expression.value === "string") {
|
|
15
|
+
type = "StringLiteral";
|
|
16
|
+
} else if (typeof expression.value === "number") {
|
|
17
|
+
type = "NumericLiteral";
|
|
18
|
+
} else {
|
|
19
|
+
type = "BooleanLiteral";
|
|
20
|
+
}
|
|
21
|
+
const ex = expression.value === null ? {
|
|
22
|
+
loc: location,
|
|
23
|
+
type
|
|
24
|
+
} : {
|
|
25
|
+
loc: location,
|
|
26
|
+
type,
|
|
27
|
+
value: expression.value
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
buildCodeFrameError: (message) => buildOxcCodeFrameError(code, location, message),
|
|
31
|
+
ex,
|
|
32
|
+
kind: ValueType.CONST,
|
|
33
|
+
source,
|
|
34
|
+
value: expression.value
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
export const expressionValue = (expression, code, loc, filename) => {
|
|
40
|
+
const source = code.slice(expression.start, expression.end);
|
|
41
|
+
const location = getSourceLocation(expression.start, expression.end, loc, filename);
|
|
42
|
+
const literal = literalExpressionValue(expression, code, source, location);
|
|
43
|
+
if (literal) {
|
|
44
|
+
return literal;
|
|
45
|
+
}
|
|
46
|
+
const helperCallName = expression.type === "CallExpression" && expression.arguments.length === 0 && expression.callee.type === "Identifier" && GENERATED_HELPER_NAME_RE.test(expression.callee.name) ? expression.callee.name : null;
|
|
47
|
+
let ex;
|
|
48
|
+
if (expression.type === "Identifier") {
|
|
49
|
+
ex = {
|
|
50
|
+
loc: location,
|
|
51
|
+
name: expression.name,
|
|
52
|
+
type: "Identifier"
|
|
53
|
+
};
|
|
54
|
+
} else if (helperCallName) {
|
|
55
|
+
ex = {
|
|
56
|
+
loc: location,
|
|
57
|
+
name: helperCallName,
|
|
58
|
+
type: "Identifier"
|
|
59
|
+
};
|
|
60
|
+
} else {
|
|
61
|
+
ex = {
|
|
62
|
+
loc: location,
|
|
63
|
+
name: code.slice(expression.start, expression.end),
|
|
64
|
+
type: "Identifier"
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
buildCodeFrameError: (message) => buildOxcCodeFrameError(code, location, message),
|
|
69
|
+
ex,
|
|
70
|
+
kind: expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression" ? ValueType.FUNCTION : ValueType.LAZY,
|
|
71
|
+
source
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
export const withCurrentExpressionLocation = (value, expression, loc, filename) => {
|
|
75
|
+
const location = getSourceLocation(expression.start, expression.end, loc, filename);
|
|
76
|
+
if (value.kind === ValueType.CONST) {
|
|
77
|
+
return {
|
|
78
|
+
...value,
|
|
79
|
+
ex: {
|
|
80
|
+
...value.ex,
|
|
81
|
+
loc: location
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (value.kind === ValueType.FUNCTION) {
|
|
86
|
+
return {
|
|
87
|
+
...value,
|
|
88
|
+
ex: {
|
|
89
|
+
...value.ex,
|
|
90
|
+
loc: location
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
...value,
|
|
96
|
+
ex: {
|
|
97
|
+
...value.ex,
|
|
98
|
+
loc: location
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
export const shiftExpressionValue = (expressionValues, expression, code, loc, filename) => expressionValues.length > 0 ? withCurrentExpressionLocation(expressionValues.shift(), expression, loc, filename) : expressionValue(expression, code, loc, filename);
|
|
103
|
+
export const zipTemplate = (template, code, loc, filename, expressionValues) => {
|
|
104
|
+
const parts = template.quasi.quasis.flatMap((quasi, idx) => {
|
|
105
|
+
const expression = template.quasi.expressions[idx];
|
|
106
|
+
const templateElement = {
|
|
107
|
+
...quasi,
|
|
108
|
+
loc: getSourceLocation(quasi.start, quasi.end, loc, filename)
|
|
109
|
+
};
|
|
110
|
+
return [templateElement, expression ? shiftExpressionValue(expressionValues, expression, code, loc, filename) : null].filter(isNotNull);
|
|
111
|
+
});
|
|
112
|
+
return ["template", parts];
|
|
113
|
+
};
|
|
114
|
+
export const buildCalleeParams = (node, code, loc, filename, expressionValues, collapseQualifiedCallee = false) => {
|
|
115
|
+
const expression = unwrapQualifiedExpression(node);
|
|
116
|
+
if (collapseQualifiedCallee && (expression.type === "Identifier" || expression.type === "MemberExpression")) {
|
|
117
|
+
return [["callee", expression]];
|
|
118
|
+
}
|
|
119
|
+
if (expression.type === "Identifier") {
|
|
120
|
+
return [["callee", {
|
|
121
|
+
name: expression.name,
|
|
122
|
+
type: "Identifier"
|
|
123
|
+
}]];
|
|
124
|
+
}
|
|
125
|
+
if (expression.type === "MemberExpression") {
|
|
126
|
+
const params = buildCalleeParams(expression.object, code, loc, filename, expressionValues, collapseQualifiedCallee);
|
|
127
|
+
const member = getMemberName(expression);
|
|
128
|
+
return params && member ? [...params, ["member", member]] : null;
|
|
129
|
+
}
|
|
130
|
+
if (expression.type === "CallExpression") {
|
|
131
|
+
const call = expression;
|
|
132
|
+
const params = buildCalleeParams(call.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
|
|
133
|
+
if (!params) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
const callValues = call.arguments.filter((arg) => arg.type !== "SpreadElement").map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
|
|
137
|
+
return [...params, ["call", ...callValues]];
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
};
|
|
141
|
+
export const buildParams = (usage, code, loc, filename, expressionValues, collapseQualifiedCallee) => {
|
|
142
|
+
const params = buildCalleeParams(usage.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
|
|
143
|
+
if (!params) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
if (usage.kind === "template") {
|
|
147
|
+
return [...params, zipTemplate(usage.target, code, loc, filename, expressionValues)];
|
|
148
|
+
}
|
|
149
|
+
const callValues = usage.target.arguments.filter((arg) => arg.type !== "SpreadElement").map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
|
|
150
|
+
return [...params, ["call", ...callValues]];
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=expressionValues.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAEA,SAAS,iBAAiB;AAG1B,SAAS,iBAAiB;AAC1B,SAAS,8BAA8B;AACvC,SAAS,eAAe,iCAAiC;AACzD,SAAS,0BAA0B,yBAAyB;AAO5D,OAAO,MAAM,0BACX,YACA,MACA,QACA,aAC2B;AAC3B,KAAI,WAAW,SAAS,WAAW;AACjC,SAAO;;AAGT,KACE,WAAW,UAAU,QACrB,OAAO,WAAW,UAAU,YAC5B,OAAO,WAAW,UAAU,YAC5B,OAAO,WAAW,UAAU,WAC5B;EACA,IAAI;AAKJ,MAAI,WAAW,UAAU,MAAM;AAC7B,UAAO;aACE,OAAO,WAAW,UAAU,UAAU;AAC/C,UAAO;aACE,OAAO,WAAW,UAAU,UAAU;AAC/C,UAAO;SACF;AACL,UAAO;;EAGT,MAAM,KACJ,WAAW,UAAU,OACjB;GAAE,KAAK;GAAU;GAAM,GACvB;GACE,KAAK;GACL;GACA,OAAO,WAAW;GACnB;AAEP,SAAO;GACL,sBAAsB,YACpB,uBAAuB,MAAM,UAAU,QAAQ;GACjD;GACA,MAAM,UAAU;GAChB;GACA,OAAO,WAAW;GACnB;;AAGH,QAAO;;AAGT,OAAO,M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C,GAAG,QAAQ,CAAC,QAAQ,GAAG,WAAW,CAAC;;AAG7C,QAAO;;AAGT,OAAO,MAAM,eACX,OACA,MACA,KACA,UACA,kBACA,4BACkB;CAClB,MAAM,SAAS,kBACb,MAAM,QACN,MACA,KACA,UACA,kBACA,wBACD;AACD,KAAI,CAAC,QAAQ;AACX,SAAO;;AAGT,KAAI,MAAM,SAAS,YAAY;AAC7B,SAAO,CACL,GAAG,QACH,YAAY,MAAM,QAAQ,MAAM,KAAK,UAAU,iBAAiB,CACjE;;CAGH,MAAM,aAAa,MAAM,OAAO,UAC7B,QAAQ,QAAQ,IAAI,SAAS,gBAAgB,CAC7C,KAAK,QACJ,qBACE,kBACA,KACA,MACA,KACA,SACD,CACF;AAEH,QAAO,CAAC,GAAG,QAAQ,CAAC,QAAQ,GAAG,WAAW,CAAC","names":[],"sources":["../../../src/utils/applyOxcProcessors/expressionValues.ts"],"version":3,"sourcesContent":["import type { Param, Params, SourceLocation } from '@wyw-in-js/processor-utils';\nimport type { ExpressionValue } from '@wyw-in-js/shared';\nimport { ValueType } from '@wyw-in-js/shared';\nimport type { Expression, TaggedTemplateExpression } from 'oxc-parser';\n\nimport { isNotNull } from '../isNotNull';\nimport { buildOxcCodeFrameError } from '../oxc/sourceLocations';\nimport { getMemberName, unwrapQualifiedExpression } from './processorUsages';\nimport { GENERATED_HELPER_NAME_RE, getSourceLocation } from './shared';\nimport type {\n CallExpressionLike,\n LocationLookup,\n ProcessorUsage,\n} from './types';\n\nexport const literalExpressionValue = (\n expression: Expression,\n code: string,\n source: string,\n location: SourceLocation\n): ExpressionValue | null => {\n if (expression.type !== 'Literal') {\n return null;\n }\n\n if (\n expression.value === null ||\n typeof expression.value === 'string' ||\n typeof expression.value === 'number' ||\n typeof expression.value === 'boolean'\n ) {\n let type:\n | 'BooleanLiteral'\n | 'NullLiteral'\n | 'NumericLiteral'\n | 'StringLiteral';\n if (expression.value === null) {\n type = 'NullLiteral';\n } else if (typeof expression.value === 'string') {\n type = 'StringLiteral';\n } else if (typeof expression.value === 'number') {\n type = 'NumericLiteral';\n } else {\n type = 'BooleanLiteral';\n }\n\n const ex =\n expression.value === null\n ? { loc: location, type }\n : {\n loc: location,\n type,\n value: expression.value,\n };\n\n return {\n buildCodeFrameError: (message: string) =>\n buildOxcCodeFrameError(code, location, message),\n ex,\n kind: ValueType.CONST,\n source,\n value: expression.value,\n } as ExpressionValue;\n }\n\n return null;\n};\n\nexport const expressionValue = (\n expression: Expression,\n code: string,\n loc: LocationLookup,\n filename?: string | null\n): ExpressionValue => {\n const source = code.slice(expression.start, expression.end);\n const location = getSourceLocation(\n expression.start,\n expression.end,\n loc,\n filename\n );\n const literal = literalExpressionValue(expression, code, source, location);\n if (literal) {\n return literal;\n }\n\n const helperCallName =\n expression.type === 'CallExpression' &&\n expression.arguments.length === 0 &&\n expression.callee.type === 'Identifier' &&\n GENERATED_HELPER_NAME_RE.test(expression.callee.name)\n ? expression.callee.name\n : null;\n\n let ex: ExpressionValue['ex'];\n if (expression.type === 'Identifier') {\n ex = { loc: location, name: expression.name, type: 'Identifier' };\n } else if (helperCallName) {\n ex = { loc: location, name: helperCallName, type: 'Identifier' };\n } else {\n ex = {\n loc: location,\n name: code.slice(expression.start, expression.end),\n type: 'Identifier',\n };\n }\n\n return {\n buildCodeFrameError: (message: string) =>\n buildOxcCodeFrameError(code, location, message),\n ex,\n kind:\n expression.type === 'ArrowFunctionExpression' ||\n expression.type === 'FunctionExpression'\n ? ValueType.FUNCTION\n : ValueType.LAZY,\n source,\n } as ExpressionValue;\n};\n\nexport const withCurrentExpressionLocation = (\n value: ExpressionValue,\n expression: Expression,\n loc: LocationLookup,\n filename?: string | null\n): ExpressionValue => {\n const location = getSourceLocation(\n expression.start,\n expression.end,\n loc,\n filename\n );\n\n if (value.kind === ValueType.CONST) {\n return {\n ...value,\n ex: {\n ...value.ex,\n loc: location,\n },\n };\n }\n\n if (value.kind === ValueType.FUNCTION) {\n return {\n ...value,\n ex: {\n ...value.ex,\n loc: location,\n },\n };\n }\n\n return {\n ...value,\n ex: {\n ...value.ex,\n loc: location,\n },\n };\n};\n\nexport const shiftExpressionValue = (\n expressionValues: ExpressionValue[],\n expression: Expression,\n code: string,\n loc: LocationLookup,\n filename?: string | null\n): ExpressionValue =>\n expressionValues.length > 0\n ? withCurrentExpressionLocation(\n expressionValues.shift()!,\n expression,\n loc,\n filename\n )\n : expressionValue(expression, code, loc, filename);\n\nexport const zipTemplate = (\n template: TaggedTemplateExpression,\n code: string,\n loc: LocationLookup,\n filename: string | null | undefined,\n expressionValues: ExpressionValue[]\n): Param => {\n const parts = template.quasi.quasis.flatMap((quasi, idx) => {\n const expression = template.quasi.expressions[idx];\n const templateElement = {\n ...quasi,\n loc: getSourceLocation(quasi.start, quasi.end, loc, filename),\n };\n\n return [\n templateElement,\n expression\n ? shiftExpressionValue(\n expressionValues,\n expression as Expression,\n code,\n loc,\n filename\n )\n : null,\n ].filter(isNotNull);\n });\n\n return ['template', parts] as Param;\n};\n\nexport const buildCalleeParams = (\n node: Expression,\n code: string,\n loc: LocationLookup,\n filename: string | null | undefined,\n expressionValues: ExpressionValue[],\n collapseQualifiedCallee = false\n): Params | null => {\n const expression = unwrapQualifiedExpression(node);\n\n if (\n collapseQualifiedCallee &&\n (expression.type === 'Identifier' || expression.type === 'MemberExpression')\n ) {\n return [['callee', expression] as Param];\n }\n\n if (expression.type === 'Identifier') {\n return [['callee', { name: expression.name, type: 'Identifier' }]];\n }\n\n if (expression.type === 'MemberExpression') {\n const params = buildCalleeParams(\n expression.object,\n code,\n loc,\n filename,\n expressionValues,\n collapseQualifiedCallee\n );\n const member = getMemberName(expression);\n return params && member ? [...params, ['member', member]] : null;\n }\n\n if (expression.type === 'CallExpression') {\n const call = expression as CallExpressionLike;\n const params = buildCalleeParams(\n call.callee,\n code,\n loc,\n filename,\n expressionValues,\n collapseQualifiedCallee\n );\n if (!params) {\n return null;\n }\n\n const callValues = call.arguments\n .filter((arg) => arg.type !== 'SpreadElement')\n .map((arg) =>\n shiftExpressionValue(\n expressionValues,\n arg as Expression,\n code,\n loc,\n filename\n )\n );\n\n return [...params, ['call', ...callValues]];\n }\n\n return null;\n};\n\nexport const buildParams = (\n usage: ProcessorUsage,\n code: string,\n loc: LocationLookup,\n filename: string | null | undefined,\n expressionValues: ExpressionValue[],\n collapseQualifiedCallee: boolean\n): Params | null => {\n const params = buildCalleeParams(\n usage.callee,\n code,\n loc,\n filename,\n expressionValues,\n collapseQualifiedCallee\n );\n if (!params) {\n return null;\n }\n\n if (usage.kind === 'template') {\n return [\n ...params,\n zipTemplate(usage.target, code, loc, filename, expressionValues),\n ];\n }\n\n const callValues = usage.target.arguments\n .filter((arg) => arg.type !== 'SpreadElement')\n .map((arg) =>\n shiftExpressionValue(\n expressionValues,\n arg as Expression,\n code,\n loc,\n filename\n )\n );\n\n return [...params, ['call', ...callValues]];\n};\n"],"file":"expressionValues.js"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { BaseProcessor, expressionToCode } from "@wyw-in-js/processor-utils";
|
|
2
|
+
import { createOxcAstService } from "../oxcAstService.js";
|
|
3
|
+
import { buildOxcCodeFrameError } from "../oxc/sourceLocations.js";
|
|
4
|
+
import { getDisplayName } from "./displayName.js";
|
|
5
|
+
import { getRootIdentifier } from "./processorUsages.js";
|
|
6
|
+
import { getSourceLocation } from "./shared.js";
|
|
7
|
+
let didWarnSkipSymbolMismatch = false;
|
|
8
|
+
export const isReplacementPure = (replacement) => replacement.type === "CallExpression";
|
|
9
|
+
export const shouldCollectStaticExpressionValues = (options) => (options.eval?.strategy ?? "hybrid") !== "execute";
|
|
10
|
+
export const createProcessor = (definedProcessor, params, target, replacementTarget, ancestors, fileContext, options, code, loc, idx, isReferenced, usedNames, replacements) => {
|
|
11
|
+
const [Processor, tagSource] = definedProcessor;
|
|
12
|
+
const astService = createOxcAstService(usedNames);
|
|
13
|
+
const replacer = (replacement, isPure) => {
|
|
14
|
+
const next = typeof replacement === "function" ? replacement(target) : replacement;
|
|
15
|
+
const replacementCode = expressionToCode(next);
|
|
16
|
+
replacements.push({
|
|
17
|
+
start: replacementTarget.start,
|
|
18
|
+
end: replacementTarget.end,
|
|
19
|
+
value: isPure && isReplacementPure(next) ? `/*#__PURE__*/${replacementCode}` : replacementCode
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
try {
|
|
23
|
+
let displayName;
|
|
24
|
+
try {
|
|
25
|
+
displayName = getDisplayName(ancestors, idx, code, fileContext.filename);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (error instanceof Error && error.message.startsWith("Couldn't determine a name for the component")) {
|
|
28
|
+
let displayNameNode = target;
|
|
29
|
+
if (target.type === "TaggedTemplateExpression") {
|
|
30
|
+
displayNameNode = target.tag;
|
|
31
|
+
} else if (target.type === "CallExpression") {
|
|
32
|
+
displayNameNode = target.callee;
|
|
33
|
+
}
|
|
34
|
+
const pointerNode = displayNameNode.type === "MemberExpression" ? getRootIdentifier(displayNameNode) ?? displayNameNode : displayNameNode;
|
|
35
|
+
throw buildOxcCodeFrameError(code, getSourceLocation(pointerNode.start, pointerNode.end, loc, fileContext.filename), error.message);
|
|
36
|
+
}
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
astService,
|
|
41
|
+
processor: new Processor(params, tagSource, astService, getSourceLocation(target.start, target.end, loc, fileContext.filename), replacer, displayName, isReferenced, idx, options, fileContext)
|
|
42
|
+
};
|
|
43
|
+
} catch (e) {
|
|
44
|
+
if (e === BaseProcessor.SKIP) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
if (typeof e === "symbol" && e.description === BaseProcessor.SKIP.description) {
|
|
48
|
+
if (!didWarnSkipSymbolMismatch) {
|
|
49
|
+
didWarnSkipSymbolMismatch = true;
|
|
50
|
+
// eslint-disable-next-line no-console
|
|
51
|
+
console.warn([
|
|
52
|
+
"[wyw-in-js] Processor threw Symbol('skip') that does not match BaseProcessor.SKIP identity.",
|
|
53
|
+
"This usually means duplicate copies of @wyw-in-js/processor-utils (or the processor) are bundled/installed.",
|
|
54
|
+
"Consider deduping dependencies to avoid subtle issues (instanceof checks, sentinels, etc)."
|
|
55
|
+
].join("\n"));
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
throw e;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=processorFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA,SAAS,eAAe,wBAAwB;AAShD,SAAS,2BAA2B;AACpC,SAAS,8BAA8B;AACvC,SAAS,sBAAsB;AAC/B,SAAS,yBAAyB;AAClC,SAAS,yBAAyB;AAQlC,IAAI,4BAA4B;AAChC,OAAO,MAAM,qBAAqB,gBAChC,YAAY,SAAS;AAEvB,OAAO,MAAM,uCACX,aACa,QAAQ,MAAM,YAAY,cAAc;AAEvD,OAAO,MAAM,mBACX,kBACA,QACA,QACA,mBACA,WACA,aACA,SAIA,MACA,KACA,KACA,cACA,WACA,iBAC4B;CAC5B,MAAM,CAAC,WAAW,aAAa;CAC/B,MAAM,aAAa,oBAAoB,UAAU;CAEjD,MAAM,YACJ,aAGA,WACG;EACH,MAAM,OACJ,OAAO,gBAAgB,aAAa,YAAY,OAAO,GAAG;EAC5D,MAAM,kBAAkB,iBAAiB,KAAK;AAC9C,eAAa,KAAK;GAChB,OAAO,kBAAkB;GACzB,KAAK,kBAAkB;GACvB,OACE,UAAU,kBAAkB,KAAK,GAC7B,gBAAgB,oBAChB;GACP,CAAC;;AAGJ,KAAI;EACF,IAAI;AACJ,MAAI;AACF,iBAAc,eAAe,WAAW,KAAK,MAAM,YAAY,SAAS;WACjE,OAAO;AACd,OACE,iBAAiB,SACjB,MAAM,QAAQ,WAAW,8CAA8C,EACvE;IACA,IAAI,kBAAwB;AAC5B,QAAI,OAAO,SAAS,4BAA4B;AAC9C,uBAAkB,OAAO;eAChB,OAAO,SAAS,kBAAkB;AAC3C,uBAAkB,OAAO;;IAE3B,MAAM,cACJ,gBAAgB,SAAS,qBACrB,kBAAkB,gBAAgB,IAAI,kBACtC;AACN,UAAM,uBACJ,MACA,kBACE,YAAY,OACZ,YAAY,KACZ,KACA,YAAY,SACb,EACD,MAAM,QACP;;AAEH,SAAM;;AAGR,SAAO;GACL;GACA,WAAW,IAAI,UACb,QACA,WACA,YACA,kBAAkB,OAAO,OAAO,OAAO,KAAK,KAAK,YAAY,SAAS,EACtE,UACA,aACA,cACA,KACA,SACA,YACD;GACF;UACM,GAAG;AACV,MAAI,MAAM,cAAc,MAAM;AAC5B,UAAO;;AAGT,MACE,OAAO,MAAM,YACb,EAAE,gBAAgB,cAAc,KAAK,aACrC;AACA,OAAI,CAAC,2BAA2B;AAC9B,gCAA4B;;AAE5B,YAAQ,KACN;KACE;KACA;KACA;KACD,CAAC,KAAK,KAAK,CACb;;AAGH,UAAO;;AAGT,QAAM","names":[],"sources":["../../../src/utils/applyOxcProcessors/processorFactory.ts"],"version":3,"sourcesContent":["import { BaseProcessor, expressionToCode } from '@wyw-in-js/processor-utils';\nimport type {\n Expression as ProcessorExpression,\n IFileContext,\n Params,\n} from '@wyw-in-js/processor-utils';\nimport type { StrictOptions } from '@wyw-in-js/shared';\nimport type { Expression, Node } from 'oxc-parser';\n\nimport { createOxcAstService } from '../oxcAstService';\nimport { buildOxcCodeFrameError } from '../oxc/sourceLocations';\nimport { getDisplayName } from './displayName';\nimport { getRootIdentifier } from './processorUsages';\nimport { getSourceLocation } from './shared';\nimport type {\n CreatedProcessor,\n DefinedProcessor,\n LocationLookup,\n Replacement,\n} from './types';\n\nlet didWarnSkipSymbolMismatch = false;\nexport const isReplacementPure = (replacement: ProcessorExpression): boolean =>\n replacement.type === 'CallExpression';\n\nexport const shouldCollectStaticExpressionValues = (\n options: Pick<StrictOptions, 'eval'>\n): boolean => (options.eval?.strategy ?? 'hybrid') !== 'execute';\n\nexport const createProcessor = (\n definedProcessor: DefinedProcessor,\n params: Params,\n target: Expression,\n replacementTarget: Expression,\n ancestors: Node[],\n fileContext: IFileContext,\n options: Pick<\n StrictOptions,\n 'classNameSlug' | 'displayName' | 'extensions' | 'tagResolver'\n >,\n code: string,\n loc: LocationLookup,\n idx: number,\n isReferenced: boolean,\n usedNames: Set<string>,\n replacements: Replacement[]\n): CreatedProcessor | null => {\n const [Processor, tagSource] = definedProcessor;\n const astService = createOxcAstService(usedNames);\n\n const replacer = (\n replacement:\n | ProcessorExpression\n | ((tagPath: unknown) => ProcessorExpression),\n isPure: boolean\n ) => {\n const next =\n typeof replacement === 'function' ? replacement(target) : replacement;\n const replacementCode = expressionToCode(next);\n replacements.push({\n start: replacementTarget.start,\n end: replacementTarget.end,\n value:\n isPure && isReplacementPure(next)\n ? `/*#__PURE__*/${replacementCode}`\n : replacementCode,\n });\n };\n\n try {\n let displayName: string;\n try {\n displayName = getDisplayName(ancestors, idx, code, fileContext.filename);\n } catch (error) {\n if (\n error instanceof Error &&\n error.message.startsWith(\"Couldn't determine a name for the component\")\n ) {\n let displayNameNode: Node = target;\n if (target.type === 'TaggedTemplateExpression') {\n displayNameNode = target.tag;\n } else if (target.type === 'CallExpression') {\n displayNameNode = target.callee;\n }\n const pointerNode =\n displayNameNode.type === 'MemberExpression'\n ? getRootIdentifier(displayNameNode) ?? displayNameNode\n : displayNameNode;\n throw buildOxcCodeFrameError(\n code,\n getSourceLocation(\n pointerNode.start,\n pointerNode.end,\n loc,\n fileContext.filename\n ),\n error.message\n );\n }\n throw error;\n }\n\n return {\n astService,\n processor: new Processor(\n params,\n tagSource,\n astService,\n getSourceLocation(target.start, target.end, loc, fileContext.filename),\n replacer,\n displayName,\n isReferenced,\n idx,\n options,\n fileContext\n ),\n };\n } catch (e) {\n if (e === BaseProcessor.SKIP) {\n return null;\n }\n\n if (\n typeof e === 'symbol' &&\n e.description === BaseProcessor.SKIP.description\n ) {\n if (!didWarnSkipSymbolMismatch) {\n didWarnSkipSymbolMismatch = true;\n // eslint-disable-next-line no-console\n console.warn(\n [\n \"[wyw-in-js] Processor threw Symbol('skip') that does not match BaseProcessor.SKIP identity.\",\n 'This usually means duplicate copies of @wyw-in-js/processor-utils (or the processor) are bundled/installed.',\n 'Consider deduping dependencies to avoid subtle issues (instanceof checks, sentinels, etc).',\n ].join('\\n')\n );\n }\n\n return null;\n }\n\n throw e;\n }\n};\n"],"file":"processorFactory.js"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { getOxcNodeChildren } from "../oxc/ast.js";
|
|
2
|
+
export const getMemberName = (node) => {
|
|
3
|
+
if (node.computed) {
|
|
4
|
+
return node.property.type === "Literal" && typeof node.property.value === "string" ? node.property.value : null;
|
|
5
|
+
}
|
|
6
|
+
return node.property.type === "Identifier" ? node.property.name : null;
|
|
7
|
+
};
|
|
8
|
+
export const unwrapQualifiedExpression = (node) => {
|
|
9
|
+
if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression" || node.type === "TSNonNullExpression" || node.type === "TSTypeAssertion" || node.type === "ParenthesizedExpression") {
|
|
10
|
+
return unwrapQualifiedExpression(node.expression);
|
|
11
|
+
}
|
|
12
|
+
if (node.type === "SequenceExpression") {
|
|
13
|
+
const sequence = node;
|
|
14
|
+
return unwrapQualifiedExpression(sequence.expressions[sequence.expressions.length - 1] ?? node);
|
|
15
|
+
}
|
|
16
|
+
return node;
|
|
17
|
+
};
|
|
18
|
+
export const getRootIdentifier = (node) => {
|
|
19
|
+
const expression = unwrapQualifiedExpression(node);
|
|
20
|
+
if (expression.type === "Identifier") {
|
|
21
|
+
return expression;
|
|
22
|
+
}
|
|
23
|
+
if (expression.type === "MemberExpression") {
|
|
24
|
+
return getRootIdentifier(expression.object);
|
|
25
|
+
}
|
|
26
|
+
if (expression.type === "CallExpression") {
|
|
27
|
+
return getRootIdentifier(expression.callee);
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
};
|
|
31
|
+
export const getQualifiedName = (node) => {
|
|
32
|
+
const expression = unwrapQualifiedExpression(node);
|
|
33
|
+
if (expression.type === "Identifier") {
|
|
34
|
+
return expression.name;
|
|
35
|
+
}
|
|
36
|
+
if (expression.type === "MemberExpression") {
|
|
37
|
+
const object = getQualifiedName(expression.object);
|
|
38
|
+
const member = getMemberName(expression);
|
|
39
|
+
return object && member ? `${object}.${member}` : null;
|
|
40
|
+
}
|
|
41
|
+
if (expression.type === "CallExpression") {
|
|
42
|
+
return getQualifiedName(expression.callee);
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
};
|
|
46
|
+
export const resolveDefinedProcessor = (callee, definedProcessors) => {
|
|
47
|
+
const qualified = getQualifiedName(callee);
|
|
48
|
+
if (qualified) {
|
|
49
|
+
const definedProcessor = definedProcessors.get(qualified);
|
|
50
|
+
if (definedProcessor) {
|
|
51
|
+
return {
|
|
52
|
+
collapseQualifiedCallee: qualified.includes("."),
|
|
53
|
+
definedProcessor
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const root = getRootIdentifier(callee);
|
|
58
|
+
if (!root) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
const definedProcessor = definedProcessors.get(root.name);
|
|
62
|
+
return definedProcessor ? {
|
|
63
|
+
collapseQualifiedCallee: false,
|
|
64
|
+
definedProcessor
|
|
65
|
+
} : null;
|
|
66
|
+
};
|
|
67
|
+
export const isCallTagOfTaggedTemplate = (node, parent) => parent?.type === "TaggedTemplateExpression" && parent.tag === node;
|
|
68
|
+
export const expandReplacementTarget = (target, ancestors) => {
|
|
69
|
+
let current = target;
|
|
70
|
+
for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
|
|
71
|
+
const ancestor = ancestors[idx];
|
|
72
|
+
if (ancestor.type === "SequenceExpression" && ancestor.expressions[ancestor.expressions.length - 1] === current) {
|
|
73
|
+
current = ancestor;
|
|
74
|
+
} else if (ancestor.type === "ParenthesizedExpression" && ancestor.expression === current) {
|
|
75
|
+
current = ancestor;
|
|
76
|
+
} else {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return current;
|
|
81
|
+
};
|
|
82
|
+
export const collectProcessorUsages = (program, definedProcessors) => {
|
|
83
|
+
const usages = [];
|
|
84
|
+
const walk = (node, ancestors, parent = null) => {
|
|
85
|
+
if (node.type === "TaggedTemplateExpression") {
|
|
86
|
+
const callee = node.tag;
|
|
87
|
+
const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
|
|
88
|
+
if (resolvedProcessor) {
|
|
89
|
+
usages.push({
|
|
90
|
+
ancestors,
|
|
91
|
+
callee,
|
|
92
|
+
collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
|
|
93
|
+
definedProcessor: resolvedProcessor.definedProcessor,
|
|
94
|
+
kind: "template",
|
|
95
|
+
replacementTarget: expandReplacementTarget(node, ancestors),
|
|
96
|
+
target: node
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
} else if (node.type === "CallExpression" && !isCallTagOfTaggedTemplate(node, parent)) {
|
|
100
|
+
const { callee } = node;
|
|
101
|
+
const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
|
|
102
|
+
if (resolvedProcessor) {
|
|
103
|
+
usages.push({
|
|
104
|
+
ancestors,
|
|
105
|
+
callee,
|
|
106
|
+
collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
|
|
107
|
+
definedProcessor: resolvedProcessor.definedProcessor,
|
|
108
|
+
kind: "call",
|
|
109
|
+
replacementTarget: expandReplacementTarget(node, ancestors),
|
|
110
|
+
target: node
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
getOxcNodeChildren(node).forEach((child) => walk(child, [...ancestors, node], node));
|
|
115
|
+
};
|
|
116
|
+
walk(program, []);
|
|
117
|
+
return usages.sort((a, b) => a.target.start - b.target.start);
|
|
118
|
+
};
|
|
119
|
+
export const expressionSpan = (expression) => ({
|
|
120
|
+
end: expression.end,
|
|
121
|
+
start: expression.start
|
|
122
|
+
});
|
|
123
|
+
export const collectCallArgumentSpans = (node) => {
|
|
124
|
+
const expression = unwrapQualifiedExpression(node);
|
|
125
|
+
if (expression.type === "CallExpression") {
|
|
126
|
+
const call = expression;
|
|
127
|
+
const calleeSpans = collectCallArgumentSpans(call.callee);
|
|
128
|
+
const argumentSpans = call.arguments.flatMap((arg) => arg.type === "SpreadElement" ? [] : [expressionSpan(arg)]);
|
|
129
|
+
return [...calleeSpans, ...argumentSpans];
|
|
130
|
+
}
|
|
131
|
+
if (expression.type === "MemberExpression") {
|
|
132
|
+
return collectCallArgumentSpans(expression.object);
|
|
133
|
+
}
|
|
134
|
+
return [];
|
|
135
|
+
};
|
|
136
|
+
export const collectUsageExpressionSpans = (usage) => {
|
|
137
|
+
const calleeSpans = collectCallArgumentSpans(usage.callee);
|
|
138
|
+
if (usage.kind === "template") {
|
|
139
|
+
return [...calleeSpans, ...usage.target.quasi.expressions.map((expression) => expressionSpan(expression))];
|
|
140
|
+
}
|
|
141
|
+
return [...calleeSpans, ...usage.target.arguments.flatMap((arg) => arg.type === "SpreadElement" ? [] : [expressionSpan(arg)])];
|
|
142
|
+
};
|
|
143
|
+
//# sourceMappingURL=processorUsages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAQA,SAAS,0BAA0B;AAWnC,OAAO,MAAM,iBAAiB,SAA0C;AACtE,KAAI,KAAK,UAAU;AACjB,SAAO,KAAK,SAAS,SAAS,aAC5B,OAAO,KAAK,SAAS,UAAU,WAC7B,KAAK,SAAS,QACd;;AAGN,QAAO,KAAK,SAAS,SAAS,eAAe,KAAK,SAAS,OAAO;;AAGpE,OAAO,MAAM,6BAA6B,SAAiC;AACzE,KACE,KAAK,SAAS,oBACd,KAAK,SAAS,2BACd,KAAK,SAAS,yBACd,KAAK,SAAS,qBACd,KAAK,SAAS,2BACd;AACA,SAAO,0BACJ,KAA0D,WAC5D;;AAGH,KAAI,KAAK,SAAS,sBAAsB;EACtC,MAAM,WAAW;A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;EACb,MAAM,cAAc,yBAAyB,KAAK,OAAO;EACzD,MAAM,gBAAgB,KAAK,UAAU,SAAS,QAC5C,IAAI,SAAS,kBAAkB,EAAE,GAAG,CAAC,eAAe,IAAkB,CAAC,CACxE;AACD,SAAO,CAAC,GAAG,aAAa,GAAG,cAAc;;AAG3C,KAAI,WAAW,SAAS,oBAAoB;AAC1C,SAAO,yBAAyB,WAAW,OAAO;;AAGpD,QAAO,EAAE;;AAGX,OAAO,MAAM,+BACX,UACqB;CACrB,MAAM,cAAc,yBAAyB,MAAM,OAAO;AAC1D,KAAI,MAAM,SAAS,YAAY;AAC7B,SAAO,CACL,GAAG,aACH,GAAG,MAAM,OAAO,MAAM,YAAY,KAAK,eACrC,eAAe,WAAyB,CACzC,CACF;;AAGH,QAAO,CACL,GAAG,aACH,GAAG,MAAM,OAAO,UAAU,SAAS,QACjC,IAAI,SAAS,kBAAkB,EAAE,GAAG,CAAC,eAAe,IAAkB,CAAC,CACxE,CACF","names":[],"sources":["../../../src/utils/applyOxcProcessors/processorUsages.ts"],"version":3,"sourcesContent":["import type {\n CallExpression,\n Expression,\n MemberExpression,\n Node,\n Program,\n} from 'oxc-parser';\n\nimport { getOxcNodeChildren } from '../oxc/ast';\nimport type {\n CallExpressionLike,\n DefinedProcessor,\n ExpressionSpan,\n OxcIdentifier,\n ProcessorUsage,\n QualifiedExpression,\n SequenceExpressionLike,\n} from './types';\n\nexport const getMemberName = (node: MemberExpression): string | null => {\n if (node.computed) {\n return node.property.type === 'Literal' &&\n typeof node.property.value === 'string'\n ? node.property.value\n : null;\n }\n\n return node.property.type === 'Identifier' ? node.property.name : null;\n};\n\nexport const unwrapQualifiedExpression = (node: Expression): Expression => {\n if (\n node.type === 'TSAsExpression' ||\n node.type === 'TSSatisfiesExpression' ||\n node.type === 'TSNonNullExpression' ||\n node.type === 'TSTypeAssertion' ||\n node.type === 'ParenthesizedExpression'\n ) {\n return unwrapQualifiedExpression(\n (node as QualifiedExpression & { expression: Expression }).expression\n );\n }\n\n if (node.type === 'SequenceExpression') {\n const sequence = node as SequenceExpressionLike;\n return unwrapQualifiedExpression(\n sequence.expressions[sequence.expressions.length - 1] ?? node\n );\n }\n\n return node;\n};\n\nexport const getRootIdentifier = (node: Expression): OxcIdentifier | null => {\n const expression = unwrapQualifiedExpression(node);\n\n if (expression.type === 'Identifier') {\n return expression;\n }\n\n if (expression.type === 'MemberExpression') {\n return getRootIdentifier(expression.object);\n }\n\n if (expression.type === 'CallExpression') {\n return getRootIdentifier((expression as CallExpressionLike).callee);\n }\n\n return null;\n};\n\nexport const getQualifiedName = (node: Expression): string | null => {\n const expression = unwrapQualifiedExpression(node);\n\n if (expression.type === 'Identifier') {\n return expression.name;\n }\n\n if (expression.type === 'MemberExpression') {\n const object = getQualifiedName(expression.object);\n const member = getMemberName(expression);\n return object && member ? `${object}.${member}` : null;\n }\n\n if (expression.type === 'CallExpression') {\n return getQualifiedName((expression as CallExpressionLike).callee);\n }\n\n return null;\n};\n\nexport const resolveDefinedProcessor = (\n callee: Expression,\n definedProcessors: Map<string, DefinedProcessor>\n): {\n collapseQualifiedCallee: boolean;\n definedProcessor: DefinedProcessor;\n} | null => {\n const qualified = getQualifiedName(callee);\n if (qualified) {\n const definedProcessor = definedProcessors.get(qualified);\n if (definedProcessor) {\n return {\n collapseQualifiedCallee: qualified.includes('.'),\n definedProcessor,\n };\n }\n }\n\n const root = getRootIdentifier(callee);\n if (!root) {\n return null;\n }\n\n const definedProcessor = definedProcessors.get(root.name);\n return definedProcessor\n ? {\n collapseQualifiedCallee: false,\n definedProcessor,\n }\n : null;\n};\n\nexport const isCallTagOfTaggedTemplate = (\n node: Node,\n parent: Node | null\n): boolean =>\n parent?.type === 'TaggedTemplateExpression' && parent.tag === node;\n\nexport const expandReplacementTarget = (\n target: Expression,\n ancestors: Node[]\n): Expression => {\n let current: Expression = target;\n\n for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {\n const ancestor = ancestors[idx];\n if (\n ancestor.type === 'SequenceExpression' &&\n ancestor.expressions[ancestor.expressions.length - 1] === current\n ) {\n current = ancestor as Expression;\n } else if (\n ancestor.type === 'ParenthesizedExpression' &&\n ancestor.expression === current\n ) {\n current = ancestor as Expression;\n } else {\n break;\n }\n }\n\n return current;\n};\n\nexport const collectProcessorUsages = (\n program: Program,\n definedProcessors: Map<string, DefinedProcessor>\n): ProcessorUsage[] => {\n const usages: ProcessorUsage[] = [];\n\n const walk = (\n node: Node,\n ancestors: Node[],\n parent: Node | null = null\n ): void => {\n if (node.type === 'TaggedTemplateExpression') {\n const callee = node.tag as Expression;\n const resolvedProcessor = resolveDefinedProcessor(\n callee,\n definedProcessors\n );\n if (resolvedProcessor) {\n usages.push({\n ancestors,\n callee,\n collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,\n definedProcessor: resolvedProcessor.definedProcessor,\n kind: 'template',\n replacementTarget: expandReplacementTarget(node, ancestors),\n target: node,\n });\n }\n } else if (\n node.type === 'CallExpression' &&\n !isCallTagOfTaggedTemplate(node, parent)\n ) {\n const { callee } = node as CallExpressionLike;\n const resolvedProcessor = resolveDefinedProcessor(\n callee,\n definedProcessors\n );\n if (resolvedProcessor) {\n usages.push({\n ancestors,\n callee,\n collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,\n definedProcessor: resolvedProcessor.definedProcessor,\n kind: 'call',\n replacementTarget: expandReplacementTarget(\n node as CallExpression,\n ancestors\n ),\n target: node as CallExpression,\n });\n }\n }\n\n getOxcNodeChildren(node).forEach((child) =>\n walk(child, [...ancestors, node], node)\n );\n };\n\n walk(program, []);\n\n return usages.sort((a, b) => a.target.start - b.target.start);\n};\n\nexport const expressionSpan = (expression: Expression): ExpressionSpan => ({\n end: expression.end,\n start: expression.start,\n});\n\nexport const collectCallArgumentSpans = (\n node: Expression\n): ExpressionSpan[] => {\n const expression = unwrapQualifiedExpression(node);\n\n if (expression.type === 'CallExpression') {\n const call = expression as CallExpressionLike;\n const calleeSpans = collectCallArgumentSpans(call.callee);\n const argumentSpans = call.arguments.flatMap((arg) =>\n arg.type === 'SpreadElement' ? [] : [expressionSpan(arg as Expression)]\n );\n return [...calleeSpans, ...argumentSpans];\n }\n\n if (expression.type === 'MemberExpression') {\n return collectCallArgumentSpans(expression.object);\n }\n\n return [];\n};\n\nexport const collectUsageExpressionSpans = (\n usage: ProcessorUsage\n): ExpressionSpan[] => {\n const calleeSpans = collectCallArgumentSpans(usage.callee);\n if (usage.kind === 'template') {\n return [\n ...calleeSpans,\n ...usage.target.quasi.expressions.map((expression) =>\n expressionSpan(expression as Expression)\n ),\n ];\n }\n\n return [\n ...calleeSpans,\n ...usage.target.arguments.flatMap((arg) =>\n arg.type === 'SpreadElement' ? [] : [expressionSpan(arg as Expression)]\n ),\n ];\n};\n"],"file":"processorUsages.js"}
|