Skip to content

Commit 9022cf2

Browse files
committed
💥 Allow dynamic import for Node.js >=12.17 <13 || >=13.2
* Extend case.supported to support explicit Range instances * Update error string * Update tests to check around those constraints
1 parent cd97880 commit 9022cf2

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/.vscode
2+
/.idea
13
/.nyc_output
24
/coverage
35
/npm-debug.log

‎lib/rules/no-unsupported-features/es-syntax.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
const { rules: esRules } = require("eslint-plugin-es")
88
const { getInnermostScope } = require("eslint-utils")
9-
const { Range } = require("semver") //eslint-disable-line no-unused-vars
9+
const { Range } = require("semver")
1010
const getConfiguredNodeVersion = require("../../util/get-configured-node-version")
1111
const getSemverRange = require("../../util/get-semver-range")
1212
const mergeVisitorsInPlace = require("../../util/merge-visitors-in-place")
@@ -378,7 +378,7 @@ const features = {
378378
ruleId: "no-dynamic-import",
379379
cases: [
380380
{
381-
supported: null,
381+
supported: new Range(">=12.17 <13 || >=13.2"),
382382
messageId: "no-dynamic-import",
383383
},
384384
],
@@ -435,10 +435,15 @@ function defineVisitor(context, options) {
435435
* @returns {boolean} `true` if it's supporting.
436436
*/
437437
function isNotSupportingVersion(aCase) {
438-
return (
439-
!aCase.supported ||
440-
options.version.intersects(getSemverRange(`<${aCase.supported}`))
441-
)
438+
if (!aCase.supported) {
439+
return true
440+
}
441+
442+
if (aCase.supported instanceof Range) {
443+
return !options.version.intersects(aCase.supported)
444+
}
445+
446+
return options.version.intersects(getSemverRange(`<${aCase.supported}`))
442447
}
443448

444449
/**
@@ -628,7 +633,7 @@ module.exports = {
628633
"no-bigint-property-names":
629634
"Bigint literal property names are not supported yet.",
630635
"no-dynamic-import":
631-
"'import()' expressions are not supported yet.",
636+
"'import()' expressions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
632637
},
633638
},
634639
create(context) {

‎tests/lib/rules/no-unsupported-features/es-syntax.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const path = require("path")
88
const { Linter, RuleTester } = require("eslint")
99
const { builtin } = require("globals")
10+
const { Range } = require("semver")
1011
const rule = require("../../../../lib/rules/no-unsupported-features/es-syntax")
1112

1213
const ES2020Supported = (() => {
@@ -2489,27 +2490,27 @@ ruleTester.run(
24892490
code: "obj.import(source)",
24902491
options: [{ version: "12.0.0" }],
24912492
},
2492-
{
2493+
...["12.17.0", "13.2.0"].map(v => ({
24932494
code: "import(source)",
2494-
options: [
2495-
{ version: "13.1.0", ignores: ["dynamicImport"] },
2496-
],
2497-
},
2495+
options: [{ version: v }],
2496+
})),
24982497
],
24992498
invalid: [
2500-
{
2499+
...["12.16.0", "13.0.0", "13.1.0"].map(v => ({
25012500
code: "import(source)",
2502-
options: [{ version: "13.3.0" }],
2501+
options: [{ version: v }],
25032502
errors: [
25042503
{
25052504
messageId: "no-dynamic-import",
25062505
data: {
2507-
supported: null,
2508-
version: "13.3.0",
2506+
supported: new Range(
2507+
">=12.17 <13 || >=13.2"
2508+
).toString(),
2509+
version: v,
25092510
},
25102511
},
25112512
],
2512-
},
2513+
})),
25132514
],
25142515
},
25152516

0 commit comments

Comments
 (0)