-
-
Notifications
You must be signed in to change notification settings - Fork 173
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
Allow dynamic import for Node.js >=12.17 <13 || >=13.2 #256
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
|
||
const { rules: esRules } = require("eslint-plugin-es") | ||
const { getInnermostScope } = require("eslint-utils") | ||
const { Range } = require("semver") //eslint-disable-line no-unused-vars | ||
const { Range } = require("semver") | ||
const getConfiguredNodeVersion = require("../../util/get-configured-node-version") | ||
const getSemverRange = require("../../util/get-semver-range") | ||
const mergeVisitorsInPlace = require("../../util/merge-visitors-in-place") | ||
|
@@ -378,7 +378,7 @@ const features = { | |
ruleId: "no-dynamic-import", | ||
cases: [ | ||
{ | ||
supported: null, | ||
supported: new Range(">=12.17 <13 || >=13.2"), | ||
messageId: "no-dynamic-import", | ||
}, | ||
], | ||
|
@@ -453,10 +453,15 @@ function defineVisitor(context, options) { | |
* @returns {boolean} `true` if it's supporting. | ||
*/ | ||
function isNotSupportingVersion(aCase) { | ||
return ( | ||
!aCase.supported || | ||
options.version.intersects(getSemverRange(`<${aCase.supported}`)) | ||
) | ||
if (!aCase.supported) { | ||
return true | ||
} | ||
|
||
if (aCase.supported instanceof Range) { | ||
return !options.version.intersects(aCase.supported) | ||
} | ||
|
||
return options.version.intersects(getSemverRange(`<${aCase.supported}`)) | ||
Comment on lines
+456
to
+464
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be written as a single Boolean expression, but I figured expanding it was a bit more clear. |
||
} | ||
|
||
/** | ||
|
@@ -646,7 +651,7 @@ module.exports = { | |
"no-bigint-property-names": | ||
"Bigint literal property names are not supported yet.", | ||
"no-dynamic-import": | ||
"'import()' expressions are not supported yet.", | ||
"'import()' expressions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.", | ||
"no-optional-chaining": | ||
"Optional chainings are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.", | ||
"no-nullish-coalescing-operators": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
const path = require("path") | ||
const { Linter, RuleTester } = require("eslint") | ||
const { builtin } = require("globals") | ||
const { Range } = require("semver") | ||
const rule = require("../../../../lib/rules/no-unsupported-features/es-syntax") | ||
|
||
const ES2020Supported = (() => { | ||
|
@@ -2489,27 +2490,27 @@ ruleTester.run( | |
code: "obj.import(source)", | ||
options: [{ version: "12.0.0" }], | ||
}, | ||
{ | ||
...["12.17.0", "13.2.0"].map(v => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In both cases, I did this spread-from-map approach to keep the object DRY, but we could also just duplicate the |
||
code: "import(source)", | ||
options: [ | ||
{ version: "13.1.0", ignores: ["dynamicImport"] }, | ||
], | ||
}, | ||
options: [{ version: v }], | ||
})), | ||
], | ||
invalid: [ | ||
{ | ||
...["12.16.0", "13.0.0", "13.1.0"].map(v => ({ | ||
code: "import(source)", | ||
options: [{ version: "13.3.0" }], | ||
options: [{ version: v }], | ||
errors: [ | ||
{ | ||
messageId: "no-dynamic-import", | ||
data: { | ||
supported: null, | ||
version: "13.3.0", | ||
supported: new Range( | ||
">=12.17 <13 || >=13.2" | ||
).toString(), | ||
Comment on lines
+2506
to
+2508
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured it was better to configure this with whatever |
||
version: v, | ||
}, | ||
}, | ||
], | ||
}, | ||
})), | ||
], | ||
}, | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought through several ways of accomplishing this, and landed on allowing
case.supported
to be aRange
instance. Here are my reasons:getSemverRange
function is called fromisNotSupportingVersion
while automatically prepending a<
character, to make it a negative assertion to match theNot
in the function name, so I'd have to mess with that behavior. I can't changegetSemverRange
because it's nicely memoized and used all over the place in different ways.supported
allow arrays, but then exactly how to combine the array values wasn't clear.Range
and we'll use that."