@@ -832,176 +832,162 @@ module m3 { }\
832832 insertCode ( source , index , "Fo" ) ;
833833 } ) ;
834834
835- describe ( "comment directives" , ( ) => {
836- const tsIgnoreComment = "@ts-ignore" ;
837- const textWithIgnoreComment = `const x = 10;
838- function foo() {
839- // @ts-ignore
840- let y: string = x;
841- return y;
842- }
843- function bar() {
844- // @ts-ignore
845- let z : string = x;
846- return z;
847- }
848- function bar2() {
849- // @ts-ignore
850- let z : string = x;
851- return z;
852- }
853- function bar3() {
854- /* @ts-ignore */
855- let z : string = x;
856- return z;
857- }
858- function bar4() {
859- /*
860- @ts-ignore */
861- let z : string = x;
862- return z;
863- }
864- foo();
865- bar();
866- bar3();
867- bar4();
868- bar5()` ;
869- verifyScenario ( "when deleting ts-ignore comment" , verifyDelete ) ;
870- verifyScenario ( "when inserting ts-ignore comment" , verifyInsert ) ;
871- verifyScenario ( "when changing ts-ignore comment to blah" , verifyChangeToBlah ) ;
872- verifyScenario ( "when changing blah comment to ts-ignore" , verifyChangeBackToDirective ) ;
873- verifyScenario ( "when deleting blah comment" , verifyDeletingBlah ) ;
874- verifyScenario ( "when changing text that adds another comment" , verifyChangeDirectiveType ) ;
875- verifyScenario ( "when changing text that keeps the comment but adds more nodes" , verifyReuseChange ) ;
876-
877- function verifyCommentDirectives ( oldText : IScriptSnapshot , newTextAndChange : { text : IScriptSnapshot ; textChangeRange : TextChangeRange ; } ) {
878- const { incrementalNewTree, newTree } = compareTrees ( oldText , newTextAndChange . text , newTextAndChange . textChangeRange , - 1 ) ;
879- assert . deepEqual ( incrementalNewTree . commentDirectives , newTree . commentDirectives ) ;
880- }
881-
882- function verifyScenario ( scenario : string , verifyChange : ( atIndex : number , singleIgnore ?: true ) => void ) {
883- it ( `${ scenario } - 0` , ( ) => {
884- verifyChange ( 0 ) ;
885- } ) ;
886- it ( `${ scenario } - 1` , ( ) => {
887- verifyChange ( 1 ) ;
888- } ) ;
889- it ( `${ scenario } - 2` , ( ) => {
890- verifyChange ( 2 ) ;
891- } ) ;
892- it ( `${ scenario } - 3` , ( ) => {
893- verifyChange ( 3 ) ;
894- } ) ;
895- it ( `${ scenario } - 4` , ( ) => {
896- verifyChange ( 4 ) ;
897- } ) ;
898- it ( `${ scenario } - with single ts-ignore` , ( ) => {
899- verifyChange ( 0 , /*singleIgnore*/ true ) ;
900- } ) ;
901- }
902-
903- function getIndexOfTsIgnoreComment ( atIndex : number ) {
904- let index = 0 ;
905- for ( let i = 0 ; i <= atIndex ; i ++ ) {
906- index = textWithIgnoreComment . indexOf ( tsIgnoreComment , index ) ;
835+ for ( const tsIgnoreComment of [
836+ "// @ts-ignore" ,
837+ "/* @ts-ignore */" ,
838+ "/*\n @ts-ignore */"
839+ ] ) {
840+ describe ( `${ tsIgnoreComment } comment directives` , ( ) => {
841+ const textWithIgnoreComment = `const x = 10;
842+ function foo() {
843+ ${ tsIgnoreComment }
844+ let y: string = x;
845+ return y;
846+ }
847+ function bar() {
848+ ${ tsIgnoreComment }
849+ let z : string = x;
850+ return z;
851+ }
852+ function bar3() {
853+ ${ tsIgnoreComment }
854+ let z : string = x;
855+ return z;
856+ }
857+ foo();
858+ bar();
859+ bar3();` ;
860+ verifyScenario ( "when deleting ts-ignore comment" , verifyDelete ) ;
861+ verifyScenario ( "when inserting ts-ignore comment" , verifyInsert ) ;
862+ verifyScenario ( "when changing ts-ignore comment to blah" , verifyChangeToBlah ) ;
863+ verifyScenario ( "when changing blah comment to ts-ignore" , verifyChangeBackToDirective ) ;
864+ verifyScenario ( "when deleting blah comment" , verifyDeletingBlah ) ;
865+ verifyScenario ( "when changing text that adds another comment" , verifyChangeDirectiveType ) ;
866+ verifyScenario ( "when changing text that keeps the comment but adds more nodes" , verifyReuseChange ) ;
867+
868+ function verifyCommentDirectives ( oldText : IScriptSnapshot , newTextAndChange : { text : IScriptSnapshot ; textChangeRange : TextChangeRange ; } ) {
869+ const { incrementalNewTree, newTree } = compareTrees ( oldText , newTextAndChange . text , newTextAndChange . textChangeRange , - 1 ) ;
870+ assert . deepEqual ( incrementalNewTree . commentDirectives , newTree . commentDirectives ) ;
907871 }
908- return index ;
909- }
910-
911- function textWithIgnoreCommentFrom ( text : string , singleIgnore : true | undefined ) {
912- if ( ! singleIgnore ) return text ;
913- const splits = text . split ( tsIgnoreComment ) ;
914- if ( splits . length > 2 ) {
915- const tail = splits [ splits . length - 2 ] + splits [ splits . length - 1 ] ;
916- splits . length = splits . length - 2 ;
917- return splits . join ( tsIgnoreComment ) + tail ;
872+
873+ function verifyScenario ( scenario : string , verifyChange : ( atIndex : number , singleIgnore ?: true ) => void ) {
874+ it ( `${ scenario } - 0` , ( ) => {
875+ verifyChange ( 0 ) ;
876+ } ) ;
877+ it ( `${ scenario } - 1` , ( ) => {
878+ verifyChange ( 1 ) ;
879+ } ) ;
880+ it ( `${ scenario } - 2` , ( ) => {
881+ verifyChange ( 2 ) ;
882+ } ) ;
883+ it ( `${ scenario } - with single ts-ignore` , ( ) => {
884+ verifyChange ( 0 , /*singleIgnore*/ true ) ;
885+ } ) ;
918886 }
919- else {
920- return splits . join ( tsIgnoreComment ) ;
887+
888+ function getIndexOfTsIgnoreComment ( atIndex : number ) {
889+ let index = 0 ;
890+ for ( let i = 0 ; i <= atIndex ; i ++ ) {
891+ index = textWithIgnoreComment . indexOf ( tsIgnoreComment , index ) ;
892+ }
893+ return index ;
921894 }
922- }
923-
924- function verifyDelete ( atIndex : number , singleIgnore ?: true ) {
925- const index = getIndexOfTsIgnoreComment ( atIndex ) ;
926- const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( textWithIgnoreComment , singleIgnore ) ) ;
927- const newTextAndChange = withDelete ( oldText , index , tsIgnoreComment . length ) ;
928- verifyCommentDirectives ( oldText , newTextAndChange ) ;
929- }
930-
931- function verifyInsert ( atIndex : number , singleIgnore ?: true ) {
932- const index = getIndexOfTsIgnoreComment ( atIndex ) ;
933- const source = textWithIgnoreCommentFrom ( textWithIgnoreComment . slice ( 0 , index ) + textWithIgnoreComment . slice ( index + tsIgnoreComment . length ) , singleIgnore ) ;
934- const oldText = ScriptSnapshot . fromString ( source ) ;
935- const newTextAndChange = withInsert ( oldText , index , tsIgnoreComment ) ;
936- verifyCommentDirectives ( oldText , newTextAndChange ) ;
937- }
938-
939- function verifyChangeToBlah ( atIndex : number , singleIgnore ?: true ) {
940- const index = getIndexOfTsIgnoreComment ( atIndex ) + "// " . length ;
941- const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( textWithIgnoreComment , singleIgnore ) ) ;
942- const newTextAndChange = withChange ( oldText , index , 1 , "blah " ) ;
943- verifyCommentDirectives ( oldText , newTextAndChange ) ;
944- }
945-
946- function verifyChangeBackToDirective ( atIndex : number , singleIgnore ?: true ) {
947- const index = getIndexOfTsIgnoreComment ( atIndex ) + "// " . length ;
948- const source = textWithIgnoreCommentFrom ( textWithIgnoreComment . slice ( 0 , index ) + "blah " + textWithIgnoreComment . slice ( index + 1 ) , singleIgnore ) ;
949- const oldText = ScriptSnapshot . fromString ( source ) ;
950- const newTextAndChange = withChange ( oldText , index , "blah " . length , "@" ) ;
951- verifyCommentDirectives ( oldText , newTextAndChange ) ;
952- }
953-
954- function verifyDeletingBlah ( atIndex : number , singleIgnore ?: true ) {
955- const tsIgnoreIndex = getIndexOfTsIgnoreComment ( atIndex ) ;
956- const index = tsIgnoreIndex + "// " . length ;
957- const source = textWithIgnoreCommentFrom ( textWithIgnoreComment . slice ( 0 , index ) + "blah " + textWithIgnoreComment . slice ( index + 1 ) , singleIgnore ) ;
958- const oldText = ScriptSnapshot . fromString ( source ) ;
959- const newTextAndChange = withDelete ( oldText , tsIgnoreIndex , tsIgnoreComment . length + "blah" . length ) ;
960- verifyCommentDirectives ( oldText , newTextAndChange ) ;
961- }
962-
963- function verifyChangeDirectiveType ( atIndex : number , singleIgnore ?: true ) {
964- const index = getIndexOfTsIgnoreComment ( atIndex ) + "// @ts-" . length ;
965- const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( textWithIgnoreComment , singleIgnore ) ) ;
966- const newTextAndChange = withChange ( oldText , index , "ignore" . length , "expect-error" ) ;
967- verifyCommentDirectives ( oldText , newTextAndChange ) ;
968- }
969-
970- function verifyReuseChange ( atIndex : number , singleIgnore ?: true ) {
971- const source = `const x = 10;
972- function foo1() {
973- const x1 = 10;
974- // @ts-ignore
975- let y0: string = x;
976- let y1: string = x;
977- return y1;
978- }
979- function foo2() {
980- const x2 = 10;
981- // @ts-ignore
982- let y0: string = x;
983- let y2: string = x;
984- return y2;
985- }
986- function foo3() {
987- const x3 = 10;
988- // @ts-ignore
989- let y0: string = x;
990- let y3: string = x;
991- return y3;
992- }
993- foo1();
994- foo2();
995- foo3();` ;
996- const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( source , singleIgnore ) ) ;
997- const start = source . indexOf ( `const x${ atIndex + 1 } ` ) ;
998- const letStr = `let y${ atIndex + 1 } : string = x;` ;
999- const end = source . indexOf ( letStr ) + letStr . length ;
1000- const oldSubStr = source . slice ( start , end ) ;
1001- const newText = oldSubStr . replace ( letStr , `let yn : string = x;` ) ;
1002- const newTextAndChange = withChange ( oldText , start , end - start , newText ) ;
1003- verifyCommentDirectives ( oldText , newTextAndChange ) ;
1004- }
1005- } ) ;
895+
896+ function textWithIgnoreCommentFrom ( text : string , singleIgnore : true | undefined ) {
897+ if ( ! singleIgnore ) return text ;
898+ const splits = text . split ( tsIgnoreComment ) ;
899+ if ( splits . length > 2 ) {
900+ const tail = splits [ splits . length - 2 ] + splits [ splits . length - 1 ] ;
901+ splits . length = splits . length - 2 ;
902+ return splits . join ( tsIgnoreComment ) + tail ;
903+ }
904+ else {
905+ return splits . join ( tsIgnoreComment ) ;
906+ }
907+ }
908+
909+ function verifyDelete ( atIndex : number , singleIgnore ?: true ) {
910+ const index = getIndexOfTsIgnoreComment ( atIndex ) ;
911+ const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( textWithIgnoreComment , singleIgnore ) ) ;
912+ const newTextAndChange = withDelete ( oldText , index , tsIgnoreComment . length ) ;
913+ verifyCommentDirectives ( oldText , newTextAndChange ) ;
914+ }
915+
916+ function verifyInsert ( atIndex : number , singleIgnore ?: true ) {
917+ const index = getIndexOfTsIgnoreComment ( atIndex ) ;
918+ const source = textWithIgnoreCommentFrom ( textWithIgnoreComment . slice ( 0 , index ) + textWithIgnoreComment . slice ( index + tsIgnoreComment . length ) , singleIgnore ) ;
919+ const oldText = ScriptSnapshot . fromString ( source ) ;
920+ const newTextAndChange = withInsert ( oldText , index , tsIgnoreComment ) ;
921+ verifyCommentDirectives ( oldText , newTextAndChange ) ;
922+ }
923+
924+ function verifyChangeToBlah ( atIndex : number , singleIgnore ?: true ) {
925+ const index = getIndexOfTsIgnoreComment ( atIndex ) + "// " . length ;
926+ const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( textWithIgnoreComment , singleIgnore ) ) ;
927+ const newTextAndChange = withChange ( oldText , index , 1 , "blah " ) ;
928+ verifyCommentDirectives ( oldText , newTextAndChange ) ;
929+ }
930+
931+ function verifyChangeBackToDirective ( atIndex : number , singleIgnore ?: true ) {
932+ const index = getIndexOfTsIgnoreComment ( atIndex ) + "// " . length ;
933+ const source = textWithIgnoreCommentFrom ( textWithIgnoreComment . slice ( 0 , index ) + "blah " + textWithIgnoreComment . slice ( index + 1 ) , singleIgnore ) ;
934+ const oldText = ScriptSnapshot . fromString ( source ) ;
935+ const newTextAndChange = withChange ( oldText , index , "blah " . length , "@" ) ;
936+ verifyCommentDirectives ( oldText , newTextAndChange ) ;
937+ }
938+
939+ function verifyDeletingBlah ( atIndex : number , singleIgnore ?: true ) {
940+ const tsIgnoreIndex = getIndexOfTsIgnoreComment ( atIndex ) ;
941+ const index = tsIgnoreIndex + "// " . length ;
942+ const source = textWithIgnoreCommentFrom ( textWithIgnoreComment . slice ( 0 , index ) + "blah " + textWithIgnoreComment . slice ( index + 1 ) , singleIgnore ) ;
943+ const oldText = ScriptSnapshot . fromString ( source ) ;
944+ const newTextAndChange = withDelete ( oldText , tsIgnoreIndex , tsIgnoreComment . length + "blah" . length ) ;
945+ verifyCommentDirectives ( oldText , newTextAndChange ) ;
946+ }
947+
948+ function verifyChangeDirectiveType ( atIndex : number , singleIgnore ?: true ) {
949+ const index = getIndexOfTsIgnoreComment ( atIndex ) + "// @ts-" . length ;
950+ const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( textWithIgnoreComment , singleIgnore ) ) ;
951+ const newTextAndChange = withChange ( oldText , index , "ignore" . length , "expect-error" ) ;
952+ verifyCommentDirectives ( oldText , newTextAndChange ) ;
953+ }
954+
955+ function verifyReuseChange ( atIndex : number , singleIgnore ?: true ) {
956+ const source = `const x = 10;
957+ function foo1() {
958+ const x1 = 10;
959+ // @ts-ignore
960+ let y0: string = x;
961+ let y1: string = x;
962+ return y1;
963+ }
964+ function foo2() {
965+ const x2 = 10;
966+ // @ts-ignore
967+ let y0: string = x;
968+ let y2: string = x;
969+ return y2;
970+ }
971+ function foo3() {
972+ const x3 = 10;
973+ // @ts-ignore
974+ let y0: string = x;
975+ let y3: string = x;
976+ return y3;
977+ }
978+ foo1();
979+ foo2();
980+ foo3();` ;
981+ const oldText = ScriptSnapshot . fromString ( textWithIgnoreCommentFrom ( source , singleIgnore ) ) ;
982+ const start = source . indexOf ( `const x${ atIndex + 1 } ` ) ;
983+ const letStr = `let y${ atIndex + 1 } : string = x;` ;
984+ const end = source . indexOf ( letStr ) + letStr . length ;
985+ const oldSubStr = source . slice ( start , end ) ;
986+ const newText = oldSubStr . replace ( letStr , `let yn : string = x;` ) ;
987+ const newTextAndChange = withChange ( oldText , start , end - start , newText ) ;
988+ verifyCommentDirectives ( oldText , newTextAndChange ) ;
989+ }
990+ } ) ;
991+ }
1006992 } ) ;
1007993}
0 commit comments