Skip to content
Permalink
Browse files

Fall back to `JSON.stringify` in `console.log` if Symbol is unavailable

Summary: Symbol is not available in older versions of JSON resulting in crashes in `prettyFormat` because we are using a clowny transform.

Reviewed By: sebmck

Differential Revision: D16501208

fbshipit-source-id: 9952bf4993ae05335707cd386f9aa4bbc14b7564
  • Loading branch information...
cpojer authored and facebook-github-bot committed Jul 25, 2019
1 parent d550256 commit 179889704b6f9d56cb990d5b9bba6ee5ea2cd13f
Showing with 22 additions and 10 deletions.
  1. +22 −10 Libraries/Utilities/HMRClient.js
@@ -11,7 +11,6 @@

const Platform = require('./Platform');
const invariant = require('invariant');
const prettyFormat = require('pretty-format');

const MetroHMRClient = require('metro/src/lib/bundle-modules/HMRClient');

@@ -104,23 +103,36 @@ const HMRClient: HMRClientNativeInterface = {
log(level: LogLevel, data: Array<mixed>) {
try {
if (hmrClient) {
hmrClient.send(
JSON.stringify({
let message;
if (global.Symbol) {
message = JSON.stringify({
type: 'log',
level,
data: data.map(message =>
typeof message === 'string'
? message
: prettyFormat(message, {
data: data.map(item =>
typeof item === 'string'
? item
: require('pretty-format')(item, {
escapeString: true,
highlight: true,
maxDepth: 3,
min: true,
plugins: [prettyFormat.plugins.ReactElement],
plugins: [require('pretty-format').plugins.ReactElement],
}),
),
}),
);
});
} else {
try {
message = JSON.stringify({type: 'log', level, data});
} catch (error) {
message = JSON.stringify({
type: 'log',
level,
data: [error.message],
});
}
}

hmrClient.send(message);
}
} catch (error) {
// If sending logs causes any failures we want to silently ignore them

0 comments on commit 1798897

Please sign in to comment.
You can’t perform that action at this time.