Skip to content

Commit

Permalink
[New] no-commonjs: Allow expressionless template literals
Browse files Browse the repository at this point in the history
  • Loading branch information
FloEdelmann authored and ljharb committed Dec 1, 2020
1 parent 802ce7d commit 4c82ad0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

### Added
- [`no-commonjs`]: Also detect require calls with expressionless template literals: ``` require(`x`) ``` ([#1958], thanks [@FloEdelmann])

### Fixed
- [`export`]/TypeScript: properly detect export specifiers as children of a TS module block ([#1889], thanks [@andreubotella])
- [`order`]: ignore non-module-level requires ([#1940], thanks [@golopot])
Expand Down Expand Up @@ -752,6 +755,7 @@ for info on changes for earlier releases.
[#1944]: https://github.com/benmosher/eslint-plugin-import/pull/1944
[#1924]: https://github.com/benmosher/eslint-plugin-import/issues/1924
[#1965]: https://github.com/benmosher/eslint-plugin-import/issues/1965
[#1958]: https://github.com/benmosher/eslint-plugin-import/pull/1958
[#1948]: https://github.com/benmosher/eslint-plugin-import/pull/1948
[#1947]: https://github.com/benmosher/eslint-plugin-import/pull/1947
[#1940]: https://github.com/benmosher/eslint-plugin-import/pull/1940
Expand Down Expand Up @@ -1314,3 +1318,4 @@ for info on changes for earlier releases.
[@fsmaia]: https://github.com/fsmaia
[@MatthiasKunnen]: https://github.com/MatthiasKunnen
[@paztis]: https://github.com/paztis
[@FloEdelmann]: https://github.com/FloEdelmann
10 changes: 6 additions & 4 deletions src/rules/no-commonjs.js
Expand Up @@ -45,6 +45,11 @@ function isConditional(node) {
return false;
}

function isLiteralString(node) {
return (node.type === 'Literal' && typeof node.value === 'string') ||
(node.type === 'TemplateLiteral' && node.expressions.length === 0);
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -114,10 +119,7 @@ module.exports = {
if (call.callee.name !== 'require') return;

if (call.arguments.length !== 1) return;
const module = call.arguments[0];

if (module.type !== 'Literal') return;
if (typeof module.value !== 'string') return;
if (!isLiteralString(call.arguments[0])) return;

if (allowRequire(call, options)) return;

Expand Down
6 changes: 6 additions & 0 deletions tests/src/rules/no-commonjs.js
Expand Up @@ -37,6 +37,7 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
{ code: "var bar = require('./bar', true);" },
{ code: "var bar = proxyquire('./bar');" },
{ code: "var bar = require('./ba' + 'r');" },
{ code: 'var bar = require(`x${1}`);', parserOptions: { ecmaVersion: 2015 } },
{ code: 'var zero = require(0);' },
{ code: 'require("x")', options: [{ allowRequire: true }] },

Expand Down Expand Up @@ -71,6 +72,11 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
{ code: 'var x = require("x")', output: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'x = require("x")', output: 'x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require("x")', output: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require(`x`)',
parserOptions: { ecmaVersion: 2015 },
output: 'require(`x`)',
errors: [ { message: IMPORT_MESSAGE }],
},

{ code: 'if (typeof window !== "undefined") require("x")',
options: [{ allowConditionalRequire: false }],
Expand Down
8 changes: 8 additions & 0 deletions tests/src/rules/no-dynamic-require.js
Expand Up @@ -44,5 +44,13 @@ ruleTester.run('no-dynamic-require', rule, {
code: 'require(name + "foo", "bar")',
errors: [error],
}),
test({
code: 'require(`foo${x}`)',
errors: [error],
}),
test({
code: 'var foo = require(`foo${x}`)',
errors: [error],
}),
],
});

0 comments on commit 4c82ad0

Please sign in to comment.