diff --git a/packages/eslint-plugin/src/rules/no-require-imports.ts b/packages/eslint-plugin/src/rules/no-require-imports.ts index 9b83ac66aeca..1956694484f4 100644 --- a/packages/eslint-plugin/src/rules/no-require-imports.ts +++ b/packages/eslint-plugin/src/rules/no-require-imports.ts @@ -42,16 +42,23 @@ export default util.createRule({ function isImportPathAllowed(importPath: string): boolean { return allowPatterns.some(pattern => importPath.match(pattern)); } + function isStringOrTemplateLiteral(node: TSESTree.Node): boolean { + return ( + (node.type === AST_NODE_TYPES.Literal && + typeof node.value === 'string') || + node.type === AST_NODE_TYPES.TemplateLiteral + ); + } + return { 'CallExpression[callee.name="require"]'( node: TSESTree.CallExpression, ): void { - if ( - node.arguments[0]?.type === AST_NODE_TYPES.Literal && - typeof node.arguments[0].value === 'string' && - isImportPathAllowed(node.arguments[0].value) - ) { - return; + if (node.arguments[0] && isStringOrTemplateLiteral(node.arguments[0])) { + const argValue = util.getStaticStringValue(node.arguments[0]); + if (typeof argValue === 'string' && isImportPathAllowed(argValue)) { + return; + } } const variable = ASTUtils.findVariable( context.sourceCode.getScope(node), @@ -68,12 +75,11 @@ export default util.createRule({ } }, TSExternalModuleReference(node): void { - if ( - node.expression.type === AST_NODE_TYPES.Literal && - typeof node.expression.value === 'string' && - isImportPathAllowed(node.expression.value) - ) { - return; + if (isStringOrTemplateLiteral(node.expression)) { + const argValue = util.getStaticStringValue(node.expression); + if (typeof argValue === 'string' && isImportPathAllowed(argValue)) { + return; + } } context.report({ node, diff --git a/packages/eslint-plugin/src/rules/no-var-requires.ts b/packages/eslint-plugin/src/rules/no-var-requires.ts index 30838ed7b99d..9bb9fb7c1921 100644 --- a/packages/eslint-plugin/src/rules/no-var-requires.ts +++ b/packages/eslint-plugin/src/rules/no-var-requires.ts @@ -1,7 +1,7 @@ import type { TSESTree } from '@typescript-eslint/utils'; import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils'; -import { createRule } from '../util'; +import { createRule, getStaticStringValue } from '../util'; type Options = [ { @@ -43,16 +43,24 @@ export default createRule({ function isImportPathAllowed(importPath: string): boolean { return allowPatterns.some(pattern => importPath.match(pattern)); } + + function isStringOrTemplateLiteral(node: TSESTree.Node): boolean { + return ( + (node.type === AST_NODE_TYPES.Literal && + typeof node.value === 'string') || + node.type === AST_NODE_TYPES.TemplateLiteral + ); + } + return { 'CallExpression[callee.name="require"]'( node: TSESTree.CallExpression, ): void { - if ( - node.arguments[0]?.type === AST_NODE_TYPES.Literal && - typeof node.arguments[0].value === 'string' && - isImportPathAllowed(node.arguments[0].value) - ) { - return; + if (node.arguments[0] && isStringOrTemplateLiteral(node.arguments[0])) { + const argValue = getStaticStringValue(node.arguments[0]); + if (typeof argValue === 'string' && isImportPathAllowed(argValue)) { + return; + } } const parent = node.parent.type === AST_NODE_TYPES.ChainExpression diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index 42374ccffbf8..bdfddbc25ae0 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -31,6 +31,10 @@ require('remark-preset-prettier'); code: "const pkg = require('../package.json');", options: [{ allow: ['/package\\.json$'] }], }, + { + code: 'const pkg = require(`./package.json`);', + options: [{ allow: ['/package\\.json$'] }], + }, { code: "const pkg = require('../packages/package.json');", options: [{ allow: ['/package\\.json$'] }], @@ -47,6 +51,10 @@ require('remark-preset-prettier'); code: "import pkg = require('some-package');", options: [{ allow: ['^some-package$'] }], }, + { + code: 'import pkg = require(`some-package`);', + options: [{ allow: ['^some-package$'] }], + }, ], invalid: [ { @@ -156,6 +164,17 @@ var lib5 = require?.('lib5'), }, ], }, + { + code: 'const pkg = require(`./package.jsonc`);', + options: [{ allow: ['/package\\.json$'] }], + errors: [ + { + line: 1, + column: 13, + messageId: 'noRequireImports', + }, + ], + }, { code: "import pkg = require('./package.json');", errors: [ @@ -188,5 +207,16 @@ var lib5 = require?.('lib5'), }, ], }, + { + code: 'import pkg = require(`./package.json`);', + options: [{ allow: ['^some-package$'] }], + errors: [ + { + line: 1, + column: 14, + messageId: 'noRequireImports', + }, + ], + }, ], }); diff --git a/packages/eslint-plugin/tests/rules/no-var-requires.test.ts b/packages/eslint-plugin/tests/rules/no-var-requires.test.ts index 718bcc46db31..2720475b3b8d 100644 --- a/packages/eslint-plugin/tests/rules/no-var-requires.test.ts +++ b/packages/eslint-plugin/tests/rules/no-var-requires.test.ts @@ -36,6 +36,10 @@ const json = require('./some.json'); code: "const pkg = require('some-package');", options: [{ allow: ['^some-package$'] }], }, + { + code: 'const pkg = require(`some-package`);', + options: [{ allow: ['^some-package$'] }], + }, ], invalid: [ { @@ -209,5 +213,16 @@ configValidator.addSchema(require('./a.json')); }, ], }, + { + code: 'const pkg = require(`./package.json`);', + options: [{ allow: ['^some-package$'] }], + errors: [ + { + line: 1, + column: 13, + messageId: 'noVarReqs', + }, + ], + }, ], });