Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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