@@ -78,6 +78,7 @@ let isDeepEqual;
7878let isDeepStrictEqual ;
7979let parseExpressionAt ;
8080let findNodeAround ;
81+ let tokenizer ;
8182let decoder ;
8283
8384function 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
280284function getErrMessage ( message , fn ) {
0 commit comments