Skip to content

Commit 1480a84

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 f45c614 commit 1480a84

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

‎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
],
@@ -453,10 +453,15 @@ function defineVisitor(context, options) {
453453
* @returns {boolean} `true` if it's supporting.
454454
*/
455455
function isNotSupportingVersion(aCase) {
456-
return (
457-
!aCase.supported ||
458-
options.version.intersects(getSemverRange(`<${aCase.supported}`))
459-
)
456+
if (!aCase.supported) {
457+
return true
458+
}
459+
460+
if (aCase.supported instanceof Range) {
461+
return !options.version.intersects(aCase.supported)
462+
}
463+
464+
return options.version.intersects(getSemverRange(`<${aCase.supported}`))
460465
}
461466

462467
/**
@@ -646,7 +651,7 @@ module.exports = {
646651
"no-bigint-property-names":
647652
"Bigint literal property names are not supported yet.",
648653
"no-dynamic-import":
649-
"'import()' expressions are not supported yet.",
654+
"'import()' expressions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
650655
"no-optional-chaining":
651656
"Optional chainings are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
652657
"no-nullish-coalescing-operators":

‎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)