Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw a better error when transforming imported bindings in types #13739

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -44,6 +44,25 @@ interface RewriteBindingInitVisitorState {
scope: Scope;
}

function isInType(path) {
do {
switch (path.parent.type) {
case "TSTypeAnnotation":
case "TSTypeAliasDeclaration":
case "TSTypeReference":
case "TypeAnnotation":
case "TypeAlias":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have an alias to cover all type usage for flow/ts? (or is this it anyway)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's enough to cover the nodes where the AST stops being an expression and starts being a type. The problem with a TypeScript/Flow catch-all is that both TS and Flow introduce some expression nodes (for example, in enums).

return true;
case "ExportSpecifier":
return path.parentPath.parent.exportKind === "type";
default:
if (path.parentPath.isStatement() || path.parentPath.isExpression()) {
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}
} while ((path = path.parentPath));
}

export default function rewriteLiveReferences(
programPath: NodePath<t.Program>,
metadata: ModuleMetadata,
Expand Down Expand Up @@ -224,6 +243,13 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {

const importData = imported.get(localName);
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 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 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."
}