Throw a better error when transforming imported bindings in types (#13739)
* Throw a better error when transforming imported bindings in types * Also type casts * Update error message
This commit is contained in:
parent
2ffb19828f
commit
8fb50429ea
@ -44,6 +44,25 @@ interface RewriteBindingInitVisitorState {
|
|||||||
scope: Scope;
|
scope: Scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isInType(path) {
|
||||||
|
do {
|
||||||
|
switch (path.parent.type) {
|
||||||
|
case "TSTypeAnnotation":
|
||||||
|
case "TSTypeAliasDeclaration":
|
||||||
|
case "TSTypeReference":
|
||||||
|
case "TypeAnnotation":
|
||||||
|
case "TypeAlias":
|
||||||
|
return true;
|
||||||
|
case "ExportSpecifier":
|
||||||
|
return path.parentPath.parent.exportKind === "type";
|
||||||
|
default:
|
||||||
|
if (path.parentPath.isStatement() || path.parentPath.isExpression()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while ((path = path.parentPath));
|
||||||
|
}
|
||||||
|
|
||||||
export default function rewriteLiveReferences(
|
export default function rewriteLiveReferences(
|
||||||
programPath: NodePath<t.Program>,
|
programPath: NodePath<t.Program>,
|
||||||
metadata: ModuleMetadata,
|
metadata: ModuleMetadata,
|
||||||
@ -224,6 +243,13 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
|
|||||||
|
|
||||||
const importData = imported.get(localName);
|
const importData = imported.get(localName);
|
||||||
if (importData) {
|
if (importData) {
|
||||||
|
if (isInType(path)) {
|
||||||
|
throw path.buildCodeFrameError(
|
||||||
|
`Cannot transform the imported binding "${localName}" since it's also used in a type annotation. ` +
|
||||||
|
`Please strip type annotations using @babel/preset-typescript or @babel/preset-flow.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const localBinding = path.scope.getBinding(localName);
|
const localBinding = path.scope.getBinding(localName);
|
||||||
const rootBinding = scope.getBinding(localName);
|
const rootBinding = scope.getBinding(localName);
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,5 @@
|
|||||||
|
import A from "x";
|
||||||
|
|
||||||
|
export function fn(x: A.b[2]) {
|
||||||
|
return A.method(x);
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"externalHelpers": true,
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["transform-modules-commonjs", "syntax-flow"],
|
||||||
|
"throws": "Cannot transform the imported binding \"A\" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { A } from "x";
|
||||||
|
|
||||||
|
var x = ({} : A.b[0]);
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"externalHelpers": true,
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["transform-modules-commonjs", "syntax-flow"],
|
||||||
|
"throws": "Cannot transform the imported binding \"A\" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
import A from "x";
|
||||||
|
|
||||||
|
export function fn(x: A) {
|
||||||
|
return A.method(x);
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"externalHelpers": true,
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["transform-modules-commonjs", "syntax-flow"],
|
||||||
|
"throws": "Cannot transform the imported binding \"A\" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
import A from "x";
|
||||||
|
|
||||||
|
export function fn(x: A.b[2]) {
|
||||||
|
return A.method(x);
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"externalHelpers": true,
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["transform-modules-commonjs", "syntax-typescript"],
|
||||||
|
"throws": "Cannot transform the imported binding \"A\" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { A } from "x";
|
||||||
|
|
||||||
|
var x = {} as A.b[0];
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"externalHelpers": true,
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["transform-modules-commonjs", "syntax-typescript"],
|
||||||
|
"throws": "Cannot transform the imported binding \"A\" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { A } from "x";
|
||||||
|
|
||||||
|
var x = <A.b[0]> {};
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"externalHelpers": true,
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["transform-modules-commonjs", "syntax-typescript"],
|
||||||
|
"throws": "Cannot transform the imported binding \"A\" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
import A from "x";
|
||||||
|
|
||||||
|
export function fn(x: A) {
|
||||||
|
return A.method(x);
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"externalHelpers": true,
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["transform-modules-commonjs", "syntax-typescript"],
|
||||||
|
"throws": "Cannot transform the imported binding \"A\" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user