Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions packages/uniwind/src/metro/utils/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from '../logger'
import { addMissingSpaces, isNumber, isValidJSValue, pipe, roundToPrecision, smartSplit } from './common'
import { addMissingSpaces, isNumber, isValidJSValue, roundToPrecision, smartSplit } from './common'

const parseStringValue = (value: string) => {
if (isValidJSValue(value)) {
Expand Down Expand Up @@ -97,26 +97,25 @@ export const serialize = (value: any): string => {
}

export const serializeJSObject = (obj: Record<string, any>, serializer: (key: string, value: string) => string) => {
const serializedObject = pipe(obj)(
Object.entries,
entries => entries.map(([key, value]) => serializer(key, serialize(value))),
entries => entries.join(','),
result => {
if (result === '') {
return ''
}
const entries = Object.entries(obj).map(([key, value]) => serializer(key, serialize(value)))

const validEntries = entries.filter(entry => {
try {
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
new Function(`function v() { const o = ({ ${entry} }) }`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the original naming

Suggested change
new Function(`function v() { const o = ({ ${entry} }) }`)
new Function(`function validateJS() { const obj = ({ ${entry} }) }`)

return true
} catch {
return false
}
})
Comment on lines -100 to +110

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the pipe function like it was done before so we don't need random variable names


return `${result},`
},
)
if (validEntries.length < entries.length) {
Logger.warn(`Skipped ${entries.length - validEntries.length} invalid style entries during serialization`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be Logger.error, warn is not logged without debug flag enabled, we want to resolve all serialization errors and this would limit number of reported errors

}

try {
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
new Function(`function validateJS() { const obj = ({ ${serializedObject} }) }`)
} catch {
Logger.error('Failed to serialize javascript object')
if (validEntries.length === 0) {
return ''
}

return serializedObject
return `${validEntries.join(',')},`
}