From c501a76deb00836d9557978c9a9dbb677e899f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 10 Oct 2023 12:04:22 +0900 Subject: [PATCH] [es-shims] Add ES2024 string methods (#184) --- .../babel-plugin-polyfill-es-shims/README.md | 28 +++++++++++ .../data/polyfills.json | 47 ++++++++++++++++-- .../src/mappings.ts | 13 +++++ .../build-es-shims-data/es-shims-features.js | 49 +++++++++++++++++-- scripts/build-es-shims-data/index.js | 23 +++++---- scripts/download-compat-table.sh | 2 +- 6 files changed, 139 insertions(+), 23 deletions(-) diff --git a/packages/babel-plugin-polyfill-es-shims/README.md b/packages/babel-plugin-polyfill-es-shims/README.md index 51dd4769..0c3d201a 100644 --- a/packages/babel-plugin-polyfill-es-shims/README.md +++ b/packages/babel-plugin-polyfill-es-shims/README.md @@ -30,8 +30,36 @@ This package supports the `usage-pure` and `usage-global` methods. This polyfill provider is compatible with polyfills that follow the [`es-shims-api`](https://github.com/es-shims/es-shim-api) specification. Those polyfills must live under the [`@es-shims`](https://github.com/es-shims) organization, but for historical reasons some of them are owned by different people: they are explicitly marked as such in the following tables. + + +### ES2024 + +| Builtin object or function | Package name | Owner | +| :------------------------------ | :------------------------------------------------------------------------------------------- | :---- | +| `String.prototype.isWellFormed` | [`string.prototype.iswellformed`](https://github.com/es-shims/String.prototype.isWellFormed) | +| `String.prototype.toWellFormed` | [`string.prototype.towellformed`](https://github.com/es-shims/String.prototype.toWellFormed) | + +### ES2023 + | Builtin object or function | Package name | Owner | | :------------------------------ | :------------------------------------------------------------------------------------------- | :---- | | `Array.prototype.findLast` | [`array.prototype.findlast`](https://github.com/es-shims/Array.prototype.findLast) | diff --git a/packages/babel-plugin-polyfill-es-shims/data/polyfills.json b/packages/babel-plugin-polyfill-es-shims/data/polyfills.json index 05bf41b4..93397b62 100644 --- a/packages/babel-plugin-polyfill-es-shims/data/polyfills.json +++ b/packages/babel-plugin-polyfill-es-shims/data/polyfills.json @@ -324,6 +324,7 @@ "samsung": "9", "electron": "3" }, + "ArrayBuffer.prototype.transfer": {}, "Date.prototype.toISOString": { "chrome": "5", "opera": "10.50", @@ -389,6 +390,19 @@ "samsung": "1", "electron": "0.20" }, + "Iterator": {}, + "Iterator.from": {}, + "Iterator.prototype.drop": {}, + "Iterator.prototype.every": {}, + "Iterator.prototype.filter": {}, + "Iterator.prototype.find": {}, + "Iterator.prototype.flatMap": {}, + "Iterator.prototype.forEach": {}, + "Iterator.prototype.map": {}, + "Iterator.prototype.reduce": {}, + "Iterator.prototype.some": {}, + "Iterator.prototype.take": {}, + "Iterator.prototype.toArray": {}, "Map": { "chrome": "51", "opera": "38", @@ -776,13 +790,25 @@ "samsung": "5", "electron": "1.2" }, - "Set.prototype.difference": {}, - "Set.prototype.intersection": {}, + "Set.prototype.difference": { + "safari": "17", + "ios": "17" + }, + "Set.prototype.intersection": { + "safari": "17", + "ios": "17" + }, "Set.prototype.isDisjointFrom": {}, "Set.prototype.isSubsetOf": {}, "Set.prototype.isSupersetOf": {}, - "Set.prototype.symmetricDifference": {}, - "Set.prototype.union": {}, + "Set.prototype.symmetricDifference": { + "safari": "17", + "ios": "17" + }, + "Set.prototype.union": { + "safari": "17", + "ios": "17" + }, "String.fromCodePoint": { "chrome": "41", "opera": "28", @@ -1175,5 +1201,16 @@ "Array.prototype.toReversed": {}, "Array.prototype.toSorted": {}, "Array.prototype.toSpliced": {}, - "Array.prototype.with": {} + "Array.prototype.with": {}, + "ArrayBuffer.prototype.detached": {}, + "ArrayBuffer.prototype.transferToFixedLength": {}, + "AsyncDisposableStack": {}, + "DisposableStack": {}, + "Iterator.prototype.constructor": {}, + "Promise.withResolvers": {}, + "String.prototype.isWellFormed": {}, + "String.prototype.toWellFormed": {}, + "SuppressedError": {}, + "Symbol.asyncDispose": {}, + "Symbol.dispose": {} } diff --git a/packages/babel-plugin-polyfill-es-shims/src/mappings.ts b/packages/babel-plugin-polyfill-es-shims/src/mappings.ts index eb6485cf..c770a4b6 100644 --- a/packages/babel-plugin-polyfill-es-shims/src/mappings.ts +++ b/packages/babel-plugin-polyfill-es-shims/src/mappings.ts @@ -38,6 +38,7 @@ for (const [name, causeArgNum] of [ ["SyntaxError", 2], ["TypeError", 2], ["URIError", 2], + // SuppressedError is not in this list because it does not have a cause ] as [string, number][]) { defineGlobal(name, "1.0.1", "error-cause", { exclude: lessThanArgs(causeArgNum), @@ -56,6 +57,16 @@ defineGlobal("parseInt", "2.0.0"); defineGlobal("Map", "1.0.4", "es-map"); defineGlobal("Set", "1.1.0", "es-set"); +// TODO: Uncomment when stage 4 +// defineGlobal("SuppressedError", "1.0.2", "suppressed-error"); +// const DisposableStackPackage = ["1.1.2", "disposablestack"] as const; +// defineGlobal("AsyncDisposableStack", ...DisposableStackPackage, { +// subfolder: "AsyncDisposableStack", +// }); +// defineGlobal("DisposableStack", ...DisposableStackPackage, { +// subfolder: "DisposableStack", +// }); + const arrayCheck = thisObj => expr`Array.isArray(${thisObj})`; const typeofCheck = type => thisObj => expr`typeof ${thisObj} === "${type}"`; const instanceofCheck = Class => thisObj => @@ -186,6 +197,7 @@ defineStatic("String", "raw", "1.0.1"); defineInstance("String", "codePoitAt", "1.0.0", stringCheck); defineInstance("String", "endsWith", "1.0.0", stringCheck); defineInstance("String", "includes", "2.0.0", stringCheck); +defineInstance("String", "isWellFormed", "1.0.1", stringCheck); defineInstance("String", "at", "1.0.0", stringCheck); defineInstance("String", "matchAll", "4.0.2", stringCheck); defineInstance("String", "padEnd", "1.1.1", stringCheck); @@ -201,6 +213,7 @@ defineInstance( ); defineInstance("String", "startsWith", "1.0.0", stringCheck); defineInstance("String", "substr", "1.0.0", stringCheck); +defineInstance("String", "toWellFormed", "1.0.1", stringCheck); defineInstance("String", "trim", "1.2.1", stringCheck); defineInstance("String", "trimEnd", "1.0.0", stringCheck); defineInstance("String", "trimLeft", "2.1.1", stringCheck); diff --git a/scripts/build-es-shims-data/es-shims-features.js b/scripts/build-es-shims-data/es-shims-features.js index cb079430..495f0961 100644 --- a/scripts/build-es-shims-data/es-shims-features.js +++ b/scripts/build-es-shims-data/es-shims-features.js @@ -1,5 +1,7 @@ "use strict"; +const MISSING = "@@MISSING@@"; + module.exports = { globalThis: "globalThis", parseInt: "Miscellaneous / parseInt ignores leading zeros", @@ -38,13 +40,19 @@ module.exports = { "Array.prototype.reduceRight": "Array methods / Array.prototype.reduceRight", "Array.prototype.some": "Array methods / Array.prototype.some", "Array.prototype.splice": "Array.prototype methods / Array.prototype.splice", - //"Array.prototype.toReversed": MISSING, - //"Array.prototype.toSpliced": MISSING, - //"Array.prototype.toSorted": MISSING, + "Array.prototype.toReversed": MISSING, + "Array.prototype.toSorted": MISSING, + "Array.prototype.toSpliced": MISSING, "Array.prototype.unshift": "Array methods / Array.prototype.unshift", "Array.prototype.values": "Array.prototype methods / Array.prototype.values", - //"Array.prototype.with": MISSING, - //"Date": MISSING, + "Array.prototype.with": MISSING, + "ArrayBuffer.prototype.detached": MISSING, + "ArrayBuffer.prototype.transfer": + "ArrayBuffer.prototype.transfer / ArrayBuffer.prototype.transfer()", + "ArrayBuffer.prototype.transferToFixedLength": MISSING, + AsyncDisposableStack: MISSING, + // Commented out to avoid polyfilling in every environment until when we have data + //Date: MISSING, //"Date.prototype.getFullYear": MISSING, //"Date.prototype.getMonth": MISSING, //"Date.prototype.getDate": MISSING, @@ -58,6 +66,7 @@ module.exports = { "Date.prototype.toJSON": "Date methods / Date.prototype.toJSON", "Date.now": "Date methods / Date.now", //"Date.parse": MISSING, + DisposableStack: MISSING, "Error cause": "Error.cause property", "Function.prototype.name": { features: [ @@ -65,6 +74,30 @@ module.exports = { 'function "name" property / function expressions', ], }, + Iterator: { + features: [ + "Iterator Helpers / instanceof Iterator", + "Iterator Helpers / extends Iterator", + ], + }, + "Iterator.from": { + features: [ + "Iterator Helpers / Iterator.from, iterable", + "Iterator Helpers / Iterator.from, iterator", + ], + }, + "Iterator.prototype.constructor": MISSING, + "Iterator.prototype.drop": "Iterator Helpers / Iterator.prototype.drop", + "Iterator.prototype.every": "Iterator Helpers / Iterator.prototype.every", + "Iterator.prototype.filter": "Iterator Helpers / Iterator.prototype.filter", + "Iterator.prototype.find": "Iterator Helpers / Iterator.prototype.find", + "Iterator.prototype.flatMap": "Iterator Helpers / Iterator.prototype.flatMap", + "Iterator.prototype.forEach": "Iterator Helpers / Iterator.prototype.forEach", + "Iterator.prototype.map": "Iterator Helpers / Iterator.prototype.map", + "Iterator.prototype.reduce": "Iterator Helpers / Iterator.prototype.reduce", + "Iterator.prototype.some": "Iterator Helpers / Iterator.prototype.some", + "Iterator.prototype.take": "Iterator Helpers / Iterator.prototype.take", + "Iterator.prototype.toArray": "Iterator Helpers / Iterator.prototype.toArray", Map: "Map /", // Trailing / to exclude Map.prototype.upsert "Math.acosh": "Math methods / Math.acosh", "Math.atanh": "Math methods / Math.atanh", @@ -100,6 +133,7 @@ module.exports = { "Promise.allSettled": "Promise.allSettled", "Promise.any": "Promise.any", "Promise.try": [], + "Promise.withResolvers": MISSING, "Promise.prototype.finally": "Promise.prototype.finally", "Reflect.apply": "Reflect / Reflect.apply", "Reflect.getPrototypeOf": "Reflect / Reflect.getPrototypeOf", @@ -123,6 +157,7 @@ module.exports = { "String.prototype methods / String.prototype.endsWith", "String.prototype.includes": "String.prototype methods / String.prototype.includes", + "String.prototype.isWellFormed": MISSING, "String.prototype.at": "`.item` method / String.prototype.item", "String.prototype.matchAll": "String.prototype.matchAll", "String.prototype.padEnd": "String padding / String.prototype.padEnd", @@ -136,12 +171,16 @@ module.exports = { "String.prototype methods / String.prototype.startsWith", "String.prototype.substr": "String properties and methods / String.prototype.substr", + "String.prototype.toWellFormed": MISSING, "String.prototype.trim": "String properties and methods / String.prototype.trim", "String.prototype.trimEnd": "string trimming / String.prototype.trimEnd", "String.prototype.trimLeft": "string trimming / String.prototype.trimLeft", "String.prototype.trimRight": "string trimming / String.prototype.trimRight", "String.prototype.trimStart": "string trimming / String.prototype.trimStart", + SuppressedError: MISSING, + "Symbol.asyncDispose": MISSING, + "Symbol.dispose": MISSING, "Symbol.prototype.description": "Symbol.prototype.description", // Annex B diff --git a/scripts/build-es-shims-data/index.js b/scripts/build-es-shims-data/index.js index 9dbfe6b0..95027938 100644 --- a/scripts/build-es-shims-data/index.js +++ b/scripts/build-es-shims-data/index.js @@ -4,20 +4,19 @@ const path = require("path"); const { generateData, environments, writeFile } = require("./utils-build-data"); -const newData = generateData(environments, require(`./es-shims-features`)); - -// These features are missing from compat-table. Remove this from this list once they are added. -const missing = [ - "Array.prototype.toReversed", - "Array.prototype.toSorted", - "Array.prototype.toSpliced", - "Array.prototype.with", -]; -for (const name of missing) { - if (newData[name]) throw new Error(`Missing feature is present: ${name}`); - newData[name] = {}; +const features = require(`./es-shims-features`); +const missing = []; +for (const name of Object.keys(features)) { + if (features[name] === "@@MISSING@@") { + missing.push(name); + delete features[name]; + } } +const newData = generateData(environments, features); + +for (const name of missing) newData[name] = {}; + const dataPath = path.join( __dirname, `../../packages/babel-plugin-polyfill-es-shims/data/polyfills.json` diff --git a/scripts/download-compat-table.sh b/scripts/download-compat-table.sh index d8a6a244..0207ae75 100755 --- a/scripts/download-compat-table.sh +++ b/scripts/download-compat-table.sh @@ -1,7 +1,7 @@ #!/bin/bash set -e -COMPAT_TABLE_COMMIT=d5e9c83b96b23f15318e6b2dc3f5bf51314aa340 +COMPAT_TABLE_COMMIT=459ee154ee35e1cf55fc9179178d072badf8c67d GIT_HEAD=build/compat-table/.git/HEAD if [ -d "build/compat-table" ]; then