feat(typescript): implement namespace alias (#13528)
support `import alias = Namespace`, reject `import alias = require('foo')`
fix #12345
This commit is contained in:
parent
7e50ee2d82
commit
0542f0d7da
@ -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;
|
||||||
|
|||||||
@ -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."
|
||||||
}
|
}
|
||||||
|
|||||||
22
packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/input.ts
vendored
Normal file
22
packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/input.ts
vendored
Normal 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);
|
||||||
11
packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/output.mjs
vendored
Normal file
11
packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/output.mjs
vendored
Normal 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);
|
||||||
Loading…
x
Reference in New Issue
Block a user