Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

Commit 020d012

Browse files
joahzoo
authored andcommitted
Treat type alias declarationlike function declaration (#584)
A type alias shouldn't trigger a no-use-before-define warning just like a function declaration. Cyclic type dependencies are common when using flow. For instance: type Node<T> = { head: T; tail: Node<T> } Fixes #485
1 parent b400cb1 commit 020d012

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/babylon-to-espree/toAST.js

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ var astTransformVisitor = {
7272
delete node.bound;
7373
}
7474

75+
if (path.isTypeAlias()) {
76+
node.type = "FunctionDeclaration";
77+
node.generator = false;
78+
node.async = false;
79+
node.expression = false;
80+
node.params = [];
81+
node.body = node.right;
82+
}
83+
7584
// flow: prevent "no-undef"
7685
// for "Component" in: "let x: React.Component"
7786
if (path.isQualifiedTypeIdentifier()) {

test/non-regression.js

+12
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,18 @@ describe("verify", () => {
10911091
{ "no-unused-vars": 1, "no-undef": 1 }
10921092
);
10931093
});
1094+
1095+
it("cyclic type dependencies #485", () => {
1096+
verifyAndAssertMessages(
1097+
unpad(`
1098+
type Node<T> = { head: T, tail: Node<T> };
1099+
type A = B[];
1100+
type B = number;
1101+
`),
1102+
{ "no-use-before-define": 1 },
1103+
[]
1104+
);
1105+
});
10941106
});
10951107

10961108
it("class usage", () => {

0 commit comments

Comments
 (0)