mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
assert: fix exception message for assert(0) on try catch block
Fixes: https://github.com/nodejs/node/issues/30872 PR-URL: https://github.com/nodejs/node/pull/46760 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
This commit is contained in:
parent
ee1ce1872f
commit
3c0131a419
@ -78,6 +78,7 @@ let isDeepEqual;
|
||||
let isDeepStrictEqual;
|
||||
let parseExpressionAt;
|
||||
let findNodeAround;
|
||||
let tokenizer;
|
||||
let decoder;
|
||||
|
||||
function lazyLoadComparison() {
|
||||
@ -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) {
|
||||
|
@ -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'),
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user