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

Commit

Permalink
Treat type alias declarationlike function declaration (#584)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joa authored and hzoo committed Sep 25, 2018
1 parent b400cb1 commit 020d012
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/babylon-to-espree/toAST.js
Expand Up @@ -72,6 +72,15 @@ var astTransformVisitor = {
delete node.bound;
}

if (path.isTypeAlias()) {
node.type = "FunctionDeclaration";
node.generator = false;
node.async = false;
node.expression = false;
node.params = [];
node.body = node.right;
}

// flow: prevent "no-undef"
// for "Component" in: "let x: React.Component"
if (path.isQualifiedTypeIdentifier()) {
Expand Down
12 changes: 12 additions & 0 deletions test/non-regression.js
Expand Up @@ -1091,6 +1091,18 @@ describe("verify", () => {
{ "no-unused-vars": 1, "no-undef": 1 }
);
});

it("cyclic type dependencies #485", () => {
verifyAndAssertMessages(
unpad(`
type Node<T> = { head: T, tail: Node<T> };
type A = B[];
type B = number;
`),
{ "no-use-before-define": 1 },
[]
);
});
});

it("class usage", () => {
Expand Down

0 comments on commit 020d012

Please sign in to comment.