From 0ee61e465a1fda2c96206ede24535845d8374ac1 Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 2 Oct 2024 15:33:30 -0400 Subject: [PATCH] Fix shim test fails --- packages/npm/date/Date.parse/shim.js | 2 +- .../Iterator.prototype.drop/index.js | 9 +++-- .../Iterator.prototype.filter/index.js | 11 +++-- .../Iterator.prototype.flatMap/index.js | 11 +++-- .../Iterator.prototype.map/index.js | 11 +++-- packages/npm/regexp.prototype.flags/auto.js | 4 +- .../regexp.prototype.flags/implementation.js | 40 ++++++++++++++++++- packages/npm/regexp.prototype.flags/index.js | 9 +++-- .../npm/regexp.prototype.flags/polyfill.js | 14 ++++++- .../npm/regexp.prototype.flags/shared.d.ts | 5 +++ packages/npm/regexp.prototype.flags/shared.js | 7 ++++ packages/npm/regexp.prototype.flags/shim.js | 17 +++++++- 12 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 packages/npm/regexp.prototype.flags/shared.d.ts create mode 100644 packages/npm/regexp.prototype.flags/shared.js diff --git a/packages/npm/date/Date.parse/shim.js b/packages/npm/date/Date.parse/shim.js index 0e79d3f7..d89acf0f 100644 --- a/packages/npm/date/Date.parse/shim.js +++ b/packages/npm/date/Date.parse/shim.js @@ -6,7 +6,7 @@ const { defineProperty: ObjectDefineProperty } = Object module.exports = function shimDateParse() { const polyfill = getPolyfill() - if (polyfill && Date.parse !== polyfill) { + if (Date.parse !== polyfill) { ObjectDefineProperty(Date, 'parse', { __proto__: null, configurable: true, diff --git a/packages/npm/es-iterator-helpers/Iterator.prototype.drop/index.js b/packages/npm/es-iterator-helpers/Iterator.prototype.drop/index.js index 387e6478..0b404192 100644 --- a/packages/npm/es-iterator-helpers/Iterator.prototype.drop/index.js +++ b/packages/npm/es-iterator-helpers/Iterator.prototype.drop/index.js @@ -1,6 +1,7 @@ 'use strict' -const impl = require('./implementation') +const getPolyfill = require('./polyfill') +const polyfill = getPolyfill() const desc = value => ({ __proto__: null, @@ -11,11 +12,11 @@ const desc = value => ({ module.exports = Object.defineProperties( function drop(thisArg, limit = 0) { - return new.target ? impl() : Reflect.apply(impl, thisArg, [limit]) + return new.target ? polyfill() : Reflect.apply(polyfill, thisArg, [limit]) }, { - getPolyfill: desc(require('./polyfill')), - implementation: desc(impl), + getPolyfill: desc(getPolyfill), + implementation: desc(require('./implementation')), shim: desc(require('./shim')) } ) diff --git a/packages/npm/es-iterator-helpers/Iterator.prototype.filter/index.js b/packages/npm/es-iterator-helpers/Iterator.prototype.filter/index.js index 74a17d8e..27bbad0d 100644 --- a/packages/npm/es-iterator-helpers/Iterator.prototype.filter/index.js +++ b/packages/npm/es-iterator-helpers/Iterator.prototype.filter/index.js @@ -1,6 +1,7 @@ 'use strict' -const impl = require('./implementation') +const getPolyfill = require('./polyfill') +const polyfill = getPolyfill() const desc = value => ({ __proto__: null, @@ -11,11 +12,13 @@ const desc = value => ({ module.exports = Object.defineProperties( function filter(thisArg, predicate) { - return new.target ? new impl() : Reflect.apply(impl, thisArg, [predicate]) + return new.target + ? new polyfill() + : Reflect.apply(polyfill, thisArg, [predicate]) }, { - getPolyfill: desc(require('./polyfill')), - implementation: desc(impl), + getPolyfill: desc(getPolyfill), + implementation: desc(require('./implementation')), shim: desc(require('./shim')) } ) diff --git a/packages/npm/es-iterator-helpers/Iterator.prototype.flatMap/index.js b/packages/npm/es-iterator-helpers/Iterator.prototype.flatMap/index.js index b18c32b0..cdb536bb 100644 --- a/packages/npm/es-iterator-helpers/Iterator.prototype.flatMap/index.js +++ b/packages/npm/es-iterator-helpers/Iterator.prototype.flatMap/index.js @@ -1,6 +1,7 @@ 'use strict' -const impl = require('./implementation') +const getPolyfill = require('./polyfill') +const polyfill = getPolyfill() const desc = value => ({ __proto__: null, @@ -11,11 +12,13 @@ const desc = value => ({ module.exports = Object.defineProperties( function flatMap(thisArg, mapper) { - return new.target ? new impl() : Reflect.apply(impl, thisArg, [mapper]) + return new.target + ? new polyfill() + : Reflect.apply(polyfill, thisArg, [mapper]) }, { - getPolyfill: desc(require('./polyfill')), - implementation: desc(impl), + getPolyfill: desc(getPolyfill), + implementation: desc(require('./implementation')), shim: desc(require('./shim')) } ) diff --git a/packages/npm/es-iterator-helpers/Iterator.prototype.map/index.js b/packages/npm/es-iterator-helpers/Iterator.prototype.map/index.js index c3a0d794..9f5d935a 100644 --- a/packages/npm/es-iterator-helpers/Iterator.prototype.map/index.js +++ b/packages/npm/es-iterator-helpers/Iterator.prototype.map/index.js @@ -1,6 +1,7 @@ 'use strict' -const impl = require('./implementation') +const getPolyfill = require('./polyfill') +const polyfill = getPolyfill() const desc = value => ({ __proto__: null, @@ -11,11 +12,13 @@ const desc = value => ({ module.exports = Object.defineProperties( function map(thisArg, mapper) { - return new.target ? new impl() : Reflect.apply(impl, thisArg, [mapper]) + return new.target + ? new polyfill() + : Reflect.apply(polyfill, thisArg, [mapper]) }, { - getPolyfill: desc(require('./polyfill')), - implementation: desc(impl), + getPolyfill: desc(getPolyfill), + implementation: desc(require('./implementation')), shim: desc(require('./shim')) } ) diff --git a/packages/npm/regexp.prototype.flags/auto.js b/packages/npm/regexp.prototype.flags/auto.js index 40a8c178..67d3d96d 100644 --- a/packages/npm/regexp.prototype.flags/auto.js +++ b/packages/npm/regexp.prototype.flags/auto.js @@ -1 +1,3 @@ -/* empty */ +'use strict' + +require('./shim')() diff --git a/packages/npm/regexp.prototype.flags/implementation.js b/packages/npm/regexp.prototype.flags/implementation.js index b46adf3e..ad1152da 100644 --- a/packages/npm/regexp.prototype.flags/implementation.js +++ b/packages/npm/regexp.prototype.flags/implementation.js @@ -1,3 +1,41 @@ 'use strict' -module.exports = RegExp.prototype.__lookupGetter__('flags') +const { flagsGetter } = require('./shared') + +module.exports = { + get flags() { + if (new.target || this === null || typeof this !== 'object') { + // Defer to builtin flags getter for the error case. + return flagsGetter.call(this) + } + // Flag check order defined as hasIndices, global, ignoreCase, multiline, + // dotAll, unicode, unicodeSets, and sticky. + // https://tc39.es/ecma262/#sec-get-regexp.prototype.flags + let result = '' + if (this.hasIndices) { + result += 'd' + } + if (this.global) { + result += 'g' + } + if (this.ignoreCase) { + result += 'i' + } + if (this.multiline) { + result += 'm' + } + if (this.dotAll) { + result += 's' + } + if (this.unicode) { + result += 'u' + } + if (this.unicodeSets) { + result += 'v' + } + if (this.sticky) { + result += 'y' + } + return result + } +}.__lookupGetter__('flags') diff --git a/packages/npm/regexp.prototype.flags/index.js b/packages/npm/regexp.prototype.flags/index.js index 6703a57b..d15d2a47 100644 --- a/packages/npm/regexp.prototype.flags/index.js +++ b/packages/npm/regexp.prototype.flags/index.js @@ -1,6 +1,7 @@ 'use strict' -const impl = require('./implementation') +const getPolyfill = require('./polyfill') +const polyfill = getPolyfill() const desc = value => ({ __proto__: null, @@ -11,11 +12,11 @@ const desc = value => ({ module.exports = Object.defineProperties( function flags(thisArg) { - return new.target ? new impl() : Reflect.apply(impl, thisArg, []) + return new.target ? new polyfill() : Reflect.apply(polyfill, thisArg, []) }, { - getPolyfill: desc(require('./polyfill')), - implementation: desc(impl), + getPolyfill: desc(getPolyfill), + implementation: desc(require('./implementation')), shim: desc(require('./shim')) } ) diff --git a/packages/npm/regexp.prototype.flags/polyfill.js b/packages/npm/regexp.prototype.flags/polyfill.js index ff68a60c..bd9fd025 100644 --- a/packages/npm/regexp.prototype.flags/polyfill.js +++ b/packages/npm/regexp.prototype.flags/polyfill.js @@ -1,7 +1,19 @@ 'use strict' const impl = require('./implementation') +const { flagsGetter } = require('./shared') module.exports = function getPolyfill() { - return impl + let calls = '' + flagsGetter.call({ + // eslint-disable-next-line getter-return + get hasIndices() { + calls += 'd' + }, + // eslint-disable-next-line getter-return + get sticky() { + calls += 'y' + } + }) + return calls === 'dy' ? flagsGetter : impl } diff --git a/packages/npm/regexp.prototype.flags/shared.d.ts b/packages/npm/regexp.prototype.flags/shared.d.ts new file mode 100644 index 00000000..45c28818 --- /dev/null +++ b/packages/npm/regexp.prototype.flags/shared.d.ts @@ -0,0 +1,5 @@ +declare interface InternalShared { + flagsGetter(): string +} +declare const shared: InternalShared +export = shared diff --git a/packages/npm/regexp.prototype.flags/shared.js b/packages/npm/regexp.prototype.flags/shared.js new file mode 100644 index 00000000..e3d2e350 --- /dev/null +++ b/packages/npm/regexp.prototype.flags/shared.js @@ -0,0 +1,7 @@ +'use strict' + +const flagsGetter = RegExp.prototype.__lookupGetter__('flags') + +module.exports = { + flagsGetter +} diff --git a/packages/npm/regexp.prototype.flags/shim.js b/packages/npm/regexp.prototype.flags/shim.js index c5061d1f..d7e4a37c 100644 --- a/packages/npm/regexp.prototype.flags/shim.js +++ b/packages/npm/regexp.prototype.flags/shim.js @@ -1,7 +1,20 @@ 'use strict' -const impl = require('./implementation') +const getPolyfill = require('./polyfill') + +const { defineProperty: ObjectDefineProperty } = Object +const { __lookupGetter__: ObjectProtoLookupGetter } = Object.prototype +const { prototype: RegExpPrototype } = RegExp module.exports = function shimRegExpProtoFlags() { - return impl + const polyfill = getPolyfill() + if (ObjectProtoLookupGetter.call(RegExpPrototype, 'flags') !== polyfill) { + ObjectDefineProperty(RegExpPrototype, 'flags', { + __proto__: null, + configurable: true, + enumerable: false, + get: polyfill + }) + } + return polyfill }