@@ -3,6 +3,7 @@ import read_expression from '../read/expression.js';
3
3
import * as e from '../../../errors.js' ;
4
4
import { create_fragment } from '../utils/create.js' ;
5
5
import { walk } from 'zimmerframe' ;
6
+ import { parse_expression_at } from '../acorn.js' ;
6
7
7
8
const regex_whitespace_with_closing_curly_brace = / ^ \s * } / ;
8
9
@@ -270,68 +271,41 @@ function open(parser) {
270
271
271
272
parser . eat ( '(' , true ) ;
272
273
273
- parser . allow_whitespace ( ) ;
274
-
275
- /** @type {import('estree').Pattern[] } */
276
- const parameters = [ ] ;
277
-
278
- while ( ! parser . match ( ')' ) ) {
279
- let pattern = read_pattern ( parser , true ) ;
280
-
281
- parser . allow_whitespace ( ) ;
282
- if ( parser . eat ( '=' ) ) {
283
- parser . allow_whitespace ( ) ;
284
-
285
- let index = parser . index ;
286
- let right = read_expression ( parser ) ;
287
-
288
- parser . allow_whitespace ( ) ;
289
-
290
- // multiple assignment expression will be confused by the parser for a sequence expression
291
- // so if the returned value is a sequence expression we use the first expression as the
292
- // right and we reset the parser.index
293
- if ( right . type === 'SequenceExpression' && parser . template [ index ] !== '(' ) {
294
- right = right . expressions [ 0 ] ;
295
- parser . index = /** @type {number } */ ( right . end ) ;
296
- }
297
-
298
- pattern = {
299
- type : 'AssignmentPattern' ,
300
- left : pattern ,
301
- right,
302
- start : pattern . start ,
303
- end : right . end
304
- } ;
305
- }
306
-
307
- parameters . push ( pattern ) ;
274
+ let parentheses = 1 ;
275
+ let params = '' ;
308
276
309
- if ( ! parser . eat ( ',' ) ) break ;
310
- parser . allow_whitespace ( ) ;
277
+ while ( ! parser . match ( ')' ) || parentheses !== 1 ) {
278
+ if ( parser . match ( '(' ) ) parentheses ++ ;
279
+ if ( parser . match ( ')' ) ) parentheses -- ;
280
+ params += parser . read ( / ^ ./ ) ;
311
281
}
312
282
313
- parser . eat ( ')' , true ) ;
283
+ const function_expression = parse_expression_at ( `function ${ name } ( ${ params } ){}` , parser . ts , 0 ) ;
314
284
315
- parser . allow_whitespace ( ) ;
316
- parser . eat ( '}' , true ) ;
285
+ parser . index += 2 ;
317
286
318
- /** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>> } */
319
- const block = parser . append ( {
320
- type : 'SnippetBlock' ,
321
- start,
322
- end : - 1 ,
323
- expression : {
324
- type : 'Identifier' ,
325
- start : name_start ,
326
- end : name_end ,
327
- name
328
- } ,
329
- parameters,
330
- body : create_fragment ( )
331
- } ) ;
332
-
333
- parser . stack . push ( block ) ;
334
- parser . fragments . push ( block . body ) ;
287
+ if ( function_expression . type === 'FunctionExpression' ) {
288
+ /** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>> } */
289
+ const block = parser . append ( {
290
+ type : 'SnippetBlock' ,
291
+ start,
292
+ end : - 1 ,
293
+ expression : {
294
+ type : 'Identifier' ,
295
+ start : name_start ,
296
+ end : name_end ,
297
+ name
298
+ } ,
299
+ parameters : function_expression . params . map ( ( param ) => {
300
+ param . start += start + 1 ;
301
+ param . end += start + 1 ;
302
+ return param ;
303
+ } ) ,
304
+ body : create_fragment ( )
305
+ } ) ;
306
+ parser . stack . push ( block ) ;
307
+ parser . fragments . push ( block . body ) ;
308
+ }
335
309
336
310
return ;
337
311
}
0 commit comments