Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
[FIX][no-use-before-define] cleanup logic (#202)
Browse files Browse the repository at this point in the history
This PR changes logic behind `no-use-before-define` to traverse all scopes

fixes: #141
  • Loading branch information
armano2 authored and bradzacher committed Dec 6, 2018
1 parent f89e02f commit 2c21510
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
43 changes: 4 additions & 39 deletions lib/rules/no-use-before-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,49 +268,14 @@ module.exports = {
data: reference.identifier,
});
});
}

/**
* Validates variables inside of a node's scope.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function findVariables() {
const scope = context.getScope();

findVariablesInScope(scope);
scope.childScopes.forEach(findVariablesInScope);
}

const ruleDefinition = {
"Program:exit"(node) {
const scope = context.getScope(),
ecmaFeatures = context.parserOptions.ecmaFeatures || {};

findVariablesInScope(scope);

// both Node.js and Modules have an extra scope
if (ecmaFeatures.globalReturn || node.sourceType === "module") {
findVariablesInScope(scope.childScopes[0]);
}
return {
Program() {
findVariablesInScope(context.getScope());
},
};

if (context.parserOptions.ecmaVersion >= 6) {
ruleDefinition["BlockStatement:exit"] = findVariables;
ruleDefinition["SwitchStatement:exit"] = findVariables;

ruleDefinition["ArrowFunctionExpression:exit"] = function(node) {
if (node.body.type !== "BlockStatement") {
findVariables();
}
};
} else {
ruleDefinition["FunctionExpression:exit"] = findVariables;
ruleDefinition["FunctionDeclaration:exit"] = findVariables;
ruleDefinition["ArrowFunctionExpression:exit"] = findVariables;
}

return ruleDefinition;
},
};
28 changes: 26 additions & 2 deletions tests/lib/rules/no-use-before-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ const rule = require("../../../lib/rules/no-use-before-define"),
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const ruleTester = new RuleTester({
parser: "typescript-eslint-parser",
});

ruleTester.run("no-use-before-define", rule, {
valid: [
"var a=10; alert(a);",
"function b(a) { alert(a); }",
"Object.hasOwnProperty.call(a);",
"function a() { alert(arguments);}",
"declare function a()",
"declare class a { foo() }",
{
code: `
a();
Expand Down Expand Up @@ -177,7 +181,6 @@ var x: Foo = 2;
type Foo = string | number
`,
options: [{ typedefs: false }],
parser: "typescript-eslint-parser",
},

// test for https://github.com/bradzacher/eslint-plugin-typescript/issues/142
Expand All @@ -199,6 +202,26 @@ export class Test {}
parserOptions: { ecmaVersion: 6, sourceType: "module" },
options: [{ classes: false }],
},
// https://github.com/bradzacher/eslint-plugin-typescript/issues/141
{
code: `
interface ITest {
first : boolean;
second : string;
third : boolean;
}
let first = () => console.log('first');
export let second = () => console.log('second');
export namespace Third {
export let third = () => console.log('third');
}
`,
parserOptions: { ecmaVersion: 6, sourceType: "module" },
parser: "typescript-eslint-parser",
},
],
invalid: [
{
Expand Down Expand Up @@ -337,6 +360,7 @@ a();
function a() {}
}
`,
parser: "espree",
errors: [
{
message: "'a' was used before it was defined.",
Expand Down

0 comments on commit 2c21510

Please sign in to comment.