Skip to content

Commit

Permalink
support comprehensions (no-undef) - fixes babel#9
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Jun 3, 2015
1 parent fdeb022 commit f921efa
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
38 changes: 28 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,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 +238,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 +262,21 @@ 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]);
}
}
}
}
}
}

exports.attachComments = function (ast, comments, tokens) {
Expand Down
34 changes: 34 additions & 0 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,40 @@ describe("verify", function () {
);
});

describe("comprehensions", function () {
it("array #9", function () {
verifyAndAssertMessages(
"let b = [for (e of a) String(e)];",
{ "no-undef": 1 },
[]
);
});

it("array, if statement, multiple blocks", function () {
verifyAndAssertMessages(
"[for (x of array) for (y of array2) if (x === true) x];",
{ "no-undef": 1 },
[]
);
});

it("expression, if statement, multiple blocks", function () {
verifyAndAssertMessages(
"(for (x of array) for (y of array2) if (x === true) x)",
{ "no-undef": 1 },
[]
);
});

it("ArrayPattern", function () {
verifyAndAssertMessages(
"[for ([,x] of array) for ({[start.x]: x, [start.y]: y} of array2) x]",
{ "no-undef": 1 },
[]
);
});
});

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

0 comments on commit f921efa

Please sign in to comment.