From 21e8e59f7d807ce4683e3c31bbf4935bc9d8c9d2 Mon Sep 17 00:00:00 2001 From: Bogdan Savluk Date: Thu, 18 Mar 2021 16:05:44 +0100 Subject: [PATCH] convert `@babel/helper-module-imports` to typescript (#12924) --- .../babel-helper-module-imports/package.json | 3 +- .../{import-builder.js => import-builder.ts} | 7 ++-- ...{import-injector.js => import-injector.ts} | 37 +++++++++---------- .../src/{index.js => index.ts} | 8 ++-- .../src/{is-module.js => is-module.ts} | 5 ++- yarn.lock | 1 + 6 files changed, 33 insertions(+), 28 deletions(-) rename packages/babel-helper-module-imports/src/{import-builder.js => import-builder.ts} (95%) rename packages/babel-helper-module-imports/src/{import-injector.js => import-injector.ts} (95%) rename packages/babel-helper-module-imports/src/{index.js => index.ts} (63%) rename packages/babel-helper-module-imports/src/{is-module.js => is-module.ts} (67%) diff --git a/packages/babel-helper-module-imports/package.json b/packages/babel-helper-module-imports/package.json index d43f7d2a53c7..3ad403f00577 100644 --- a/packages/babel-helper-module-imports/package.json +++ b/packages/babel-helper-module-imports/package.json @@ -18,6 +18,7 @@ "@babel/types": "workspace:^7.12.13" }, "devDependencies": { - "@babel/core": "workspace:*" + "@babel/core": "workspace:*", + "@babel/traverse": "workspace:*" } } diff --git a/packages/babel-helper-module-imports/src/import-builder.js b/packages/babel-helper-module-imports/src/import-builder.ts similarity index 95% rename from packages/babel-helper-module-imports/src/import-builder.js rename to packages/babel-helper-module-imports/src/import-builder.ts index 7513c8549324..7591be459760 100644 --- a/packages/babel-helper-module-imports/src/import-builder.js +++ b/packages/babel-helper-module-imports/src/import-builder.ts @@ -11,6 +11,7 @@ export default class ImportBuilder { _scope = null; _hub = null; + private _importedSource: any; constructor(importedSource, scope, hub) { this._scope = scope; @@ -44,13 +45,13 @@ export default class ImportBuilder { } namespace(name = "namespace") { - name = this._scope.generateUidIdentifier(name); + const local = this._scope.generateUidIdentifier(name); const statement = this._statements[this._statements.length - 1]; assert(statement.type === "ImportDeclaration"); assert(statement.specifiers.length === 0); - statement.specifiers = [t.importNamespaceSpecifier(name)]; - this._resultName = t.cloneNode(name); + statement.specifiers = [t.importNamespaceSpecifier(local)]; + this._resultName = t.cloneNode(local); return this; } default(name) { diff --git a/packages/babel-helper-module-imports/src/import-injector.js b/packages/babel-helper-module-imports/src/import-injector.ts similarity index 95% rename from packages/babel-helper-module-imports/src/import-injector.js rename to packages/babel-helper-module-imports/src/import-injector.ts index 1da9e1f821c1..c7e6f2d540ef 100644 --- a/packages/babel-helper-module-imports/src/import-injector.js +++ b/packages/babel-helper-module-imports/src/import-injector.ts @@ -1,5 +1,6 @@ import assert from "assert"; import * as t from "@babel/types"; +import type { NodePath, Scope, HubInterface } from "@babel/traverse"; import ImportBuilder from "./import-builder"; import isModule from "./is-module"; @@ -8,16 +9,14 @@ export type ImportOptions = { /** * The module being referenced. */ - importedSource: string | null, - + importedSource: string | null; /** * The type of module being imported: * * * 'es6' - An ES6 module. * * 'commonjs' - A CommonJS module. (Default) */ - importedType: "es6" | "commonjs", - + importedType: "es6" | "commonjs"; /** * The type of interop behavior for namespace/default/named when loading * CommonJS modules. @@ -59,8 +58,7 @@ export type ImportOptions = { * * Default: The module.exports object. * * Named: The .named property of module.exports. */ - importedInterop: "babel" | "node" | "compiled" | "uncompiled", - + importedInterop: "babel" | "node" | "compiled" | "uncompiled"; /** * The type of CommonJS interop included in the environment that will be * loading the output code. @@ -70,8 +68,7 @@ export type ImportOptions = { * * See descriptions in 'importedInterop' for more details. */ - importingInterop: "babel" | "node", - + importingInterop: "babel" | "node"; /** * Define whether we explicitly care that the import be a live reference. * Only applies when importing default and named imports, not the namespace. @@ -79,8 +76,7 @@ export type ImportOptions = { * * true - Force imported values to be live references. * * false - No particular requirements. Keeps the code simplest. (Default) */ - ensureLiveReference: boolean, - + ensureLiveReference: boolean; /** * Define if we explicitly care that the result not be a property reference. * @@ -88,14 +84,16 @@ export type ImportOptions = { * be used as function callee. * * false - No particular requirements for context of the access. (Default) */ - ensureNoContext: boolean, - + ensureNoContext: boolean; /** * Define whether the import should be loaded before or after the existing imports. * "after" is only allowed inside ECMAScript modules, since it's not possible to * reliably pick the location _after_ require() calls but _before_ other code in CJS. */ - importPosition: "before" | "after", + importPosition: "before" | "after"; + + nameHint?; + blockHoist?; }; /** @@ -105,17 +103,17 @@ export default class ImportInjector { /** * The path used for manipulation. */ - declare _programPath: NodePath; + declare _programPath: NodePath; /** * The scope used to generate unique variable names. */ - declare _programScope; + declare _programScope: Scope; /** * The file used to inject helpers and resolve paths. */ - declare _hub; + declare _hub: HubInterface; /** * The default options to use with this instance when imports are added. @@ -130,8 +128,8 @@ export default class ImportInjector { importPosition: "before", }; - constructor(path, importedSource, opts) { - const programPath = path.find(p => p.isProgram()); + constructor(path: NodePath, importedSource?, opts?) { + const programPath = path.find(p => p.isProgram()) as NodePath; this._programPath = programPath; this._programScope = programPath.scope; @@ -178,7 +176,7 @@ export default class ImportInjector { optsList.push(importedSource); } - const newOpts = { + const newOpts: ImportOptions = { ...this._defaultOpts, }; for (const opts of optsList) { @@ -439,6 +437,7 @@ export default class ImportInjector { }); const targetPath = body.find(p => { + // @ts-expect-error todo(flow->ts): avoid mutations const val = p.node._blockHoist; return Number.isFinite(val) && val < 4; }); diff --git a/packages/babel-helper-module-imports/src/index.js b/packages/babel-helper-module-imports/src/index.ts similarity index 63% rename from packages/babel-helper-module-imports/src/index.js rename to packages/babel-helper-module-imports/src/index.ts index 722631adce14..44b98d5c2054 100644 --- a/packages/babel-helper-module-imports/src/index.js +++ b/packages/babel-helper-module-imports/src/index.ts @@ -4,18 +4,18 @@ export { ImportInjector }; export { default as isModule } from "./is-module"; -export function addDefault(path, importedSource, opts) { +export function addDefault(path, importedSource, opts?) { return new ImportInjector(path).addDefault(importedSource, opts); } -export function addNamed(path, name, importedSource, opts) { +export function addNamed(path, name, importedSource, opts?) { return new ImportInjector(path).addNamed(name, importedSource, opts); } -export function addNamespace(path, importedSource, opts) { +export function addNamespace(path, importedSource, opts?) { return new ImportInjector(path).addNamespace(importedSource, opts); } -export function addSideEffect(path, importedSource, opts) { +export function addSideEffect(path, importedSource, opts?) { return new ImportInjector(path).addSideEffect(importedSource, opts); } diff --git a/packages/babel-helper-module-imports/src/is-module.js b/packages/babel-helper-module-imports/src/is-module.ts similarity index 67% rename from packages/babel-helper-module-imports/src/is-module.js rename to packages/babel-helper-module-imports/src/is-module.ts index 39cff11148d8..bfcd74802cb4 100644 --- a/packages/babel-helper-module-imports/src/is-module.js +++ b/packages/babel-helper-module-imports/src/is-module.ts @@ -1,7 +1,10 @@ +import type { NodePath } from "@babel/traverse"; +import type * as t from "@babel/types"; + /** * A small utility to check if a file qualifies as a module. */ -export default function isModule(path: NodePath) { +export default function isModule(path: NodePath) { const { sourceType } = path.node; if (sourceType !== "module" && sourceType !== "script") { throw path.buildCodeFrameError( diff --git a/yarn.lock b/yarn.lock index 0c7429fa2533..3760fee11bb2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -639,6 +639,7 @@ __metadata: resolution: "@babel/helper-module-imports@workspace:packages/babel-helper-module-imports" dependencies: "@babel/core": "workspace:*" + "@babel/traverse": "workspace:*" "@babel/types": "workspace:^7.12.13" languageName: unknown linkType: soft