Skip to content
Open
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,28 @@ function getLocFromIndex(sourceCode, index) {
*/
function reportDifference(context, difference) {
const { operation, offset, deleteText = '', insertText = '' } = difference;
// Provided that new characters need to be added, we create a fake one
// indent so that the editor can suggest corrections within
// the specified range.
const insertSuggestionOffset = Math.min(insertText.length, 1);
/** @type {AST.Range} */
const range = [offset, offset + deleteText.length];
const [start, end] = range;
// `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
// with the `sourceCode` property.
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
const sourceCode = context.sourceCode ?? context.getSourceCode();

const [start, end] = range.map(index => getLocFromIndex(sourceCode, index));

context.report({
messageId: operation,
data: {
deleteText: showInvisibles(deleteText),
insertText: showInvisibles(insertText),
},
loc: { start, end },
loc: {
start: getLocFromIndex(sourceCode, start),
end: getLocFromIndex(sourceCode, end + insertSuggestionOffset),
},
fix: fixer => fixer.replaceTextRange(range, insertText),
});
}
Expand Down