@@ -103,7 +103,7 @@ namespace M
103
103
/*body */ createBlock ( statements )
104
104
) ;
105
105
106
- changeTracker . insertNodeBefore ( sourceFile , /*before*/ findChild ( "M2" , sourceFile ) , newFunction , { suffix : newLineCharacter } ) ;
106
+ changeTracker . insertNodeBefore ( sourceFile , /*before*/ findChild ( "M2" , sourceFile ) , newFunction ) ;
107
107
108
108
// replace statements with return statement
109
109
const newStatement = createReturn (
@@ -129,12 +129,11 @@ function bar() {
129
129
changeTracker . deleteRange ( sourceFile , { pos : text . indexOf ( "function foo" ) , end : text . indexOf ( "function bar" ) } ) ;
130
130
} ) ;
131
131
}
132
- function findVariableStatementContaining ( name : string , sourceFile : SourceFile ) {
133
- const varDecl = findChild ( name , sourceFile ) ;
134
- assert . equal ( varDecl . kind , SyntaxKind . VariableDeclaration ) ;
135
- const varStatement = varDecl . parent . parent ;
136
- assert . equal ( varStatement . kind , SyntaxKind . VariableStatement ) ;
137
- return varStatement ;
132
+ function findVariableStatementContaining ( name : string , sourceFile : SourceFile ) : VariableStatement {
133
+ return cast ( findVariableDeclarationContaining ( name , sourceFile ) . parent . parent , isVariableStatement ) ;
134
+ }
135
+ function findVariableDeclarationContaining ( name : string , sourceFile : SourceFile ) : VariableDeclaration {
136
+ return cast ( findChild ( name , sourceFile ) , isVariableDeclaration ) ;
138
137
}
139
138
{
140
139
const text = `
@@ -306,11 +305,11 @@ var y; // comment 4
306
305
var z = 3; // comment 5
307
306
// comment 6
308
307
var a = 4; // comment 7` ;
309
- runSingleFileTest ( "insertNodeAt1 " , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ true , ( sourceFile , changeTracker ) => {
310
- changeTracker . insertNodeAt ( sourceFile , text . indexOf ( "var y" ) , createTestClass ( ) , { suffix : newLineCharacter } ) ;
308
+ runSingleFileTest ( "insertNodeBefore3 " , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ true , ( sourceFile , changeTracker ) => {
309
+ changeTracker . insertNodeBefore ( sourceFile , findVariableStatementContaining ( "y" , sourceFile ) , createTestClass ( ) ) ;
311
310
} ) ;
312
- runSingleFileTest ( "insertNodeAt2 " , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
313
- changeTracker . insertNodeAt ( sourceFile , text . indexOf ( "; // comment 4" ) , createTestVariableDeclaration ( "z1" ) ) ;
311
+ runSingleFileTest ( "insertNodeAfterVariableDeclaration " , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
312
+ changeTracker . insertNodeAfter ( sourceFile , findVariableDeclarationContaining ( "y" , sourceFile ) , createTestVariableDeclaration ( "z1" ) ) ;
314
313
} ) ;
315
314
}
316
315
{
@@ -325,23 +324,22 @@ namespace M {
325
324
var a = 4; // comment 7
326
325
}` ;
327
326
runSingleFileTest ( "insertNodeBefore1" , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ true , ( sourceFile , changeTracker ) => {
328
- changeTracker . insertNodeBefore ( sourceFile , findVariableStatementContaining ( "y" , sourceFile ) , createTestClass ( ) , { suffix : newLineCharacter } ) ;
327
+ changeTracker . insertNodeBefore ( sourceFile , findVariableStatementContaining ( "y" , sourceFile ) , createTestClass ( ) ) ;
329
328
} ) ;
330
329
runSingleFileTest ( "insertNodeBefore2" , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ true , ( sourceFile , changeTracker ) => {
331
- changeTracker . insertNodeBefore ( sourceFile , findChild ( "M" , sourceFile ) , createTestClass ( ) , { suffix : newLineCharacter } ) ;
330
+ changeTracker . insertNodeBefore ( sourceFile , findChild ( "M" , sourceFile ) , createTestClass ( ) ) ;
332
331
} ) ;
333
332
runSingleFileTest ( "insertNodeAfter1" , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ true , ( sourceFile , changeTracker ) => {
334
- changeTracker . insertNodeAfter ( sourceFile , findVariableStatementContaining ( "y" , sourceFile ) , createTestClass ( ) , { suffix : newLineCharacter } ) ;
333
+ changeTracker . insertNodeAfter ( sourceFile , findVariableStatementContaining ( "y" , sourceFile ) , createTestClass ( ) ) ;
335
334
} ) ;
336
335
runSingleFileTest ( "insertNodeAfter2" , /*placeOpenBraceOnNewLineForFunctions*/ true , text , /*validateNodes*/ true , ( sourceFile , changeTracker ) => {
337
- changeTracker . insertNodeAfter ( sourceFile , findChild ( "M" , sourceFile ) , createTestClass ( ) , { prefix : newLineCharacter } ) ;
336
+ changeTracker . insertNodeAfter ( sourceFile , findChild ( "M" , sourceFile ) , createTestClass ( ) ) ;
338
337
} ) ;
339
338
}
340
339
341
- function findOpenBraceForConstructor ( sourceFile : SourceFile ) {
340
+ function findConstructor ( sourceFile : SourceFile ) : ConstructorDeclaration {
342
341
const classDecl = < ClassDeclaration > sourceFile . statements [ 0 ] ;
343
- const constructorDecl = forEach ( classDecl . members , m => m . kind === SyntaxKind . Constructor && ( < ConstructorDeclaration > m ) . body && < ConstructorDeclaration > m ) ;
344
- return constructorDecl . body . getFirstToken ( ) ;
342
+ return find < ClassElement , ConstructorDeclaration > ( classDecl . members , ( m ) : m is ConstructorDeclaration => isConstructorDeclaration ( m ) && ! ! m . body ) ! ;
345
343
}
346
344
function createTestSuperCall ( ) {
347
345
const superCall = createCall (
@@ -359,8 +357,8 @@ class A {
359
357
}
360
358
}
361
359
` ;
362
- runSingleFileTest ( "insertNodeAfter3 " , /*placeOpenBraceOnNewLineForFunctions*/ false , text1 , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
363
- changeTracker . insertNodeAfter ( sourceFile , findOpenBraceForConstructor ( sourceFile ) , createTestSuperCall ( ) , { suffix : newLineCharacter } ) ;
360
+ runSingleFileTest ( "insertNodeAtConstructorStart " , /*placeOpenBraceOnNewLineForFunctions*/ false , text1 , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
361
+ changeTracker . insertNodeAtConstructorStart ( sourceFile , findConstructor ( sourceFile ) , createTestSuperCall ( ) ) ;
364
362
} ) ;
365
363
const text2 = `
366
364
class A {
@@ -370,7 +368,7 @@ class A {
370
368
}
371
369
` ;
372
370
runSingleFileTest ( "insertNodeAfter4" , /*placeOpenBraceOnNewLineForFunctions*/ false , text2 , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
373
- changeTracker . insertNodeAfter ( sourceFile , findVariableStatementContaining ( "x" , sourceFile ) , createTestSuperCall ( ) , { suffix : newLineCharacter } ) ;
371
+ changeTracker . insertNodeAfter ( sourceFile , findVariableStatementContaining ( "x" , sourceFile ) , createTestSuperCall ( ) ) ;
374
372
} ) ;
375
373
const text3 = `
376
374
class A {
@@ -379,8 +377,8 @@ class A {
379
377
}
380
378
}
381
379
` ;
382
- runSingleFileTest ( "insertNodeAfter3 -block with newline" , /*placeOpenBraceOnNewLineForFunctions*/ false , text3 , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
383
- changeTracker . insertNodeAfter ( sourceFile , findOpenBraceForConstructor ( sourceFile ) , createTestSuperCall ( ) , { suffix : newLineCharacter } ) ;
380
+ runSingleFileTest ( "insertNodeAtConstructorStart -block with newline" , /*placeOpenBraceOnNewLineForFunctions*/ false , text3 , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
381
+ changeTracker . insertNodeAtConstructorStart ( sourceFile , findConstructor ( sourceFile ) , createTestSuperCall ( ) ) ;
384
382
} ) ;
385
383
}
386
384
{
@@ -638,7 +636,7 @@ class A {
638
636
}
639
637
const insertAfter = findChild ( "x" , sourceFile ) ;
640
638
for ( const newNode of newNodes ) {
641
- changeTracker . insertNodeAfter ( sourceFile , insertAfter , newNode , { suffix : newLineCharacter } ) ;
639
+ changeTracker . insertNodeAfter ( sourceFile , insertAfter , newNode ) ;
642
640
}
643
641
} ) ;
644
642
}
@@ -649,7 +647,7 @@ class A {
649
647
}
650
648
` ;
651
649
runSingleFileTest ( "insertNodeAfterInClass1" , /*placeOpenBraceOnNewLineForFunctions*/ false , text , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
652
- changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , createProperty ( undefined , undefined , "a" , undefined , createKeywordTypeNode ( SyntaxKind . BooleanKeyword ) , undefined ) , { suffix : newLineCharacter } ) ;
650
+ changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , createProperty ( undefined , undefined , "a" , undefined , createKeywordTypeNode ( SyntaxKind . BooleanKeyword ) , undefined ) ) ;
653
651
} ) ;
654
652
}
655
653
{
@@ -659,7 +657,7 @@ class A {
659
657
}
660
658
` ;
661
659
runSingleFileTest ( "insertNodeAfterInClass2" , /*placeOpenBraceOnNewLineForFunctions*/ false , text , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
662
- changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , createProperty ( undefined , undefined , "a" , undefined , createKeywordTypeNode ( SyntaxKind . BooleanKeyword ) , undefined ) , { suffix : newLineCharacter } ) ;
660
+ changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , createProperty ( undefined , undefined , "a" , undefined , createKeywordTypeNode ( SyntaxKind . BooleanKeyword ) , undefined ) ) ;
663
661
} ) ;
664
662
}
665
663
{
@@ -698,7 +696,7 @@ class A {
698
696
/*questionToken*/ undefined ,
699
697
createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ,
700
698
/*initializer*/ undefined ) ;
701
- changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode , { suffix : newLineCharacter } ) ;
699
+ changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode ) ;
702
700
} ) ;
703
701
}
704
702
{
@@ -716,7 +714,7 @@ class A {
716
714
/*questionToken*/ undefined ,
717
715
createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ,
718
716
/*initializer*/ undefined ) ;
719
- changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode , { suffix : newLineCharacter } ) ;
717
+ changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode ) ;
720
718
} ) ;
721
719
}
722
720
{
@@ -733,7 +731,7 @@ interface A {
733
731
/*questionToken*/ undefined ,
734
732
createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ,
735
733
/*initializer*/ undefined ) ;
736
- changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode , { suffix : newLineCharacter } ) ;
734
+ changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode ) ;
737
735
} ) ;
738
736
}
739
737
{
@@ -750,7 +748,7 @@ interface A {
750
748
/*questionToken*/ undefined ,
751
749
createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ,
752
750
/*initializer*/ undefined ) ;
753
- changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode , { suffix : newLineCharacter } ) ;
751
+ changeTracker . insertNodeAfter ( sourceFile , findChild ( "x" , sourceFile ) , newNode ) ;
754
752
} ) ;
755
753
}
756
754
{
@@ -759,7 +757,7 @@ let x = foo
759
757
` ;
760
758
runSingleFileTest ( "insertNodeInStatementListAfterNodeWithoutSeparator1" , /*placeOpenBraceOnNewLineForFunctions*/ false , text , /*validateNodes*/ false , ( sourceFile , changeTracker ) => {
761
759
const newNode = createStatement ( createParen ( createLiteral ( 1 ) ) ) ;
762
- changeTracker . insertNodeAfter ( sourceFile , findVariableStatementContaining ( "x" , sourceFile ) , newNode , { suffix : newLineCharacter } ) ;
760
+ changeTracker . insertNodeAfter ( sourceFile , findVariableStatementContaining ( "x" , sourceFile ) , newNode ) ;
763
761
} ) ;
764
762
}
765
763
} ) ;
0 commit comments