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

Commit

Permalink
Merge pull request #114 from hzoo/i-9
Browse files Browse the repository at this point in the history
support comprehensions (no-undef) - fixes #9
  • Loading branch information
hzoo committed Jun 3, 2015
2 parents fdeb022 + 2f4c294 commit 270cfbd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 22 deletions.
45 changes: 31 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ function monkeypatch() {
var visitClass = referencer.prototype.visitClass;
referencer.prototype.visitClass = function(node) {
visitDecorators.call(this, node);
// visit class
if (node.id) {
this.visit(node.id);
}
// visit flow type: ClassImplements
if (node.implements) {
for (var i = 0; i < node.implements.length; i++) {
Expand All @@ -198,7 +194,7 @@ function monkeypatch() {
// visit decorators that are in: Property / MethodDefinition
var visitProperty = referencer.prototype.visitProperty;
referencer.prototype.visitProperty = function(node) {
if (node.value.type === 'TypeCastExpression') {
if (node.value.type === "TypeCastExpression") {
visitTypeAnnotation.call(this, node.value);
}
visitDecorators.call(this, node);
Expand Down Expand Up @@ -238,18 +234,21 @@ function monkeypatch() {
variableDeclaration.call(this, node);
};

referencer.prototype.TypeAlias = function(node) {
this.currentScope().__define(
node.id,
function createScopeVariable (node, name) {
this.currentScope().variableScope.__define(name,
new Definition(
"Variable",
node.id,
node,
null,
null,
null
"Variable",
name,
node,
null,
null,
null
)
);
}

referencer.prototype.TypeAlias = function(node) {
createScopeVariable.call(this, node, node.id);
if (node.right) {
visitTypeAnnotation.call(this, node.right);
}
Expand All @@ -259,6 +258,24 @@ function monkeypatch() {
}
}
}

referencer.prototype.ComprehensionBlock = function(node) {
var left = node.left;
if (left) {
if (left.type === "Identifier") {
createScopeVariable.call(this, node, left);
} else if (left.type === "ArrayPattern") {
for (var i = 0; i < left.elements.length; i++) {
if (left.elements[i]) {
createScopeVariable.call(this, left.elements, left.elements[i]);
}
}
}
}
if (node.right) {
this.visit(node.right);
}
}
}

exports.attachComments = function (ast, comments, tokens) {
Expand Down
73 changes: 65 additions & 8 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ describe("verify", function () {

it("ClassImplements", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"import type Bar from 'foo';",
"class Foo implements Bar {}"
"export default class Foo implements Bar {}"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
Expand Down Expand Up @@ -459,7 +458,7 @@ describe("verify", function () {
[
"import type Foo from 'foo';",
"import type Foo2 from 'foo';",
"class Bar {set fooProp(value:Foo):Foo2{ value; }}"
"export default class Bar {set fooProp(value:Foo):Foo2{ value; }}"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
Expand All @@ -469,8 +468,8 @@ describe("verify", function () {
it("15", function () {
verifyAndAssertMessages(
[
"import type Foo from 'foo';",
"class Foo {get fooProp():Foo{}}"
"import type Foo2 from 'foo';",
"export default class Foo {get fooProp(): Foo2{}}"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
Expand Down Expand Up @@ -581,7 +580,7 @@ describe("verify", function () {
"import type Foo from 'foo';",
"import type Foo2 from 'foo';",
"import Baz from 'foo';",
"class Bar<Foo> extends Baz<Foo2> { };"
"export default class Bar<Foo> extends Baz<Foo2> { };"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
Expand All @@ -594,7 +593,7 @@ describe("verify", function () {
"import type Foo from 'foo';",
"import type Foo2 from 'foo';",
"import type Foo3 from 'foo';",
"class Bar<Foo> { bar<Foo2>():Foo3 { return 42; }}"
"export default class Bar<Foo> { bar<Foo2>():Foo3 { return 42; }}"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
Expand All @@ -606,7 +605,7 @@ describe("verify", function () {
[
"import type Foo from 'foo';",
"import type Foo2 from 'foo';",
"class Bar { static prop1:Foo; prop2:Foo2; }"
"export default class Bar { static prop1:Foo; prop2:Foo2; }"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
Expand Down Expand Up @@ -885,6 +884,19 @@ describe("verify", function () {
);
});

it("class definition: gaearon/redux#24", function () {
verifyAndAssertMessages([
"export default function root(stores) {",
"return DecoratedComponent => class ReduxRootDecorator {",
"a() { DecoratedComponent; }",
"};",
"}",
].join("\n"),
{ "no-undef": 1, "no-unused-vars": 1 },
[]
);
});

it("class properties", function () {
verifyAndAssertMessages(
"class Lol { foo = 'bar'; }",
Expand Down Expand Up @@ -914,6 +926,51 @@ describe("verify", function () {
);
});

describe("comprehensions", function () {
it("array #9", function () {
verifyAndAssertMessages([
"let arr = [1, 2, 3];",
"let b = [for (e of arr) String(e)];"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
);
});

it("array, if statement, multiple blocks", function () {
verifyAndAssertMessages([
"let arr = [1, 2, 3];",
"let arr2 = [1, 2, 3];",
"[for (x of arr) for (y of arr2) if (x === true && y === true) x + y];"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
);
});

it("expression, if statement, multiple blocks", function () {
verifyAndAssertMessages([
"let arr = [1, 2, 3];",
"let arr2 = [1, 2, 3];",
"(for (x of arr) for (y of arr2) if (x === true && y === true) x + y)"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
);
});

it("ArrayPattern", function () {
verifyAndAssertMessages([
"let arr = [1, 2, 3];",
"let arr2 = [1, 2, 3];",
"[for ([,x] of arr) for ({[start.x]: x, [start.y]: y} of arr2) x]"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[]
);
});
});

describe("decorators #72", function () {
it("class declaration", function () {
verifyAndAssertMessages(
Expand Down

0 comments on commit 270cfbd

Please sign in to comment.