Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-require-imports] report only global require #3871

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/eslint-plugin/src/rules/no-require-imports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { 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 @@ -18,13 +18,19 @@ export default util.createRule({
defaultOptions: [],
create(context) {
return {
'CallExpression > Identifier[name="require"]'(
node: TSESTree.Identifier,
'CallExpression[callee.name="require"]'(
node: TSESTree.CallExpression,
): void {
context.report({
node: node.parent!,
messageId: 'noRequireImports',
});
const variable = ASTUtils.findVariable(context.getScope(), 'require');

// ignore non-global require usage as it's something user-land custom instead
// of the commonjs standard
if (!variable?.identifiers.length) {
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
context.report({
node,
messageId: 'noRequireImports',
});
}
},
TSExternalModuleReference(node): void {
context.report({
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin/tests/rules/no-require-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ ruleTester.run('no-require-imports', rule, {
'import lib9 = lib2.anotherSubImport;',
"import lib10 from 'lib10';",
"var lib3 = load?.('not_an_import');",
`
import { createRequire } from 'module';
const require = createRequire();
require('remark-preset-prettier');
`,
],
invalid: [
{
Expand Down