fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 91915cf · Browse files · 3 files·+48 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+48 added−4 removed
- tests:+33 added−1 removed
- other:+15 added−3 removed
Provenance
- Commit
c7a47e2- Tree
217124a- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
91915cf
3 files changed+48 added−4 removed
modified:__tests__/polyfills.js+33 added-1 removed
1123456789101112131415161718192021222328 unmodified lines5253545556575859606162636465666768697071727374757677787980818283848586const {assign} = Objectconst {assign, getOwnPropertyDescriptors} = Objectconst {ownKeys} = Reflectconst SymbolConstructor = Symbol
const testSymbol = Symbol("test-symbol")
Symbol = undefinedObject.assign = undefinedReflect.ownKeys = undefinedObject.getOwnPropertyDescriptors = undefined
jest.resetModules()const common = require("../src/internal")
// Reset the globals to avoid unintended effects.Symbol = SymbolConstructorObject.assign = assignReflect.ownKeys = ownKeysObject.getOwnPropertyDescriptors = getOwnPropertyDescriptors
if (!global.USES_BUILD) describe("Symbol", () => { test("NOTHING", () => {28 unmodified lines expect(ownKeys(obj)).toEqual(["a", "b"]) }) })
if (!global.USES_BUILD) describe("Object.getOwnPropertyDescriptors", () => { const {getOwnPropertyDescriptors} = common
// Symbol keys are always last. it("gets symbolic and nonsymbolic properties", () => { expect( Object.getOwnPropertyDescriptors({ x: 1, [testSymbol]: 2 }) ).toEqual({ x: { value: 1, enumerable: true, configurable: true, writable: true }, [testSymbol]: { value: 2, enumerable: true, configurable: true, writable: true } }) }) })
test("suppress jest warning", () => { expect(true).toBe(true)})modified:src/plugins/es5.ts+3 added-2 removed
15 unmodified lines16 getCurrentScope,17 die,18 markChanged,19 objectTraps,20- ownKeys20+ ownKeys,21+ getOwnPropertyDescriptors22 } from "../internal"23 24 type ES5State = ES5ArrayState | ES5ObjectState25 20 unmodified lines46 }47 48 function createES5Draft(isArray: boolean, base: any) {49 // Create a new object / array, where each own property is trapped with an accessor49- const descriptors = Object.getOwnPropertyDescriptors(base)50+ const descriptors = getOwnPropertyDescriptors(base)51 // Descriptors we want to skip:52 if (isArray) delete descriptors.length53 delete descriptors[DRAFT_STATE as any]54 const keys = ownKeys(descriptors)218 unmodified linesmodified:src/utils/common.ts+12 added-1 removed
60 unmodified lines61 Object.getOwnPropertySymbols(obj) as any62 )63 : /* istanbul ignore next */ Object.getOwnPropertyNames64 65+export const getOwnPropertyDescriptors =66+ Object.getOwnPropertyDescriptors ||67+ function getOwnPropertyDescriptors(target: any) {68+ // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/27469+ const res: any = {}70+ ownKeys(target).forEach(key => {71+ res[key] = Object.getOwnPropertyDescriptor(target, key)72+ })73+ return res74+ }75+76 export function each<T extends Objectish>(77 obj: T,78 iter: (key: string | number, value: any, source: T) => void,79 enumerableOnly?: boolean74 unmodified lines154 155 /*#__PURE__*/156 export function shallowCopy(base: any) {157 if (Array.isArray(base)) return base.slice()147- const descriptors = Object.getOwnPropertyDescriptors(base)158+ const descriptors = getOwnPropertyDescriptors(base)159 delete descriptors[DRAFT_STATE as any]160 let keys = ownKeys(descriptors)161 for (let i = 0; i < keys.length; i++) {162 const key: any = keys[i]37 unmodified lines