Skip to content

Commit

Permalink
Fix: Support ESLint <3.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed May 18, 2017
1 parent 8b55518 commit 4943e2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ node_js:
- '4'
- '6'
- '7'
env:
- ESLINT_VERSION=latest
- ESLINT_VERSION=3.15.0
before_script:
- if [[ $ESLINT_VERSION != "latest" ]]; then
yarn upgrade "eslint@$ESLINT_VERSION";
fi
32 changes: 27 additions & 5 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,37 @@ let prettier;
* @returns {Object} An object containing numeric `line` and `column` keys.
*/
function getLocFromIndex(context, index) {
// If `sourceCode.getLocFromIndex` is available from ESLint, use it.
// Otherwise, use the private version from eslint/lib/ast-utils.
// `sourceCode.getLocFromIndex` was added in ESLint 3.16.0.
// If `sourceCode.getLocFromIndex` is available from ESLint, use it - added
// in ESLint 3.16.0.
const sourceCode = context.getSourceCode();
if (typeof sourceCode.getLocFromIndex === 'function') {
return sourceCode.getLocFromIndex(index);
}
const astUtils = require('eslint/lib/ast-utils');
return astUtils.getLocationFromRangeIndex(sourceCode, index);
const text = sourceCode.getText();
if (typeof index !== 'number') {
throw new TypeError('Expected `index` to be a number.');
}
if (index < 0 || index > text.length) {
throw new RangeError('Index out of range.');
}
// Loosely based on
// https://github.com/eslint/eslint/blob/18a519fa/lib/ast-utils.js#L408-L438
const lineEndingPattern = /\r\n|[\r\n\u2028\u2029]/g;
let offset = 0;
let line = 0;
let match;
while ((match = lineEndingPattern.exec(text))) {
const next = match.index + match[0].length;
if (index < next) {
break;
}
line++;
offset = next;
}
return {
line: line + 1,
column: index - offset
};
}

/**
Expand Down

0 comments on commit 4943e2d

Please sign in to comment.