Skip to content

Commit fde7fdf

Browse files
authored
Fix: Support ESLint <3.11.0 (#24)
1 parent 8b55518 commit fde7fdf

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ node_js:
44
- '4'
55
- '6'
66
- '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

eslint-plugin-prettier.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,37 @@ let prettier;
4545
* @returns {Object} An object containing numeric `line` and `column` keys.
4646
*/
4747
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.
5150
const sourceCode = context.getSourceCode();
5251
if (typeof sourceCode.getLocFromIndex === 'function') {
5352
return sourceCode.getLocFromIndex(index);
5453
}
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+
};
5779
}
5880

5981
/**

0 commit comments

Comments
 (0)