convert @babel/helper-module-imports to typescript (#12924)

This commit is contained in:
Bogdan Savluk 2021-03-18 16:05:44 +01:00 committed by GitHub
parent ff8e9f74d8
commit 21e8e59f7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 28 deletions

View File

@ -18,6 +18,7 @@
"@babel/types": "workspace:^7.12.13" "@babel/types": "workspace:^7.12.13"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "workspace:*" "@babel/core": "workspace:*",
"@babel/traverse": "workspace:*"
} }
} }

View File

@ -11,6 +11,7 @@ export default class ImportBuilder {
_scope = null; _scope = null;
_hub = null; _hub = null;
private _importedSource: any;
constructor(importedSource, scope, hub) { constructor(importedSource, scope, hub) {
this._scope = scope; this._scope = scope;
@ -44,13 +45,13 @@ export default class ImportBuilder {
} }
namespace(name = "namespace") { namespace(name = "namespace") {
name = this._scope.generateUidIdentifier(name); const local = this._scope.generateUidIdentifier(name);
const statement = this._statements[this._statements.length - 1]; const statement = this._statements[this._statements.length - 1];
assert(statement.type === "ImportDeclaration"); assert(statement.type === "ImportDeclaration");
assert(statement.specifiers.length === 0); assert(statement.specifiers.length === 0);
statement.specifiers = [t.importNamespaceSpecifier(name)]; statement.specifiers = [t.importNamespaceSpecifier(local)];
this._resultName = t.cloneNode(name); this._resultName = t.cloneNode(local);
return this; return this;
} }
default(name) { default(name) {

View File

@ -1,5 +1,6 @@
import assert from "assert"; import assert from "assert";
import * as t from "@babel/types"; import * as t from "@babel/types";
import type { NodePath, Scope, HubInterface } from "@babel/traverse";
import ImportBuilder from "./import-builder"; import ImportBuilder from "./import-builder";
import isModule from "./is-module"; import isModule from "./is-module";
@ -8,16 +9,14 @@ export type ImportOptions = {
/** /**
* The module being referenced. * The module being referenced.
*/ */
importedSource: string | null, importedSource: string | null;
/** /**
* The type of module being imported: * The type of module being imported:
* *
* * 'es6' - An ES6 module. * * 'es6' - An ES6 module.
* * 'commonjs' - A CommonJS module. (Default) * * 'commonjs' - A CommonJS module. (Default)
*/ */
importedType: "es6" | "commonjs", importedType: "es6" | "commonjs";
/** /**
* The type of interop behavior for namespace/default/named when loading * The type of interop behavior for namespace/default/named when loading
* CommonJS modules. * CommonJS modules.
@ -59,8 +58,7 @@ export type ImportOptions = {
* * Default: The module.exports object. * * Default: The module.exports object.
* * Named: The .named property of module.exports. * * 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 * The type of CommonJS interop included in the environment that will be
* loading the output code. * loading the output code.
@ -70,8 +68,7 @@ export type ImportOptions = {
* *
* See descriptions in 'importedInterop' for more details. * 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. * Define whether we explicitly care that the import be a live reference.
* Only applies when importing default and named imports, not the namespace. * 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. * * true - Force imported values to be live references.
* * false - No particular requirements. Keeps the code simplest. (Default) * * 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. * 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. * be used as function callee.
* * false - No particular requirements for context of the access. (Default) * * 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. * 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 * "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. * 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. * The path used for manipulation.
*/ */
declare _programPath: NodePath; declare _programPath: NodePath<t.Program>;
/** /**
* The scope used to generate unique variable names. * The scope used to generate unique variable names.
*/ */
declare _programScope; declare _programScope: Scope;
/** /**
* The file used to inject helpers and resolve paths. * 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. * The default options to use with this instance when imports are added.
@ -130,8 +128,8 @@ export default class ImportInjector {
importPosition: "before", importPosition: "before",
}; };
constructor(path, importedSource, opts) { constructor(path: NodePath, importedSource?, opts?) {
const programPath = path.find(p => p.isProgram()); const programPath = path.find(p => p.isProgram()) as NodePath<t.Program>;
this._programPath = programPath; this._programPath = programPath;
this._programScope = programPath.scope; this._programScope = programPath.scope;
@ -178,7 +176,7 @@ export default class ImportInjector {
optsList.push(importedSource); optsList.push(importedSource);
} }
const newOpts = { const newOpts: ImportOptions = {
...this._defaultOpts, ...this._defaultOpts,
}; };
for (const opts of optsList) { for (const opts of optsList) {
@ -439,6 +437,7 @@ export default class ImportInjector {
}); });
const targetPath = body.find(p => { const targetPath = body.find(p => {
// @ts-expect-error todo(flow->ts): avoid mutations
const val = p.node._blockHoist; const val = p.node._blockHoist;
return Number.isFinite(val) && val < 4; return Number.isFinite(val) && val < 4;
}); });

View File

@ -4,18 +4,18 @@ export { ImportInjector };
export { default as isModule } from "./is-module"; 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); 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); 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); 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); return new ImportInjector(path).addSideEffect(importedSource, opts);
} }

View File

@ -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. * A small utility to check if a file qualifies as a module.
*/ */
export default function isModule(path: NodePath) { export default function isModule(path: NodePath<t.Program>) {
const { sourceType } = path.node; const { sourceType } = path.node;
if (sourceType !== "module" && sourceType !== "script") { if (sourceType !== "module" && sourceType !== "script") {
throw path.buildCodeFrameError( throw path.buildCodeFrameError(

View File

@ -639,6 +639,7 @@ __metadata:
resolution: "@babel/helper-module-imports@workspace:packages/babel-helper-module-imports" resolution: "@babel/helper-module-imports@workspace:packages/babel-helper-module-imports"
dependencies: dependencies:
"@babel/core": "workspace:*" "@babel/core": "workspace:*"
"@babel/traverse": "workspace:*"
"@babel/types": "workspace:^7.12.13" "@babel/types": "workspace:^7.12.13"
languageName: unknown languageName: unknown
linkType: soft linkType: soft