Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a37eb32

Browse filesBrowse 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 23c98fa commit a37eb32
Copy full SHA for a37eb32

File tree

Expand file treeCollapse file tree

2 files changed

+11
-35
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+11
-35
lines changed
Open diff view settings
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+1-31Lines 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);
@@ -859,7 +829,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
859829
} else if (keyStrRegExp.test(key)) {
860830
name = ctx.stylize(key, 'name');
861831
} else {
862-
name = ctx.stylize(keyEscape(key), 'string');
832+
name = ctx.stylize(strEscape(key), 'string');
863833
}
864834

865835
return `${name}: ${str}`;
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+10-4Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,25 +567,31 @@ assert.doesNotThrow(() => {
567567
assert.strictEqual(util.inspect(x).includes('inspect'), true);
568568
}
569569

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

579581
const y = ['a', 'b', 'c'];
580-
y['\\\\\\'] = 'd';
582+
y['\\\\'] = 'd';
583+
y['\n'] = 'e';
584+
y['\r'] = 'f';
581585

582586
assert.strictEqual(
583587
util.inspect(w),
584-
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }'
588+
'{ \'\\\\\': 1, \'\\\\\\\\\': 2, \'\\\\\\\\\\\\\': 3, ' +
589+
'\'\\\\\\\\\\\\\\\\\': 4, \'\\n\': 5, \'\\r\': 6 }'
585590
);
586591
assert.strictEqual(
587592
util.inspect(y),
588-
'[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]'
593+
'[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\', ' +
594+
'\'\\n\': \'e\', \'\\r\': \'f\' ]'
589595
);
590596
}
591597

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.