Skip to content

Commit

Permalink
Throw a better error when transforming imported bindings in types
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 7, 2021
1 parent d87a3d9 commit 95c0689
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 0 deletions.
Expand Up @@ -44,6 +44,24 @@ interface RewriteBindingInitVisitorState {
scope: Scope;
}

function isInType(path) {
do {
switch (path.parent.type) {
case "TSTypeAnnotation":
case "TSTypeAliasDeclaration":
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(
programPath: NodePath<t.Program>,
metadata: ModuleMetadata,
Expand Down Expand Up @@ -224,6 +242,13 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {

const importData = imported.get(localName);
if (importData) {
if (isInType(path)) {
throw path.buildCodeFrameError(
`Cannot transform the imported ${localName} binding since it's used in a type annotation. ` +
`Please strip type annotations using @babel/preset-typescript or @babel/preset-flow.`,
);
}

const localBinding = path.scope.getBinding(localName);
const rootBinding = scope.getBinding(localName);

Expand Down
@@ -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 A binding since it's 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 A binding since it's 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 A binding since it's 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 A binding since it's used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow."
}

0 comments on commit 95c0689

Please sign in to comment.