Skip to content

Commit

Permalink
Fix: avoid relying on an internal eslint function
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Feb 26, 2017
1 parent 3fd855d commit 5296930
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
'eslint-plugin/report-message-format': ['error', '^[^a-z].*\\.$'],
'array-bracket-spacing': 'off',
'comma-dangle': 'off',
indent: 'off',
'lines-around-directive': 'off',
'space-before-function-paren': 'off'
}
Expand Down
21 changes: 18 additions & 3 deletions lib/rules/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

const util = require('util');
const prettier = require('prettier');
const astUtils = require('eslint/lib/ast-utils');

// ------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -16,6 +15,23 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();

/**
* Gets the location of a given index in the source code
* @param {number} index An index in the source code
* @returns {object} An object containing numberic `line` and `column` keys
*/
function getLocation(index) {
// If sourceCode.getLocFromIndex is available from eslint, use it.
// Otherwise, use the private version from eslint/lib/ast-utils.

return sourceCode.getLocFromIndex
? sourceCode.getLocFromIndex(index)
: require('eslint/lib/ast-utils').getLocationFromRangeIndex(
sourceCode,
index
);
}

return {
Program() {
// This isn't really very performant (prettier needs to reparse the text).
Expand All @@ -40,8 +56,7 @@ module.exports = {
: sourceCode.text[firstBadIndex];

context.report({
loc: astUtils.getLocationFromRangeIndex(
sourceCode,
loc: getLocation(
firstBadIndex === -1 ? desiredText.length : firstBadIndex
),
message: 'Follow `prettier` formatting (expected {{expectedChar}} but found {{foundChar}}).',
Expand Down

0 comments on commit 5296930

Please sign in to comment.