Skip to content

Commit 46ac276

Browse files
committed
format string tests
1 parent 2c8a145 commit 46ac276

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,26 @@ describe('utils', () => {
189189
]);
190190
expect(formatWithStyles(['%%c%c'], 'color: gray')).toEqual(['%%c%c']);
191191
});
192+
193+
it('should format non string inputs as the first argument', () => {
194+
expect(formatWithStyles([{foo: 'bar'}])).toEqual([{foo: 'bar'}]);
195+
expect(formatWithStyles([[1, 2, 3]])).toEqual([[1, 2, 3]]);
196+
expect(formatWithStyles([{foo: 'bar'}], 'color: gray')).toEqual([
197+
'%c%o',
198+
'color: gray',
199+
{foo: 'bar'},
200+
]);
201+
expect(formatWithStyles([[1, 2, 3]], 'color: gray')).toEqual([
202+
'%c%o',
203+
'color: gray',
204+
[1, 2, 3],
205+
]);
206+
expect(formatWithStyles([{foo: 'bar'}, 'hi'], 'color: gray')).toEqual([
207+
'%c%o %s',
208+
'color: gray',
209+
{foo: 'bar'},
210+
'hi',
211+
]);
212+
});
192213
});
193214
});

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,16 @@ export function formatWithStyles(
181181
inputArgs === undefined ||
182182
inputArgs === null ||
183183
inputArgs.length === 0 ||
184-
typeof inputArgs[0] !== 'string' ||
185184
// Matches any of %c but not %%c
186-
inputArgs[0].match(/([^%]|^)(%c)/g) ||
185+
(typeof inputArgs[0] === 'string' && inputArgs[0].match(/([^%]|^)(%c)/g)) ||
187186
style === undefined
188187
) {
189188
return inputArgs;
190189
}
191190

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

packages/react-devtools-shared/src/hook.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,17 @@ export function installHook(target: any): DevToolsHook | null {
180180
inputArgs === undefined ||
181181
inputArgs === null ||
182182
inputArgs.length === 0 ||
183-
typeof inputArgs[0] !== 'string' ||
184183
// Matches any of %c but not %%c
185-
inputArgs[0].match(/([^%]|^)(%c)/g) ||
184+
(typeof inputArgs[0] === 'string' &&
185+
inputArgs[0].match(/([^%]|^)(%c)/g)) ||
186186
style === undefined
187187
) {
188188
return inputArgs;
189189
}
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)