feat(typescript): implement namespace alias (#13528)

support `import alias = Namespace`, reject `import alias = require('foo')`

fix #12345
This commit is contained in:
王清雨 2021-08-04 05:55:56 +08:00 committed by GitHub
parent 7e50ee2d82
commit 0542f0d7da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 7 deletions

View File

@ -465,12 +465,26 @@ export default declare((api, opts) => {
transpileEnum(path, t); transpileEnum(path, t);
}, },
TSImportEqualsDeclaration(path) { TSImportEqualsDeclaration(path: NodePath<t.TSImportEqualsDeclaration>) {
throw path.buildCodeFrameError( if (t.isTSExternalModuleReference(path.node.moduleReference)) {
"`import =` is not supported by @babel/plugin-transform-typescript\n" + // import alias = require('foo');
"Please consider using " + throw path.buildCodeFrameError(
"`import <moduleName> from '<moduleName>';` alongside " + `\`import ${path.node.id.name} = require('${path.node.moduleReference.expression.value}')\` ` +
"Typescript's --allowSyntheticDefaultImports option.", "is not supported by @babel/plugin-transform-typescript\n" +
"Please consider using " +
`\`import ${path.node.id.name} from '${path.node.moduleReference.expression.value}';\` alongside ` +
"Typescript's --allowSyntheticDefaultImports option.",
);
}
// import alias = Namespace;
path.replaceWith(
t.variableDeclaration("var", [
t.variableDeclarator(
path.node.id,
entityNameToExpr(path.node.moduleReference),
),
]),
); );
}, },
@ -519,6 +533,14 @@ export default declare((api, opts) => {
}, },
}; };
function entityNameToExpr(node: t.TSEntityName): t.Expression {
if (t.isTSQualifiedName(node)) {
return t.memberExpression(entityNameToExpr(node.left), node.right);
}
return node;
}
function visitPattern({ node }) { function visitPattern({ node }) {
if (node.typeAnnotation) node.typeAnnotation = null; if (node.typeAnnotation) node.typeAnnotation = null;
if (t.isIdentifier(node) && node.optional) node.optional = null; if (t.isIdentifier(node) && node.optional) node.optional = null;

View File

@ -1,3 +1,3 @@
{ {
"throws": "`import =` is not supported by @babel/plugin-transform-typescript\nPlease consider using `import <moduleName> from '<moduleName>';` alongside Typescript's --allowSyntheticDefaultImports option." "throws": "`import lib = require('lib')` is not supported by @babel/plugin-transform-typescript\nPlease consider using `import lib from 'lib';` alongside Typescript's --allowSyntheticDefaultImports option."
} }

View File

@ -0,0 +1,22 @@
declare module LongNameModule {
export type SomeType = number;
export const foo: number;
module Inner {
export type T = string;
export const bar: boolean;
}
}
import * as babel from '@babel/core';
/** type only */
import b = babel;
import AliasModule = LongNameModule;
const some: AliasModule.SomeType = 3;
const bar = AliasModule.foo;
const baz = AliasModule.Inner.bar;
let str: LongNameModule.Inner.T;
let node: b.OptionManager;
console.log(some);

View File

@ -0,0 +1,11 @@
import * as babel from '@babel/core';
/** type only */
var b = babel;
var AliasModule = LongNameModule;
const some = 3;
const bar = AliasModule.foo;
const baz = AliasModule.Inner.bar;
let str;
let node;
console.log(some);