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

Commit 5192c20

Browse files
authored
[FIX] [no-use-before-define] fix top level scope handling (#175)
1 parent f92e07a commit 5192c20

2 files changed

Lines changed: 52 additions & 35 deletions

File tree

lib/rules/no-use-before-define.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ function parseOptions(options) {
3939
return { functions, classes, variables, typedefs };
4040
}
4141

42+
/**
43+
* @param {Scope} scope - a scope to check
44+
* @returns {boolean} `true` if the scope is toplevel
45+
*/
46+
function isTopLevelScope(scope) {
47+
return scope.type === "module" || scope.type === "global";
48+
}
49+
4250
/**
4351
* Checks whether or not a given variable is a function declaration.
4452
*
@@ -57,10 +65,18 @@ function isFunction(variable) {
5765
* @returns {boolean} `true` if the variable is a class declaration.
5866
*/
5967
function isOuterClass(variable, reference) {
60-
return (
61-
variable.defs[0].type === "ClassName" &&
62-
variable.scope.variableScope !== reference.from.variableScope
63-
);
68+
if (variable.defs[0].type !== "ClassName") {
69+
return false;
70+
}
71+
72+
if (variable.scope.variableScope === reference.from.variableScope) {
73+
// allow the same scope only if it's the top level global/module scope
74+
if (!isTopLevelScope(variable.scope.variableScope)) {
75+
return false;
76+
}
77+
}
78+
79+
return true;
6480
}
6581

6682
/**
@@ -70,10 +86,18 @@ function isOuterClass(variable, reference) {
7086
* @returns {boolean} `true` if the variable is a variable declaration.
7187
*/
7288
function isOuterVariable(variable, reference) {
73-
return (
74-
variable.defs[0].type === "Variable" &&
75-
variable.scope.variableScope !== reference.from.variableScope
76-
);
89+
if (variable.defs[0].type !== "Variable") {
90+
return false;
91+
}
92+
93+
if (variable.scope.variableScope === reference.from.variableScope) {
94+
// allow the same scope only if it's the top level global/module scope
95+
if (!isTopLevelScope(variable.scope.variableScope)) {
96+
return false;
97+
}
98+
}
99+
100+
return true;
77101
}
78102

79103
/**

tests/lib/rules/no-use-before-define.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ type Foo = string | number
178178
`,
179179
options: [{ typedefs: false }],
180180
parser: "typescript-eslint-parser"
181+
},
182+
183+
// test for https://github.com/bradzacher/eslint-plugin-typescript/issues/142
184+
{
185+
code: `
186+
var alias = Test;
187+
188+
class Test {}
189+
`,
190+
parserOptions: { ecmaVersion: 6 },
191+
options: [{ classes: false }]
192+
},
193+
{
194+
code: `
195+
var alias = Test;
196+
197+
export class Test {}
198+
`,
199+
parserOptions: { ecmaVersion: 6, sourceType: "module" },
200+
options: [{ classes: false }]
181201
}
182202
],
183203
invalid: [
@@ -509,20 +529,6 @@ var a=function() {};
509529
{
510530
code: `
511531
new A();
512-
class A {};
513-
`,
514-
options: [{ functions: false, classes: false }],
515-
parserOptions: { ecmaVersion: 6 },
516-
errors: [
517-
{
518-
message: "'A' was used before it was defined.",
519-
type: "Identifier"
520-
}
521-
]
522-
},
523-
{
524-
code: `
525-
new A();
526532
var A = class {};
527533
`,
528534
options: [{ classes: false }],
@@ -688,19 +694,6 @@ var bar;
688694
type: "Identifier"
689695
}
690696
]
691-
},
692-
{
693-
code: `
694-
foo;
695-
var foo;
696-
`,
697-
options: [{ variables: false }],
698-
errors: [
699-
{
700-
message: "'foo' was used before it was defined.",
701-
type: "Identifier"
702-
}
703-
]
704697
}
705698
]
706699
});

0 commit comments

Comments
 (0)