Skip to content

Commit 1e192b1

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 b814735 commit 1e192b1

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
/**
@@ -645,7 +650,7 @@ module.exports = {
645650
"no-bigint-property-names":
646651
"Bigint literal property names are not supported yet.",
647652
"no-dynamic-import":
648-
"'import()' expressions are not supported yet.",
653+
"'import()' expressions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
649654
"no-optional-chaining":
650655
"Optional chainings are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
651656
"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 = (() => {
@@ -2486,27 +2487,27 @@ ruleTester.run(
24862487
code: "obj.import(source)",
24872488
options: [{ version: "12.0.0" }],
24882489
},
2489-
{
2490+
...["12.17.0", "13.2.0"].map(v => ({
24902491
code: "import(source)",
2491-
options: [
2492-
{ version: "13.1.0", ignores: ["dynamicImport"] },
2493-
],
2494-
},
2492+
options: [{ version: v }],
2493+
})),
24952494
],
24962495
invalid: [
2497-
{
2496+
...["12.16.0", "13.0.0", "13.1.0"].map(v => ({
24982497
code: "import(source)",
2499-
options: [{ version: "13.3.0" }],
2498+
options: [{ version: v }],
25002499
errors: [
25012500
{
25022501
messageId: "no-dynamic-import",
25032502
data: {
2504-
supported: null,
2505-
version: "13.3.0",
2503+
supported: new Range(
2504+
">=12.17 <13 || >=13.2"
2505+
).toString(),
2506+
version: v,
25062507
},
25072508
},
25082509
],
2509-
},
2510+
})),
25102511
],
25112512
},
25122513
{

0 commit comments

Comments
 (0)