Skip to content

Commit 5296930

Browse files
Fix: avoid relying on an internal eslint function
1 parent 3fd855d commit 5296930

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
'eslint-plugin/report-message-format': ['error', '^[^a-z].*\\.$'],
2424
'array-bracket-spacing': 'off',
2525
'comma-dangle': 'off',
26+
indent: 'off',
2627
'lines-around-directive': 'off',
2728
'space-before-function-paren': 'off'
2829
}

lib/rules/prettier.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

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

1110
// ------------------------------------------------------------------------------
1211
// Rule Definition
@@ -16,6 +15,23 @@ module.exports = {
1615
create(context) {
1716
const sourceCode = context.getSourceCode();
1817

18+
/**
19+
* Gets the location of a given index in the source code
20+
* @param {number} index An index in the source code
21+
* @returns {object} An object containing numberic `line` and `column` keys
22+
*/
23+
function getLocation(index) {
24+
// If sourceCode.getLocFromIndex is available from eslint, use it.
25+
// Otherwise, use the private version from eslint/lib/ast-utils.
26+
27+
return sourceCode.getLocFromIndex
28+
? sourceCode.getLocFromIndex(index)
29+
: require('eslint/lib/ast-utils').getLocationFromRangeIndex(
30+
sourceCode,
31+
index
32+
);
33+
}
34+
1935
return {
2036
Program() {
2137
// This isn't really very performant (prettier needs to reparse the text).
@@ -40,8 +56,7 @@ module.exports = {
4056
: sourceCode.text[firstBadIndex];
4157

4258
context.report({
43-
loc: astUtils.getLocationFromRangeIndex(
44-
sourceCode,
59+
loc: getLocation(
4560
firstBadIndex === -1 ? desiredText.length : firstBadIndex
4661
),
4762
message: 'Follow `prettier` formatting (expected {{expectedChar}} but found {{foundChar}}).',

0 commit comments

Comments
 (0)