Skip to content

Commit

Permalink
drop Node < 18.12 support in tools
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 9, 2023
1 parent 147e303 commit b4aa4b2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 82 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:
strategy:
matrix:
node:
- 16.13
- 18.12
- 20
runs-on: ubuntu-latest
Expand All @@ -57,7 +56,6 @@ jobs:
- windows-latest
- macos-latest
node:
- 16.13
- 18.12
- 20
runs-on: ${{ matrix.os }}
Expand Down
16 changes: 5 additions & 11 deletions packages/core-js-builder/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
'use strict';
/* eslint-disable no-console -- output */
const { promisify } = require('util');
const fs = require('fs');
// TODO: replace by `fs.promises` after dropping NodeJS < 10 support
const readFile = promisify(fs.readFile);
const unlink = promisify(fs.unlink);
const writeFile = promisify(fs.writeFile);
const { dirname, join } = require('path');
const tmpdir = require('os').tmpdir();
// TODO: replace by `mkdir` with `recursive: true` after dropping NodeJS < 10.12 support
const mkdirp = promisify(require('mkdirp'));
const { promisify } = require('node:util');
const { mkdir, readFile, unlink, writeFile } = require('node:fs/promises');
const { dirname, join } = require('node:path');
const tmpdir = require('node:os').tmpdir();
const webpack = promisify(require('webpack'));
const compat = require('core-js-compat/compat');
const { banner } = require('./config');
Expand Down Expand Up @@ -95,7 +89,7 @@ module.exports = async function ({
}

if (!(filename === null || filename === undefined)) {
await mkdirp(dirname(filename));
await mkdir(dirname(filename), { recursive: true });
await writeFile(filename, script);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"webpack": ">=4.47.0 <5"
},
"engines": {
"node": ">=8.9.0"
"node": "^18.12.0 || >=20.0.0"
}
}
11 changes: 6 additions & 5 deletions packages/core-js-compat/compat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const { compare, filterOutStabilizedProposals, has, intersection } = require('./helpers');
const { compare, filterOutStabilizedProposals, intersection } = require('./helpers');
const data = require('./data');
const entries = require('./entries');
const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
Expand All @@ -8,6 +8,8 @@ const targetsParser = require('./targets-parser');

const actualModules = entries['core-js/actual'];

const { hasOwn } = Object;

function throwInvalidFilter(filter) {
throw new TypeError(`Specified invalid module name or pattern: ${ filter }`);
}
Expand All @@ -19,15 +21,14 @@ function atLeastSomeModules(modules, filter) {

function getModules(filter) {
if (typeof filter == 'string') {
if (has(entries, filter)) return entries[filter];
if (hasOwn(entries, filter)) return entries[filter];
return atLeastSomeModules(allModules.filter(it => it.startsWith(filter)), filter);
} else if (filter instanceof RegExp) return atLeastSomeModules(allModules.filter(it => filter.test(it)), filter);
throwInvalidFilter(filter);
}

function normalizeModules(option) {
// TODO: use `.flatMap` in core-js@4
return new Set(Array.isArray(option) ? [].concat(...option.map(getModules)) : getModules(option));
return new Set(Array.isArray(option) ? option.flatMap(getModules) : getModules(option));
}

function checkModule(name, targets) {
Expand All @@ -41,7 +42,7 @@ function checkModule(name, targets) {
const requirements = data[name];

for (const [engine, version] of targets) {
if (!has(requirements, engine) || compare(version, '<', requirements[engine])) {
if (!hasOwn(requirements, engine) || compare(version, '<', requirements[engine])) {
result.required = true;
result.targets[engine] = version;
}
Expand Down
14 changes: 5 additions & 9 deletions packages/core-js-compat/helpers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';
// eslint-disable-next-line es/no-object-hasown -- safe
const has = Object.hasOwn || Function.call.bind({}.hasOwnProperty);

function semver(input) {
if (input instanceof semver) return input;
// eslint-disable-next-line new-cap -- ok
if (!(this instanceof semver)) return new semver(input);
const match = /(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(input);
const match = /(?<major>\d+)(?:\.(?<minor>\d+))?(?:\.(?<patch>\d+))?/.exec(input);
if (!match) throw new TypeError(`Invalid version: ${ input }`);
const [, $major, $minor, $patch] = match;
this.major = +$major;
this.minor = $minor ? +$minor : 0;
this.patch = $patch ? +$patch : 0;
const { major, minor, patch } = match.groups;
this.major = parseInt(major, 10);
this.minor = minor ? parseInt(minor, 10) : 0;
this.patch = patch ? parseInt(patch, 10) : 0;
}

semver.prototype.toString = function () {
Expand Down Expand Up @@ -54,7 +51,6 @@ function sortObjectByKey(object, fn) {
module.exports = {
compare,
filterOutStabilizedProposals,
has,
intersection,
semver,
sortObjectByKey,
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"types": "index.d.ts",
"dependencies": {
"browserslist": "^4.22.1"
},
"engines": {
"node": "^18.12.0 || >=20.0.0"
}
}
19 changes: 11 additions & 8 deletions packages/core-js-compat/targets-parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';
const browserslist = require('browserslist');
const { compare, has } = require('./helpers');
const { compare } = require('./helpers');
const external = require('./external');

const { entries, hasOwn } = Object;
const { isArray } = Array;

const aliases = new Map([
['and_chr', 'chrome-android'],
['and_ff', 'firefox-android'],
Expand Down Expand Up @@ -40,34 +43,34 @@ const validTargets = new Set([
]);

const toLowerKeys = function (object) {
return Object.entries(object).reduce((accumulator, [key, value]) => {
return entries(object).reduce((accumulator, [key, value]) => {
accumulator[key.toLowerCase()] = value;
return accumulator;
}, {});
};

module.exports = function (targets) {
const { browsers, esmodules, node, ...rest } = (typeof targets != 'object' || Array.isArray(targets))
const { browsers, esmodules, node, ...rest } = (typeof targets != 'object' || isArray(targets))
? { browsers: targets } : toLowerKeys(targets);

const list = Object.entries(rest);
const list = entries(rest);

if (browsers) {
if (typeof browsers == 'string' || Array.isArray(browsers)) {
if (typeof browsers == 'string' || isArray(browsers)) {
list.push(...browserslist(browsers).map(it => it.split(' ')));
} else {
list.push(...Object.entries(browsers));
list.push(...entries(browsers));
}
}
if (esmodules) {
list.push(...Object.entries(external.modules));
list.push(...entries(external.modules));
}
if (node) {
list.push(['node', node === 'current' ? process.versions.node : node]);
}

const normalized = list.map(([engine, version]) => {
if (has(browserslist.aliases, engine)) {
if (hasOwn(browserslist.aliases, engine)) {
engine = browserslist.aliases[engine];
}
if (aliases.has(engine)) {
Expand Down
13 changes: 7 additions & 6 deletions scripts/build-compat/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import external from 'core-js-compat/src/external.mjs';
import mappings from 'core-js-compat/src/mapping.mjs';
import helpers from 'core-js-compat/helpers.js';

const { compare, has, semver, sortObjectByKey } = helpers;
const { compare, semver, sortObjectByKey } = helpers;
const { hasOwn } = Object;

for (const scope of [data, external]) {
for (const [key, module] of Object.entries(scope)) {
Expand All @@ -14,7 +15,7 @@ for (const scope of [data, external]) {
const [engine, targetKey] = mappingKey.split('To')
.map(it => it.replace(/(?<lower>[a-z])(?<upper>[A-Z])/, '$<lower>-$<upper>').toLowerCase());
const version = module[engine];
if (!version || has(module, targetKey)) return;
if (!version || hasOwn(module, targetKey)) return;
const mapping = mappings[mappingKey];
if (typeof mapping == 'function') {
return module[targetKey] = String(mapping(version));
Expand All @@ -31,7 +32,7 @@ for (const scope of [data, external]) {
map('ChromeToDeno');
map('ChromeToNode');
}
if (!has(module, 'edge')) {
if (!hasOwn(module, 'edge')) {
if (ie && !key.includes('immediate')) {
module.edge = '12';
} else if (chrome) {
Expand All @@ -44,11 +45,11 @@ for (const scope of [data, external]) {
map('ChromeToOpera');
map('ChromeToChromeAndroid');
map('ChromeToAndroid');
if (!has(module, 'android') && module['chrome-android']) {
if (!hasOwn(module, 'android') && module['chrome-android']) {
// https://github.com/mdn/browser-compat-data/blob/main/docs/matching-browser-releases/index.md#version-numbers-for-features-in-android-webview
module.android = String(Math.max(module['chrome-android'], 37));
}
if (!has(module, 'opera-android') && module.opera <= 42) {
if (!hasOwn(module, 'opera-android') && module.opera <= 42) {
module['opera-android'] = module.opera;
} else {
map('ChromeAndroidToOperaAndroid');
Expand All @@ -60,7 +61,7 @@ for (const scope of [data, external]) {
}
map('FirefoxToFirefoxAndroid');
map('SafariToIOS');
if (!has(module, 'ios') && has(module, 'safari') && semver(module.safari).major >= 15) {
if (!hasOwn(module, 'ios') && hasOwn(module, 'safari') && semver(module.safari).major >= 15) {
module.ios = module.safari;
}
map('SafariToPhantom');
Expand Down
46 changes: 6 additions & 40 deletions tests/eslint/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const pluginSonarJS = require('eslint-plugin-sonarjs');
const pluginUnicorn = require('eslint-plugin-unicorn');
const PACKAGES_NODE_VERSIONS = require('core-js-builder/package').engines.node;

const DEV_NODE_VERSIONS = '^16.13';
const DEV_NODE_VERSIONS = '^18.12';

const ERROR = 'error';
const OFF = 'off';
Expand Down Expand Up @@ -1158,57 +1158,23 @@ const transpiledAndPolyfilled = {
};

const nodePackages = {
// disallow logical assignment operator shorthand
'logical-assignment-operators': [ERROR, NEVER],
// enforces the use of `catch()` on un-returned promises
'promise/catch-or-return': ERROR,
// disallow third-party modules which are hiding core modules
'node/no-hide-core-modules': OFF,
// disallow unsupported ECMAScript built-ins on the specified version
'node/no-unsupported-features/node-builtins': [ERROR, { version: PACKAGES_NODE_VERSIONS }],
// prefer promises
'node/prefer-promises/dns': OFF,
'node/prefer-promises/fs': OFF,
// prefer lookarounds over capturing group that do not replace
'regexp/prefer-lookaround': [ERROR, { lookbehind: false, strictTypes: true }],
// enforce using named capture group in regular expression
'regexp/prefer-named-capture-group': OFF,
// prefer using a logical operator over a ternary
'unicorn/prefer-logical-operator-over-ternary': OFF,
// prefer using the `node:` protocol when importing Node builtin modules
'unicorn/prefer-node-protocol': OFF,
// prefer omitting the `catch` binding parameter
'unicorn/prefer-optional-catch-binding': OFF,
...disable(forbidES5BuiltIns),
...disable(forbidES2015BuiltIns),
...disable(forbidES2016BuiltIns),
...disable(forbidES2017BuiltIns),
'es/no-atomics': ERROR,
'es/no-shared-array-buffer': ERROR,
// disallow top-level `await`
'es/no-top-level-await': ERROR,
...forbidES2018BuiltIns,
...forbidES2019BuiltIns,
...forbidES2020BuiltIns,
...forbidES2021BuiltIns,
...forbidES2022BuiltIns,
...forbidES2023BuiltIns,
'es/no-array-prototype-findlast-findlastindex': OFF,
...forbidES2024BuiltIns,
...disable(forbidES2016IntlBuiltIns),
...disable(forbidES2017IntlBuiltIns),
...forbidES2018IntlBuiltIns,
...forbidES2020IntlBuiltIns,
...forbidES2021IntlBuiltIns,
...forbidES2022IntlBuiltIns,
'es/no-intl-supportedvaluesof': ERROR,
...forbidES2023IntlBuiltIns,
// prefer top-level await
'unicorn/prefer-top-level-await': ERROR,
};

const nodeDev = {
// disallow unsupported ECMAScript built-ins on the specified version
'node/no-unsupported-features/node-builtins': [ERROR, { version: DEV_NODE_VERSIONS }],
...disable(forbidModernESBuiltIns),
'es/no-regexp-d-flag': ERROR,
...forbidES2023BuiltIns,
'es/no-array-prototype-findlast-findlastindex': OFF,
...forbidES2024BuiltIns,
'es/no-intl-supportedvaluesof': ERROR,
...forbidES2023IntlBuiltIns,
Expand Down

0 comments on commit b4aa4b2

Please sign in to comment.