[flow] Process polymorphic type bounds on functions (babel/babel-eslint#444)

This commit is contained in:
Alex Rattray 2017-03-20 14:46:07 -07:00
parent 2bee348c9a
commit 65413344bd
2 changed files with 16 additions and 3 deletions

View File

@ -176,6 +176,18 @@ function monkeypatch() {
}
}
function visitTypeParameters(typeParameters) {
var params = typeParameters.params;
// visit bounds on polymorphpic types, eg; `Foo` in `fn<T: Foo>(a: T): T`
for (var i = 0; i < params.length; i++) {
var param = params[i];
if (param.typeAnnotation) {
visitTypeAnnotation.call(this, param.typeAnnotation);
}
}
}
function checkIdentifierOrVisit(node) {
if (node.typeAnnotation) {
visitTypeAnnotation.call(this, node.typeAnnotation);
@ -249,6 +261,7 @@ function monkeypatch() {
var typeParamScope;
if (node.typeParameters) {
typeParamScope = nestTypeParamScope(this.scopeManager, node);
visitTypeParameters.call(this, node.typeParameters);
}
if (node.returnType) {
checkIdentifierOrVisit.call(this, node.returnType);

View File

@ -222,13 +222,13 @@ describe("verify", () => {
);
});
it("type parameters", () => {
it("type parameter bounds", () => {
verifyAndAssertMessages(
unpad(`
import type Foo from 'foo';
import type Foo2 from 'foo';
function log<T1, T2>(a: T1, b: T2) { return a + b; }
log<Foo, Foo2>(1, 2);
function log<T1: Foo, T2: Foo2>(a: T1, b: T2) { return a + b; }
log(1, 2);
`),
{ "no-unused-vars": 1, "no-undef": 1 },
[]