Skip to content

Commit

Permalink
Fix: vars-on-top conflict with ES6 import (fixes #2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyandeeps committed Mar 18, 2015
1 parent 66b43cb commit 9e226ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/rules/vars-on-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ module.exports = function (context) {
node.expression.type === "Literal" && typeof node.expression.value === "string";
}

/**
* Check to see if its a ES6 import declaration
* @param {ASTNode} node - any node
* @returns {Boolean} whether the given node represents a import declaration
*/
function looksLikeImport(node) {
return node.type === "ImportDeclaration" || node.type === "ImportSpecifier" ||
node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier";
}

/**
* Checks whether this variable is on top of the block body
* @param {ASTNode} node - The node to check
Expand All @@ -38,7 +48,7 @@ module.exports = function (context) {

// skip over directives
for (; i < l; ++i) {
if (!looksLikeDirective(statements[i])) {
if (!looksLikeDirective(statements[i]) && !looksLikeImport(statements[i])) {
break;
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/lib/rules/vars-on-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ eslintTester.addRuleTest("lib/rules/vars-on-top", {
"'use strict'; var x; f();",
"'use strict'; 'directive'; var x; var y; f();",
"function f() { 'use strict'; var x; f(); }",
"function f() { 'use strict'; 'directive'; var x; var y; f(); }"
"function f() { 'use strict'; 'directive'; var x; var y; f(); }",
{code: "import React from 'react'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }},
{code: "'use strict'; import React from 'react'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }},
{code: "import React from 'react'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }},
{code: "import * as foo from 'mod.js'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }},
{code: "import { square, diag } from 'lib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }},
{code: "import { default as foo } from 'lib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }},
{code: "import 'src/mylib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }},
{code: "import theDefault, { named1, named2 } from 'src/mylib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", ecmaFeatures: { modules: true }}
],

invalid: [
Expand Down

0 comments on commit 9e226ad

Please sign in to comment.