@@ -45,15 +45,45 @@ let prettier;
45
45
* @returns {Object } An object containing numeric `line` and `column` keys.
46
46
*/
47
47
function getLocFromIndex ( context , index ) {
48
- // If `sourceCode.getLocFromIndex` is available from ESLint, use it.
49
- // Otherwise, use the private version from eslint/lib/ast-utils.
50
- // `sourceCode.getLocFromIndex` was added in ESLint 3.16.0.
48
+ // If `sourceCode.getLocFromIndex` is available from ESLint, use it - added
49
+ // in ESLint 3.16.0.
51
50
const sourceCode = context . getSourceCode ( ) ;
52
51
if ( typeof sourceCode . getLocFromIndex === 'function' ) {
53
52
return sourceCode . getLocFromIndex ( index ) ;
54
53
}
54
+ // Otherwise, try to use the private version from eslint/lib/ast-utils -
55
+ // added in ESLint 3.11.0.
55
56
const astUtils = require ( 'eslint/lib/ast-utils' ) ;
56
- return astUtils . getLocationFromRangeIndex ( sourceCode , index ) ;
57
+ if ( typeof astUtils . getLocationFromRangeIndex === 'function' ) {
58
+ return astUtils . getLocationFromRangeIndex ( sourceCode , index ) ;
59
+ }
60
+ // `getLocFromIndex` and `getLocationFromRangeIndex` are preferred because
61
+ // they use a line index cache, so lastly use this unoptimized version.
62
+ const text = sourceCode . getText ( ) ;
63
+ const lines = sourceCode . getLines ( ) ;
64
+ if ( typeof index !== 'number' ) {
65
+ throw new TypeError ( 'Expected `index` to be a number.' ) ;
66
+ }
67
+ if ( index < 0 || index > text . length ) {
68
+ throw new RangeError ( 'Index out of range.' ) ;
69
+ }
70
+ if ( index === text . length ) {
71
+ return {
72
+ line : lines . length ,
73
+ column : lines [ lines . length - 1 ] . length
74
+ } ;
75
+ }
76
+ let offset = 0 ;
77
+ for ( let i = 0 ; i < lines . length ; i ++ ) {
78
+ if ( index >= offset && index <= offset + lines [ i ] . length ) {
79
+ return {
80
+ line : i + 1 ,
81
+ column : index - offset
82
+ } ;
83
+ }
84
+ // Add 1 for the line ending character
85
+ offset = offset + lines [ i ] . length + 1 ;
86
+ }
57
87
}
58
88
59
89
/**
0 commit comments