Skip to content

Commit

Permalink
chore(eslint-plugin): human readable refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wwbsluxs11 committed Jul 29, 2022
1 parent b71d1a7 commit 4045bc9
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/eslint-plugin/src/rules/no-use-before-define.ts
Expand Up @@ -317,21 +317,32 @@ export default util.createRule<Options, MessageIds>({
return true;
}

function isDefinedBeforeUse(
variable: TSESLint.Scope.Variable,
reference: TSESLint.Scope.Reference,
): boolean {
return (
variable.identifiers[0].range[1] <= reference.identifier.range[1] &&
!isInInitializer(variable, reference)
);
}

/**
* Finds and validates all variables in a given scope.
*/
function findVariablesInScope(scope: TSESLint.Scope.Scope): void {
scope.references.forEach(reference => {
const variable = reference.resolved;

const report = (): void =>
function report(): void {
context.report({
node: reference.identifier,
messageId: 'noUseBeforeDefine',
data: {
name: reference.identifier.name,
},
});
}

// Skips when the reference is:
// - initializations.
Expand All @@ -344,14 +355,10 @@ export default util.createRule<Options, MessageIds>({
}

if (!options.allowNamedExports && isNamedExports(reference)) {
if (
!variable ||
variable.identifiers[0].range[1] > reference.identifier.range[1] ||
isInInitializer(variable, reference)
) {
if (!variable || !isDefinedBeforeUse(variable, reference)) {
report();
return;
}
return;
}

if (!variable) {
Expand All @@ -360,8 +367,7 @@ export default util.createRule<Options, MessageIds>({

if (
variable.identifiers.length === 0 ||
(variable.identifiers[0].range[1] <= reference.identifier.range[1] &&
!isInInitializer(variable, reference)) ||
isDefinedBeforeUse(variable, reference) ||
!isForbidden(variable, reference) ||
isClassRefInClassDecorator(variable, reference) ||
reference.from.type === TSESLint.Scope.ScopeType.functionType
Expand Down

0 comments on commit 4045bc9

Please sign in to comment.