File tree 2 files changed +34
-5
lines changed
2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change @@ -4,3 +4,10 @@ node_js:
4
4
- ' 4'
5
5
- ' 6'
6
6
- ' 7'
7
+ env :
8
+ - ESLINT_VERSION=latest
9
+ - ESLINT_VERSION=3.15.0
10
+ before_script :
11
+ - if [[ $ESLINT_VERSION != "latest" ]]; then
12
+ yarn upgrade "eslint@$ESLINT_VERSION";
13
+ fi
Original file line number Diff line number Diff line change @@ -45,15 +45,37 @@ 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
}
55
- const astUtils = require ( 'eslint/lib/ast-utils' ) ;
56
- return astUtils . getLocationFromRangeIndex ( sourceCode , index ) ;
54
+ const text = sourceCode . getText ( ) ;
55
+ if ( typeof index !== 'number' ) {
56
+ throw new TypeError ( 'Expected `index` to be a number.' ) ;
57
+ }
58
+ if ( index < 0 || index > text . length ) {
59
+ throw new RangeError ( 'Index out of range.' ) ;
60
+ }
61
+ // Loosely based on
62
+ // https://github.com/eslint/eslint/blob/18a519fa/lib/ast-utils.js#L408-L438
63
+ const lineEndingPattern = / \r \n | [ \r \n \u2028 \u2029 ] / g;
64
+ let offset = 0 ;
65
+ let line = 0 ;
66
+ let match ;
67
+ while ( ( match = lineEndingPattern . exec ( text ) ) ) {
68
+ const next = match . index + match [ 0 ] . length ;
69
+ if ( index < next ) {
70
+ break ;
71
+ }
72
+ line ++ ;
73
+ offset = next ;
74
+ }
75
+ return {
76
+ line : line + 1 ,
77
+ column : index - offset
78
+ } ;
57
79
}
58
80
59
81
/**
You can’t perform that action at this time.
0 commit comments