From e0e0ef7bab0b80e8ba76c8ecaf1eb0c8e2a365cc Mon Sep 17 00:00:00 2001 From: buji Date: Mon, 13 Nov 2017 19:44:16 +0800 Subject: [PATCH] util: escaping object keys in util.inspect() PR-URL: https://github.com/nodejs/node/pull/16986 Fixes: https://github.com/nodejs/node/issues/16979 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Anatoli Papirovski --- lib/util.js | 32 +----------------------------- test/parallel/test-util-inspect.js | 14 +++++++++---- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/lib/util.js b/lib/util.js index 0b1d25ba22a520..4c6db1a479e7b3 100644 --- a/lib/util.js +++ b/lib/util.js @@ -77,9 +77,7 @@ var Debug; /* eslint-disable */ const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/; -const keyEscapeSequencesRegExp = /[\x00-\x1f\x27]/; const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g; -const keyEscapeSequencesReplacer = /[\x00-\x1f\x27]/g; /* eslint-enable */ const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/; const colorRegExp = /\u001b\[\d\d?m/g; @@ -133,34 +131,6 @@ function strEscape(str) { return `'${result}'`; } -// Escape control characters and single quotes. -// Note: for performance reasons this is not combined with strEscape -function keyEscape(str) { - if (str.length < 5000 && !keyEscapeSequencesRegExp.test(str)) - return `'${str}'`; - if (str.length > 100) - return `'${str.replace(keyEscapeSequencesReplacer, escapeFn)}'`; - var result = ''; - var last = 0; - for (var i = 0; i < str.length; i++) { - const point = str.charCodeAt(i); - if (point === 39 || point < 32) { - if (last === i) { - result += meta[point]; - } else { - result += `${str.slice(last, i)}${meta[point]}`; - } - last = i + 1; - } - } - if (last === 0) { - result = str; - } else if (last !== i) { - result += str.slice(last); - } - return `'${result}'`; -} - function tryStringify(arg) { try { return JSON.stringify(arg); @@ -851,7 +821,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) { } else if (keyStrRegExp.test(key)) { name = ctx.stylize(key, 'name'); } else { - name = ctx.stylize(keyEscape(key), 'string'); + name = ctx.stylize(strEscape(key), 'string'); } return `${name}: ${str}`; diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 81c669fa126d8e..d9f8e30616657d 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -558,25 +558,31 @@ assert.doesNotThrow(() => { assert.strictEqual(util.inspect(x).includes('inspect'), true); } -// util.inspect should not display the escaped value of a key. +// util.inspect should display the escaped value of a key. { const w = { '\\': 1, '\\\\': 2, '\\\\\\': 3, '\\\\\\\\': 4, + '\n': 5, + '\r': 6 }; const y = ['a', 'b', 'c']; - y['\\\\\\'] = 'd'; + y['\\\\'] = 'd'; + y['\n'] = 'e'; + y['\r'] = 'f'; assert.strictEqual( util.inspect(w), - '{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }' + '{ \'\\\\\': 1, \'\\\\\\\\\': 2, \'\\\\\\\\\\\\\': 3, ' + + '\'\\\\\\\\\\\\\\\\\': 4, \'\\n\': 5, \'\\r\': 6 }' ); assert.strictEqual( util.inspect(y), - '[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]' + '[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\', ' + + '\'\\n\': \'e\', \'\\r\': \'f\' ]' ); }