@@ -914,38 +914,49 @@ describe("ast-utils", () => {
914914 } ) ;
915915
916916 describe ( "getNextLocation" , ( ) => {
917- const code = "foo;\n" ;
918- const ast = espree . parse ( code , ESPREE_CONFIG ) ;
919- const sourceCode = new SourceCode ( code , ast ) ;
920-
921- it ( "should handle normal case" , ( ) => {
922- assert . deepStrictEqual (
923- astUtils . getNextLocation (
924- sourceCode ,
925- { line : 1 , column : 0 }
926- ) ,
927- { line : 1 , column : 1 }
928- ) ;
929- } ) ;
930917
931- it ( "should handle linebreaks" , ( ) => {
932- assert . deepStrictEqual (
933- astUtils . getNextLocation (
934- sourceCode ,
935- { line : 1 , column : 4 }
936- ) ,
937- { line : 2 , column : 0 }
938- ) ;
939- } ) ;
918+ /* eslint-disable quote-props */
919+ const expectedResults = {
920+ "" : [ [ 1 , 0 ] , null ] ,
921+ "\n" : [ [ 1 , 0 ] , [ 2 , 0 ] , null ] ,
922+ "\r\n" : [ [ 1 , 0 ] , [ 2 , 0 ] , null ] ,
923+ "foo" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 1 , 3 ] , null ] ,
924+ "foo\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 1 , 3 ] , [ 2 , 0 ] , null ] ,
925+ "foo\r\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 1 , 3 ] , [ 2 , 0 ] , null ] ,
926+ "foo;\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 2 , 0 ] , null ] ,
927+ "a\nb" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 2 , 0 ] , [ 2 , 1 ] , null ] ,
928+ "a\nb\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 2 , 0 ] , [ 2 , 1 ] , [ 3 , 0 ] , null ] ,
929+ "a\r\nb\r\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 2 , 0 ] , [ 2 , 1 ] , [ 3 , 0 ] , null ] ,
930+ "a\nb\r\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 2 , 0 ] , [ 2 , 1 ] , [ 3 , 0 ] , null ] ,
931+ "a\n\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 2 , 0 ] , [ 3 , 0 ] , null ] ,
932+ "a\r\n\r\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 2 , 0 ] , [ 3 , 0 ] , null ] ,
933+ "\n\r\n\n\r\n" : [ [ 1 , 0 ] , [ 2 , 0 ] , [ 3 , 0 ] , [ 4 , 0 ] , [ 5 , 0 ] , null ] ,
934+ "ab\u2029c" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 0 ] , [ 2 , 1 ] , null ] ,
935+ "ab\ncde\n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 0 ] , [ 2 , 1 ] , [ 2 , 2 ] , [ 2 , 3 ] , [ 3 , 0 ] , null ] ,
936+ "a " : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , null ] ,
937+ "a\t" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , null ] ,
938+ "a \n" : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 0 ] , null ]
939+ } ;
940+ /* eslint-enable quote-props */
940941
941- it ( "should return null when result is out of bound" , ( ) => {
942- assert . strictEqual (
943- astUtils . getNextLocation (
944- sourceCode ,
945- { line : 2 , column : 0 }
946- ) ,
947- null
948- ) ;
942+ Object . keys ( expectedResults ) . forEach ( code => {
943+ it ( `should return expected locations for "${ code } ".` , ( ) => {
944+ const ast = espree . parse ( code , ESPREE_CONFIG ) ;
945+ const sourceCode = new SourceCode ( code , ast ) ;
946+ const locations = expectedResults [ code ] ;
947+
948+ for ( let i = 0 ; i < locations . length - 1 ; i ++ ) {
949+ const location = { line : locations [ i ] [ 0 ] , column : locations [ i ] [ 1 ] } ;
950+ const expectedNextLocation = locations [ i + 1 ]
951+ ? { line : locations [ i + 1 ] [ 0 ] , column : locations [ i + 1 ] [ 1 ] }
952+ : null ;
953+
954+ assert . deepStrictEqual (
955+ astUtils . getNextLocation ( sourceCode , location ) ,
956+ expectedNextLocation
957+ ) ;
958+ }
959+ } ) ;
949960 } ) ;
950961 } ) ;
951962
0 commit comments