Skip to content

Commit

Permalink
[es-shims] Add ES2024 string methods (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 10, 2023
1 parent 67518e8 commit c501a76
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 23 deletions.
28 changes: 28 additions & 0 deletions packages/babel-plugin-polyfill-es-shims/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!--
### Proposals (Stage 3)
| Builtin object or function | Package name | Owner |
| :--------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- | :---- |
| `ArrayBuffer.prototype.detached` | [`arraybuffer.prototype.detached`](https://github.com/es-shims/ArrayBuffer.prototype.detached) |
| `ArrayBuffer.prototype.transfer` | [`arraybuffer.prototype.transfer`](https://github.com/es-shims/ArrayBuffer.prototype.transfer) |
| `ArrayBuffer.prototype.transferToFixedLength` | [`arraybuffer.prototype.transfertofixedlength`](https://github.com/es-shims/ArrayBuffer.prototype.transferToFixedLength) |
| `DisposableStack`, `AsyncDisposableStack`, `Symbol.dispose`, `Symbol.asyncDispose` | [`disposablestack`](https://github.com/es-shims/DisposableStack) |
| `Iterator`, `Iterator.from`, `Iterator.prototype.*` helpers | [`es-iterator-helpers`](https://github.com/es-shims/iterator-helpers) |
| `Promise.withResolvers` | [`promise.withresolvers`](https://github.com/es-shims/Promise.withResolvers) |
| `Set.prototype.difference` | [`set.prototype.difference`](https://github.com/es-shims/Set.prototype.difference) |
| `Set.prototype.intersection` | [`set.prototype.intersection`](https://github.com/es-shims/Set.prototype.intersection) |
| `Set.prototype.isDisjointFrom` | [`set.prototype.isDisjointFrom`](https://github.com/es-shims/Set.prototype.isDisjointFrom) |
| `Set.prototype.isSubsetOf` | [`set.prototype.isSubsetOf`](https://github.com/es-shims/Set.prototype.isSubsetOf) |
| `Set.prototype.isSupersetOf` | [`set.prototype.isSupersetOf`](https://github.com/es-shims/Set.prototype.isSupersetOf) |
| `Set.prototype.symmetricDifference` | [`set.prototype.symmetricDifference`](https://github.com/es-shims/Set.prototype.symmetricDifference) |
| `Set.prototype.union` | [`set.prototype.union`](https://github.com/es-shims/Set.prototype.union) |
| `SuppressedError` | [`suppressed-error`](https://github.com/es-shims/SuppressedError) |
-->

### 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) |
Expand Down
47 changes: 42 additions & 5 deletions packages/babel-plugin-polyfill-es-shims/data/polyfills.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
"samsung": "9",
"electron": "3"
},
"ArrayBuffer.prototype.transfer": {},
"Date.prototype.toISOString": {
"chrome": "5",
"opera": "10.50",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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": {}
}
13 changes: 13 additions & 0 deletions packages/babel-plugin-polyfill-es-shims/src/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 =>
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
49 changes: 44 additions & 5 deletions scripts/build-es-shims-data/es-shims-features.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

const MISSING = "@@MISSING@@";

module.exports = {
globalThis: "globalThis",
parseInt: "Miscellaneous / parseInt ignores leading zeros",
Expand Down Expand Up @@ -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,
Expand All @@ -58,13 +66,38 @@ 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: [
'function "name" property / function statements',
'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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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
Expand Down
23 changes: 11 additions & 12 deletions scripts/build-es-shims-data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion scripts/download-compat-table.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c501a76

Please sign in to comment.