Skip to content

Commit

Permalink
Fix shim test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Oct 2, 2024
1 parent a89ab4f commit 04c6065
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/npm/date/Date.parse/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const impl = require('./implementation')
const getPolyfill = require('./polyfill')
const polyfill = getPolyfill()

const desc = value => ({
__proto__: null,
Expand All @@ -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'))
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const impl = require('./implementation')
const getPolyfill = require('./polyfill')
const polyfill = getPolyfill()

const desc = value => ({
__proto__: null,
Expand All @@ -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'))
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const impl = require('./implementation')
const getPolyfill = require('./polyfill')
const polyfill = getPolyfill()

const desc = value => ({
__proto__: null,
Expand All @@ -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'))
}
)
Expand Down
11 changes: 7 additions & 4 deletions packages/npm/es-iterator-helpers/Iterator.prototype.map/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const impl = require('./implementation')
const getPolyfill = require('./polyfill')
const polyfill = getPolyfill()

const desc = value => ({
__proto__: null,
Expand All @@ -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'))
}
)
Expand Down
4 changes: 3 additions & 1 deletion packages/npm/regexp.prototype.flags/auto.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/* empty */
'use strict'

require('./shim')()
40 changes: 39 additions & 1 deletion packages/npm/regexp.prototype.flags/implementation.js
Original file line number Diff line number Diff line change
@@ -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')
9 changes: 5 additions & 4 deletions packages/npm/regexp.prototype.flags/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const impl = require('./implementation')
const getPolyfill = require('./polyfill')
const polyfill = getPolyfill()

const desc = value => ({
__proto__: null,
Expand All @@ -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'))
}
)
Expand Down
14 changes: 13 additions & 1 deletion packages/npm/regexp.prototype.flags/polyfill.js
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions packages/npm/regexp.prototype.flags/shared.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare interface InternalShared {
flagsGetter(): string
}
declare const shared: InternalShared
export = shared
7 changes: 7 additions & 0 deletions packages/npm/regexp.prototype.flags/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

const flagsGetter = RegExp.prototype.__lookupGetter__('flags')

module.exports = {
flagsGetter
}
17 changes: 15 additions & 2 deletions packages/npm/regexp.prototype.flags/shim.js
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 3 additions & 3 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ const lazyPrettierConfigPromise = () =>
prettier.resolveConfig(prettierConfigPath, { editorconfig: true })
const lazyPrettierIgnoreFile = () => includeIgnoreFile(prettierIgnorePath)
const lazyRunScriptParallelExecPath = () => whichSync('run-p')
const lazyTapRunExecPath = () => whichSync('tap-run')
const lazyTapExecPath = () => whichSync('tap')

const copyLeftLicenses = new Set([
'AGPL-3.0-or-later',
Expand Down Expand Up @@ -526,7 +526,7 @@ const constants = Object.freeze(
rootTapConfigPath,
rootTsConfigPath,
runScriptParallelExecPath: undefined,
tapRunExecPath: undefined,
tapExecPath: undefined,
templatesPath,
testNpmPath,
testNpmPkgJsonPath,
Expand All @@ -551,7 +551,7 @@ const constants = Object.freeze(
npmPackageNames: lazyNpmPackageNames,
prettierConfigPromise: lazyPrettierConfigPromise,
prettierIgnoreFile: lazyPrettierIgnoreFile,
tapRunExecPath: lazyTapRunExecPath,
tapExecPath: lazyTapExecPath,
runScriptParallelExecPath: lazyRunScriptParallelExecPath
}
)
Expand Down
1 change: 0 additions & 1 deletion scripts/make-npm-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ function toChoice(value) {
],
{
cwd: rootPath,
shell: true,
stdio: 'inherit'
}
)
Expand Down
5 changes: 2 additions & 3 deletions scripts/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ const { runBin } = require('@socketregistry/scripts/utils/npm')

;(async () => {
await runBin(
// Lazily access constants.tapRunExecPath.
constants.tapRunExecPath,
// Lazily access constants.tapExecPath.
constants.tapExecPath,
process.argv.slice(2),
{
cwd: rootPath,
shell: true,
stdio: 'inherit',
env: {
__proto__: null,
Expand Down
6 changes: 5 additions & 1 deletion scripts/utils/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ async function runBin(binPath, args, options) {
return await spawn(
WIN_32 ? binPath : execPath,
[...(WIN_32 ? [] : [binPath]), ...args],
options
{
__proto__: null,
...options,
shell: true,
}
)
}

Expand Down
16 changes: 11 additions & 5 deletions test/packages.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assertLoose from 'node:assert'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
import path from 'node:path'
Expand Down Expand Up @@ -314,12 +315,17 @@ for (const eco of constants.ecosystems) {
assert.ok(shimApiKeys.every(k => mainKeys.includes(k)))
})

it('getPolyfill() === implementation', async t => {
it('getPolyfill() is like implementation', async t => {
if (skipping) return t.skip(skipMessage)
assert.strictEqual(
req('./polyfill.js')(),
req('./implementation.js')
)
const impl = req('./implementation.js')
const polyfill = req('./polyfill.js')()
assert.strictEqual(typeof impl, typeof polyfill)
if (typeof impl === 'function') {
assert.strictEqual(impl.name, polyfill.name)
assert.strictEqual(impl.length, polyfill.length)
} else {
assertLoose.equal(impl, polyfill)
}
})
})
}
Expand Down

0 comments on commit 04c6065

Please sign in to comment.