| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -78,6 +78,7 @@ let isDeepEqual; | |||
| 78 | 78 | let isDeepStrictEqual; | |
| 79 | 79 | let parseExpressionAt; | |
| 80 | 80 | let findNodeAround; | |
| 81 | + let tokenizer; | ||
| 81 | 82 | let decoder; | |
| 82 | 83 | ||
| 83 | 84 | function lazyLoadComparison() { | |
@@ -247,34 +248,37 @@ function parseCode(code, offset) { | |||
| 247 | 248 | ({ findNodeAround } = require('internal/deps/acorn/acorn-walk/dist/walk')); | |
| 248 | 249 | ||
| 249 | 250 | parseExpressionAt = FunctionPrototypeBind(Parser.parseExpressionAt, Parser); | |
| 251 | + tokenizer = FunctionPrototypeBind(Parser.tokenizer, Parser); | ||
| 250 | 252 | } | |
| 251 | 253 | let node; | |
| 252 | - let start = 0; | ||
| 254 | + let start; | ||
| 253 | 255 | // 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 | + } | ||
| 255 | 263 | try { | |
| 256 | 264 | node = parseExpressionAt(code, start, { ecmaVersion: 'latest' }); | |
| 257 | - start = node.end + 1 || start; | ||
| 258 | 265 | // Find the CallExpression in the tree. | |
| 259 | 266 | 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 | + ]; | ||
| 268 | 274 | } | |
| 275 | + // eslint-disable-next-line no-unused-vars | ||
| 276 | + } catch (err) { | ||
| 277 | + continue; | ||
| 269 | 278 | } | |
| 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; | ||
| 278 | 282 | } | |
| 279 | 283 | ||
| 280 | 284 | function getErrMessage(message, fn) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -726,6 +726,61 @@ assert.throws( | |||
| 726 | 726 | 'assert.ok(null)\n' | |
| 727 | 727 | } | |
| 728 | 728 | ); | |
| 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 | + ); | ||
| 729 | 784 | assert.throws( | |
| 730 | 785 | () => assert(typeof 123n === 'string'), | |
| 731 | 786 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments