Skip to content

Commit 71ee0d9

Browse files
ah-yuaddaleax
authored andcommitted
util: escaping object keys in util.inspect()
PR-URL: #16986 Fixes: #16979 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent 95d9a58 commit 71ee0d9

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

lib/util.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ var Debug;
8181

8282
/* eslint-disable */
8383
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
84-
const keyEscapeSequencesRegExp = /[\x00-\x1f\x27]/;
8584
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
86-
const keyEscapeSequencesReplacer = /[\x00-\x1f\x27]/g;
8785
/* eslint-enable */
8886
const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
8987
const colorRegExp = /\u001b\[\d\d?m/g;
@@ -137,34 +135,6 @@ function strEscape(str) {
137135
return `'${result}'`;
138136
}
139137

140-
// Escape control characters and single quotes.
141-
// Note: for performance reasons this is not combined with strEscape
142-
function keyEscape(str) {
143-
if (str.length < 5000 && !keyEscapeSequencesRegExp.test(str))
144-
return `'${str}'`;
145-
if (str.length > 100)
146-
return `'${str.replace(keyEscapeSequencesReplacer, escapeFn)}'`;
147-
var result = '';
148-
var last = 0;
149-
for (var i = 0; i < str.length; i++) {
150-
const point = str.charCodeAt(i);
151-
if (point === 39 || point < 32) {
152-
if (last === i) {
153-
result += meta[point];
154-
} else {
155-
result += `${str.slice(last, i)}${meta[point]}`;
156-
}
157-
last = i + 1;
158-
}
159-
}
160-
if (last === 0) {
161-
result = str;
162-
} else if (last !== i) {
163-
result += str.slice(last);
164-
}
165-
return `'${result}'`;
166-
}
167-
168138
function tryStringify(arg) {
169139
try {
170140
return JSON.stringify(arg);
@@ -868,7 +838,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
868838
} else if (keyStrRegExp.test(key)) {
869839
name = ctx.stylize(key, 'name');
870840
} else {
871-
name = ctx.stylize(keyEscape(key), 'string');
841+
name = ctx.stylize(strEscape(key), 'string');
872842
}
873843

874844
return `${name}: ${str}`;

test/parallel/test-util-inspect.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,25 +566,31 @@ assert.doesNotThrow(() => {
566566
assert.strictEqual(util.inspect(x).includes('inspect'), true);
567567
}
568568

569-
// util.inspect should not display the escaped value of a key.
569+
// util.inspect should display the escaped value of a key.
570570
{
571571
const w = {
572572
'\\': 1,
573573
'\\\\': 2,
574574
'\\\\\\': 3,
575575
'\\\\\\\\': 4,
576+
'\n': 5,
577+
'\r': 6
576578
};
577579

578580
const y = ['a', 'b', 'c'];
579-
y['\\\\\\'] = 'd';
581+
y['\\\\'] = 'd';
582+
y['\n'] = 'e';
583+
y['\r'] = 'f';
580584

581585
assert.strictEqual(
582586
util.inspect(w),
583-
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }'
587+
'{ \'\\\\\': 1, \'\\\\\\\\\': 2, \'\\\\\\\\\\\\\': 3, ' +
588+
'\'\\\\\\\\\\\\\\\\\': 4, \'\\n\': 5, \'\\r\': 6 }'
584589
);
585590
assert.strictEqual(
586591
util.inspect(y),
587-
'[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]'
592+
'[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\', ' +
593+
'\'\\n\': \'e\', \'\\r\': \'f\' ]'
588594
);
589595
}
590596

0 commit comments

Comments
 (0)