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

Commit

Permalink
[FIX] [no-use-before-define] fix top level scope handling (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher authored Nov 21, 2018
1 parent f92e07a commit 5192c20
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
40 changes: 32 additions & 8 deletions lib/rules/no-use-before-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ function parseOptions(options) {
return { functions, classes, variables, typedefs };
}

/**
* @param {Scope} scope - a scope to check
* @returns {boolean} `true` if the scope is toplevel
*/
function isTopLevelScope(scope) {
return scope.type === "module" || scope.type === "global";
}

/**
* Checks whether or not a given variable is a function declaration.
*
Expand All @@ -57,10 +65,18 @@ function isFunction(variable) {
* @returns {boolean} `true` if the variable is a class declaration.
*/
function isOuterClass(variable, reference) {
return (
variable.defs[0].type === "ClassName" &&
variable.scope.variableScope !== reference.from.variableScope
);
if (variable.defs[0].type !== "ClassName") {
return false;
}

if (variable.scope.variableScope === reference.from.variableScope) {
// allow the same scope only if it's the top level global/module scope
if (!isTopLevelScope(variable.scope.variableScope)) {
return false;
}
}

return true;
}

/**
Expand All @@ -70,10 +86,18 @@ function isOuterClass(variable, reference) {
* @returns {boolean} `true` if the variable is a variable declaration.
*/
function isOuterVariable(variable, reference) {
return (
variable.defs[0].type === "Variable" &&
variable.scope.variableScope !== reference.from.variableScope
);
if (variable.defs[0].type !== "Variable") {
return false;
}

if (variable.scope.variableScope === reference.from.variableScope) {
// allow the same scope only if it's the top level global/module scope
if (!isTopLevelScope(variable.scope.variableScope)) {
return false;
}
}

return true;
}

/**
Expand Down
47 changes: 20 additions & 27 deletions tests/lib/rules/no-use-before-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ type Foo = string | number
`,
options: [{ typedefs: false }],
parser: "typescript-eslint-parser"
},

// test for https://github.com/bradzacher/eslint-plugin-typescript/issues/142
{
code: `
var alias = Test;
class Test {}
`,
parserOptions: { ecmaVersion: 6 },
options: [{ classes: false }]
},
{
code: `
var alias = Test;
export class Test {}
`,
parserOptions: { ecmaVersion: 6, sourceType: "module" },
options: [{ classes: false }]
}
],
invalid: [
Expand Down Expand Up @@ -509,20 +529,6 @@ var a=function() {};
{
code: `
new A();
class A {};
`,
options: [{ functions: false, classes: false }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "'A' was used before it was defined.",
type: "Identifier"
}
]
},
{
code: `
new A();
var A = class {};
`,
options: [{ classes: false }],
Expand Down Expand Up @@ -688,19 +694,6 @@ var bar;
type: "Identifier"
}
]
},
{
code: `
foo;
var foo;
`,
options: [{ variables: false }],
errors: [
{
message: "'foo' was used before it was defined.",
type: "Identifier"
}
]
}
]
});

0 comments on commit 5192c20

Please sign in to comment.