Skip to content

Commit c742493

Browse files
hidecologydanielleadams
authored andcommitted
assert: fix exception message for assert(0) on try catch block
Fixes: #30872 PR-URL: #46760 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Kohei Ueno <[email protected]>
1 parent d4af671 commit c742493

File tree

2 files changed

+78
-19
lines changed

2 files changed

+78
-19
lines changed

lib/assert.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ let isDeepEqual;
7878
let isDeepStrictEqual;
7979
let parseExpressionAt;
8080
let findNodeAround;
81+
let tokenizer;
8182
let decoder;
8283

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

249250
parseExpressionAt = FunctionPrototypeBind(Parser.parseExpressionAt, Parser);
251+
tokenizer = FunctionPrototypeBind(Parser.tokenizer, Parser);
250252
}
251253
let node;
252-
let start = 0;
254+
let start;
253255
// Parse the read code until the correct expression is found.
254-
do {
256+
for (const token of tokenizer(code, { ecmaVersion: 'latest' })) {
257+
start = token.start;
258+
if (start > offset) {
259+
// No matching expression found. This could happen if the assert
260+
// expression is bigger than the provided buffer.
261+
break;
262+
}
255263
try {
256264
node = parseExpressionAt(code, start, { ecmaVersion: 'latest' });
257-
start = node.end + 1 || start;
258265
// Find the CallExpression in the tree.
259266
node = findNodeAround(node, offset, 'CallExpression');
260-
} catch (err) {
261-
// Unexpected token error and the like.
262-
start += err.raisedAt || 1;
263-
if (start > offset) {
264-
// No matching expression found. This could happen if the assert
265-
// expression is bigger than the provided buffer.
266-
// eslint-disable-next-line no-throw-literal
267-
throw null;
267+
if (node?.node.end >= offset) {
268+
return [
269+
node.node.start,
270+
StringPrototypeReplace(StringPrototypeSlice(code,
271+
node.node.start, node.node.end),
272+
escapeSequencesRegExp, escapeFn),
273+
];
268274
}
275+
// eslint-disable-next-line no-unused-vars
276+
} catch (err) {
277+
continue;
269278
}
270-
} while (node === undefined || node.node.end < offset);
271-
272-
return [
273-
node.node.start,
274-
StringPrototypeReplace(StringPrototypeSlice(code,
275-
node.node.start, node.node.end),
276-
escapeSequencesRegExp, escapeFn),
277-
];
279+
}
280+
// eslint-disable-next-line no-throw-literal
281+
throw null;
278282
}
279283

280284
function getErrMessage(message, fn) {

test/parallel/test-assert.js

+55
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,61 @@ assert.throws(
726726
'assert.ok(null)\n'
727727
}
728728
);
729+
assert.throws(
730+
() => {
731+
// This test case checks if `try` left brace without a line break
732+
// before the assertion causes any wrong assertion message.
733+
// Therefore, don't reformat the following code.
734+
// Refs: https://github.com/nodejs/node/issues/30872
735+
try { assert.ok(0); // eslint-disable-line no-useless-catch, brace-style
736+
} catch (err) {
737+
throw err;
738+
}
739+
},
740+
{
741+
code: 'ERR_ASSERTION',
742+
constructor: assert.AssertionError,
743+
generatedMessage: true,
744+
message: 'The expression evaluated to a falsy value:\n\n ' +
745+
'assert.ok(0)\n'
746+
}
747+
);
748+
assert.throws(
749+
() => {
750+
try {
751+
throw new Error();
752+
// This test case checks if `catch` left brace without a line break
753+
// before the assertion causes any wrong assertion message.
754+
// Therefore, don't reformat the following code.
755+
// Refs: https://github.com/nodejs/node/issues/30872
756+
} catch (err) { assert.ok(0); } // eslint-disable-line no-unused-vars
757+
},
758+
{
759+
code: 'ERR_ASSERTION',
760+
constructor: assert.AssertionError,
761+
generatedMessage: true,
762+
message: 'The expression evaluated to a falsy value:\n\n ' +
763+
'assert.ok(0)\n'
764+
}
765+
);
766+
assert.throws(
767+
() => {
768+
// This test case checks if `function` left brace without a line break
769+
// before the assertion causes any wrong assertion message.
770+
// Therefore, don't reformat the following code.
771+
// Refs: https://github.com/nodejs/node/issues/30872
772+
function test() { assert.ok(0); // eslint-disable-line brace-style
773+
}
774+
test();
775+
},
776+
{
777+
code: 'ERR_ASSERTION',
778+
constructor: assert.AssertionError,
779+
generatedMessage: true,
780+
message: 'The expression evaluated to a falsy value:\n\n ' +
781+
'assert.ok(0)\n'
782+
}
783+
);
729784
assert.throws(
730785
() => assert(typeof 123n === 'string'),
731786
{

0 commit comments

Comments
 (0)