Skip to content

Commit

Permalink
fix(eslint-plugin): [no-var-requires] report problems within `NewExpr…
Browse files Browse the repository at this point in the history
…ession` (#3884)
  • Loading branch information
rafaelss95 committed Sep 20, 2021
1 parent 1861356 commit ed5e459
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/eslint-plugin/src/rules/no-var-requires.ts
Expand Up @@ -25,20 +25,24 @@ export default util.createRule<Options, MessageIds>({
defaultOptions: [],
create(context) {
return {
CallExpression(node: TSESTree.CallExpression): void {
'CallExpression[callee.name="require"]'(
node: TSESTree.CallExpression,
): void {
const parent =
node.parent?.type === AST_NODE_TYPES.ChainExpression
? node.parent.parent
: node.parent;

if (
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'require' &&
parent &&
(parent.type === AST_NODE_TYPES.VariableDeclarator ||
parent.type === AST_NODE_TYPES.CallExpression ||
parent.type === AST_NODE_TYPES.TSAsExpression ||
parent.type === AST_NODE_TYPES.TSTypeAssertion ||
parent.type === AST_NODE_TYPES.MemberExpression)
[
AST_NODE_TYPES.CallExpression,
AST_NODE_TYPES.MemberExpression,
AST_NODE_TYPES.NewExpression,
AST_NODE_TYPES.TSAsExpression,
AST_NODE_TYPES.TSTypeAssertion,
AST_NODE_TYPES.VariableDeclarator,
].includes(parent.type)
) {
context.report({
node,
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin/tests/rules/no-var-requires.test.ts
Expand Up @@ -132,5 +132,24 @@ ruleTester.run('no-var-requires', rule, {
},
],
},
{
// https://github.com/typescript-eslint/typescript-eslint/issues/3883
code: `
const configValidator = new Validator(require('./a.json'));
configValidator.addSchema(require('./a.json'));
`,
errors: [
{
messageId: 'noVarReqs',
line: 2,
column: 39,
},
{
messageId: 'noVarReqs',
line: 3,
column: 27,
},
],
},
],
});

0 comments on commit ed5e459

Please sign in to comment.