From 8a739476977574f8e94a77efa87f7002d38a966a Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Thu, 24 Aug 2023 07:43:35 -0400 Subject: [PATCH] Remove useless iterator library --- dist/package.json | 2 +- esbuild.ts | 23 +++++++++++++++++- package.json | 4 ++-- pnpm-lock.yaml | 18 +++++++-------- src/polyfill.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 src/polyfill.ts diff --git a/dist/package.json b/dist/package.json index 91d2eca..b28695f 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "@aet/eslint-rules", - "version": "0.0.1-beta.30", + "version": "0.0.1-beta.32", "license": "UNLICENSED", "peerDependencies": { "esbin": "^0.0.2", diff --git a/esbuild.ts b/esbuild.ts index c8a6355..b665d9b 100755 --- a/esbuild.ts +++ b/esbuild.ts @@ -8,9 +8,12 @@ import type { Loader, Plugin } from 'esbuild'; import * as babel from '@babel/core'; import { memoize } from 'lodash'; import { gray, green } from 'picocolors'; -import type { types as t } from '@babel/core'; +import type { types as t, types } from '@babel/core'; import { dependencies } from './dist/package.json'; import { createMacro, type MacroHandler } from 'babel-plugin-macros'; +import * as polyfill from './src/polyfill'; + +const polyfills = Object.keys(polyfill); const ENV = process.env.NODE_ENV || 'development'; const PROD = ENV === 'production'; @@ -91,6 +94,24 @@ const map = new HandlerMap() ), ); +// es-iterator-helpers/Iterator.prototype.* +const polyfillPath = resolve(__dirname, './src/polyfill.ts'); +const requirePolyfill = (t: typeof types, name: string) => + t.memberExpression( + t.callExpression(t.identifier('require'), [t.stringLiteral(polyfillPath)]), + t.identifier(name), + ); +map.set( + `es-iterator-helpers/Iterator.from`, + replace(t => requirePolyfill(t, 'from')), +); +for (const name of polyfills) { + map.set( + `es-iterator-helpers/Iterator.prototype.${name}`, + replace(t => requirePolyfill(t, name)), + ); +} + function replace(getReplacement: (types: typeof t) => t.Expression): MacroHandler { return ({ references, babel: { types: t } }) => { references.default.forEach(referencePath => { diff --git a/package.json b/package.json index 362811c..a0c8cf1 100644 --- a/package.json +++ b/package.json @@ -15,11 +15,11 @@ "@types/estree": "^1.0.1", "@types/estree-jsx": "^1.0.0", "@types/lodash": "^4.14.197", - "@types/node": "^20.5.1", + "@types/node": "^20.5.4", "@typescript-eslint/types": "^6.4.1", "babel-plugin-macros": "^3.1.0", "dts-bundle-generator": "^8.0.1", - "esbin": "0.0.1-beta.3", + "esbin": "0.0.2", "esbuild": "0.19.2", "esbuild-plugin-alias": "^0.2.1", "esbuild-register": "3.4.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 53447c1..06fdbfe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -33,8 +33,8 @@ devDependencies: specifier: ^4.14.197 version: 4.14.197 '@types/node': - specifier: ^20.5.1 - version: 20.5.1 + specifier: ^20.5.4 + version: 20.5.4 '@typescript-eslint/types': specifier: ^6.4.1 version: 6.4.1 @@ -45,8 +45,8 @@ devDependencies: specifier: ^8.0.1 version: 8.0.1 esbin: - specifier: 0.0.1-beta.3 - version: 0.0.1-beta.3(esbuild@0.19.2) + specifier: 0.0.2 + version: 0.0.2(esbuild@0.19.2) esbuild: specifier: 0.19.2 version: 0.19.2 @@ -1719,8 +1719,8 @@ packages: resolution: {integrity: sha512-BMVOiWs0uNxHVlHBgzTIqJYmj+PgCo4euloGF+5m4okL3rEYzM2EEv78mw8zWSMM57dM7kVIgJ2QDvwHSoCI5g==} dev: true - /@types/node@20.5.1: - resolution: {integrity: sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==} + /@types/node@20.5.4: + resolution: {integrity: sha512-Y9vbIAoM31djQZrPYjpTLo0XlaSwOIsrlfE3LpulZeRblttsLQRFRlBAppW0LOxyT3ALj2M5vU1ucQQayQH3jA==} dev: true /@types/normalize-package-data@2.4.1: @@ -2177,11 +2177,11 @@ packages: is-symbol: 1.0.4 dev: true - /esbin@0.0.1-beta.3(esbuild@0.19.2): - resolution: {integrity: sha512-r8elIQs0Q0ArVkZJylVqiXxeNTlwvgjzK5V4lzjGDKbwobSuW2EzHBOmF8/Jb2pSTeysh0oRtXO+3YDne2U/PA==} + /esbin@0.0.2(esbuild@0.19.2): + resolution: {integrity: sha512-kmQ0D4pW0t1+VjA3YbSJQrhuuRjhqlSEPTTMrVVlJZOnfCoPbm1Hr648phOWLD2tMi9dDlFi5qE/p3ceJJcNjQ==} hasBin: true peerDependencies: - esbuild: '>=0.17 <1' + esbuild: '>=0.19 <1' dependencies: debug: 4.3.4 esbuild: 0.19.2 diff --git a/src/polyfill.ts b/src/polyfill.ts new file mode 100644 index 0000000..79a30d7 --- /dev/null +++ b/src/polyfill.ts @@ -0,0 +1,59 @@ +// es-iterator-helpers/Iterator.prototype.filter +export function* filter( + iterable: Iterable, + predicate: (value: T) => boolean, +): Iterable { + for (const value of iterable) { + if (predicate(value)) { + yield value; + } + } +} + +// es-iterator-helpers/Iterator.prototype.forEach +export function forEach(iterable: Iterable, callback: (value: T) => void) { + for (const value of iterable) { + callback(value); + } +} + +// es-iterator-helpers/Iterator.prototype.some +export function some( + iterable: Iterable, + predicate: (value: T) => boolean, +): boolean { + for (const value of iterable) { + if (predicate(value)) { + return true; + } + } + + return false; +} + +// es-iterator-helpers/Iterator.prototype.find +export function find( + iterable: Iterable, + predicate: (value: T) => boolean, +): T | undefined { + for (const value of iterable) { + if (predicate(value)) { + return value; + } + } +} + +// es-iterator-helpers/Iterator.from +export function from(iterable: Iterable) { + return iterable; +} + +// es-iterator-helpers/Iterator.prototype.map +export function* map( + iterable: Iterable, + callback: (value: T) => U, +): Iterable { + for (const value of iterable) { + yield callback(value); + } +}