Skip to content

Commit

Permalink
fix(eslint-plugin): [no-var-requires] do not report require created f…
Browse files Browse the repository at this point in the history
…rom createRequire (#4221)
  • Loading branch information
asdf93074 committed Nov 28, 2021
1 parent 853d799 commit 0040186
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/eslint-plugin/src/rules/no-var-requires.ts
@@ -1,4 +1,5 @@
import {
ASTUtils,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
Expand Down Expand Up @@ -43,10 +44,14 @@ export default util.createRule<Options, MessageIds>({
AST_NODE_TYPES.VariableDeclarator,
].includes(parent.type)
) {
context.report({
node,
messageId: 'noVarReqs',
});
const variable = ASTUtils.findVariable(context.getScope(), 'require');

if (!variable?.identifiers.length) {
context.report({
node,
messageId: 'noVarReqs',
});
}
}
},
};
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin/tests/rules/no-var-requires.test.ts
Expand Up @@ -10,6 +10,11 @@ ruleTester.run('no-var-requires', rule, {
"import foo = require('foo');",
"require('foo');",
"require?.('foo');",
`
import { createRequire } from 'module';
const require = createRequire('foo');
const json = require('./some.json');
`,
],
invalid: [
{
Expand Down

0 comments on commit 0040186

Please sign in to comment.