Skip to content

Commit 0522243

Browse files
ah-yuMylesBorins
authored andcommitted
util: escaping object keys in util.inspect()
PR-URL: #16986 Fixes: #16979 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent 5586089 commit 0522243

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

lib/util.js

+1-31
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ var Debug;
7777

7878
/* eslint-disable */
7979
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
80-
const keyEscapeSequencesRegExp = /[\x00-\x1f\x27]/;
8180
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
82-
const keyEscapeSequencesReplacer = /[\x00-\x1f\x27]/g;
8381
/* eslint-enable */
8482
const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
8583
const colorRegExp = /\u001b\[\d\d?m/g;
@@ -133,34 +131,6 @@ function strEscape(str) {
133131
return `'${result}'`;
134132
}
135133

136-
// Escape control characters and single quotes.
137-
// Note: for performance reasons this is not combined with strEscape
138-
function keyEscape(str) {
139-
if (str.length < 5000 && !keyEscapeSequencesRegExp.test(str))
140-
return `'${str}'`;
141-
if (str.length > 100)
142-
return `'${str.replace(keyEscapeSequencesReplacer, escapeFn)}'`;
143-
var result = '';
144-
var last = 0;
145-
for (var i = 0; i < str.length; i++) {
146-
const point = str.charCodeAt(i);
147-
if (point === 39 || point < 32) {
148-
if (last === i) {
149-
result += meta[point];
150-
} else {
151-
result += `${str.slice(last, i)}${meta[point]}`;
152-
}
153-
last = i + 1;
154-
}
155-
}
156-
if (last === 0) {
157-
result = str;
158-
} else if (last !== i) {
159-
result += str.slice(last);
160-
}
161-
return `'${result}'`;
162-
}
163-
164134
function tryStringify(arg) {
165135
try {
166136
return JSON.stringify(arg);
@@ -851,7 +821,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
851821
} else if (keyStrRegExp.test(key)) {
852822
name = ctx.stylize(key, 'name');
853823
} else {
854-
name = ctx.stylize(keyEscape(key), 'string');
824+
name = ctx.stylize(strEscape(key), 'string');
855825
}
856826

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

test/parallel/test-util-inspect.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -558,25 +558,31 @@ assert.doesNotThrow(() => {
558558
assert.strictEqual(util.inspect(x).includes('inspect'), true);
559559
}
560560

561-
// util.inspect should not display the escaped value of a key.
561+
// util.inspect should display the escaped value of a key.
562562
{
563563
const w = {
564564
'\\': 1,
565565
'\\\\': 2,
566566
'\\\\\\': 3,
567567
'\\\\\\\\': 4,
568+
'\n': 5,
569+
'\r': 6
568570
};
569571

570572
const y = ['a', 'b', 'c'];
571-
y['\\\\\\'] = 'd';
573+
y['\\\\'] = 'd';
574+
y['\n'] = 'e';
575+
y['\r'] = 'f';
572576

573577
assert.strictEqual(
574578
util.inspect(w),
575-
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }'
579+
'{ \'\\\\\': 1, \'\\\\\\\\\': 2, \'\\\\\\\\\\\\\': 3, ' +
580+
'\'\\\\\\\\\\\\\\\\\': 4, \'\\n\': 5, \'\\r\': 6 }'
576581
);
577582
assert.strictEqual(
578583
util.inspect(y),
579-
'[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]'
584+
'[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\', ' +
585+
'\'\\n\': \'e\', \'\\r\': \'f\' ]'
580586
);
581587
}
582588

0 commit comments

Comments
 (0)