@@ -322,49 +322,24 @@ namespace ts.formatting {
322
322
return false ;
323
323
}
324
324
325
- function getListIfVisualStartEndIsInListRange ( list : NodeArray < Node > | undefined , start : number , end : number , node : Node , sourceFile : SourceFile ) {
326
- return list && rangeContainsVisualStartEnd ( list ) ? list : undefined ;
327
-
328
- // Assumes a list is wrapped by list tokens
329
- function rangeContainsVisualStartEnd ( textRange : TextRange ) : boolean {
330
- const children = node . getChildren ( ) ;
331
- for ( let i = 1 ; i < children . length - 1 ; i ++ ) {
332
- if ( children [ i ] . pos === textRange . pos && children [ i ] . end === textRange . end ) {
333
- return rangeContainsStartEnd ( { pos : children [ i - 1 ] . end , end : children [ i + 1 ] . getStart ( sourceFile ) } , start , end ) ;
334
- }
335
- }
336
- return rangeContainsStartEnd ( textRange , start , end ) ;
337
- }
338
- }
339
-
340
- function getListIfStartEndIsInListRange ( list : NodeArray < Node > | undefined , start : number , end : number ) {
341
- return list && rangeContainsStartEnd ( list , start , end ) ? list : undefined ;
342
- }
343
-
344
325
export function getContainingList ( node : Node , sourceFile : SourceFile ) : NodeArray < Node > | undefined {
345
- if ( node . parent ) {
346
- return getListByRange ( node . getStart ( sourceFile ) , node . getEnd ( ) , node . parent , sourceFile ) ;
347
- }
348
- return undefined ;
326
+ return node . parent && getListByRange ( node . getStart ( sourceFile ) , node . getEnd ( ) , node . parent , sourceFile ) ;
349
327
}
350
328
351
329
function getListByPosition ( pos : number , node : Node , sourceFile : SourceFile ) : NodeArray < Node > | undefined {
352
- if ( ! node ) {
353
- return ;
354
- }
355
- return getListByRange ( pos , pos , node , sourceFile ) ;
330
+ return node && getListByRange ( pos , pos , node , sourceFile ) ;
356
331
}
357
332
358
333
function getListByRange ( start : number , end : number , node : Node , sourceFile : SourceFile ) : NodeArray < Node > | undefined {
359
334
switch ( node . kind ) {
360
335
case SyntaxKind . TypeReference :
361
- return getListIfVisualStartEndIsInListRange ( ( < TypeReferenceNode > node ) . typeArguments , start , end , node , sourceFile ) ;
336
+ return getList ( ( < TypeReferenceNode > node ) . typeArguments ) ;
362
337
case SyntaxKind . ObjectLiteralExpression :
363
- return getListIfVisualStartEndIsInListRange ( ( < ObjectLiteralExpression > node ) . properties , start , end , node , sourceFile ) ;
338
+ return getList ( ( < ObjectLiteralExpression > node ) . properties ) ;
364
339
case SyntaxKind . ArrayLiteralExpression :
365
- return getListIfVisualStartEndIsInListRange ( ( < ArrayLiteralExpression > node ) . elements , start , end , node , sourceFile ) ;
340
+ return getList ( ( < ArrayLiteralExpression > node ) . elements ) ;
366
341
case SyntaxKind . TypeLiteral :
367
- return getListIfVisualStartEndIsInListRange ( ( < TypeLiteralNode > node ) . members , start , end , node , sourceFile ) ;
342
+ return getList ( ( < TypeLiteralNode > node ) . members ) ;
368
343
case SyntaxKind . FunctionDeclaration :
369
344
case SyntaxKind . FunctionExpression :
370
345
case SyntaxKind . ArrowFunction :
@@ -373,32 +348,40 @@ namespace ts.formatting {
373
348
case SyntaxKind . CallSignature :
374
349
case SyntaxKind . Constructor :
375
350
case SyntaxKind . ConstructorType :
376
- case SyntaxKind . ConstructSignature : {
377
- return getListIfVisualStartEndIsInListRange ( ( < SignatureDeclaration > node ) . typeParameters , start , end , node , sourceFile ) ||
378
- getListIfVisualStartEndIsInListRange ( ( < SignatureDeclaration > node ) . parameters , start , end , node , sourceFile ) ;
379
- }
351
+ case SyntaxKind . ConstructSignature :
352
+ return getList ( ( < SignatureDeclaration > node ) . typeParameters ) || getList ( ( < SignatureDeclaration > node ) . parameters ) ;
380
353
case SyntaxKind . ClassDeclaration :
381
354
case SyntaxKind . ClassExpression :
382
355
case SyntaxKind . InterfaceDeclaration :
383
356
case SyntaxKind . TypeAliasDeclaration :
384
- case SyntaxKind . JSDocTemplateTag : {
385
- const { typeParameters } = < ClassDeclaration | ClassExpression | InterfaceDeclaration | TypeAliasDeclaration | JSDocTemplateTag > node ;
386
- return getListIfVisualStartEndIsInListRange ( typeParameters , start , end , node , sourceFile ) ;
387
- }
357
+ case SyntaxKind . JSDocTemplateTag :
358
+ return getList ( ( < ClassDeclaration | ClassExpression | InterfaceDeclaration | TypeAliasDeclaration | JSDocTemplateTag > node ) . typeParameters ) ;
388
359
case SyntaxKind . NewExpression :
389
- case SyntaxKind . CallExpression : {
390
- return getListIfVisualStartEndIsInListRange ( ( < CallExpression > node ) . typeArguments , start , end , node , sourceFile ) ||
391
- getListIfVisualStartEndIsInListRange ( ( < CallExpression > node ) . arguments , start , end , node , sourceFile ) ;
392
- }
360
+ case SyntaxKind . CallExpression :
361
+ return getList ( ( < CallExpression > node ) . typeArguments ) || getList ( ( < CallExpression > node ) . arguments ) ;
393
362
case SyntaxKind . VariableDeclarationList :
394
- return getListIfStartEndIsInListRange ( ( < VariableDeclarationList > node ) . declarations , start , end ) ;
363
+ return getList ( ( < VariableDeclarationList > node ) . declarations ) ;
395
364
case SyntaxKind . NamedImports :
396
365
case SyntaxKind . NamedExports :
397
- return getListIfVisualStartEndIsInListRange ( ( < NamedImportsOrExports > node ) . elements , start , end , node , sourceFile ) ;
366
+ return getList ( ( < NamedImportsOrExports > node ) . elements ) ;
398
367
case SyntaxKind . ObjectBindingPattern :
399
368
case SyntaxKind . ArrayBindingPattern :
400
- return getListIfVisualStartEndIsInListRange ( ( < ObjectBindingPattern | ArrayBindingPattern > node ) . elements , start , end , node , sourceFile ) ;
369
+ return getList ( ( < ObjectBindingPattern | ArrayBindingPattern > node ) . elements ) ;
370
+ }
371
+
372
+ function getList ( list : NodeArray < Node > | undefined ) : NodeArray < Node > | undefined {
373
+ return list && rangeContainsStartEnd ( getVisualListRange ( node , list , sourceFile ) , start , end ) ? list : undefined ;
374
+ }
375
+ }
376
+
377
+ function getVisualListRange ( node : Node , list : TextRange , sourceFile : SourceFile ) : TextRange {
378
+ const children = node . getChildren ( sourceFile ) ;
379
+ for ( let i = 1 ; i < children . length - 1 ; i ++ ) {
380
+ if ( children [ i ] . pos === list . pos && children [ i ] . end === list . end ) {
381
+ return { pos : children [ i - 1 ] . end , end : children [ i + 1 ] . getStart ( sourceFile ) } ;
382
+ }
401
383
}
384
+ return list ;
402
385
}
403
386
404
387
function getActualIndentationForListStartLine ( list : NodeArray < Node > , sourceFile : SourceFile , options : EditorSettings ) : number {
0 commit comments