Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e3d53f9

Browse files
BridgeARcodebytere
authored andcommittedJun 27, 2020
util: gracefully handle unknown colors
This makes sure colors that are unknown won't cause an error. This is especially important in case a library wants to use colors defined by Node.js core, if available and fall back to the default otherwise. Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #33797 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 4715a41 commit e3d53f9

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed
 

‎lib/internal/util/inspect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ function stylizeWithColor(str, styleType) {
478478
const style = inspect.styles[styleType];
479479
if (style !== undefined) {
480480
const color = inspect.colors[style];
481-
return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
481+
if (color !== undefined)
482+
return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
482483
}
483484
return str;
484485
}

‎test/parallel/test-util-inspect.js

+6
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,12 @@ assert.strictEqual(
22452245
assert.deepStrictEqual(inspect.colors[bgColor], [40 + i, 49]);
22462246
assert.deepStrictEqual(inspect.colors[`${bgColor}Bright`], [100 + i, 49]);
22472247
});
2248+
2249+
// Unknown colors are handled gracefully:
2250+
const stringStyle = inspect.styles.string;
2251+
inspect.styles.string = 'UNKNOWN';
2252+
assert.strictEqual(inspect('foobar', { colors: true }), "'foobar'");
2253+
inspect.styles.string = stringStyle;
22482254
}
22492255

22502256
assert.strictEqual(

0 commit comments

Comments
 (0)
Please sign in to comment.