Skip to content

Commit

Permalink
util: validate formatWithOptions inspectOptions
Browse files Browse the repository at this point in the history
This makes sure that the `inspectOptions` are validated. This could
otherwise cause confusion.

Fixes: #29726

PR-URL: #29824
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
BridgeAR authored and Trott committed Oct 11, 2019
1 parent 3aeae8d commit 2664dac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1542,11 +1542,6 @@ function reduceToSingleString(
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
}

function format(...args) {
return formatWithOptions(undefined, ...args);
}


const firstErrorLine = (error) => error.message.split('\n')[0];
let CIRCULAR_ERROR_MESSAGE;
function tryStringify(arg) {
Expand All @@ -1569,7 +1564,19 @@ function tryStringify(arg) {
}
}

function format(...args) {
return formatWithOptionsInternal(undefined, ...args);
}

function formatWithOptions(inspectOptions, ...args) {
if (typeof inspectOptions !== 'object' || inspectOptions === null) {
throw new ERR_INVALID_ARG_TYPE(
'inspectOptions', 'object', inspectOptions);
}
return formatWithOptionsInternal(inspectOptions, ...args);
}

function formatWithOptionsInternal(inspectOptions, ...args) {
const first = args[0];
let a = 0;
let str = '';
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,20 @@ assert.strictEqual(
),
'[ 1, [Object] ]'
);

[
undefined,
null,
false,
5n,
5,
'test',
Symbol()
].forEach((invalidOptions) => {
assert.throws(() => {
util.formatWithOptions(invalidOptions, { a: true });
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: /"inspectOptions".+object/
});
});

0 comments on commit 2664dac

Please sign in to comment.