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
This commit is contained in:
Christoph Nakazawa 2019-07-25 15:26:46 -07:00 коммит произвёл Facebook Github Bot
Родитель d55025694b
Коммит 179889704b
1 изменённых файлов: 22 добавлений и 10 удалений

Просмотреть файл

@ -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