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

[New] no-dynamic-require: support dynamic import with espree #2371

Merged
merged 2 commits into from Jan 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Added
- [`no-named-default`, `no-default-export`, `prefer-default-export`, `no-named-export`, `export`, `named`, `namespace`, `no-unused-modules`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
- [`no-dynamic-require`]: support dynamic import with espree ([#2371], thanks [@sosukesuzuki])

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
Expand Down Expand Up @@ -963,6 +964,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#2371]: https://github.com/import-js/eslint-plugin-import/pull/2371
[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367
[#2332]: https://github.com/import-js/eslint-plugin-import/pull/2332
[#2358]: https://github.com/import-js/eslint-plugin-import/pull/2358
Expand Down
13 changes: 12 additions & 1 deletion src/rules/no-dynamic-require.js
Expand Up @@ -19,6 +19,8 @@ function isStaticValue(arg) {
(arg.type === 'TemplateLiteral' && arg.expressions.length === 0);
}

const dynamicImportErrorMessage = 'Calls to import() should use string literals';

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -55,10 +57,19 @@ module.exports = {
if (options.esmodule && isDynamicImport(node)) {
return context.report({
node,
message: 'Calls to import() should use string literals',
message: dynamicImportErrorMessage,
});
}
},
ImportExpression(node) {
if (!options.esmodule || isStaticValue(node.source)) {
return;
}
return context.report({
node,
message: dynamicImportErrorMessage,
});
},
};
},
};
206 changes: 132 additions & 74 deletions tests/src/rules/no-dynamic-require.js
@@ -1,6 +1,7 @@
import { parsers, test } from '../utils';
import { parsers, test, testVersion } from '../utils';

import { RuleTester } from 'eslint';
import flatMap from 'array.prototype.flatmap';

const ruleTester = new RuleTester();
const rule = require('rules/no-dynamic-require');
Expand Down Expand Up @@ -28,56 +29,93 @@ ruleTester.run('no-dynamic-require', rule, {
test({ code: 'var foo = require("@scope/foo")' }),

//dynamic import
test({
code: 'import("foo")',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'import(`foo`)',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'import("./foo")',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'import("@scope/foo")',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'var foo = import("foo")',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'var foo = import(`foo`)',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'var foo = import("./foo")',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'var foo = import("@scope/foo")',
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'import("../" + name)',
errors: [dynamicImportError],
parser: parsers.BABEL_OLD,
options: [{ esmodule: false }],
}),
test({
code: 'import(`../${name}`)',
errors: [dynamicImportError],
parser: parsers.BABEL_OLD,
...flatMap([parsers.ESPREE, parsers.BABEL_OLD], (parser) => {
const _test =
parser === parsers.ESPREE
? (testObj) => testVersion('>= 6.2.0', () => testObj)
: (testObj) => test(testObj);
return [].concat(
_test({
code: 'import("foo")',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import(`foo`)',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import("./foo")',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import("@scope/foo")',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'var foo = import("foo")',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'var foo = import(`foo`)',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'var foo = import("./foo")',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'var foo = import("@scope/foo")',
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import("../" + name)',
errors: [dynamicImportError],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import(`../${name}`)',
errors: [dynamicImportError],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
);
}),
],
invalid: [
Expand All @@ -104,29 +142,49 @@ ruleTester.run('no-dynamic-require', rule, {
}),

// dynamic import
test({
code: 'import("../" + name)',
errors: [dynamicImportError],
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'import(`../${name}`)',
errors: [dynamicImportError],
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'import(name)',
errors: [dynamicImportError],
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
}),
test({
code: 'import(name())',
errors: [dynamicImportError],
parser: parsers.BABEL_OLD,
options: [{ esmodule: true }],
...flatMap([parsers.ESPREE, parsers.BABEL_OLD], (parser) => {
const _test =
parser === parsers.ESPREE
? (testObj) => testVersion('>= 6.2.0', () => testObj)
: (testObj) => test(testObj);
return [].concat(
_test({
code: 'import("../" + name)',
errors: [dynamicImportError],
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import(`../${name}`)',
errors: [dynamicImportError],
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import(name)',
errors: [dynamicImportError],
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
_test({
code: 'import(name())',
errors: [dynamicImportError],
options: [{ esmodule: true }],
parser,
parserOptions: {
ecmaVersion: 2020,
},
}),
);
}),
test({
code: 'require(`foo${x}`)',
Expand Down
1 change: 1 addition & 0 deletions tests/src/utils.js
Expand Up @@ -6,6 +6,7 @@ import semver from 'semver';
import 'babel-eslint';

export const parsers = {
ESPREE: require.resolve('espree'),
TS_OLD: semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0') && require.resolve('typescript-eslint-parser'),
TS_NEW: semver.satisfies(eslintPkg.version, '>5.0.0') && require.resolve('@typescript-eslint/parser'),
BABEL_OLD: require.resolve('babel-eslint'),
Expand Down