Skip to content

Commit

Permalink
Add hasOwn into utils as safe replacement for hasOwnProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
iegik committed Feb 27, 2024
1 parent b51b046 commit 800ef70
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
38 changes: 38 additions & 0 deletions packages/react/__tests__/babel/has-own-property copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render } from '@testing-library/react'
console.error = jest.fn()

test('hasOwnProperty', async () => {
'use strict'
// Freeze Object.prototype to not accidentally export its properties again
// see https://github.com/emotion-js/emotion/issues/3158
Object.freeze(Object.prototype)

// Previous version:
// const utils = (await import('../../dist/emotion-element-48d2c2e4.cjs.dev.js'))
// .default

// Current version:
// const utils = (await import('../../dist/emotion-element-e909c831.cjs.dev'))
// .default

// console.log(utils)
// expect(
// Object.prototype.hasOwnProperty.call(utils, 'hasOwnProperty')
// ).toBeFalsy()

const element = await import('../../dist/emotion-react.worker.esm.js').default
console.log(element)
expect(
Object.prototype.hasOwnProperty.call(element, 'hasOwnProperty')
).toBeFalsy()

// /** @jsx jsx */
// const { jsx } = await import('@emotion/react')
// // const { CacheProvider } = await import('@emotion/react')
// const { CacheProvider } = await import('../../dist/emotion-react.cjs.dev.js')
// const createCache = await (await import('@emotion/cache')).default

// const cache = createCache({ key: 'context' })
// render(<CacheProvider cache={cache} />)
expect(console.error).not.toHaveBeenCalled()
})
23 changes: 23 additions & 0 deletions packages/react/__tests__/babel/has-own-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { render } from '@testing-library/react'
console.error = jest.fn()

test('hasOwnProperty', async () => {
// Freeze Object.prototype to not accidentally export its properties again
// see https://github.com/emotion-js/emotion/issues/3158
Object.freeze(Object.prototype)

// const utils = (await import('../../dist/emotion-react.cjs.dev.js')).default
// Previous version:
// const utils = (await import('../../dist/emotion-element-48d2c2e4.cjs.dev.js'))
// .default

// Current version:
const utils = (await import('../../dist/emotion-element-e909c831.cjs.dev'))
.default

expect(
Object.prototype.hasOwnProperty.call(utils, 'hasOwnProperty')
).toBeFalsy()

expect(console.error).not.toHaveBeenCalled()
})
6 changes: 3 additions & 3 deletions packages/react/src/emotion-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
insertStyles,
registerStyles
} from '@emotion/utils'
import { isBrowser } from './utils'
import { hasOwn, isBrowser } from './utils'
import { serializeStyles } from '@emotion/serialize'
import { getLabelFromStackTrace } from './get-label-from-stack-trace'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
Expand All @@ -31,7 +31,7 @@ export const createEmotionProps = (type: React.ElementType, props: Object) => {
let newProps: any = {}

for (let key in props) {
if (Object.hasOwn(props, key)) {
if (hasOwn(props, key)) {
newProps[key] = props[key]
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ let Emotion = /* #__PURE__ */ withEmotionCache<any, any>(
const newProps = {}
for (let key in props) {
if (
Object.hasOwn(props, key) &&
hasOwn(props, key) &&
key !== 'css' &&
key !== typePropName &&
(process.env.NODE_ENV === 'production' || key !== labelPropName)
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/jsx-dev-runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import * as ReactJSXRuntimeDev from 'react/jsx-dev-runtime'
import Emotion, { createEmotionProps } from './emotion-element'
import { hasOwn } from './utils'

export const Fragment = ReactJSXRuntimeDev.Fragment

Expand All @@ -12,7 +13,7 @@ export function jsxDEV(
source: any,
self: any
) {
if (!Object.hasOwn(props, 'css')) {
if (!hasOwn(props, 'css')) {
return ReactJSXRuntimeDev.jsxDEV(
type,
props,
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/jsx-runtime.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// @flow
import * as ReactJSXRuntime from 'react/jsx-runtime'
import Emotion, { createEmotionProps } from './emotion-element'
import { hasOwn } from './utils'

export const Fragment = ReactJSXRuntime.Fragment

export function jsx(type: any, props: any, key: any) {
if (!Object.hasOwn(props, 'css')) {
if (!hasOwn(props, 'css')) {
return ReactJSXRuntime.jsx(type, props, key)
}

return ReactJSXRuntime.jsx(Emotion, createEmotionProps(type, props), key)
}

export function jsxs(type: any, props: any, key: any) {
if (!Object.hasOwn(props, 'css')) {
if (!hasOwn(props, 'css')) {
return ReactJSXRuntime.jsxs(type, props, key)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/jsx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import * as React from 'react'
import Emotion, { createEmotionProps } from './emotion-element'
import { hasOwn } from './utils'

// $FlowFixMe
export const jsx: typeof React.createElement = function (
Expand All @@ -9,7 +10,7 @@ export const jsx: typeof React.createElement = function (
) {
let args = arguments

if (props == null || !Object.hasOwn(props, 'css')) {
if (props == null || !hasOwn(props, 'css')) {
// $FlowFixMe
return React.createElement.apply(undefined, args)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// @flow
export let isBrowser = typeof document !== 'undefined'

export const hasOwn = Object.hasOwn

0 comments on commit 800ef70

Please sign in to comment.