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

feat: Add support for ignoring experemental features #269

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rules/no-unsupported-features/node-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
type: "object",
properties: {
version: getConfiguredNodeVersion.schema,
allowExperimental: { type: "boolean" },
ignores: {
type: "array",
items: {
Expand Down
34 changes: 21 additions & 13 deletions lib/util/check-unsupported-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,43 @@ const semverRangeSubset = require("semver/ranges/subset")
* Parses the options.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @returns {Readonly<{
* version: import('semver').Range,
* ignores: Set<string>
* version: import('semver').Range;
* ignores: Set<string>;
* allowExperimental: boolean;
* }>} Parsed value.
*/
function parseOptions(context) {
const raw = context.options[0] || {}
const version = getConfiguredNodeVersion(context)
const ignores = new Set(raw.ignores || [])
const allowExperimental = raw.allowExperimental ?? false

return Object.freeze({ version, ignores })
return Object.freeze({ version, ignores, allowExperimental })
}

/**
* Check if it has been supported.
* @param {import('../unsupported-features/types.js').SupportInfo} info The support info.
* @param {import('semver').Range} configured The configured version range.
* @param {string[] | undefined} featureRange The target features supported range
* @param {import('semver').Range} requestedRange The configured version range.
* @returns {boolean}
*/
function isSupported({ supported }, configured) {
if (supported == null || supported.length === 0) {
function isInRange(featureRange, requestedRange) {
if (featureRange == null || featureRange.length === 0) {
return false
}

const [latest] = rsort(supported)
const [latest] = rsort(featureRange)
const range = getSemverRange(
[...supported.map(version => `^${version}`), `>= ${latest}`].join("||")
[...featureRange.map(version => `^${version}`), `>= ${latest}`].join(
"||"
)
)

if (range == null) {
return false
}

return semverRangeSubset(configured, range)
return semverRangeSubset(requestedRange, range)
}

/**
Expand Down Expand Up @@ -92,11 +97,14 @@ module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins(

for (const { node, path, info } of references) {
const name = unprefixNodeColon(path.join("."))
const supported = isSupported(info, options.version)

if (supported === true || options.ignores.has(name)) {
if (
isInRange(info.supported, options.version) ||
options.ignores.has(name) ||
(options.allowExperimental === true && info.experimental != null)
scagood marked this conversation as resolved.
Show resolved Hide resolved
) {
continue
}

const supportedVersion = supportedVersionToString(info)
context.report({
node,
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/rules/no-unsupported-features/node-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5371,5 +5371,30 @@ new RuleTester({ languageOptions: { sourceType: "module" } }).run(
},
],
},

{
valid: [
{
code: "fetch('/asd')",
options: [{ version: "16.0.0", allowExperimental: true }],
},
],
invalid: [
{
code: "fetch('/asd')",
options: [{ version: "16.0.0", allowExperimental: false }],
errors: [
{
messageId: "not-supported-till",
data: {
name: "fetch",
supported: "21.0.0",
version: "16.0.0",
},
},
],
},
],
},
])
)