Skip to content

Commit

Permalink
✨ no-unsupported-features/node-builtins recognize backporting
Browse files Browse the repository at this point in the history
Fixes #192
Closes #194
  • Loading branch information
mysticatea committed Nov 24, 2019
1 parent 276a9ad commit 972f828
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 65 deletions.
86 changes: 66 additions & 20 deletions lib/rules/no-unsupported-features/node-builtins.js
Expand Up @@ -23,7 +23,7 @@ const trackMap = {
modules: {
assert: {
strict: {
[READ]: { supported: "9.9.0" },
[READ]: { supported: "9.9.0", backported: ["8.13.0"] },
doesNotReject: { [READ]: { supported: "10.0.0" } },
rejects: { [READ]: { supported: "10.0.0" } },
},
Expand Down Expand Up @@ -51,9 +51,11 @@ const trackMap = {
ChildProcess: { [READ]: { supported: "2.2.0" } },
},
console: {
clear: { [READ]: { supported: "8.3.0" } },
count: { [READ]: { supported: "8.3.0" } },
countReset: { [READ]: { supported: "8.3.0" } },
clear: { [READ]: { supported: "8.3.0", backported: ["6.13.0"] } },
count: { [READ]: { supported: "8.3.0", backported: ["6.13.0"] } },
countReset: {
[READ]: { supported: "8.3.0", backported: ["6.13.0"] },
},
debug: { [READ]: { supported: "8.0.0" } },
dirxml: { [READ]: { supported: "8.0.0" } },
group: { [READ]: { supported: "8.5.0" } },
Expand All @@ -74,6 +76,7 @@ const trackMap = {
exportPublicKey: { [READ]: { supported: "9.0.0" } },
verifySpkac: { [READ]: { supported: "9.0.0" } },
},
ECDH: { [READ]: { supported: "8.8.0", backported: ["6.13.0"] } },
KeyObject: { [READ]: { supported: "11.6.0" } },
createPrivateKey: { [READ]: { supported: "11.6.0" } },
createPublicKey: { [READ]: { supported: "11.6.0" } },
Expand All @@ -86,8 +89,12 @@ const trackMap = {
getFips: { [READ]: { supported: "10.0.0" } },
privateEncrypt: { [READ]: { supported: "1.1.0" } },
publicDecrypt: { [READ]: { supported: "1.1.0" } },
randomFillSync: { [READ]: { supported: "7.10.0" } },
randomFill: { [READ]: { supported: "7.10.0" } },
randomFillSync: {
[READ]: { supported: "7.10.0", backported: ["6.13.0"] },
},
randomFill: {
[READ]: { supported: "7.10.0", backported: ["6.13.0"] },
},
scrypt: { [READ]: { supported: "10.5.0" } },
scryptSync: { [READ]: { supported: "10.5.0" } },
setFips: { [READ]: { supported: "10.0.0" } },
Expand All @@ -99,14 +106,20 @@ const trackMap = {
Resolver: { [READ]: { supported: "8.3.0" } },
resolvePtr: { [READ]: { supported: "6.0.0" } },
promises: {
[READ]: { supported: "11.14.0", experimental: "10.6.0" },
[READ]: {
supported: "11.14.0",
backported: ["10.17.0"],
experimental: "10.6.0",
},
},
},
events: {
EventEmitter: {
once: { [READ]: { supported: "11.13.0" } },
once: {
[READ]: { supported: "11.13.0", backported: ["10.16.0"] },
},
},
once: { [READ]: { supported: "11.13.0" } },
once: { [READ]: { supported: "11.13.0", backported: ["10.16.0"] } },
},
fs: {
Dirent: { [READ]: { supported: "10.10.0" } },
Expand All @@ -121,25 +134,43 @@ const trackMap = {
native: { [READ]: { supported: "9.2.0" } },
},
promises: {
[READ]: { supported: "11.14.0", experimental: "10.1.0" },
[READ]: {
supported: "11.14.0",
backported: ["10.17.0"],
experimental: "10.1.0",
},
},
writev: { [READ]: { supported: "12.9.0" } },
writevSync: { [READ]: { supported: "12.9.0" } },
},
http2: {
[READ]: { supported: "10.10.0", experimental: "8.4.0" },
[READ]: {
supported: "10.10.0",
backported: ["8.13.0"],
experimental: "8.4.0",
},
},
inspector: {
[READ]: { supported: null, experimental: "8.0.0" },
},
module: {
Module: {
builtinModules: { [READ]: { supported: "9.3.0" } },
builtinModules: {
[READ]: {
supported: "9.3.0",
backported: ["6.13.0", "8.10.0"],
},
},
createRequireFromPath: { [READ]: { supported: "10.12.0" } },
createRequire: { [READ]: { supported: "12.2.0" } },
syncBuiltinESMExports: { [READ]: { supported: "12.12.0" } },
},
builtinModules: { [READ]: { supported: "9.3.0" } },
builtinModules: {
[READ]: {
supported: "9.3.0",
backported: ["6.13.0", "8.10.0"],
},
},
createRequireFromPath: { [READ]: { supported: "10.12.0" } },
createRequire: { [READ]: { supported: "12.2.0" } },
syncBuiltinESMExports: { [READ]: { supported: "12.12.0" } },
Expand Down Expand Up @@ -175,7 +206,12 @@ const trackMap = {
hrtime: {
bigint: { [READ]: { supported: "10.7.0" } },
},
ppid: { [READ]: { supported: "9.2.0" } },
ppid: {
[READ]: {
supported: "9.2.0",
backported: ["6.13.0", "8.10.0"],
},
},
release: { [READ]: { supported: "3.0.0" } },
report: { [READ]: { supported: null, experimental: "11.8.0" } },
resourceUsage: { [READ]: { supported: "12.6.0" } },
Expand All @@ -195,7 +231,9 @@ const trackMap = {
},
stream: {
Readable: {
from: { [READ]: { supported: "12.3.0" } },
from: {
[READ]: { supported: "12.3.0", backported: ["10.17.0"] },
},
},
finished: { [READ]: { supported: "10.0.0" } },
pipeline: { [READ]: { supported: "10.0.0" } },
Expand All @@ -204,24 +242,32 @@ const trackMap = {
[READ]: { supported: "10.0.0" },
},
url: {
URL: { [READ]: { supported: "7.0.0" } },
URLSearchParams: { [READ]: { supported: "7.5.0" } },
URL: { [READ]: { supported: "7.0.0", backported: ["6.13.0"] } },
URLSearchParams: {
[READ]: { supported: "7.5.0", backported: ["6.13.0"] },
},
domainToASCII: { [READ]: { supported: "7.4.0" } },
domainToUnicode: { [READ]: { supported: "7.4.0" } },
},
util: {
callbackify: { [READ]: { supported: "8.2.0" } },
formatWithOptions: { [READ]: { supported: "10.0.0" } },
getSystemErrorName: { [READ]: { supported: "9.7.0" } },
getSystemErrorName: {
[READ]: { supported: "9.7.0", backported: ["8.12.0"] },
},
inspect: {
custom: { [READ]: { supported: "6.6.0" } },
defaultOptions: { [READ]: { supported: "6.4.0" } },
replDefaults: { [READ]: { supported: "11.12.0" } },
},
isDeepStrictEqual: { [READ]: { supported: "9.0.0" } },
promisify: { [READ]: { supported: "8.0.0" } },
TextDecoder: { [READ]: { supported: "8.3.0" } },
TextEncoder: { [READ]: { supported: "8.3.0" } },
TextDecoder: {
[READ]: { supported: "8.9.0", experimental: "8.3.0" },
},
TextEncoder: {
[READ]: { supported: "8.9.0", experimental: "8.3.0" },
},
types: {
[READ]: { supported: "10.0.0" },
isBoxedPrimitive: { [READ]: { supported: "10.11.0" } },
Expand Down
59 changes: 54 additions & 5 deletions lib/util/check-unsupported-builtins.js
Expand Up @@ -4,11 +4,18 @@
*/
"use strict"

const { Range } = require("semver") //eslint-disable-line no-unused-vars
const { Range, lt, major } = require("semver") //eslint-disable-line no-unused-vars
const { ReferenceTracker } = require("eslint-utils")
const getConfiguredNodeVersion = require("./get-configured-node-version")
const getSemverRange = require("./get-semver-range")

/**
* @typedef {Object} SupportInfo
* @property {string | null} supported The stably supported version. If `null` is present, it hasn't been supported yet.
* @property {string[]} [backported] The backported versions.
* @property {string} [experimental] The added version as experimental.
*/

/**
* Parses the options.
* @param {RuleContext} context The rule context.
Expand All @@ -23,6 +30,50 @@ function parseOptions(context) {
return Object.freeze({ version, ignores })
}

/**
* Check if it has been supported.
* @param {SupportInfo} info The support info.
* @param {Range} configured The configured version range.
*/
function isSupported({ backported, supported }, configured) {
if (
backported &&
backported.length >= 2 &&
!backported.every((v, i) => i === 0 || lt(backported[i - 1], v))
) {
throw new Error("Invalid BackportConfiguration")
}

if (supported == null) {
return false
}
if (backported == null || backported.length === 0) {
return !configured.intersects(getSemverRange(`<${supported}`))
}

return !configured.intersects(
getSemverRange(
[...backported, supported]
.map((v, i) => (i === 0 ? `<${v}` : `>=${major(v)}.0.0 <${v}`))
.join(" || ")
)
)
}

/**
* Get the formatted text of a given supported version.
* @param {SupportInfo} info The support info.
*/
function supportedVersionToString({ backported, supported }) {
if (supported == null) {
return "(none yet)"
}
if (backported == null || backported.length === 0) {
return supported
}
return `${supported} (backported: ^${backported.join(", ^")})`
}

/**
* Verify the code to report unsupported APIs.
* @param {RuleContext} context The rule context.
Expand All @@ -40,17 +91,15 @@ module.exports = function checkUnsupportedBuiltins(context, trackMap) {

for (const { node, path, info } of references) {
const name = path.join(".")
const supported =
Boolean(info.supported) &&
!options.version.intersects(getSemverRange(`<${info.supported}`))
const supported = isSupported(info, options.version)

if (!supported && !options.ignores.has(name)) {
context.report({
node,
messageId: "unsupported",
data: {
name,
supported: info.supported || "???",
supported: supportedVersionToString(info),
version: options.version.raw,
},
})
Expand Down

0 comments on commit 972f828

Please sign in to comment.