diff --git a/package.json b/package.json index 15c696e0..6d3cb3d1 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "test:bun": "bun --bun test test/bun" }, "devDependencies": { + "@babel/helper-module-imports": "^7.24.7", "@babel/core": "^7.24.9", "@babel/helper-module-transforms": "^7.24.9", "@babel/helper-plugin-utils": "^7.24.8", @@ -59,9 +60,13 @@ "@babel/plugin-transform-typescript": "^7.24.8", "@babel/preset-typescript": "^7.24.7", "@babel/template": "^7.24.7", + "@babel/traverse": "^7.24.7", "@babel/types": "^7.24.9", "@types/babel__core": "^7.20.5", + "@types/babel__helper-module-imports": "^7.18.3", + "@types/babel__helper-plugin-utils": "^7.10.3", "@types/babel__template": "^7.4.4", + "@types/babel__traverse": "^7.20.6", "@types/node": "^20.14.12", "@vitest/coverage-v8": "^2.0.4", "acorn": "^8.12.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 13aca275..6819b8ea 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@babel/core': specifier: ^7.24.9 version: 7.24.9 + '@babel/helper-module-imports': + specifier: ^7.24.7 + version: 7.24.7 '@babel/helper-module-transforms': specifier: ^7.24.9 version: 7.24.9(@babel/core@7.24.9) @@ -41,15 +44,27 @@ importers: '@babel/template': specifier: ^7.24.7 version: 7.24.7 + '@babel/traverse': + specifier: ^7.24.7 + version: 7.24.8 '@babel/types': specifier: ^7.24.9 version: 7.24.9 '@types/babel__core': specifier: ^7.20.5 version: 7.20.5 + '@types/babel__helper-module-imports': + specifier: ^7.18.3 + version: 7.18.3 + '@types/babel__helper-plugin-utils': + specifier: ^7.10.3 + version: 7.10.3 '@types/babel__template': specifier: ^7.4.4 version: 7.4.4 + '@types/babel__traverse': + specifier: ^7.20.6 + version: 7.20.6 '@types/node': specifier: ^20.14.12 version: 20.14.12 @@ -647,6 +662,12 @@ packages: '@types/babel__generator@7.6.8': resolution: {integrity: sha512-ASsj+tpEDsEiFr1arWrlN6V3mdfjRMZt6LtK/Vp/kreFLnr5QH5+DhvD5nINYZXzwJvXeGq+05iUXcAzVrqWtw==} + '@types/babel__helper-module-imports@7.18.3': + resolution: {integrity: sha512-2pyr9Vlriessj2KI85SEF7qma8vA3vzquQMw3wn6kL5lsfjH/YxJ1Noytk4/FJElpYybUbyaC37CVfEgfyme9A==} + + '@types/babel__helper-plugin-utils@7.10.3': + resolution: {integrity: sha512-FcLBBPXInqKfULB2nvOBskQPcnSMZ0s1Y2q76u9H1NPPWaLcTeq38xBeKfF/RBUECK333qeaqRdYoPSwW7rTNQ==} + '@types/babel__template@7.4.4': resolution: {integrity: sha512-h/NUaSyG5EyxBIp8YRxo4RMe2/qQgvyowRwVMzhYhBCONbW8PUsg4lkFMrhgZhUe5z3L3MiLDuvyJ/CaPa2A8A==} @@ -2932,6 +2953,15 @@ snapshots: dependencies: '@babel/types': 7.24.9 + '@types/babel__helper-module-imports@7.18.3': + dependencies: + '@types/babel__core': 7.20.5 + '@types/babel__traverse': 7.20.6 + + '@types/babel__helper-plugin-utils@7.10.3': + dependencies: + '@types/babel__core': 7.20.5 + '@types/babel__template@7.4.4': dependencies: '@babel/parser': 7.24.8 diff --git a/src/_types/babel-core.d.ts b/src/_types/babel-core.d.ts new file mode 100644 index 00000000..d265eeda --- /dev/null +++ b/src/_types/babel-core.d.ts @@ -0,0 +1,9 @@ +import type { HubInterface } from "@babel/traverse"; + +declare module "@babel/core" { + export interface BabelFile + extends HubInterface, + Pick {} +} + +export {}; diff --git a/src/_types/babel-helper-module-transforms.d.ts b/src/_types/babel-helper-module-transforms.d.ts new file mode 100644 index 00000000..9b568efc --- /dev/null +++ b/src/_types/babel-helper-module-transforms.d.ts @@ -0,0 +1,203 @@ +declare module "@babel/helper-module-transforms" { + import type { NodePath } from "@babel/core"; + import { types as t } from "@babel/core"; + + interface LocalExportMetadata { + /** + * names of exports + */ + names: string[]; + kind: "import" | "hoisted" | "block" | "var"; + } + + type InteropType = + /** + * Babel interop for default-only imports + */ + | "default" + /** + * Babel interop for namespace or default+named imports + */ + | "namespace" + /** + * Node.js interop for default-only imports + */ + | "node-default" + /** + * Node.js interop for namespace or default+named imports + */ + | "node-namespace" + /** + * No interop, or named-only imports + */ + | "none"; + + interface SourceModuleMetadata { + /** + * A unique variable name to use for this namespace object. + * Centralized for simplicity. + */ + name: string; + loc: t.SourceLocation | undefined | null; + interop: InteropType; + /** + * Local binding to reference from this source namespace. + * Key: Local name, value: Import name + */ + imports: Map; + /** + * Local names that reference namespace object. + */ + importsNamespace: Set; + /** + * Reexports to create for namespace. Key: Export name, value: Import name + */ + reexports: Map; + /** + * List of names to re-export namespace as. + */ + reexportNamespace: Set; + /** + * Tracks if the source should be re-exported. + */ + reexportAll: null | { + loc: t.SourceLocation | undefined | null; + }; + wrap?: unknown; + referenced: boolean; + } + + interface ModuleMetadata { + exportName: string; + /** + * The name of the variable that will reference an object + * containing export names. + */ + exportNameListName: null | string; + hasExports: boolean; + /** + * Lookup from local binding to export information. + */ + local: Map; + /** + * Lookup of source file to source file metadata. + */ + source: Map; + /** + * List of names that should only be printed as string literals. + * i.e. `import { "any unicode" as foo } from "some-module"` + * `stringSpecifiers` is `Set(1) ["any unicode"]` + * In most cases `stringSpecifiers` is an empty Set + */ + stringSpecifiers: Set; + } + + type ImportInterop = + | "none" + | "babel" + | "node" + | ((source: string, filename?: string) => "none" | "babel" | "node"); + + type Lazy = boolean | string[] | ((source: string) => boolean); + + type RootOptions = { + filename?: string | null; + filenameRelative?: string | null; + sourceRoot?: string | null; + }; + + export type PluginOptions = { + moduleId?: string; + moduleIds?: boolean; + getModuleId?: (moduleName: string) => string | null | undefined; + moduleRoot?: string; + }; + + export function getModuleName( + rootOpts: RootOptions, + pluginOpts: PluginOptions, + ): string | null; + + export interface RewriteModuleStatementsAndPrepareHeaderOptions { + exportName?: string; + strict?: boolean; + allowTopLevelThis?: boolean; + strictMode?: boolean; + loose?: boolean; + importInterop?: ImportInterop; + noInterop?: boolean; + lazy?: Lazy; + getWrapperPayload?: ( + source: string, + metadata: SourceModuleMetadata, + importNodes: t.Node[], + ) => unknown; + wrapReference?: ( + ref: t.Expression, + payload: unknown, + ) => t.Expression | null | undefined; + esNamespaceOnly?: boolean; + filename: string | undefined | null; + constantReexports?: boolean | void; + enumerableModuleMeta?: boolean | void; + noIncompleteNsImportDetection?: boolean | void; + } + + /** + * Perform all of the generic ES6 module rewriting needed to handle initial + * module processing. This function will rewrite the majority of the given + * program to reference the modules described by the returned metadata, + * and returns a list of statements for use when initializing the module. + */ + export function rewriteModuleStatementsAndPrepareHeader( + path: NodePath, + options: RewriteModuleStatementsAndPrepareHeaderOptions, + ): { + meta: ModuleMetadata; + headers: t.Statement[]; + }; + + /** + * Check if a given source is an anonymous import, e.g. `import 'foo';` + */ + export function isSideEffectImport(source: SourceModuleMetadata): boolean; + + /** + * Create the runtime initialization statements for a given requested source. + * These will initialize all of the runtime import/export logic that + * can't be handled statically by the statements created by + * `buildExportInitializationStatements()`. + */ + export function buildNamespaceInitStatements( + metadata: ModuleMetadata, + sourceMetadata: SourceModuleMetadata, + constantReexports?: boolean | void, + wrapReference?: ( + ref: t.Identifier, + payload: unknown, + ) => t.Expression | null | undefined, + ): t.Statement[]; + + /** + * Flag a set of statements as hoisted above all else so that module init + * statements all run before user code. + */ + export function ensureStatementsHoisted(statements: t.Statement[]): void; + + /** + * Given an expression for a standard import object, like `require('foo')`, + * wrap it in a call to the interop helpers based on the type. + */ + export function wrapInterop( + programPath: NodePath, + expr: t.Expression, + type: InteropType, + ): t.CallExpression; + + export function buildDynamicImport( + node: t.CallExpression | t.ImportExpression, + deferToThen: boolean, + wrapWithPromise: boolean, + builder: (specifier: t.Expression) => t.Expression, + ): t.Expression; +} diff --git a/src/_types/babel-helper-plugin-utils.d.ts b/src/_types/babel-helper-plugin-utils.d.ts new file mode 100644 index 00000000..85881303 --- /dev/null +++ b/src/_types/babel-helper-plugin-utils.d.ts @@ -0,0 +1,64 @@ +import type { ConfigAPI } from "@babel/core"; + +declare module "@babel/helper-plugin-utils" { + const knownAssumptions: readonly [ + "arrayLikeIsIterable", + "constantReexports", + "constantSuper", + "enumerableModuleMeta", + "ignoreFunctionLength", + "ignoreToPrimitiveHint", + "iterableIsArray", + "mutableTemplateObject", + "noClassCalls", + "noDocumentAll", + "noIncompleteNsImportDetection", + "noNewArrows", + "noUninitializedPrivateFieldAccess", + "objectRestNoSymbols", + "privateFieldsAsSymbols", + "privateFieldsAsProperties", + "pureGetters", + "setClassMethods", + "setComputedProperties", + "setPublicClassFields", + "setSpreadProperties", + "skipForOfIteratorClosing", + "superIsCallableConstructor", + ]; + + export type AssumptionName = (typeof knownAssumptions)[number]; + + type AssumptionFunction = (name: AssumptionName) => boolean | undefined; + + export type Target = + | "node" + | "deno" + | "chrome" + | "opera" + | "edge" + | "firefox" + | "safari" + | "ie" + | "ios" + | "android" + | "electron" + | "samsung" + | "opera_mobile"; + + export type Targets = { + [target in Target]?: string; + }; + + type TargetsFunction = () => Targets; + + export type PresetAPI = { + targets: TargetsFunction; + addExternalDependency: (ref: string) => void; + } & ConfigAPI; + + export type PluginAPI = { + assumption: AssumptionFunction; + } & PresetAPI & + BabelAPI; +} diff --git a/src/_types/babel-helper-simple-access.d.ts b/src/_types/babel-helper-simple-access.d.ts new file mode 100644 index 00000000..3486e243 --- /dev/null +++ b/src/_types/babel-helper-simple-access.d.ts @@ -0,0 +1,9 @@ +declare module "@babel/helper-simple-access" { + import type { NodePath } from "@babel/traverse"; + + export default function simplifyAccess( + path: NodePath, + bindingNames: Set, + includeUpdateExpression?: boolean, + ): void; +} diff --git a/src/_types/babel-plugin-parameter-decorator.d.ts b/src/_types/babel-plugin-parameter-decorator.d.ts new file mode 100644 index 00000000..2f27a45d --- /dev/null +++ b/src/_types/babel-plugin-parameter-decorator.d.ts @@ -0,0 +1,5 @@ +declare module "babel-plugin-parameter-decorator" { + import type { declare } from "@babel/helper-plugin-utils"; + const parameterDecoratorPlugin: ReturnType; + export default parameterDecoratorPlugin; +} diff --git a/src/_types/babel-plugin-proposal-decorators.d.ts b/src/_types/babel-plugin-proposal-decorators.d.ts new file mode 100644 index 00000000..d2ccbc46 --- /dev/null +++ b/src/_types/babel-plugin-proposal-decorators.d.ts @@ -0,0 +1,14 @@ +declare module "@babel/plugin-proposal-decorators" { + import type { declare } from "@babel/helper-plugin-utils"; + import type { Options as SyntaxOptions } from "@babel/plugin-syntax-decorators"; + + export interface Options extends SyntaxOptions { + /** + * @deprecated use `constantSuper` assumption instead. Only supported in 2021-12 version. + */ + loose?: boolean; + } + + const proposalDecoratorsPlugin: ReturnType>; + export default proposalDecoratorsPlugin; +} diff --git a/src/_types/babel-plugin-syntax-class-properties.d.ts b/src/_types/babel-plugin-syntax-class-properties.d.ts new file mode 100644 index 00000000..58326c71 --- /dev/null +++ b/src/_types/babel-plugin-syntax-class-properties.d.ts @@ -0,0 +1,5 @@ +declare module "@babel/plugin-syntax-class-properties" { + import type { declare } from "@babel/helper-plugin-utils"; + const syntaxClassPropertiesPlugin: ReturnType; + export default syntaxClassPropertiesPlugin; +} diff --git a/src/_types/babel-plugin-syntax-decorators.d.ts b/src/_types/babel-plugin-syntax-decorators.d.ts new file mode 100644 index 00000000..e643d99e --- /dev/null +++ b/src/_types/babel-plugin-syntax-decorators.d.ts @@ -0,0 +1,18 @@ +declare module "@babel/plugin-syntax-decorators" { + export interface Options { + legacy?: boolean; + version?: + | "legacy" + | "2018-09" + | "2021-12" + | "2022-03" + | "2023-01" + | "2023-05" + | "2023-11"; + decoratorsBeforeExport?: boolean; + } + + import type { declare } from "@babel/helper-plugin-utils"; + const syntaxDecoratorsPlugin: ReturnType>; + export default syntaxDecoratorsPlugin; +} diff --git a/src/_types/babel-plugin-syntax-import-assertions.d.ts b/src/_types/babel-plugin-syntax-import-assertions.d.ts new file mode 100644 index 00000000..810f700f --- /dev/null +++ b/src/_types/babel-plugin-syntax-import-assertions.d.ts @@ -0,0 +1,5 @@ +declare module "@babel/plugin-syntax-import-assertions" { + import type { declare } from "@babel/helper-plugin-utils"; + const syntaxImportAssertionsPlugin: ReturnType; + export default syntaxImportAssertionsPlugin; +} diff --git a/src/_types/babel-plugin-syntax-typescript.d.ts b/src/_types/babel-plugin-syntax-typescript.d.ts new file mode 100644 index 00000000..d92a4edd --- /dev/null +++ b/src/_types/babel-plugin-syntax-typescript.d.ts @@ -0,0 +1,11 @@ +declare module "@babel/plugin-syntax-typescript" { + export interface Options { + disallowAmbiguousJSXLike?: boolean; + dts?: boolean; + isTSX?: boolean; + } + + import type { declare } from "@babel/helper-plugin-utils"; + const syntaxTypeScriptPlugin: ReturnType>; + export default syntaxTypeScriptPlugin; +} diff --git a/src/_types/babel-plugin-transform-export-namespace-from.d.ts b/src/_types/babel-plugin-transform-export-namespace-from.d.ts new file mode 100644 index 00000000..5eb5d888 --- /dev/null +++ b/src/_types/babel-plugin-transform-export-namespace-from.d.ts @@ -0,0 +1,5 @@ +declare module "@babel/plugin-transform-export-namespace-from" { + import type { declare } from "@babel/helper-plugin-utils"; + const transformExportNamespaceFromPlugin: ReturnType; + export default transformExportNamespaceFromPlugin; +} diff --git a/src/_types/babel-plugin-transform-typescript-metadata.d.ts b/src/_types/babel-plugin-transform-typescript-metadata.d.ts new file mode 100644 index 00000000..9160f3f7 --- /dev/null +++ b/src/_types/babel-plugin-transform-typescript-metadata.d.ts @@ -0,0 +1,5 @@ +declare module "babel-plugin-transform-typescript-metadata" { + import type { declare } from "@babel/helper-plugin-utils"; + const transformTypeScriptMetaPlugin: ReturnType; + export default transformTypeScriptMetaPlugin; +} diff --git a/src/_types/babel-plugin-transform-typescript.d.ts b/src/_types/babel-plugin-transform-typescript.d.ts new file mode 100644 index 00000000..105d82c2 --- /dev/null +++ b/src/_types/babel-plugin-transform-typescript.d.ts @@ -0,0 +1,19 @@ +declare module "@babel/plugin-transform-typescript" { + import type { declare } from "@babel/helper-plugin-utils"; + import type { Options as SyntaxOptions } from "@babel/plugin-syntax-typescript"; + + export interface Options extends SyntaxOptions { + /** @default true */ + allowNamespaces?: boolean; + /** @default "React.createElement" */ + jsxPragma?: string; + /** @default "React.Fragment" */ + jsxPragmaFrag?: string; + onlyRemoveTypeImports?: boolean; + optimizeConstEnums?: boolean; + allowDeclareFields?: boolean; + } + + const transformTypeScriptMetaPlugin: ReturnType>; + export default transformTypeScriptMetaPlugin; +} diff --git a/src/babel.ts b/src/babel.ts index d897bd22..c2ca8ffc 100644 --- a/src/babel.ts +++ b/src/babel.ts @@ -1,25 +1,19 @@ -import { transformSync } from "@babel/core"; import type { TransformOptions as BabelTransformOptions, PluginItem, } from "@babel/core"; -import type { TransformOptions, TransformResult } from "./types"; -import { TransformImportMetaPlugin } from "./plugins/babel-plugin-transform-import-meta"; -import { importMetaEnvPlugin } from "./plugins/import-meta-env"; -import transformModulesPlugin from "./plugins/transform-module"; -import tansformTypescriptMetaPlugin from "babel-plugin-transform-typescript-metadata"; -// @ts-ignore +import { transformSync } from "@babel/core"; +import proposalDecoratorsPlugin from "@babel/plugin-proposal-decorators"; import syntaxClassPropertiesPlugin from "@babel/plugin-syntax-class-properties"; -// @ts-ignore +import syntaxImportAssertionsPlugin from "@babel/plugin-syntax-import-assertions"; import transformExportNamespaceFromPlugin from "@babel/plugin-transform-export-namespace-from"; -// @ts-ignore -import tansformTypescriptPlugin from "@babel/plugin-transform-typescript"; -// @ts-ignore -import proposalDecoratorsPlugin from "@babel/plugin-proposal-decorators"; -// @ts-ignore +import transformTypeScriptPlugin from "@babel/plugin-transform-typescript"; import parameterDecoratorPlugin from "babel-plugin-parameter-decorator"; -// @ts-ignore -import syntaxImportAssertionsPlugin from "@babel/plugin-syntax-import-assertions"; +import transformTypeScriptMetaPlugin from "babel-plugin-transform-typescript-metadata"; +import { TransformImportMetaPlugin } from "./plugins/babel-plugin-transform-import-meta"; +import { importMetaEnvPlugin } from "./plugins/import-meta-env"; +import transformModulesPlugin from "./plugins/transform-module"; +import type { TransformOptions, TransformResult } from "./types"; export default function transform(opts: TransformOptions): TransformResult { const _opts: BabelTransformOptions & { plugins: PluginItem[] } = { @@ -49,12 +43,12 @@ export default function transform(opts: TransformOptions): TransformResult { if (opts.ts) { _opts.plugins.push([ - tansformTypescriptPlugin, + transformTypeScriptPlugin, { allowDeclareFields: true }, ]); // `unshift` because these plugin must come before `@babel/plugin-syntax-class-properties` _opts.plugins.unshift( - [tansformTypescriptMetaPlugin], + [transformTypeScriptMetaPlugin], [proposalDecoratorsPlugin, { legacy: true }], ); _opts.plugins.push(parameterDecoratorPlugin); diff --git a/src/plugins/transform-module/dynamic-import.ts b/src/plugins/transform-module/dynamic-import.ts index db025475..12db331f 100644 --- a/src/plugins/transform-module/dynamic-import.ts +++ b/src/plugins/transform-module/dynamic-import.ts @@ -1,19 +1,17 @@ -// Basd on babel-plugin-transform-modules-commonjs v7.24.7 +// Based on babel-plugin-transform-modules-commonjs v7.24.7 // MIT - Copyright (c) 2014-present Sebastian McKenzie and other contributors // https://github.com/babel/babel/tree/c7bb6e0f/packages/babel-plugin-transform-modules-commonjs/src // Heavily inspired by // https://github.com/airbnb/babel-plugin-dynamic-import-node/blob/master/src/utils.js -// @ts-expect-error -import type { File, NodePath } from "@babel/core"; +import type { BabelFile, NodePath } from "@babel/core"; import { types as t, template } from "@babel/core"; -// @ts-expect-error import { buildDynamicImport } from "@babel/helper-module-transforms"; const requireInterop = ( source: t.Expression, - file: File, + file: BabelFile, noInterop: boolean | undefined, ) => { const exp = template.expression.ast`jitiImport(${source})`; @@ -33,10 +31,9 @@ const requireInterop = ( export function transformDynamicImport( path: NodePath, noInterop: boolean | undefined, - file: File, + file: BabelFile, ) { path.replaceWith( - // @ts-expect-error buildDynamicImport(path.node, true, false, (specifier) => requireInterop(specifier, file, noInterop), ), diff --git a/src/plugins/transform-module/hooks.ts b/src/plugins/transform-module/hooks.ts index 3d957911..652685b1 100644 --- a/src/plugins/transform-module/hooks.ts +++ b/src/plugins/transform-module/hooks.ts @@ -1,10 +1,8 @@ -// Basd on babel-plugin-transform-modules-commonjs v7.24.7 +// Based on babel-plugin-transform-modules-commonjs v7.24.7 // MIT - Copyright (c) 2014-present Sebastian McKenzie and other contributors // https://github.com/babel/babel/tree/c7bb6e0f/packages/babel-plugin-transform-modules-commonjs/src -// @ts-expect-error -import type { types as t, File } from "@babel/core"; -// @ts-expect-error +import type { BabelFile, types as t } from "@babel/core"; import type { isSideEffectImport } from "@babel/helper-module-transforms"; const commonJSHooksKey = @@ -37,7 +35,7 @@ export interface CommonJSHook { ): string | null | undefined; } -export function defineCommonJSHook(file: File, hook: CommonJSHook) { +export function defineCommonJSHook(file: BabelFile, hook: CommonJSHook) { let hooks = file.get(commonJSHooksKey); if (!hooks) file.set(commonJSHooksKey, (hooks = [])); hooks.push(hook); @@ -56,7 +54,7 @@ function findMap( } export function makeInvokers( - file: File, + file: BabelFile, ): Pick< CommonJSHook, "wrapReference" | "getWrapperPayload" | "buildRequireWrapper" diff --git a/src/plugins/transform-module/index.ts b/src/plugins/transform-module/index.ts index d4bfa6f9..b9fdbf97 100644 --- a/src/plugins/transform-module/index.ts +++ b/src/plugins/transform-module/index.ts @@ -1,30 +1,29 @@ -// Basd on babel-plugin-transform-modules-commonjs v7.24.7 +// Based on babel-plugin-transform-modules-commonjs v7.24.7 // MIT - Copyright (c) 2014-present Sebastian McKenzie and other contributors // https://github.com/babel/babel/tree/c7bb6e0f/packages/babel-plugin-transform-modules-commonjs/src -// @ts-expect-error -import { declare } from "@babel/helper-plugin-utils"; +import type { NodePath, PluginPass, Visitor } from "@babel/core"; +import { types as t, template } from "@babel/core"; +import { isModule } from "@babel/helper-module-imports"; +import type { + PluginOptions, + RewriteModuleStatementsAndPrepareHeaderOptions, +} from "@babel/helper-module-transforms"; import { - isModule, - rewriteModuleStatementsAndPrepareHeader, - type RewriteModuleStatementsAndPrepareHeaderOptions, - isSideEffectImport, buildNamespaceInitStatements, ensureStatementsHoisted, - wrapInterop, getModuleName, - // @ts-expect-error + isSideEffectImport, + rewriteModuleStatementsAndPrepareHeader, + wrapInterop, } from "@babel/helper-module-transforms"; -// @ts-expect-error +import type { PluginAPI } from "@babel/helper-plugin-utils"; +import { declare } from "@babel/helper-plugin-utils"; import simplifyAccess from "@babel/helper-simple-access"; -import { template, types as t } from "@babel/core"; -// @ts-expect-error -import type { PluginPass, Visitor, Scope, NodePath } from "@babel/core"; -// @ts-expect-error -import type { PluginOptions } from "@babel/helper-module-transforms"; +import type { Scope } from "@babel/traverse"; import { transformDynamicImport } from "./dynamic-import"; -import { lazyImportsHook } from "./lazy"; import { defineCommonJSHook, makeInvokers } from "./hooks"; +import { lazyImportsHook } from "./lazy"; export interface Options extends PluginOptions { allowCommonJSExports?: boolean; @@ -40,14 +39,15 @@ export interface Options extends PluginOptions { async: boolean; } -export default declare((api: any, options: Options) => { +// @ts-expect-error +export default declare((api: PluginAPI, options: Options) => { // api.assertVersion(REQUIRED_VERSION(7)); const { // 'true' for imports to strictly have .default, instead of having // destructuring-like behavior for their properties. This matches the behavior // of the initial Node.js (v12) behavior when importing a CommonJS without - // the __esMoule property. + // the __esModule property. // .strictNamespace is for non-mjs files, mjsStrictNamespace if for mjs files. strictNamespace = false, mjsStrictNamespace = strictNamespace, @@ -177,15 +177,14 @@ export default declare((api: any, options: Options) => { name: "transform-modules-commonjs", pre() { - // @ts-expect-error this.file.set("@babel/plugin-transform-modules-*", "commonjs"); - // @ts-expect-error if (lazy) defineCommonJSHook(this.file, lazyImportsHook(lazy)); }, visitor: { ["CallExpression" + + // @ts-expect-error (api.types.importExpression ? "|ImportExpression" : "")]( this: PluginPass, path: NodePath, @@ -201,7 +200,7 @@ export default declare((api: any, options: Options) => { }, Program: { - exit(path: NodePath, state: any) { + exit(path, state) { if (!isModule(path)) return; // Rename the bindings auto-injected into the scope so there is no @@ -219,7 +218,6 @@ export default declare((api: any, options: Options) => { if (process.env.BABEL_8_BREAKING) { simplifyAccess(path, new Set(["module", "exports"])); } else { - // @ts-ignore(Babel 7 vs Babel 8) The third param has been removed in Babel 8. simplifyAccess(path, new Set(["module", "exports"]), false); } path.traverse(moduleExportsVisitor, { @@ -227,11 +225,10 @@ export default declare((api: any, options: Options) => { }); } - // @ts-expect-error let moduleName = getModuleName(this.file.opts, options); + // @ts-expect-error if (moduleName) moduleName = t.stringLiteral(moduleName); - // @ts-expect-error const hooks = makeInvokers(this.file); const { meta, headers } = rewriteModuleStatementsAndPrepareHeader( @@ -253,7 +250,6 @@ export default declare((api: any, options: Options) => { ? mjsStrictNamespace : strictNamespace, noIncompleteNsImportDetection, - // @ts-expect-error filename: this.file.opts.filename, }, ); @@ -310,10 +306,9 @@ export default declare((api: any, options: Options) => { } ensureStatementsHoisted(headers); - // @ts-expect-error path.unshiftContainer("body", headers); // eslint-disable-next-line unicorn/no-array-for-each - (path.get("body") as any[]).forEach((path) => { + path.get("body").forEach((path) => { if (!headers.includes(path.node)) return; if (path.isVariableDeclaration()) { path.scope.registerDeclaration(path); diff --git a/src/plugins/transform-module/lazy.ts b/src/plugins/transform-module/lazy.ts index 6491e4fb..6a7e67c0 100644 --- a/src/plugins/transform-module/lazy.ts +++ b/src/plugins/transform-module/lazy.ts @@ -1,8 +1,7 @@ -// Basd on babel-plugin-transform-modules-commonjs v7.24.7 +// Based on babel-plugin-transform-modules-commonjs v7.24.7 // MIT - Copyright (c) 2014-present Sebastian McKenzie and other contributors // https://github.com/babel/babel/tree/c7bb6e0f/packages/babel-plugin-transform-modules-commonjs/src import { template, types as t } from "@babel/core"; -// @ts-ignore import { isSideEffectImport } from "@babel/helper-module-transforms"; import type { CommonJSHook } from "./hooks.ts"; diff --git a/test/fixtures.test.ts b/test/fixtures.test.ts index f8e1c8dd..f6f882bc 100644 --- a/test/fixtures.test.ts +++ b/test/fixtures.test.ts @@ -41,6 +41,8 @@ describe("fixtures", async () => { ) .replace("internal/errors:496", "events:276") .replace(" ^", " ^") + // eslint-disable-next-line no-control-regex + .replace(/\u001B\[[\d;]*m/gu, "") .trim() ); }