Skip to content

Commit

Permalink
refactor: update from CR
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 committed Sep 21, 2021
1 parent 9f7dc26 commit c3d5452
Showing 1 changed file with 4 additions and 27 deletions.
31 changes: 4 additions & 27 deletions packages/eslint-plugin/src/rules/no-require-imports.ts
@@ -1,4 +1,4 @@
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import * as util from '../util';

export default util.createRule({
Expand All @@ -21,8 +21,10 @@ export default util.createRule({
'CallExpression[callee.name="require"]'(
node: TSESTree.CallExpression,
): void {
const variable = getVariableByName(context.getScope(), 'require');
const variable = ASTUtils.findVariable(context.getScope(), 'require');

// ignore non-global require usage as it's something userland custom instead
// of the commonjs standard
if (!variable?.identifiers.length) {
context.report({
node,
Expand All @@ -39,28 +41,3 @@ export default util.createRule({
};
},
});

/**
* Finds the variable by a given name in a given scope and its upper scopes.
* @param initScope A scope to start find.
* @param name A variable name to find.
* @returns A found variable or `null`.
*/
function getVariableByName(
initScope: TSESLint.Scope.Scope | null,
name: string,
): TSESLint.Scope.Variable | null {
let scope = initScope;

while (scope) {
const variable = scope.set.get(name);

if (variable) {
return variable;
}

scope = scope.upper;
}

return null;
}

0 comments on commit c3d5452

Please sign in to comment.