From fcd22f553c2037a9eb3c92e8d5d8af4bb7b6bc31 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Tue, 16 Jun 2015 21:47:17 -0400 Subject: [PATCH] support flow declarations correctly, lint - fixes #132 --- acorn-to-esprima.js | 11 +---------- index.js | 17 +++++++++++++++-- test/non-regression.js | 16 ++++++++++++++-- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/acorn-to-esprima.js b/acorn-to-esprima.js index 9dfdfffa..7809f04a 100644 --- a/acorn-to-esprima.js +++ b/acorn-to-esprima.js @@ -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; @@ -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()) { diff --git a/index.js b/index.js index e78025c0..f69d9f0a 100644 --- a/index.js +++ b/index.js @@ -178,7 +178,7 @@ function monkeypatch() { } scope.__define = function() { return parentScope.__define.apply(parentScope, arguments); - } + }; return scope; } @@ -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]); } @@ -304,6 +304,19 @@ function monkeypatch() { this.visit(node.right); } }; + + referencer.prototype.DeclareModule = + referencer.prototype.DeclareFunction = + referencer.prototype.DeclareVariable = + referencer.prototype.DeclareClass = function(node) { + var typeParamScope; + if (node.typeParameters) { + typeParamScope = nestTypeParamScope(this.scopeManager, node); + } + if (typeParamScope) { + this.close(node); + } + }; } exports.attachComments = function (ast, comments, tokens) { diff --git a/test/non-regression.js b/test/non-regression.js index de0bfbf6..4b4665f2 100644 --- a/test/non-regression.js +++ b/test/non-regression.js @@ -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' ] ); }); @@ -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(): void;", + "declare var bar" + ].join("\n"), + { "no-undef": 1, "no-unused-vars": 1 }, + [] + ); + }); + it("1", function () { verifyAndAssertMessages( [ @@ -1133,6 +1145,6 @@ describe("verify", function () { "var originalObject = {}; var {field1, field2, ...clone} = originalObject;", { "no-undef": 1, "no-unused-vars": 1 }, [] - ) + ); }); });