From b5fb53b718d15e95252197b5f30787ca807d069a Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Thu, 23 Mar 2017 12:49:19 -0400 Subject: [PATCH] Fix type param and interface declaration scoping (#449) --- index.js | 40 +++++++++++++++------------ test/non-regression.js | 63 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 39b6142a..8d05f794 100644 --- a/index.js +++ b/index.js @@ -180,18 +180,6 @@ function monkeypatch() { } } - function visitTypeParameters(typeParameters) { - var params = typeParameters.params; - - // visit bounds on polymorphpic types, eg; `Foo` in `fn(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); @@ -209,6 +197,9 @@ function monkeypatch() { for (var j = 0; j < node.typeParameters.params.length; j++) { var name = node.typeParameters.params[j]; scope.__define(name, new Definition("TypeParameter", name, name)); + if (name.typeAnnotation) { + checkIdentifierOrVisit.call(this, name); + } } scope.__define = function() { return parentScope.__define.apply(parentScope, arguments); @@ -222,7 +213,7 @@ function monkeypatch() { visitDecorators.call(this, node); var typeParamScope; if (node.typeParameters) { - typeParamScope = nestTypeParamScope(this.scopeManager, node); + typeParamScope = nestTypeParamScope.call(this, this.scopeManager, node); } // visit flow type: ClassImplements if (node.implements) { @@ -264,8 +255,7 @@ function monkeypatch() { referencer.prototype.visitFunction = function(node) { var typeParamScope; if (node.typeParameters) { - typeParamScope = nestTypeParamScope(this.scopeManager, node); - visitTypeParameters.call(this, node.typeParameters); + typeParamScope = nestTypeParamScope.call(this, this.scopeManager, node); } if (node.returnType) { checkIdentifierOrVisit.call(this, node.returnType); @@ -328,11 +318,27 @@ function monkeypatch() { ); } + referencer.prototype.InterfaceDeclaration = function(node) { + createScopeVariable.call(this, node, node.id); + var typeParamScope; + if (node.typeParameters) { + typeParamScope = nestTypeParamScope.call(this, this.scopeManager, node); + } + // TODO: Handle mixins + for (var i = 0; i < node.extends.length; i++) { + visitTypeAnnotation.call(this, node.extends[i]); + } + visitTypeAnnotation.call(this, node.body); + if (typeParamScope) { + this.close(node); + } + }; + referencer.prototype.TypeAlias = function(node) { createScopeVariable.call(this, node, node.id); var typeParamScope; if (node.typeParameters) { - typeParamScope = nestTypeParamScope(this.scopeManager, node); + typeParamScope = nestTypeParamScope.call(this, this.scopeManager, node); } if (node.right) { visitTypeAnnotation.call(this, node.right); @@ -352,7 +358,7 @@ function monkeypatch() { var typeParamScope; if (node.typeParameters) { - typeParamScope = nestTypeParamScope(this.scopeManager, node); + typeParamScope = nestTypeParamScope.call(this, this.scopeManager, node); } if (typeParamScope) { this.close(node); diff --git a/test/non-regression.js b/test/non-regression.js index ba83722a..107802ce 100644 --- a/test/non-regression.js +++ b/test/non-regression.js @@ -222,16 +222,73 @@ describe("verify", () => { ); }); - it("type parameter bounds", () => { + it("interface declaration", () => { + verifyAndAssertMessages( + unpad(` + interface Foo {}; + interface Bar { + foo: Foo, + }; + `), + { "no-unused-vars": 1, "no-undef": 1 }, + [ "2:11 'Bar' is defined but never used. no-unused-vars" ] + ); + }); + + it("type parameter bounds (classes)", () => { + verifyAndAssertMessages( + unpad(` + import type {Foo, Foo2} from 'foo'; + import Base from 'base'; + class Log extends Base { + messages: {[T1]: T2}; + } + new Log(); + `), + { "no-unused-vars": 1, "no-undef": 1 }, + [ "3:34 'T4' is defined but never used. no-unused-vars" ] + ); + }); + + it("type parameter bounds (interfaces)", () => { + verifyAndAssertMessages( + unpad(` + import type {Foo, Foo2, Bar} from ''; + interface Log extends Bar { + messages: {[T1]: T2}; + } + `), + { "no-unused-vars": 1, "no-undef": 1 }, + [ "2:11 'Log' is defined but never used. no-unused-vars", + "2:38 'T4' is defined but never used. no-unused-vars" ] + ); + }); + + it("type parameter bounds (type aliases)", () => { + verifyAndAssertMessages( + unpad(` + import type {Foo, Foo2, Foo3} from 'foo'; + type Log = { + messages: {[T1]: T2}; + delay: Foo3; + }; + `), + { "no-unused-vars": 1, "no-undef": 1 }, + [ "2:6 'Log' is defined but never used. no-unused-vars", + "2:29 'T3' is defined but never used. no-unused-vars" ] + ); + }); + + it("type parameter bounds (functions)", () => { verifyAndAssertMessages( unpad(` import type Foo from 'foo'; import type Foo2 from 'foo'; - function log(a: T1, b: T2) { return a + b; } + function log(a: T1, b: T2): T3 { return a + b; } log(1, 2); `), { "no-unused-vars": 1, "no-undef": 1 }, - [] + [ "3:37 'T4' is defined but never used. no-unused-vars" ] ); });