Skip to content

Commit

Permalink
convert @babel/helper-module-imports to typescript (#12924)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxbodya committed Mar 18, 2021
1 parent ff8e9f7 commit 21e8e59
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/babel-helper-module-imports/package.json
Expand Up @@ -18,6 +18,7 @@
"@babel/types": "workspace:^7.12.13"
},
"devDependencies": {
"@babel/core": "workspace:*"
"@babel/core": "workspace:*",
"@babel/traverse": "workspace:*"
}
}
Expand Up @@ -11,6 +11,7 @@ export default class ImportBuilder {

_scope = null;
_hub = null;
private _importedSource: any;

constructor(importedSource, scope, hub) {
this._scope = scope;
Expand Down Expand Up @@ -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) {
Expand Down
@@ -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";
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -70,32 +68,32 @@ 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.
*
* * 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.
*
* * true - Force calls to exclude context. Useful if the value is going to
* 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?;
};

/**
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -178,7 +176,7 @@ export default class ImportInjector {
optsList.push(importedSource);
}

const newOpts = {
const newOpts: ImportOptions = {
...this._defaultOpts,
};
for (const opts of optsList) {
Expand Down Expand Up @@ -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;
});
Expand Down
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -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
Expand Down

0 comments on commit 21e8e59

Please sign in to comment.