-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Circular json can possibly hang #1881
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5491707
remove json-stringify-safe
rhalff 2bb519d
add custom node renderer
rhalff e063717
add utility functions
rhalff 94fe359
use decycle instead of json-stringify-safe
rhalff 659bc07
use custom node renderer, sort objects keys & hide non enumerable
rhalff 4264f41
use retrocycle, no deepEqual if cyclic
rhalff be8171b
add test
rhalff a1f0583
increase max depth
rhalff ba19c34
add isObject
rhalff 87028a6
test if object before testing for cyclic
rhalff 1d8e9a9
Merge branch 'release/3.3' into master
ndelangen b3fac7e
FIX by supplying a custom cyclicObject for unit tests
ndelangen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
addons/actions/src/components/ActionLogger/NodeRenderer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { PropTypes } from 'react'; | ||
import { ObjectLabel, ObjectRootLabel } from 'react-inspector'; | ||
import { CLASS_NAME_KEY, isObject, createFakeConstructor } from '../../util'; | ||
|
||
export default function NodeRenderer({ depth, name, data, isNonEnumerable }) { | ||
let obj; | ||
if (isObject(data) && data[CLASS_NAME_KEY]) { | ||
obj = createFakeConstructor(data); | ||
} else { | ||
obj = data; | ||
} | ||
|
||
return depth === 0 ? ( | ||
<ObjectRootLabel name={name} data={obj} /> | ||
) : ( | ||
<ObjectLabel name={name} data={obj} isNonEnumerable={isNonEnumerable} /> | ||
); | ||
} | ||
|
||
NodeRenderer.propTypes = { | ||
depth: PropTypes.number.isRequired, | ||
name: PropTypes.string.isRequired, | ||
data: PropTypes.any, // eslint-disable-line react/forbid-prop-types | ||
isNonEnumerable: PropTypes.bool, | ||
}; | ||
|
||
NodeRenderer.defaultProps = { | ||
data: undefined, | ||
isNonEnumerable: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
export const CLASS_NAME_KEY = '$___storybook.className'; | ||
export const CYCLIC_KEY = '$___storybook.isCyclic'; | ||
|
||
export function muteProperties(keys, value) { | ||
keys.forEach(key => Object.defineProperty(value, key, { enumerable: false })); | ||
} | ||
|
||
export function isObject(value) { | ||
return Object.prototype.toString.call(value) === '[object Object]'; | ||
} | ||
|
||
export function createFakeConstructor(obj) { | ||
function FakeConstructor(data) { | ||
Object.assign(this, data); | ||
} | ||
|
||
Object.defineProperty(FakeConstructor, 'name', { | ||
value: obj[CLASS_NAME_KEY], | ||
}); | ||
|
||
return new FakeConstructor(obj); | ||
} | ||
|
||
// Based on: https://github.com/douglascrockford/JSON-js/blob/master/cycle.js | ||
export function decycle(object, depth = 15) { | ||
const objects = new WeakMap(); | ||
let isCyclic = false; | ||
|
||
return (function derez(value, path, _depth) { | ||
let oldPath; | ||
let obj; | ||
|
||
if (Object(value) === value && _depth > depth) { | ||
return `[${value.constructor.name}...]`; | ||
} | ||
|
||
if ( | ||
typeof value === 'object' && | ||
value !== null && | ||
!(value instanceof Boolean) && | ||
!(value instanceof Date) && | ||
!(value instanceof Number) && | ||
!(value instanceof RegExp) && | ||
!(value instanceof String) | ||
) { | ||
oldPath = objects.get(value); | ||
if (oldPath !== undefined) { | ||
isCyclic = true; | ||
|
||
return { $ref: oldPath }; | ||
} | ||
|
||
objects.set(value, path); | ||
|
||
if (Array.isArray(value)) { | ||
obj = []; | ||
for (let i = 0; i < value.length; i += 1) { | ||
obj[i] = derez(value[i], `${path}[${i}]`, _depth + 1); | ||
} | ||
} else { | ||
obj = { [CLASS_NAME_KEY]: value.constructor ? value.constructor.name : 'Object' }; | ||
|
||
Object.keys(value).forEach(name => { | ||
obj[name] = derez(value[name], `${path}[${JSON.stringify(name)}]`, _depth + 1); | ||
}); | ||
} | ||
|
||
if (_depth === 0 && isObject(value) && isCyclic) { | ||
obj[CYCLIC_KEY] = true; | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
return value; | ||
})(object, '$', 0); | ||
} | ||
|
||
export function retrocycle($) { | ||
const pathReg = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\([\\"/bfnrt]|u[0-9a-zA-Z]{4}))*")])*$/; | ||
|
||
(function rez(value) { | ||
if (value && typeof value === 'object') { | ||
if (Array.isArray(value)) { | ||
for (let i = 0; i < value.length; i += 1) { | ||
const item = value[i]; | ||
if (item && typeof item === 'object') { | ||
const path = item.$ref; | ||
if (typeof path === 'string' && pathReg.test(path)) { | ||
value[i] = eval(path); // eslint-disable-line no-eval, no-param-reassign | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} else { | ||
muteProperties([CLASS_NAME_KEY, CYCLIC_KEY], value); | ||
|
||
Object.keys(value).forEach(name => { | ||
const item = value[name]; | ||
|
||
if (typeof item === 'object' && item !== null) { | ||
const path = item.$ref; | ||
|
||
if (typeof path === 'string' && pathReg.test(path)) { | ||
value[name] = eval(path); // eslint-disable-line no-eval, no-param-reassign | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
})($); | ||
|
||
return $; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Might this be it's own npm module maybe?