Skip to content

Commit 6e79ecf

Browse files
committed
fix formatWithStyles + tests
1 parent d20c3af commit 6e79ecf

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/react-devtools-shared/src/__tests__/utils-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ describe('utils', () => {
121121
expect(formatWithStyles(undefined)).toEqual(undefined);
122122
});
123123

124+
it('should format non string inputs', () => {
125+
expect(formatWithStyles([{foo: 'bar'}])).toEqual([{foo: 'bar'}]);
126+
expect(formatWithStyles([[1, 2, 3]])).toEqual([[1, 2, 3]]);
127+
expect(formatWithStyles([{foo: 'bar'}], 'color: gray')).toEqual([
128+
'%c%o',
129+
'color: gray',
130+
{foo: 'bar'},
131+
]);
132+
expect(formatWithStyles([[1, 2, 3]], 'color: gray')).toEqual([
133+
'%c%o',
134+
'color: gray',
135+
[1, 2, 3],
136+
]);
137+
});
138+
124139
it('should bail out of strings with styles', () => {
125140
expect(
126141
formatWithStyles(['%ca', 'color: green', 'b', 'c'], 'color: gray'),

packages/react-devtools-shared/src/backend/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function formatWithStyles(
190190

191191
// Matches any of %(o|O|d|i|s|f), but not %%(o|O|d|i|s|f)
192192
const REGEXP = /([^%]|^)((%%)*)(%([oOdisf]))/g;
193-
if (inputArgs[0].match(REGEXP)) {
193+
if (typeof inputArgs[0] === 'string' && inputArgs[0].match(REGEXP)) {
194194
return [`%c${inputArgs[0]}`, style, ...inputArgs.slice(1)];
195195
} else {
196196
const firstArg = inputArgs.reduce((formatStr, elem, i) => {

0 commit comments

Comments
 (0)