convert @babel/helper-module-imports to typescript (#12924)
This commit is contained in:
parent
ff8e9f74d8
commit
21e8e59f7d
@ -18,6 +18,7 @@
|
||||
"@babel/types": "workspace:^7.12.13"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "workspace:*"
|
||||
"@babel/core": "workspace:*",
|
||||
"@babel/traverse": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
@ -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<t.Program>;
|
||||
|
||||
/**
|
||||
* 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<t.Program>;
|
||||
|
||||
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;
|
||||
});
|
||||
@ -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);
|
||||
}
|
||||
@ -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<t.Program>) {
|
||||
const { sourceType } = path.node;
|
||||
if (sourceType !== "module" && sourceType !== "script") {
|
||||
throw path.buildCodeFrameError(
|
||||
Loading…
x
Reference in New Issue
Block a user