Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util, test: improve inspect escaping #21624

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ let internalDeepEqual;
/* eslint-disable no-control-regex */
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
const strEscapeSequencesRegExpSingle = /[\x00-\x1f\x5c]/;
const strEscapeSequencesReplacerSingle = /[\x00-\x1f\x5c]/g;

/* eslint-enable no-control-regex */

const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
Expand All @@ -116,21 +119,56 @@ const meta = [
'', '', '', '', '', '', '', '\\\\'
];

function addQuotes(str, quotes) {
if (quotes === -1) {
return `"${str}"`;
}
if (quotes === -2) {
return `\`${str}\``;
}
return `'${str}'`;
}

const escapeFn = (str) => meta[str.charCodeAt(0)];

// Escape control characters, single quotes and the backslash.
// This is similar to JSON stringify escaping.
function strEscape(str) {
let escapeTest = strEscapeSequencesRegExp;
let escapeReplace = strEscapeSequencesReplacer;
let singleQuote = 39;

// Check for double quotes. If not present, do not escape single quotes and
// instead wrap the text in double quotes. If double quotes exist, check for
// backticks. If they do not exist, use those as fallback instead of the
// double quotes.
if (str.indexOf("'") !== -1) {
// This invalidates the charCode and therefore can not be matched for
// anymore.
if (str.indexOf('"') === -1) {
singleQuote = -1;
} else if (str.indexOf('`') === -1 && str.indexOf('${') === -1) {
singleQuote = -2;
}
if (singleQuote !== 39) {
escapeTest = strEscapeSequencesRegExpSingle;
escapeReplace = strEscapeSequencesReplacerSingle;
}
}

// Some magic numbers that worked out fine while benchmarking with v8 6.0
if (str.length < 5000 && !strEscapeSequencesRegExp.test(str))
return `'${str}'`;
if (str.length > 100)
return `'${str.replace(strEscapeSequencesReplacer, escapeFn)}'`;
if (str.length < 5000 && !escapeTest.test(str))
return addQuotes(str, singleQuote);
if (str.length > 100) {
str = str.replace(escapeReplace, escapeFn);
return addQuotes(str, singleQuote);
}

let result = '';
let last = 0;
for (var i = 0; i < str.length; i++) {
const point = str.charCodeAt(i);
if (point === 39 || point === 92 || point < 32) {
if (point === singleQuote || point === 92 || point < 32) {
if (last === i) {
result += meta[point];
} else {
Expand All @@ -144,7 +182,7 @@ function strEscape(str) {
} else if (last !== i) {
result += str.slice(last);
}
return `'${result}'`;
return addQuotes(result, singleQuote);
}

function tryStringify(arg) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ process.on('exit', function() {
// https://github.com/nodejs/node/pull/16485#issuecomment-350428638
// The color setting of the REPL should not have leaked over into
// the color setting of `util.inspect.defaultOptions`.
strictEqual(output.includes(`'\\'string\\''`), true);
strictEqual(output.includes(`"'string'"`), true);
strictEqual(output.includes(`'\u001b[32m\\'string\\'\u001b[39m'`), false);
strictEqual(inspect.defaultOptions.colors, false);
strictEqual(repl.writer.options.colors, true);
Expand Down
Loading