@@ -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 ) {
0 commit comments