Skip to content

Commit e65b459

Browse files
chore: use context properties where context methods are deprecated (#594)
* chore: use context properties where context methods are deprecated But keep supporting the methods for now. --------- Co-authored-by: Ben Scott <[email protected]>
1 parent 17c3c10 commit e65b459

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

eslint-plugin-prettier.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ let prettierFormat;
5454
function reportDifference(context, difference) {
5555
const { operation, offset, deleteText = '', insertText = '' } = difference;
5656
const range = /** @type {Range} */ ([offset, offset + deleteText.length]);
57+
// `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
58+
// with the `sourceCode` property.
59+
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
5760
const [start, end] = range.map(index =>
58-
context.getSourceCode().getLocFromIndex(index),
61+
(context.sourceCode ?? context.getSourceCode()).getLocFromIndex(index),
5962
);
6063

6164
context.report({
@@ -130,14 +133,26 @@ const eslintPluginPrettier = {
130133
*/
131134
const fileInfoOptions =
132135
(context.options[1] && context.options[1].fileInfoOptions) || {};
133-
const sourceCode = context.getSourceCode();
134-
const filepath = context.getFilename();
136+
137+
// `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
138+
// with the `sourceCode` property.
139+
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
140+
const sourceCode = context.sourceCode ?? context.getSourceCode();
141+
// `context.getFilename()` was deprecated in ESLint v8.40.0 and replaced
142+
// with the `filename` property.
143+
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
144+
const filepath = context.filename ?? context.getFilename();
145+
135146
// Processors that extract content from a file, such as the markdown
136147
// plugin extracting fenced code blocks may choose to specify virtual
137148
// file paths. If this is the case then we need to resolve prettier
138149
// config and file info using the on-disk path instead of the virtual
139150
// path.
140-
const onDiskFilepath = context.getPhysicalFilename();
151+
// `context.getPhysicalFilename()` was deprecated in ESLint v8.40.0 and replaced
152+
// with the `physicalFilename` property.
153+
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
154+
const onDiskFilepath =
155+
context.physicalFilename ?? context.getPhysicalFilename();
141156
const source = sourceCode.text;
142157

143158
return {

0 commit comments

Comments
 (0)