@wyw-in-js/transform 0.1.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/LICENSE +21 -0
- package/esm/babel.js +2 -0
- package/esm/babel.js.map +1 -0
- package/esm/cache.js +80 -0
- package/esm/cache.js.map +1 -0
- package/esm/debug/fileReporter.js +144 -0
- package/esm/debug/fileReporter.js.map +1 -0
- package/esm/evaluators/index.js +14 -0
- package/esm/evaluators/index.js.map +1 -0
- package/esm/index.js +24 -0
- package/esm/index.js.map +1 -0
- package/esm/module.js +316 -0
- package/esm/module.js.map +1 -0
- package/esm/options/babel-merge.d.js +2 -0
- package/esm/options/babel-merge.d.js.map +1 -0
- package/esm/options/buildOptions.js +28 -0
- package/esm/options/buildOptions.js.map +1 -0
- package/esm/options/loadBabelOptions.js +24 -0
- package/esm/options/loadBabelOptions.js.map +1 -0
- package/esm/plugins/babel-transform.js +53 -0
- package/esm/plugins/babel-transform.js.map +1 -0
- package/esm/plugins/collector.js +66 -0
- package/esm/plugins/collector.js.map +1 -0
- package/esm/plugins/dynamic-import.js +28 -0
- package/esm/plugins/dynamic-import.js.map +1 -0
- package/esm/plugins/preeval.js +75 -0
- package/esm/plugins/preeval.js.map +1 -0
- package/esm/plugins/shaker.js +296 -0
- package/esm/plugins/shaker.js.map +1 -0
- package/esm/shaker.js +50 -0
- package/esm/shaker.js.map +1 -0
- package/esm/transform/BaseEntrypoint.js +164 -0
- package/esm/transform/BaseEntrypoint.js.map +1 -0
- package/esm/transform/Entrypoint.helpers.js +168 -0
- package/esm/transform/Entrypoint.helpers.js.map +1 -0
- package/esm/transform/Entrypoint.js +212 -0
- package/esm/transform/Entrypoint.js.map +1 -0
- package/esm/transform/Entrypoint.types.js +2 -0
- package/esm/transform/Entrypoint.types.js.map +1 -0
- package/esm/transform/EvaluatedEntrypoint.js +6 -0
- package/esm/transform/EvaluatedEntrypoint.js.map +1 -0
- package/esm/transform/actions/AbortError.js +8 -0
- package/esm/transform/actions/AbortError.js.map +1 -0
- package/esm/transform/actions/BaseAction.js +134 -0
- package/esm/transform/actions/BaseAction.js.map +1 -0
- package/esm/transform/actions/UnprocessedEntrypointError.js +8 -0
- package/esm/transform/actions/UnprocessedEntrypointError.js.map +1 -0
- package/esm/transform/actions/actionRunner.js +73 -0
- package/esm/transform/actions/actionRunner.js.map +1 -0
- package/esm/transform/actions/types.js +2 -0
- package/esm/transform/actions/types.js.map +1 -0
- package/esm/transform/generators/baseProcessingHandlers.js +22 -0
- package/esm/transform/generators/baseProcessingHandlers.js.map +1 -0
- package/esm/transform/generators/collect.js +58 -0
- package/esm/transform/generators/collect.js.map +1 -0
- package/esm/transform/generators/evalFile.js +50 -0
- package/esm/transform/generators/evalFile.js.map +1 -0
- package/esm/transform/generators/explodeReexports.js +63 -0
- package/esm/transform/generators/explodeReexports.js.map +1 -0
- package/esm/transform/generators/extract.js +120 -0
- package/esm/transform/generators/extract.js.map +1 -0
- package/esm/transform/generators/getExports.js +73 -0
- package/esm/transform/generators/getExports.js.map +1 -0
- package/esm/transform/generators/index.js +13 -0
- package/esm/transform/generators/index.js.map +1 -0
- package/esm/transform/generators/processEntrypoint.js +43 -0
- package/esm/transform/generators/processEntrypoint.js.map +1 -0
- package/esm/transform/generators/processImports.js +23 -0
- package/esm/transform/generators/processImports.js.map +1 -0
- package/esm/transform/generators/resolveImports.js +148 -0
- package/esm/transform/generators/resolveImports.js.map +1 -0
- package/esm/transform/generators/transform.js +117 -0
- package/esm/transform/generators/transform.js.map +1 -0
- package/esm/transform/generators/workflow.js +82 -0
- package/esm/transform/generators/workflow.js.map +1 -0
- package/esm/transform/helpers/loadLinariaOptions.js +66 -0
- package/esm/transform/helpers/loadLinariaOptions.js.map +1 -0
- package/esm/transform/helpers/withDefaultServices.js +21 -0
- package/esm/transform/helpers/withDefaultServices.js.map +1 -0
- package/esm/transform/rootLog.js +3 -0
- package/esm/transform/rootLog.js.map +1 -0
- package/esm/transform/types.js +2 -0
- package/esm/transform/types.js.map +1 -0
- package/esm/transform.js +121 -0
- package/esm/transform.js.map +1 -0
- package/esm/types.js +2 -0
- package/esm/types.js.map +1 -0
- package/esm/utils/EventEmitter.js +52 -0
- package/esm/utils/EventEmitter.js.map +1 -0
- package/esm/utils/ShakerMetadata.js +2 -0
- package/esm/utils/ShakerMetadata.js.map +1 -0
- package/esm/utils/TransformMetadata.js +11 -0
- package/esm/utils/TransformMetadata.js.map +1 -0
- package/esm/utils/addIdentifierToLinariaPreval.js +68 -0
- package/esm/utils/addIdentifierToLinariaPreval.js.map +1 -0
- package/esm/utils/collectExportsAndImports.js +974 -0
- package/esm/utils/collectExportsAndImports.js.map +1 -0
- package/esm/utils/collectTemplateDependencies.js +219 -0
- package/esm/utils/collectTemplateDependencies.js.map +1 -0
- package/esm/utils/createId.js +6 -0
- package/esm/utils/createId.js.map +1 -0
- package/esm/utils/dispose-polyfill.js +6 -0
- package/esm/utils/dispose-polyfill.js.map +1 -0
- package/esm/utils/findIdentifiers.js +62 -0
- package/esm/utils/findIdentifiers.js.map +1 -0
- package/esm/utils/findPackageJSON.js +22 -0
- package/esm/utils/findPackageJSON.js.map +1 -0
- package/esm/utils/getFileIdx.js +10 -0
- package/esm/utils/getFileIdx.js.map +1 -0
- package/esm/utils/getPluginKey.js +13 -0
- package/esm/utils/getPluginKey.js.map +1 -0
- package/esm/utils/getScope.js +6 -0
- package/esm/utils/getScope.js.map +1 -0
- package/esm/utils/getSource.js +15 -0
- package/esm/utils/getSource.js.map +1 -0
- package/esm/utils/getTagProcessor.js +304 -0
- package/esm/utils/getTagProcessor.js.map +1 -0
- package/esm/utils/getVisitorKeys.js +5 -0
- package/esm/utils/getVisitorKeys.js.map +1 -0
- package/esm/utils/hasLinariaPreval.js +7 -0
- package/esm/utils/hasLinariaPreval.js.map +1 -0
- package/esm/utils/isExports.js +22 -0
- package/esm/utils/isExports.js.map +1 -0
- package/esm/utils/isGlobal.js +6 -0
- package/esm/utils/isGlobal.js.map +1 -0
- package/esm/utils/isNode.js +2 -0
- package/esm/utils/isNode.js.map +1 -0
- package/esm/utils/isNodePath.js +4 -0
- package/esm/utils/isNodePath.js.map +1 -0
- package/esm/utils/isNotNull.js +4 -0
- package/esm/utils/isNotNull.js.map +1 -0
- package/esm/utils/isRemoved.js +46 -0
- package/esm/utils/isRemoved.js.map +1 -0
- package/esm/utils/isRequire.js +13 -0
- package/esm/utils/isRequire.js.map +1 -0
- package/esm/utils/isSerializable.js +13 -0
- package/esm/utils/isSerializable.js.map +1 -0
- package/esm/utils/isTypedNode.js +6 -0
- package/esm/utils/isTypedNode.js.map +1 -0
- package/esm/utils/isUnnecessaryReactCall.js +72 -0
- package/esm/utils/isUnnecessaryReactCall.js.map +1 -0
- package/esm/utils/peek.js +2 -0
- package/esm/utils/peek.js.map +1 -0
- package/esm/utils/processTemplateExpression.js +11 -0
- package/esm/utils/processTemplateExpression.js.map +1 -0
- package/esm/utils/removeDangerousCode.js +127 -0
- package/esm/utils/removeDangerousCode.js.map +1 -0
- package/esm/utils/scopeHelpers.js +514 -0
- package/esm/utils/scopeHelpers.js.map +1 -0
- package/esm/utils/traversalCache.js +28 -0
- package/esm/utils/traversalCache.js.map +1 -0
- package/esm/utils/unwrapSequence.js +14 -0
- package/esm/utils/unwrapSequence.js.map +1 -0
- package/esm/utils/valueToLiteral.js +59 -0
- package/esm/utils/valueToLiteral.js.map +1 -0
- package/esm/utils/visitors/JSXElementsRemover.js +51 -0
- package/esm/utils/visitors/JSXElementsRemover.js.map +1 -0
- package/esm/vm/createVmContext.js +72 -0
- package/esm/vm/createVmContext.js.map +1 -0
- package/esm/vm/process.js +29 -0
- package/esm/vm/process.js.map +1 -0
- package/lib/babel.js +2 -0
- package/lib/babel.js.map +1 -0
- package/lib/cache.js +87 -0
- package/lib/cache.js.map +1 -0
- package/lib/debug/fileReporter.js +153 -0
- package/lib/debug/fileReporter.js.map +1 -0
- package/lib/evaluators/index.js +20 -0
- package/lib/evaluators/index.js.map +1 -0
- package/lib/index.js +225 -0
- package/lib/index.js.map +1 -0
- package/lib/module.js +327 -0
- package/lib/module.js.map +1 -0
- package/lib/options/babel-merge.d.js +2 -0
- package/lib/options/babel-merge.d.js.map +1 -0
- package/lib/options/buildOptions.js +35 -0
- package/lib/options/buildOptions.js.map +1 -0
- package/lib/options/loadBabelOptions.js +31 -0
- package/lib/options/loadBabelOptions.js.map +1 -0
- package/lib/plugins/babel-transform.js +60 -0
- package/lib/plugins/babel-transform.js.map +1 -0
- package/lib/plugins/collector.js +76 -0
- package/lib/plugins/collector.js.map +1 -0
- package/lib/plugins/dynamic-import.js +34 -0
- package/lib/plugins/dynamic-import.js.map +1 -0
- package/lib/plugins/preeval.js +82 -0
- package/lib/plugins/preeval.js.map +1 -0
- package/lib/plugins/shaker.js +303 -0
- package/lib/plugins/shaker.js.map +1 -0
- package/lib/shaker.js +58 -0
- package/lib/shaker.js.map +1 -0
- package/lib/transform/BaseEntrypoint.js +175 -0
- package/lib/transform/BaseEntrypoint.js.map +1 -0
- package/lib/transform/Entrypoint.helpers.js +182 -0
- package/lib/transform/Entrypoint.helpers.js.map +1 -0
- package/lib/transform/Entrypoint.js +226 -0
- package/lib/transform/Entrypoint.js.map +1 -0
- package/lib/transform/Entrypoint.types.js +2 -0
- package/lib/transform/Entrypoint.types.js.map +1 -0
- package/lib/transform/EvaluatedEntrypoint.js +13 -0
- package/lib/transform/EvaluatedEntrypoint.js.map +1 -0
- package/lib/transform/actions/AbortError.js +16 -0
- package/lib/transform/actions/AbortError.js.map +1 -0
- package/lib/transform/actions/BaseAction.js +144 -0
- package/lib/transform/actions/BaseAction.js.map +1 -0
- package/lib/transform/actions/UnprocessedEntrypointError.js +16 -0
- package/lib/transform/actions/UnprocessedEntrypointError.js.map +1 -0
- package/lib/transform/actions/actionRunner.js +82 -0
- package/lib/transform/actions/actionRunner.js.map +1 -0
- package/lib/transform/actions/types.js +2 -0
- package/lib/transform/actions/types.js.map +1 -0
- package/lib/transform/generators/baseProcessingHandlers.js +27 -0
- package/lib/transform/generators/baseProcessingHandlers.js.map +1 -0
- package/lib/transform/generators/collect.js +66 -0
- package/lib/transform/generators/collect.js.map +1 -0
- package/lib/transform/generators/evalFile.js +57 -0
- package/lib/transform/generators/evalFile.js.map +1 -0
- package/lib/transform/generators/explodeReexports.js +70 -0
- package/lib/transform/generators/explodeReexports.js.map +1 -0
- package/lib/transform/generators/extract.js +128 -0
- package/lib/transform/generators/extract.js.map +1 -0
- package/lib/transform/generators/getExports.js +80 -0
- package/lib/transform/generators/getExports.js.map +1 -0
- package/lib/transform/generators/index.js +19 -0
- package/lib/transform/generators/index.js.map +1 -0
- package/lib/transform/generators/processEntrypoint.js +49 -0
- package/lib/transform/generators/processEntrypoint.js.map +1 -0
- package/lib/transform/generators/processImports.js +29 -0
- package/lib/transform/generators/processImports.js.map +1 -0
- package/lib/transform/generators/resolveImports.js +158 -0
- package/lib/transform/generators/resolveImports.js.map +1 -0
- package/lib/transform/generators/transform.js +130 -0
- package/lib/transform/generators/transform.js.map +1 -0
- package/lib/transform/generators/workflow.js +90 -0
- package/lib/transform/generators/workflow.js.map +1 -0
- package/lib/transform/helpers/loadLinariaOptions.js +73 -0
- package/lib/transform/helpers/loadLinariaOptions.js.map +1 -0
- package/lib/transform/helpers/withDefaultServices.js +30 -0
- package/lib/transform/helpers/withDefaultServices.js.map +1 -0
- package/lib/transform/rootLog.js +9 -0
- package/lib/transform/rootLog.js.map +1 -0
- package/lib/transform/types.js +8 -0
- package/lib/transform/types.js.map +1 -0
- package/lib/transform.js +128 -0
- package/lib/transform.js.map +1 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/lib/utils/EventEmitter.js +61 -0
- package/lib/utils/EventEmitter.js.map +1 -0
- package/lib/utils/ShakerMetadata.js +9 -0
- package/lib/utils/ShakerMetadata.js.map +1 -0
- package/lib/utils/TransformMetadata.js +19 -0
- package/lib/utils/TransformMetadata.js.map +1 -0
- package/lib/utils/addIdentifierToLinariaPreval.js +75 -0
- package/lib/utils/addIdentifierToLinariaPreval.js.map +1 -0
- package/lib/utils/collectExportsAndImports.js +990 -0
- package/lib/utils/collectExportsAndImports.js.map +1 -0
- package/lib/utils/collectTemplateDependencies.js +233 -0
- package/lib/utils/collectTemplateDependencies.js.map +1 -0
- package/lib/utils/createId.js +13 -0
- package/lib/utils/createId.js.map +1 -0
- package/lib/utils/dispose-polyfill.js +9 -0
- package/lib/utils/dispose-polyfill.js.map +1 -0
- package/lib/utils/findIdentifiers.js +73 -0
- package/lib/utils/findIdentifiers.js.map +1 -0
- package/lib/utils/findPackageJSON.js +29 -0
- package/lib/utils/findPackageJSON.js.map +1 -0
- package/lib/utils/getFileIdx.js +16 -0
- package/lib/utils/getFileIdx.js.map +1 -0
- package/lib/utils/getPluginKey.js +21 -0
- package/lib/utils/getPluginKey.js.map +1 -0
- package/lib/utils/getScope.js +12 -0
- package/lib/utils/getScope.js.map +1 -0
- package/lib/utils/getSource.js +24 -0
- package/lib/utils/getSource.js.map +1 -0
- package/lib/utils/getTagProcessor.js +318 -0
- package/lib/utils/getTagProcessor.js.map +1 -0
- package/lib/utils/getVisitorKeys.js +11 -0
- package/lib/utils/getVisitorKeys.js.map +1 -0
- package/lib/utils/hasLinariaPreval.js +13 -0
- package/lib/utils/hasLinariaPreval.js.map +1 -0
- package/lib/utils/isExports.js +27 -0
- package/lib/utils/isExports.js.map +1 -0
- package/lib/utils/isGlobal.js +13 -0
- package/lib/utils/isGlobal.js.map +1 -0
- package/lib/utils/isNode.js +9 -0
- package/lib/utils/isNode.js.map +1 -0
- package/lib/utils/isNodePath.js +10 -0
- package/lib/utils/isNodePath.js.map +1 -0
- package/lib/utils/isNotNull.js +10 -0
- package/lib/utils/isNotNull.js.map +1 -0
- package/lib/utils/isRemoved.js +52 -0
- package/lib/utils/isRemoved.js.map +1 -0
- package/lib/utils/isRequire.js +18 -0
- package/lib/utils/isRequire.js.map +1 -0
- package/lib/utils/isSerializable.js +19 -0
- package/lib/utils/isSerializable.js.map +1 -0
- package/lib/utils/isTypedNode.js +13 -0
- package/lib/utils/isTypedNode.js.map +1 -0
- package/lib/utils/isUnnecessaryReactCall.js +81 -0
- package/lib/utils/isUnnecessaryReactCall.js.map +1 -0
- package/lib/utils/peek.js +9 -0
- package/lib/utils/peek.js.map +1 -0
- package/lib/utils/processTemplateExpression.js +18 -0
- package/lib/utils/processTemplateExpression.js.map +1 -0
- package/lib/utils/removeDangerousCode.js +134 -0
- package/lib/utils/removeDangerousCode.js.map +1 -0
- package/lib/utils/scopeHelpers.js +544 -0
- package/lib/utils/scopeHelpers.js.map +1 -0
- package/lib/utils/traversalCache.js +38 -0
- package/lib/utils/traversalCache.js.map +1 -0
- package/lib/utils/unwrapSequence.js +20 -0
- package/lib/utils/unwrapSequence.js.map +1 -0
- package/lib/utils/valueToLiteral.js +65 -0
- package/lib/utils/valueToLiteral.js.map +1 -0
- package/lib/utils/visitors/JSXElementsRemover.js +57 -0
- package/lib/utils/visitors/JSXElementsRemover.js.map +1 -0
- package/lib/vm/createVmContext.js +80 -0
- package/lib/vm/createVmContext.js.map +1 -0
- package/lib/vm/process.js +38 -0
- package/lib/vm/process.js.map +1 -0
- package/package.json +63 -0
- package/types/babel.d.ts +2 -0
- package/types/babel.js +2 -0
- package/types/cache.d.ts +24 -0
- package/types/cache.js +85 -0
- package/types/debug/fileReporter.d.ts +27 -0
- package/types/debug/fileReporter.js +151 -0
- package/types/evaluators/index.d.ts +10 -0
- package/types/evaluators/index.js +15 -0
- package/types/index.d.ts +30 -0
- package/types/index.js +69 -0
- package/types/module.d.ts +62 -0
- package/types/module.js +318 -0
- package/types/options/buildOptions.d.ts +7 -0
- package/types/options/buildOptions.js +35 -0
- package/types/options/loadBabelOptions.d.ts +3 -0
- package/types/options/loadBabelOptions.js +27 -0
- package/types/plugins/babel-transform.d.ts +4 -0
- package/types/plugins/babel-transform.js +49 -0
- package/types/plugins/collector.d.ts +16 -0
- package/types/plugins/collector.js +67 -0
- package/types/plugins/dynamic-import.d.ts +6 -0
- package/types/plugins/dynamic-import.js +32 -0
- package/types/plugins/preeval.d.ts +16 -0
- package/types/plugins/preeval.js +69 -0
- package/types/plugins/shaker.d.ts +11 -0
- package/types/plugins/shaker.js +299 -0
- package/types/shaker.d.ts +3 -0
- package/types/shaker.js +64 -0
- package/types/transform/BaseEntrypoint.d.ts +23 -0
- package/types/transform/BaseEntrypoint.js +182 -0
- package/types/transform/Entrypoint.d.ts +50 -0
- package/types/transform/Entrypoint.helpers.d.ts +14 -0
- package/types/transform/Entrypoint.helpers.js +169 -0
- package/types/transform/Entrypoint.js +223 -0
- package/types/transform/Entrypoint.types.d.ts +23 -0
- package/types/transform/Entrypoint.types.js +2 -0
- package/types/transform/EvaluatedEntrypoint.d.ts +16 -0
- package/types/transform/EvaluatedEntrypoint.js +9 -0
- package/types/transform/actions/AbortError.d.ts +4 -0
- package/types/transform/actions/AbortError.js +12 -0
- package/types/transform/actions/BaseAction.d.ts +40 -0
- package/types/transform/actions/BaseAction.js +149 -0
- package/types/transform/actions/UnprocessedEntrypointError.d.ts +6 -0
- package/types/transform/actions/UnprocessedEntrypointError.js +13 -0
- package/types/transform/actions/actionRunner.d.ts +4 -0
- package/types/transform/actions/actionRunner.js +86 -0
- package/types/transform/actions/types.d.ts +15 -0
- package/types/transform/actions/types.js +2 -0
- package/types/transform/generators/baseProcessingHandlers.d.ts +17 -0
- package/types/transform/generators/baseProcessingHandlers.js +23 -0
- package/types/transform/generators/collect.d.ts +6 -0
- package/types/transform/generators/collect.js +57 -0
- package/types/transform/generators/evalFile.d.ts +6 -0
- package/types/transform/generators/evalFile.js +56 -0
- package/types/transform/generators/explodeReexports.d.ts +7 -0
- package/types/transform/generators/explodeReexports.js +67 -0
- package/types/transform/generators/extract.d.ts +8 -0
- package/types/transform/generators/extract.js +116 -0
- package/types/transform/generators/getExports.d.ts +8 -0
- package/types/transform/generators/getExports.js +65 -0
- package/types/transform/generators/index.d.ts +15 -0
- package/types/transform/generators/index.js +15 -0
- package/types/transform/generators/processEntrypoint.d.ts +9 -0
- package/types/transform/generators/processEntrypoint.js +88 -0
- package/types/transform/generators/processImports.d.ts +5 -0
- package/types/transform/generators/processImports.js +21 -0
- package/types/transform/generators/resolveImports.d.ts +9 -0
- package/types/transform/generators/resolveImports.js +124 -0
- package/types/transform/generators/transform.d.ts +17 -0
- package/types/transform/generators/transform.js +115 -0
- package/types/transform/generators/workflow.d.ts +7 -0
- package/types/transform/generators/workflow.js +77 -0
- package/types/transform/helpers/loadLinariaOptions.d.ts +6 -0
- package/types/transform/helpers/loadLinariaOptions.js +86 -0
- package/types/transform/helpers/withDefaultServices.d.ts +5 -0
- package/types/transform/helpers/withDefaultServices.js +40 -0
- package/types/transform/rootLog.d.ts +3 -0
- package/types/transform/rootLog.js +5 -0
- package/types/transform/types.d.ts +137 -0
- package/types/transform/types.js +4 -0
- package/types/transform.d.ts +20 -0
- package/types/transform.js +121 -0
- package/types/types.d.ts +72 -0
- package/types/types.js +5 -0
- package/types/utils/EventEmitter.d.ts +58 -0
- package/types/utils/EventEmitter.js +61 -0
- package/types/utils/ShakerMetadata.d.ts +7 -0
- package/types/utils/ShakerMetadata.js +5 -0
- package/types/utils/TransformMetadata.d.ts +13 -0
- package/types/utils/TransformMetadata.js +17 -0
- package/types/utils/addIdentifierToLinariaPreval.d.ts +4 -0
- package/types/utils/addIdentifierToLinariaPreval.js +75 -0
- package/types/utils/collectExportsAndImports.d.ts +31 -0
- package/types/utils/collectExportsAndImports.js +962 -0
- package/types/utils/collectTemplateDependencies.d.ts +17 -0
- package/types/utils/collectTemplateDependencies.js +212 -0
- package/types/utils/createId.d.ts +2 -0
- package/types/utils/createId.js +9 -0
- package/types/utils/dispose-polyfill.d.ts +0 -0
- package/types/utils/dispose-polyfill.js +5 -0
- package/types/utils/findIdentifiers.d.ts +6 -0
- package/types/utils/findIdentifiers.js +68 -0
- package/types/utils/findPackageJSON.d.ts +1 -0
- package/types/utils/findPackageJSON.js +29 -0
- package/types/utils/getFileIdx.d.ts +1 -0
- package/types/utils/getFileIdx.js +13 -0
- package/types/utils/getPluginKey.d.ts +2 -0
- package/types/utils/getPluginKey.js +16 -0
- package/types/utils/getScope.d.ts +2 -0
- package/types/utils/getScope.js +11 -0
- package/types/utils/getSource.d.ts +2 -0
- package/types/utils/getSource.js +22 -0
- package/types/utils/getTagProcessor.d.ts +6 -0
- package/types/utils/getTagProcessor.js +315 -0
- package/types/utils/getVisitorKeys.d.ts +5 -0
- package/types/utils/getVisitorKeys.js +8 -0
- package/types/utils/hasLinariaPreval.d.ts +4 -0
- package/types/utils/hasLinariaPreval.js +9 -0
- package/types/utils/isExports.d.ts +6 -0
- package/types/utils/isExports.js +20 -0
- package/types/utils/isGlobal.d.ts +2 -0
- package/types/utils/isGlobal.js +9 -0
- package/types/utils/isNode.d.ts +2 -0
- package/types/utils/isNode.js +7 -0
- package/types/utils/isNodePath.d.ts +3 -0
- package/types/utils/isNodePath.js +7 -0
- package/types/utils/isNotNull.d.ts +1 -0
- package/types/utils/isNotNull.js +7 -0
- package/types/utils/isRemoved.d.ts +5 -0
- package/types/utils/isRemoved.js +42 -0
- package/types/utils/isRequire.d.ts +6 -0
- package/types/utils/isRequire.js +15 -0
- package/types/utils/isSerializable.d.ts +2 -0
- package/types/utils/isSerializable.js +18 -0
- package/types/utils/isTypedNode.d.ts +5 -0
- package/types/utils/isTypedNode.js +9 -0
- package/types/utils/isUnnecessaryReactCall.d.ts +3 -0
- package/types/utils/isUnnecessaryReactCall.js +76 -0
- package/types/utils/peek.d.ts +1 -0
- package/types/utils/peek.js +5 -0
- package/types/utils/processTemplateExpression.d.ts +5 -0
- package/types/utils/processTemplateExpression.js +17 -0
- package/types/utils/removeDangerousCode.d.ts +3 -0
- package/types/utils/removeDangerousCode.js +144 -0
- package/types/utils/scopeHelpers.d.ts +12 -0
- package/types/utils/scopeHelpers.js +568 -0
- package/types/utils/traversalCache.d.ts +5 -0
- package/types/utils/traversalCache.js +36 -0
- package/types/utils/unwrapSequence.d.ts +8 -0
- package/types/utils/unwrapSequence.js +17 -0
- package/types/utils/valueToLiteral.d.ts +3 -0
- package/types/utils/valueToLiteral.js +64 -0
- package/types/utils/visitors/JSXElementsRemover.d.ts +3 -0
- package/types/utils/visitors/JSXElementsRemover.js +52 -0
- package/types/vm/createVmContext.d.ts +7 -0
- package/types/vm/createVmContext.js +92 -0
- package/types/vm/process.d.ts +25 -0
- package/types/vm/process.js +32 -0
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
|
3
|
+
/* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.removeWithRelated = exports.mutate = exports.applyAction = exports.findActionForNode = exports.referenceAll = exports.dereference = exports.reference = void 0;
|
|
6
|
+
const types_1 = require("@babel/types");
|
|
7
|
+
const findIdentifiers_1 = require("./findIdentifiers");
|
|
8
|
+
const getScope_1 = require("./getScope");
|
|
9
|
+
const isNotNull_1 = require("./isNotNull");
|
|
10
|
+
const isRemoved_1 = require("./isRemoved");
|
|
11
|
+
function validateField(node, key, val, field) {
|
|
12
|
+
if (!(field != null && field.validate))
|
|
13
|
+
return true;
|
|
14
|
+
if (field.optional && val == null)
|
|
15
|
+
return true;
|
|
16
|
+
try {
|
|
17
|
+
field.validate(node, key, val);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function getBinding(path) {
|
|
25
|
+
const binding = (0, getScope_1.getScope)(path).getBinding(path.node.name);
|
|
26
|
+
if (!binding) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return binding;
|
|
30
|
+
}
|
|
31
|
+
function reference(path, referencePath = path, force = false) {
|
|
32
|
+
if (!force && !path.isReferencedIdentifier())
|
|
33
|
+
return;
|
|
34
|
+
const binding = getBinding(path);
|
|
35
|
+
if (!binding)
|
|
36
|
+
return;
|
|
37
|
+
if (binding.referencePaths.includes(referencePath)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
binding.referenced = true;
|
|
41
|
+
binding.referencePaths.push(referencePath ?? path);
|
|
42
|
+
binding.references = binding.referencePaths.length;
|
|
43
|
+
}
|
|
44
|
+
exports.reference = reference;
|
|
45
|
+
function isReferenced(binding) {
|
|
46
|
+
const { kind, referenced, referencePaths, path } = binding;
|
|
47
|
+
if (path.isFunctionExpression() &&
|
|
48
|
+
path.key === 'init' &&
|
|
49
|
+
path.parentPath.isVariableDeclarator()) {
|
|
50
|
+
// It is a function expression in a variable declarator
|
|
51
|
+
const id = path.parentPath.get('id');
|
|
52
|
+
if (id.isIdentifier()) {
|
|
53
|
+
const idBinding = getBinding(id);
|
|
54
|
+
return idBinding ? isReferenced(idBinding) : true;
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
if (!referenced) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
// If it's a param binding, we can't just remove it
|
|
62
|
+
// because it brakes the function signature. Keep it alive for now.
|
|
63
|
+
if (kind === 'param') {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
// If all remaining references are in TS/Flow types, binding is unreferenced
|
|
67
|
+
return (referencePaths.length > 0 ||
|
|
68
|
+
referencePaths.every((i) => i.find((ancestor) => ancestor.isTSType() || ancestor.isFlowType())));
|
|
69
|
+
}
|
|
70
|
+
function isReferencedConstantViolation(path, binding) {
|
|
71
|
+
if (path.find((p) => p === binding.path)) {
|
|
72
|
+
// function a(flag) { return (a = function(flag) { flag ? 1 : 2 }) }
|
|
73
|
+
// ^ Looks crazy, yeh? Welcome to the wonderful world of transpilers!
|
|
74
|
+
// `a = …` here isn't a reference.
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
if (!path.isReferenced()) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (path.isAssignmentExpression() &&
|
|
81
|
+
path.parentPath.isExpressionStatement()) {
|
|
82
|
+
// A root assignment without a parent expression statement is not a reference
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
function dereference(path) {
|
|
88
|
+
const binding = getBinding(path);
|
|
89
|
+
if (!binding)
|
|
90
|
+
return null;
|
|
91
|
+
const isReference = binding.referencePaths.includes(path);
|
|
92
|
+
let referencesInConstantViolations = binding.constantViolations.filter((i) => isReferencedConstantViolation(i, binding));
|
|
93
|
+
const isConstantViolation = referencesInConstantViolations.includes(path);
|
|
94
|
+
if (!isReference && !isConstantViolation) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
if (isReference) {
|
|
98
|
+
binding.referencePaths = binding.referencePaths.filter((i) => i !== path);
|
|
99
|
+
binding.references -= 1;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
referencesInConstantViolations = referencesInConstantViolations.filter((i) => i !== path);
|
|
103
|
+
}
|
|
104
|
+
const nonTypeReferences = binding.referencePaths.filter(findIdentifiers_1.nonType);
|
|
105
|
+
binding.referenced =
|
|
106
|
+
nonTypeReferences.length + referencesInConstantViolations.length > 0;
|
|
107
|
+
return binding;
|
|
108
|
+
}
|
|
109
|
+
exports.dereference = dereference;
|
|
110
|
+
function dereferenceAll(path) {
|
|
111
|
+
return (0, findIdentifiers_1.findIdentifiers)([path])
|
|
112
|
+
.map((identifierPath) => dereference(identifierPath))
|
|
113
|
+
.filter(isNotNull_1.isNotNull);
|
|
114
|
+
}
|
|
115
|
+
function referenceAll(path) {
|
|
116
|
+
(0, findIdentifiers_1.findIdentifiers)([path]).forEach((identifierPath) => reference(identifierPath));
|
|
117
|
+
}
|
|
118
|
+
exports.referenceAll = referenceAll;
|
|
119
|
+
const deletingNodes = new WeakSet();
|
|
120
|
+
const isEmptyList = (list) => list.length === 0 || list.every((i) => deletingNodes.has(i));
|
|
121
|
+
const getPathFromAction = (action) => {
|
|
122
|
+
if (!Array.isArray(action)) {
|
|
123
|
+
return action;
|
|
124
|
+
}
|
|
125
|
+
if (action[0] === 'replace' || action[0] === 'remove') {
|
|
126
|
+
return action[1];
|
|
127
|
+
}
|
|
128
|
+
throw new Error(`Unknown action type: ${action[0]}`);
|
|
129
|
+
};
|
|
130
|
+
function isPrototypeAssignment(path) {
|
|
131
|
+
if (!path.isAssignmentExpression()) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
const { left } = path.node;
|
|
135
|
+
if (!left) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
if (left.type !== 'MemberExpression') {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
const { object, property } = left;
|
|
142
|
+
if (!object || !property) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
return (object.type === 'MemberExpression' &&
|
|
146
|
+
object.property.type === 'Identifier' &&
|
|
147
|
+
object.property.name === 'prototype');
|
|
148
|
+
}
|
|
149
|
+
function canFunctionBeDelete(fnPath) {
|
|
150
|
+
if (isPrototypeAssignment(fnPath.parentPath)) {
|
|
151
|
+
// It is a prototype assignment, we can't delete it since we can't find all usages
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
const fnScope = fnPath.scope;
|
|
155
|
+
const parentScope = fnScope.parent;
|
|
156
|
+
if (parentScope.parent) {
|
|
157
|
+
// It isn't a top-level function, so we can't delete it
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
if (fnPath.listKey === 'arguments') {
|
|
161
|
+
// It is passed as an argument to another function, we can't delete it
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
function findActionForNode(path) {
|
|
167
|
+
if ((0, isRemoved_1.isRemoved)(path))
|
|
168
|
+
return null;
|
|
169
|
+
deletingNodes.add(path);
|
|
170
|
+
const parent = path.parentPath;
|
|
171
|
+
if (!parent)
|
|
172
|
+
return ['remove', path];
|
|
173
|
+
if (parent.isProgram()) {
|
|
174
|
+
// Do not delete Program node
|
|
175
|
+
return ['remove', path];
|
|
176
|
+
}
|
|
177
|
+
if (parent.isClassDeclaration() || parent.isClassExpression()) {
|
|
178
|
+
if (path.key === 'body') {
|
|
179
|
+
return ['replace', path, { type: 'ClassBody', body: [] }];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (parent.isFunction()) {
|
|
183
|
+
if (path.listKey === 'params') {
|
|
184
|
+
// Do not remove params of functions
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
if ((path.isBlockStatement() && isEmptyList(path.get('body'))) ||
|
|
188
|
+
path === parent.get('body')) {
|
|
189
|
+
if (!canFunctionBeDelete(parent)) {
|
|
190
|
+
return [
|
|
191
|
+
'replace',
|
|
192
|
+
parent,
|
|
193
|
+
{
|
|
194
|
+
...parent.node,
|
|
195
|
+
async: false,
|
|
196
|
+
body: {
|
|
197
|
+
type: 'BlockStatement',
|
|
198
|
+
body: [],
|
|
199
|
+
directives: [],
|
|
200
|
+
},
|
|
201
|
+
generator: false,
|
|
202
|
+
params: [],
|
|
203
|
+
},
|
|
204
|
+
];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (parent.isConditionalExpression()) {
|
|
209
|
+
if (path.key === 'test') {
|
|
210
|
+
return ['replace', parent, parent.node.alternate];
|
|
211
|
+
}
|
|
212
|
+
if (path.key === 'consequent') {
|
|
213
|
+
return ['replace', path, { type: 'Identifier', name: 'undefined' }];
|
|
214
|
+
}
|
|
215
|
+
if (path.key === 'alternate') {
|
|
216
|
+
return ['replace', path, { type: 'Identifier', name: 'undefined' }];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (parent.isLogicalExpression({ operator: '&&' })) {
|
|
220
|
+
return [
|
|
221
|
+
'replace',
|
|
222
|
+
parent,
|
|
223
|
+
{
|
|
224
|
+
type: 'BooleanLiteral',
|
|
225
|
+
value: false,
|
|
226
|
+
},
|
|
227
|
+
];
|
|
228
|
+
}
|
|
229
|
+
if (parent.isLogicalExpression({ operator: '||' })) {
|
|
230
|
+
return [
|
|
231
|
+
'replace',
|
|
232
|
+
parent,
|
|
233
|
+
path.key === 'left' ? parent.node.right : parent.node.left,
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
if (parent.isObjectProperty()) {
|
|
237
|
+
// let's check if it is a special case with Object.defineProperty
|
|
238
|
+
const key = parent.get('key');
|
|
239
|
+
if (key.isIdentifier({ name: 'get' })) {
|
|
240
|
+
const maybeDefineProperty = parent.parentPath.parentPath;
|
|
241
|
+
if (maybeDefineProperty?.isCallExpression() &&
|
|
242
|
+
maybeDefineProperty
|
|
243
|
+
.get('callee')
|
|
244
|
+
.matchesPattern('Object.defineProperty')) {
|
|
245
|
+
return findActionForNode(maybeDefineProperty);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return findActionForNode(parent);
|
|
249
|
+
}
|
|
250
|
+
if (parent.isTemplateLiteral()) {
|
|
251
|
+
return [
|
|
252
|
+
'replace',
|
|
253
|
+
path,
|
|
254
|
+
{
|
|
255
|
+
type: 'StringLiteral',
|
|
256
|
+
value: '',
|
|
257
|
+
},
|
|
258
|
+
];
|
|
259
|
+
}
|
|
260
|
+
if (parent.isAssignmentExpression()) {
|
|
261
|
+
return findActionForNode(parent);
|
|
262
|
+
}
|
|
263
|
+
if (parent.isCallExpression()) {
|
|
264
|
+
return findActionForNode(parent);
|
|
265
|
+
}
|
|
266
|
+
if (parent.isForInStatement({ left: path.node })) {
|
|
267
|
+
return findActionForNode(parent);
|
|
268
|
+
}
|
|
269
|
+
if (parent.isFunctionExpression({ body: path.node }) ||
|
|
270
|
+
parent.isFunctionDeclaration() ||
|
|
271
|
+
parent.isObjectMethod() ||
|
|
272
|
+
parent.isClassMethod()) {
|
|
273
|
+
return findActionForNode(parent);
|
|
274
|
+
}
|
|
275
|
+
if (parent.isBlockStatement()) {
|
|
276
|
+
const body = parent.get('body');
|
|
277
|
+
if (isEmptyList(body)) {
|
|
278
|
+
return findActionForNode(parent);
|
|
279
|
+
}
|
|
280
|
+
if (path.listKey === 'body' && typeof path.key === 'number') {
|
|
281
|
+
if (path.key > 0) {
|
|
282
|
+
// We can check whether the previous one can be removed
|
|
283
|
+
const prevStatement = body[path.key - 1];
|
|
284
|
+
if (prevStatement.isIfStatement() &&
|
|
285
|
+
prevStatement.get('consequent').isReturnStatement()) {
|
|
286
|
+
// It's `if (…) return …`, we can remove it.
|
|
287
|
+
return findActionForNode(prevStatement);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
else if (body.slice(1).every((statement) => deletingNodes.has(statement))) {
|
|
291
|
+
// If it is the first statement and all other statements
|
|
292
|
+
// are marked for deletion, we can remove the whole block.
|
|
293
|
+
return findActionForNode(parent);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (parent.isVariableDeclarator()) {
|
|
298
|
+
return findActionForNode(parent);
|
|
299
|
+
}
|
|
300
|
+
if (parent.isExportNamedDeclaration() &&
|
|
301
|
+
((path.key === 'specifiers' && isEmptyList(parent.get('specifiers'))) ||
|
|
302
|
+
(path.key === 'declaration' && parent.node.declaration === path.node))) {
|
|
303
|
+
return findActionForNode(parent);
|
|
304
|
+
}
|
|
305
|
+
for (const key of ['body', 'declarations', 'specifiers']) {
|
|
306
|
+
if (path.listKey === key && typeof path.key === 'number') {
|
|
307
|
+
const list = parent.get(key);
|
|
308
|
+
if (isEmptyList(list)) {
|
|
309
|
+
return findActionForNode(parent);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (parent.isTryStatement()) {
|
|
314
|
+
return findActionForNode(parent);
|
|
315
|
+
}
|
|
316
|
+
if (!path.listKey && path.key) {
|
|
317
|
+
const field = types_1.NODE_FIELDS[parent.type][path.key];
|
|
318
|
+
if (!validateField(parent.node, path.key, null, field)) {
|
|
319
|
+
// The parent node isn't valid without this field, so we should remove it also.
|
|
320
|
+
return findActionForNode(parent);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
for (const key of [
|
|
324
|
+
'argument',
|
|
325
|
+
'block',
|
|
326
|
+
'body',
|
|
327
|
+
'callee',
|
|
328
|
+
'discriminant',
|
|
329
|
+
'expression',
|
|
330
|
+
'id',
|
|
331
|
+
'left',
|
|
332
|
+
'object',
|
|
333
|
+
'property',
|
|
334
|
+
'right',
|
|
335
|
+
'test',
|
|
336
|
+
]) {
|
|
337
|
+
if (path.key === key && parent.get(key) === path) {
|
|
338
|
+
return findActionForNode(parent);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return ['remove', path];
|
|
342
|
+
}
|
|
343
|
+
exports.findActionForNode = findActionForNode;
|
|
344
|
+
// @babel/preset-typescript transpiles enums, but doesn't reference used identifiers.
|
|
345
|
+
function referenceEnums(program) {
|
|
346
|
+
/*
|
|
347
|
+
* We are looking for transpiled enums.
|
|
348
|
+
* (function (Colors) {
|
|
349
|
+
* Colors["BLUE"] = "#27509A";
|
|
350
|
+
* })(Colors || (Colors = {}));
|
|
351
|
+
*/
|
|
352
|
+
program.traverse({
|
|
353
|
+
ExpressionStatement(expressionStatement) {
|
|
354
|
+
const expression = expressionStatement.get('expression');
|
|
355
|
+
if (!expression.isCallExpression())
|
|
356
|
+
return;
|
|
357
|
+
const callee = expression.get('callee');
|
|
358
|
+
const args = expression.get('arguments');
|
|
359
|
+
if (!callee.isFunctionExpression() || args.length !== 1)
|
|
360
|
+
return;
|
|
361
|
+
const [arg] = args;
|
|
362
|
+
if (arg.isLogicalExpression({ operator: '||' })) {
|
|
363
|
+
referenceAll(arg);
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
const fixed = new WeakSet();
|
|
369
|
+
function removeUnreferenced(items) {
|
|
370
|
+
const referenced = new Set();
|
|
371
|
+
items.forEach((item) => {
|
|
372
|
+
if (!item.node || (0, isRemoved_1.isRemoved)(item))
|
|
373
|
+
return;
|
|
374
|
+
const binding = (0, getScope_1.getScope)(item).getBinding(item.node.name);
|
|
375
|
+
if (!binding)
|
|
376
|
+
return;
|
|
377
|
+
const hasReferences = binding.referencePaths.filter((i) => !(0, isRemoved_1.isRemoved)(i)).length > 0;
|
|
378
|
+
if (hasReferences) {
|
|
379
|
+
referenced.add(item);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const forDeleting = [binding.path, ...binding.constantViolations]
|
|
383
|
+
.map(findActionForNode)
|
|
384
|
+
.filter(isNotNull_1.isNotNull)
|
|
385
|
+
.map(getPathFromAction);
|
|
386
|
+
if (forDeleting.length === 0)
|
|
387
|
+
return;
|
|
388
|
+
(0, findIdentifiers_1.findIdentifiers)(forDeleting).forEach((identifier) => {
|
|
389
|
+
referenced.add(identifier);
|
|
390
|
+
});
|
|
391
|
+
removeWithRelated(forDeleting);
|
|
392
|
+
});
|
|
393
|
+
const result = [...referenced];
|
|
394
|
+
result.sort((a, b) => a.node?.name.localeCompare(b.node?.name));
|
|
395
|
+
return result;
|
|
396
|
+
}
|
|
397
|
+
function getNodeForValue(value) {
|
|
398
|
+
if (typeof value === 'string') {
|
|
399
|
+
return {
|
|
400
|
+
type: 'StringLiteral',
|
|
401
|
+
value,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
if (typeof value === 'number') {
|
|
405
|
+
return {
|
|
406
|
+
type: 'NumericLiteral',
|
|
407
|
+
value,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
if (typeof value === 'boolean') {
|
|
411
|
+
return {
|
|
412
|
+
type: 'BooleanLiteral',
|
|
413
|
+
value,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
if (value === null) {
|
|
417
|
+
return {
|
|
418
|
+
type: 'NullLiteral',
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
if (value === undefined) {
|
|
422
|
+
return {
|
|
423
|
+
type: 'Identifier',
|
|
424
|
+
name: 'undefined',
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
return undefined;
|
|
428
|
+
}
|
|
429
|
+
function staticEvaluate(path) {
|
|
430
|
+
if (!path)
|
|
431
|
+
return;
|
|
432
|
+
const evaluated = path.evaluate();
|
|
433
|
+
if (evaluated.confident) {
|
|
434
|
+
const node = getNodeForValue(evaluated.value);
|
|
435
|
+
if (node) {
|
|
436
|
+
applyAction(['replace', path, node]);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (path.isIfStatement()) {
|
|
441
|
+
const test = path.get('test');
|
|
442
|
+
if (!test.isBooleanLiteral()) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
const { consequent, alternate } = path.node;
|
|
446
|
+
if (test.node.value) {
|
|
447
|
+
applyAction(['replace', path, consequent]);
|
|
448
|
+
}
|
|
449
|
+
else if (alternate) {
|
|
450
|
+
applyAction(['replace', path, alternate]);
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
applyAction(['remove', path]);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function applyAction(action) {
|
|
458
|
+
mutate(action[1], (p) => {
|
|
459
|
+
if ((0, isRemoved_1.isRemoved)(p))
|
|
460
|
+
return;
|
|
461
|
+
const parent = p.parentPath;
|
|
462
|
+
if (action[0] === 'remove') {
|
|
463
|
+
p.remove();
|
|
464
|
+
}
|
|
465
|
+
if (action[0] === 'replace') {
|
|
466
|
+
p.replaceWith(action[2]);
|
|
467
|
+
}
|
|
468
|
+
staticEvaluate(parent);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
exports.applyAction = applyAction;
|
|
472
|
+
function removeWithRelated(paths) {
|
|
473
|
+
if (paths.length === 0)
|
|
474
|
+
return;
|
|
475
|
+
const rootPath = (0, getScope_1.getScope)(paths[0]).getProgramParent()
|
|
476
|
+
.path;
|
|
477
|
+
if (!fixed.has(rootPath)) {
|
|
478
|
+
// Some libraries don't care about bindings, references, and other staff
|
|
479
|
+
// So we have to fix the scope before we can detect unused code
|
|
480
|
+
referenceEnums(rootPath);
|
|
481
|
+
fixed.add(rootPath);
|
|
482
|
+
}
|
|
483
|
+
const actions = paths
|
|
484
|
+
.map(findActionForNode)
|
|
485
|
+
.filter(isNotNull_1.isNotNull);
|
|
486
|
+
const affectedPaths = actions.map(getPathFromAction);
|
|
487
|
+
let referencedIdentifiers = (0, findIdentifiers_1.findIdentifiers)(affectedPaths, 'reference');
|
|
488
|
+
referencedIdentifiers.sort((a, b) => a.node?.name.localeCompare(b.node?.name));
|
|
489
|
+
const referencesOfBinding = (0, findIdentifiers_1.findIdentifiers)(affectedPaths, 'declaration')
|
|
490
|
+
.map((i) => (i.node && (0, getScope_1.getScope)(i).getBinding(i.node.name)) ?? null)
|
|
491
|
+
.filter(isNotNull_1.isNotNull)
|
|
492
|
+
.reduce((acc, i) => [...acc, ...i.referencePaths.filter(findIdentifiers_1.nonType)], [])
|
|
493
|
+
.filter((ref) =>
|
|
494
|
+
// Do not remove `export default function`
|
|
495
|
+
!ref.isExportDefaultDeclaration() ||
|
|
496
|
+
!ref.get('declaration').isFunctionDeclaration());
|
|
497
|
+
actions.forEach(applyAction);
|
|
498
|
+
removeWithRelated(referencesOfBinding);
|
|
499
|
+
let clean = false;
|
|
500
|
+
while (!clean && referencedIdentifiers.length > 0) {
|
|
501
|
+
const referenced = removeUnreferenced(referencedIdentifiers);
|
|
502
|
+
clean =
|
|
503
|
+
referenced.map((i) => i.node?.name).join('|') ===
|
|
504
|
+
referencedIdentifiers.map((i) => i.node?.name).join('|');
|
|
505
|
+
referencedIdentifiers = referenced;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
exports.removeWithRelated = removeWithRelated;
|
|
509
|
+
function mutate(path, fn) {
|
|
510
|
+
const dereferenced = dereferenceAll(path);
|
|
511
|
+
const mutated = fn(path);
|
|
512
|
+
referenceAll(path);
|
|
513
|
+
mutated?.forEach((p) => referenceAll(p));
|
|
514
|
+
const dead = dereferenced.filter((p) => !isReferenced(p));
|
|
515
|
+
const forDeleting = [];
|
|
516
|
+
dead.forEach((binding) => {
|
|
517
|
+
const assignments = [binding.path, ...binding.constantViolations];
|
|
518
|
+
assignments.forEach((assignment) => {
|
|
519
|
+
const { scope } = assignment;
|
|
520
|
+
const declared = Object.values(assignment.getOuterBindingIdentifiers(false));
|
|
521
|
+
if (declared.length === 1 &&
|
|
522
|
+
'name' in declared[0] &&
|
|
523
|
+
declared[0].name === binding.identifier.name) {
|
|
524
|
+
const init = assignment.get('init');
|
|
525
|
+
if (!Array.isArray(init) && init?.isAssignmentExpression()) {
|
|
526
|
+
// `const a = b = 1` → `b = 1`
|
|
527
|
+
assignment.parentPath?.replaceWith({
|
|
528
|
+
type: 'ExpressionStatement',
|
|
529
|
+
expression: init.node,
|
|
530
|
+
});
|
|
531
|
+
const left = init.get('left');
|
|
532
|
+
if (left.isIdentifier()) {
|
|
533
|
+
// If it was forcefully referenced in the shaker
|
|
534
|
+
dereference(left);
|
|
535
|
+
}
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
// Only one identifier is declared, so we can remove the whole declaration
|
|
539
|
+
forDeleting.push(assignment);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (declared.every((identifier) => identifier.type === 'Identifier' &&
|
|
543
|
+
!scope.getBinding(identifier.name)?.referenced)) {
|
|
544
|
+
// No other identifier is referenced, so we can remove the whole declaration
|
|
545
|
+
forDeleting.push(assignment);
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
// We can't remove the binding, but we can remove the part of it
|
|
549
|
+
assignment.traverse({
|
|
550
|
+
Identifier(identifier) {
|
|
551
|
+
if (identifier.node.name === binding.identifier.name) {
|
|
552
|
+
const parent = identifier.parentPath;
|
|
553
|
+
if (parent.isArrayPattern() &&
|
|
554
|
+
identifier.listKey === 'elements' &&
|
|
555
|
+
typeof identifier.key === 'number') {
|
|
556
|
+
parent.node.elements[identifier.key] = null;
|
|
557
|
+
}
|
|
558
|
+
else if (parent.isObjectProperty()) {
|
|
559
|
+
forDeleting.push(parent);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
},
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
});
|
|
566
|
+
removeWithRelated(forDeleting);
|
|
567
|
+
}
|
|
568
|
+
exports.mutate = mutate;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { NodePath } from '@babel/traverse';
|
|
2
|
+
import type { Node } from '@babel/types';
|
|
3
|
+
export declare const getTraversalCache: <TValue, TKey extends Node | NodePath<Node> = NodePath<Node>>(path: NodePath, name: string) => WeakMap<TKey, TValue>;
|
|
4
|
+
export declare const clearBabelTraversalCache: () => void;
|
|
5
|
+
export declare const invalidateTraversalCache: (path: NodePath) => void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.invalidateTraversalCache = exports.clearBabelTraversalCache = exports.getTraversalCache = void 0;
|
|
7
|
+
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
8
|
+
const caches = new WeakMap();
|
|
9
|
+
const getTraversalCache = (path, name) => {
|
|
10
|
+
const programPath = path.find((p) => p.isProgram());
|
|
11
|
+
if (!programPath) {
|
|
12
|
+
throw new Error(`Could not find program for ${path.node.type}`);
|
|
13
|
+
}
|
|
14
|
+
if (!caches.has(programPath)) {
|
|
15
|
+
caches.set(programPath, new Map());
|
|
16
|
+
}
|
|
17
|
+
const cache = caches.get(programPath);
|
|
18
|
+
if (!cache.has(name)) {
|
|
19
|
+
cache.set(name, new WeakMap());
|
|
20
|
+
}
|
|
21
|
+
return cache.get(name);
|
|
22
|
+
};
|
|
23
|
+
exports.getTraversalCache = getTraversalCache;
|
|
24
|
+
const traverseCache = traverse_1.default.cache;
|
|
25
|
+
const clearBabelTraversalCache = () => {
|
|
26
|
+
traverseCache.clear();
|
|
27
|
+
};
|
|
28
|
+
exports.clearBabelTraversalCache = clearBabelTraversalCache;
|
|
29
|
+
const invalidateTraversalCache = (path) => {
|
|
30
|
+
const programPath = path.find((p) => p.isProgram());
|
|
31
|
+
if (!programPath) {
|
|
32
|
+
throw new Error(`Could not find program for ${path.node.type}`);
|
|
33
|
+
}
|
|
34
|
+
caches.delete(programPath);
|
|
35
|
+
};
|
|
36
|
+
exports.invalidateTraversalCache = invalidateTraversalCache;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NodePath } from '@babel/traverse';
|
|
2
|
+
import type { Expression, V8IntrinsicIdentifier } from '@babel/types';
|
|
3
|
+
/**
|
|
4
|
+
* If expression is a sequence like `(a, b, c)`, returns `c`
|
|
5
|
+
* otherwise returns an original expression
|
|
6
|
+
* @param path
|
|
7
|
+
*/
|
|
8
|
+
export declare function unwrapSequence(path: NodePath<Expression | V8IntrinsicIdentifier>): NodePath<Expression | V8IntrinsicIdentifier> | undefined;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unwrapSequence = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* If expression is a sequence like `(a, b, c)`, returns `c`
|
|
6
|
+
* otherwise returns an original expression
|
|
7
|
+
* @param path
|
|
8
|
+
*/
|
|
9
|
+
function unwrapSequence(path) {
|
|
10
|
+
if (path.isSequenceExpression()) {
|
|
11
|
+
const [...expressions] = path.get('expressions');
|
|
12
|
+
const lastExpression = expressions.pop();
|
|
13
|
+
return lastExpression ? unwrapSequence(lastExpression) : undefined;
|
|
14
|
+
}
|
|
15
|
+
return path;
|
|
16
|
+
}
|
|
17
|
+
exports.unwrapSequence = unwrapSequence;
|