-
Notifications
You must be signed in to change notification settings - Fork 43
fix: validate serialized style entries individually instead of all-or-nothing #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)) { | ||
|
|
@@ -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} }) }`) | ||
| return true | ||
| } catch { | ||
| return false | ||
| } | ||
| }) | ||
|
Comment on lines
-100
to
+110
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use the |
||
|
|
||
| return `${result},` | ||
| }, | ||
| ) | ||
| if (validEntries.length < entries.length) { | ||
| Logger.warn(`Skipped ${entries.length - validEntries.length} invalid style entries during serialization`) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be |
||
| } | ||
|
|
||
| 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(',')},` | ||
| } | ||
There was a problem hiding this comment.
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