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

Commit

Permalink
Fix type param and interface declaration scoping (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed Mar 23, 2017
1 parent f1cee0f commit b5fb53b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
40 changes: 23 additions & 17 deletions index.js
Expand Up @@ -180,18 +180,6 @@ 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);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
63 changes: 60 additions & 3 deletions test/non-regression.js
Expand Up @@ -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<T1: Foo, T2: Foo2, T3, T4> extends Base<T3> {
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<T1: Foo, T2: Foo2, T3, T4> extends Bar<T3> {
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<T1: Foo, T2: Foo2, T3> = {
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<T1: Foo, T2: Foo2>(a: T1, b: T2) { return a + b; }
function log<T1: Foo, T2: Foo2, T3, T4>(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" ]
);
});

Expand Down

0 comments on commit b5fb53b

Please sign in to comment.