Skip to content

Commit

Permalink
support flow declarations correctly, lint - fixes babel#132
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Jun 17, 2015
1 parent 02df9e0 commit 7d3e5f2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
11 changes: 1 addition & 10 deletions acorn-to-esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ var astTransformVisitor = {
return node.argument;
}

// prevent "no-undef"
// flow: prevent "no-undef"
// for "Component" in: "let x: React.Component"
if (this.isQualifiedTypeIdentifier()) {
delete node.id;
Expand All @@ -201,15 +201,6 @@ var astTransformVisitor = {
delete node.name;
}

// flow

if (this.isDeclareModule() ||
this.isDeclareClass() ||
this.isDeclareFunction() ||
this.isDeclareVariable()) {
return this.dangerouslyRemove();
}

// modules

if (this.isImportDeclaration()) {
Expand Down
44 changes: 42 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function monkeypatch() {
}
scope.__define = function() {
return parentScope.__define.apply(parentScope, arguments);
}
};
return scope;
}

Expand Down Expand Up @@ -250,7 +250,7 @@ function monkeypatch() {
if (typeAnnotation) {
checkIdentifierOrVisit.call(this, typeAnnotation);
}
if (id.type === 'ObjectPattern') {
if (id.type === "ObjectPattern") {
for (var j = 0; j < id.properties.length; j++) {
this.visit(id.properties[j]);
}
Expand Down Expand Up @@ -304,6 +304,46 @@ function monkeypatch() {
this.visit(node.right);
}
};

referencer.prototype.DeclareClass = function(node) {
var typeParamScope;
if (node.typeParameters) {
typeParamScope = nestTypeParamScope(this.scopeManager, node);
}
if (typeParamScope) {
this.close(node);
}
};

referencer.prototype.DeclareModule = function(node) {
var typeParamScope;
if (node.typeParameters) {
typeParamScope = nestTypeParamScope(this.scopeManager, node);
}
if (typeParamScope) {
this.close(node);
}
};

referencer.prototype.DeclareFunction = function(node) {
var typeParamScope;
if (node.typeParameters) {
typeParamScope = nestTypeParamScope(this.scopeManager, node);
}
if (typeParamScope) {
this.close(node);
}
};

referencer.prototype.DeclareVariable = function(node) {
var typeParamScope;
if (node.typeParameters) {
typeParamScope = nestTypeParamScope(this.scopeManager, node);
}
if (typeParamScope) {
this.close(node);
}
};
}

exports.attachComments = function (ast, comments, tokens) {
Expand Down
16 changes: 14 additions & 2 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe("verify", function () {
"var b: T = 1; b;"
].join("\n"),
{ "no-unused-vars": 1, "no-undef": 1 },
[ '1:20 T is defined but never used no-unused-vars',
[ "1:20 T is defined but never used no-unused-vars",
'3:7 "T" is not defined. no-undef' ]
);
});
Expand All @@ -371,6 +371,18 @@ describe("verify", function () {
);
});

it("support declarations #132", function () {
verifyAndAssertMessages([
"declare class A { static () : number }",
"declare module B { declare var x: number; }",
"declare function foo<T>(): void;",
"declare var bar"
].join("\n"),
{ "no-undef": 1, "no-unused-vars": 1 },
[]
);
});

it("1", function () {
verifyAndAssertMessages(
[
Expand Down Expand Up @@ -1133,6 +1145,6 @@ describe("verify", function () {
"var originalObject = {}; var {field1, field2, ...clone} = originalObject;",
{ "no-undef": 1, "no-unused-vars": 1 },
[]
)
);
});
});

0 comments on commit 7d3e5f2

Please sign in to comment.