Permalink
Browse files

Improve YellowBox output format

Summary:
YellowBox currently assumes the first arg is a printf like format string, this adds support for any arguments so it works more like console in the browser. This also adds `stringifySafe` to format arguments when using printf style.

The main annoyance that this fixes is when trying to log a single object it will currently print [object Object] instead of the fully stringified version.

**Test plan**

Tested a bunch of different log combinations.

```js
console.warn({test: 'a'}); // {"test":"a"} (was [object Object] before this patch)
console.warn('test %s %s', 1, {}); // test 1 {}
console.warn('test %s', 1, {}); // test 1 {}
console.warn({}, {}, {}, {}); // {} {} {} {}
```
Closes #16132

Differential Revision: D5973125

Pulled By: yungsters

fbshipit-source-id: fc17105a79473a11c9b1c4728d435fc54fb094bb
  • Loading branch information...
janicduplessis authored and facebook-github-bot committed Oct 4, 2017
1 parent 09680f7 commit eae4fe810f43266ee54a3bb89558621324d7a326
Showing with 12 additions and 7 deletions.
  1. +12 −7 Libraries/ReactNative/YellowBox.js
@@ -106,17 +106,22 @@ function sprintf(format, ...args) {
return format.replace(/%s/g, match => args[index++]);
}
function updateWarningMap(format, ...args): void {
function updateWarningMap(...args): void {
if (console.disableYellowBox) {
return;
}
format = String(format);
const argCount = (format.match(/%s/g) || []).length;
const warning = [
sprintf(format, ...args.slice(0, argCount)),
...args.slice(argCount).map(stringifySafe),
].join(' ');
let warning;
if (typeof args[0] === 'string') {
const [format, ...formatArgs] = args;
const argCount = (format.match(/%s/g) || []).length;
warning = [
sprintf(format, ...formatArgs.slice(0, argCount).map(stringifySafe)),
...formatArgs.slice(argCount).map(stringifySafe),
].join(' ');
} else {
warning = args.map(stringifySafe).join(' ');
}
if (warning.startsWith('(ADVICE)')) {
return;

1 comment on commit eae4fe8

@brentvatne

This comment has been minimized.

Show comment
Hide comment
@brentvatne

brentvatne Oct 4, 2017

Collaborator

nice one 😎

Collaborator

brentvatne commented on eae4fe8 Oct 4, 2017

nice one 😎

Please sign in to comment.