Skip to content

Commit

Permalink
assert: fix exception message for assert(0) on try catch block
Browse files Browse the repository at this point in the history
Fixes: #30872
PR-URL: #46760
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Kohei Ueno <[email protected]>
  • Loading branch information
hidecology authored and targos committed Mar 14, 2023
1 parent b3e1034 commit e11f08e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ let isDeepEqual;
let isDeepStrictEqual;
let parseExpressionAt;
let findNodeAround;
let tokenizer;
let decoder;

function lazyLoadComparison() {
Expand Down Expand Up @@ -247,34 +248,37 @@ function parseCode(code, offset) {
({ findNodeAround } = require('internal/deps/acorn/acorn-walk/dist/walk'));

parseExpressionAt = FunctionPrototypeBind(Parser.parseExpressionAt, Parser);
tokenizer = FunctionPrototypeBind(Parser.tokenizer, Parser);
}
let node;
let start = 0;
let start;
// Parse the read code until the correct expression is found.
do {
for (const token of tokenizer(code, { ecmaVersion: 'latest' })) {
start = token.start;
if (start > offset) {
// No matching expression found. This could happen if the assert
// expression is bigger than the provided buffer.
break;
}
try {
node = parseExpressionAt(code, start, { ecmaVersion: 'latest' });
start = node.end + 1 || start;
// Find the CallExpression in the tree.
node = findNodeAround(node, offset, 'CallExpression');
} catch (err) {
// Unexpected token error and the like.
start += err.raisedAt || 1;
if (start > offset) {
// No matching expression found. This could happen if the assert
// expression is bigger than the provided buffer.
// eslint-disable-next-line no-throw-literal
throw null;
if (node?.node.end >= offset) {
return [
node.node.start,
StringPrototypeReplace(StringPrototypeSlice(code,
node.node.start, node.node.end),
escapeSequencesRegExp, escapeFn),
];
}
// eslint-disable-next-line no-unused-vars
} catch (err) {
continue;
}
} while (node === undefined || node.node.end < offset);

return [
node.node.start,
StringPrototypeReplace(StringPrototypeSlice(code,
node.node.start, node.node.end),
escapeSequencesRegExp, escapeFn),
];
}
// eslint-disable-next-line no-throw-literal
throw null;
}

function getErrMessage(message, fn) {
Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,61 @@ assert.throws(
'assert.ok(null)\n'
}
);
assert.throws(
() => {
// This test case checks if `try` left brace without a line break
// before the assertion causes any wrong assertion message.
// Therefore, don't reformat the following code.
// Refs: https://github.com/nodejs/node/issues/30872
try { assert.ok(0); // eslint-disable-line no-useless-catch, brace-style
} catch (err) {
throw err;
}
},
{
code: 'ERR_ASSERTION',
constructor: assert.AssertionError,
generatedMessage: true,
message: 'The expression evaluated to a falsy value:\n\n ' +
'assert.ok(0)\n'
}
);
assert.throws(
() => {
try {
throw new Error();
// This test case checks if `catch` left brace without a line break
// before the assertion causes any wrong assertion message.
// Therefore, don't reformat the following code.
// Refs: https://github.com/nodejs/node/issues/30872
} catch (err) { assert.ok(0); } // eslint-disable-line no-unused-vars
},
{
code: 'ERR_ASSERTION',
constructor: assert.AssertionError,
generatedMessage: true,
message: 'The expression evaluated to a falsy value:\n\n ' +
'assert.ok(0)\n'
}
);
assert.throws(
() => {
// This test case checks if `function` left brace without a line break
// before the assertion causes any wrong assertion message.
// Therefore, don't reformat the following code.
// Refs: https://github.com/nodejs/node/issues/30872
function test() { assert.ok(0); // eslint-disable-line brace-style
}
test();
},
{
code: 'ERR_ASSERTION',
constructor: assert.AssertionError,
generatedMessage: true,
message: 'The expression evaluated to a falsy value:\n\n ' +
'assert.ok(0)\n'
}
);
assert.throws(
() => assert(typeof 123n === 'string'),
{
Expand Down

0 comments on commit e11f08e

Please sign in to comment.