Skip to content

Commit 761294c

Browse files
chore: use context properties where context methods are deprecated
But keep supporting the methods for now.
1 parent 17c3c10 commit 761294c

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

eslint-plugin-prettier.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ let prettierFormat;
5454
function reportDifference(context, difference) {
5555
const { operation, offset, deleteText = '', insertText = '' } = difference;
5656
const range = /** @type {Range} */ ([offset, offset + deleteText.length]);
57-
const [start, end] = range.map(index =>
58-
context.getSourceCode().getLocFromIndex(index),
59-
);
57+
const [start, end] = range.map(index => {
58+
const sourceCode = context.sourceCode ?? context.getSourceCode(); // `context.sourceCode` was added in ESLint v8.40.0. By checking both the property and the method, breaking change is avoided. TODO: Only use property when requiring a minimum version of v8.40.0 or higher of ESLint.
59+
return sourceCode.getLocFromIndex(index);
60+
});
6061

6162
context.report({
6263
messageId: operation,
@@ -130,14 +131,15 @@ const eslintPluginPrettier = {
130131
*/
131132
const fileInfoOptions =
132133
(context.options[1] && context.options[1].fileInfoOptions) || {};
133-
const sourceCode = context.getSourceCode();
134-
const filepath = context.getFilename();
134+
const sourceCode = context.sourceCode ?? context.getSourceCode(); // `context.sourceCode` was added in ESLint v8.40.0. By checking both the property and the method, breaking change is avoided. TODO: Only use property when requiring a minimum version of v8.40.0 or higher of ESLint.
135+
const filepath = context.filename ?? context.getFilename(); // `context.filename` was added in ESLint v8.40.0. By checking both the property and the method, breaking change is avoided. TODO: Only use property when requiring a minimum version of v8.40.0 or higher of ESLint.
135136
// Processors that extract content from a file, such as the markdown
136137
// plugin extracting fenced code blocks may choose to specify virtual
137138
// file paths. If this is the case then we need to resolve prettier
138139
// config and file info using the on-disk path instead of the virtual
139140
// path.
140-
const onDiskFilepath = context.getPhysicalFilename();
141+
const onDiskFilepath =
142+
context.physicalFilename ?? context.getPhysicalFilename(); // `context.physicalFilename` was added in ESLint v8.40.0. By checking both the property and the method, breaking change is avoided. TODO: Only use property when requiring a minimum version of v8.40.0 or higher of ESLint.
141143
const source = sourceCode.text;
142144

143145
return {

0 commit comments

Comments
 (0)