Skip to content

Commit f71af59

Browse files
committed
Fix: Support ESLint <3.11.0
1 parent 8b55518 commit f71af59

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ node_js:
44
- '4'
55
- '6'
66
- '7'
7+
env:
8+
- ESLINT_VERSION=latest
9+
- ESLINT_VERSION=3.16.0
10+
- ESLINT_VERSION=3.11.0
11+
- ESLINT_VERSION=3.9.0
12+
before_script:
13+
- if [[ $ESLINT_VERSION != "latest" ]]; then
14+
yarn upgrade "eslint@$ESLINT_VERSION";
15+
fi

eslint-plugin-prettier.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,45 @@ 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
}
54+
// Otherwise, try to use the private version from eslint/lib/ast-utils -
55+
// added in ESLint 3.11.0.
5556
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+
}
5787
}
5888

5989
/**

0 commit comments

Comments
 (0)