Skip to content

Commit

Permalink
Fix: vars-on-top now accepts exported variables (fixes #5711)
Browse files Browse the repository at this point in the history
  • Loading branch information
olmokramer committed Apr 1, 2016
1 parent e54598a commit ce2accd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/rules/vars-on-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ module.exports = function(context) {
}

for (; i < l; ++i) {
if (statements[i].type !== "VariableDeclaration") {
if (statements[i].type !== "VariableDeclaration" &&
(statements[i].type !== "ExportNamedDeclaration" ||
statements[i].declaration.type !== "VariableDeclaration")) {
return false;
}
if (statements[i] === node) {
Expand Down Expand Up @@ -104,6 +106,12 @@ module.exports = function(context) {
var grandParent = ancestors.pop();

if (node.kind === "var") { // check variable is `var` type and not `let` or `const`
if (parent.type === "ExportNamedDeclaration") {
node = parent;
parent = grandParent;
grandParent = ancestors.pop();
}

if (parent.type === "Program") { // That means its a global variable
globalVarCheck(node, parent);
} else {
Expand Down
58 changes: 57 additions & 1 deletion tests/lib/rules/vars-on-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,40 @@ ruleTester.run("vars-on-top", rule, {
{code: "import { square, diag } from 'lib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { sourceType: "module" }},
{code: "import { default as foo } from 'lib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { sourceType: "module" }},
{code: "import 'src/mylib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { sourceType: "module" }},
{code: "import theDefault, { named1, named2 } from 'src/mylib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { sourceType: "module" }}
{code: "import theDefault, { named1, named2 } from 'src/mylib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { sourceType: "module" }},
{
code: [
"export var x;",
"var y;",
"var z;"
].join("\n"),
parserOptions: {
ecmaVersion: 6,
sourceType: "module"
}
},
{
code: [
"var x;",
"export var y;",
"var z;"
].join("\n"),
parserOptions: {
ecmaVersion: 6,
sourceType: "module"
}
},
{
code: [
"var x;",
"var y;",
"export var z;"
].join("\n"),
parserOptions: {
ecmaVersion: 6,
sourceType: "module"
}
}
],

invalid: [
Expand Down Expand Up @@ -403,6 +436,29 @@ ruleTester.run("vars-on-top", rule, {
{
code: "function f() { 'use strict'; var x; 'directive'; var y; f(); }",
errors: [{message: "All 'var' declarations must be at the top of the function scope.", type: "VariableDeclaration"}]
},
{
code: [
"export function f() {}",
"var x;"
].join("\n"),
parserOptions: {
ecmaVersion: 6,
sourceType: "module"
},
errors: [{message: "All 'var' declarations must be at the top of the function scope.", type: "VariableDeclaration"}]
},
{
code: [
"var x;",
"export function f() {}",
"var y;"
].join("\n"),
parserOptions: {
ecmaVersion: 6,
sourceType: "module"
},
errors: [{message: "All 'var' declarations must be at the top of the function scope.", type: "VariableDeclaration"}]
}
]
});

0 comments on commit ce2accd

Please sign in to comment.