6
6
// @ts -check
7
7
8
8
/**
9
- * @typedef {import('eslint').AST.Range } Range
10
- * @typedef {import('eslint').AST.SourceLocation } SourceLocation
11
- * @typedef {import('eslint').ESLint.Plugin } Plugin
12
- * @typedef {import('eslint').ESLint.ObjectMetaProperties } ObjectMetaProperties
13
- * @typedef {import('prettier').FileInfoOptions } FileInfoOptions
14
- * @typedef {import('prettier').Options } PrettierOptions
15
- * @typedef {PrettierOptions & { onDiskFilepath: string, parserMeta?: ObjectMetaProperties['meta'], parserPath?: string, usePrettierrc?: boolean } } Options
16
- * @typedef {(source: string, options: Options, fileInfoOptions: FileInfoOptions) => string } PrettierFormat
9
+ * @import {AST, ESLint, Linter, Rule, SourceCode} from 'eslint'
10
+ * @import {FileInfoOptions, Options as PrettierOptions} from 'prettier'
11
+ * @import {Difference} from 'prettier-linter-helpers'
12
+ */
13
+
14
+ /**
15
+ * @typedef {PrettierOptions & {
16
+ * onDiskFilepath: string;
17
+ * parserMeta?: ESLint.ObjectMetaProperties['meta'];
18
+ * parserPath?: string;
19
+ * usePrettierrc?: boolean;
20
+ * }} Options
21
+ *
22
+ *
23
+ * @typedef {(
24
+ * source: string,
25
+ * options: Options,
26
+ * fileInfoOptions: FileInfoOptions,
27
+ * ) => string} PrettierFormat
17
28
*/
18
29
19
30
'use strict' ;
@@ -39,9 +50,7 @@ const { INSERT, DELETE, REPLACE } = generateDifferences;
39
50
// ------------------------------------------------------------------------------
40
51
41
52
// Lazily-loaded Prettier.
42
- /**
43
- * @type {PrettierFormat }
44
- */
53
+ /** @type {PrettierFormat } */
45
54
let prettierFormat ;
46
55
47
56
// ------------------------------------------------------------------------------
@@ -51,13 +60,14 @@ let prettierFormat;
51
60
/**
52
61
* Reports a difference.
53
62
*
54
- * @param {import('eslint'). Rule.RuleContext } context - The ESLint rule context.
55
- * @param {import('prettier-linter-helpers'). Difference } difference - The difference object.
63
+ * @param {Rule.RuleContext } context - The ESLint rule context.
64
+ * @param {Difference } difference - The difference object.
56
65
* @returns {void }
57
66
*/
58
67
function reportDifference ( context , difference ) {
59
68
const { operation, offset, deleteText = '' , insertText = '' } = difference ;
60
- const range = /** @type {Range } */ ( [ offset , offset + deleteText . length ] ) ;
69
+ /** @type {AST.Range } */
70
+ const range = [ offset , offset + deleteText . length ] ;
61
71
// `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
62
72
// with the `sourceCode` property.
63
73
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
@@ -80,9 +90,7 @@ function reportDifference(context, difference) {
80
90
// Module Definition
81
91
// ------------------------------------------------------------------------------
82
92
83
- /**
84
- * @type {Plugin }
85
- */
93
+ /** @type {ESLint.Plugin } */
86
94
const eslintPluginPrettier = {
87
95
meta : { name, version } ,
88
96
configs : {
@@ -131,18 +139,17 @@ const eslintPluginPrettier = {
131
139
} ,
132
140
} ,
133
141
create ( context ) {
134
- const usePrettierrc =
135
- ! context . options [ 1 ] || context . options [ 1 ] . usePrettierrc !== false ;
136
- /**
137
- * @type {FileInfoOptions }
138
- */
139
- const fileInfoOptions =
140
- ( context . options [ 1 ] && context . options [ 1 ] . fileInfoOptions ) || { } ;
142
+ const options = /** @type {Options | undefined } */ ( context . options [ 1 ] ) ;
143
+ const usePrettierrc = ! options || options . usePrettierrc !== false ;
144
+ /** @type {FileInfoOptions } */
145
+ const fileInfoOptions = options ?. fileInfoOptions || { } ;
141
146
142
147
// `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
143
148
// with the `sourceCode` property.
144
149
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
145
- const sourceCode = context . sourceCode ?? context . getSourceCode ( ) ;
150
+ const sourceCode = /** @type {SourceCode } */ (
151
+ context . sourceCode ?? context . getSourceCode ( )
152
+ ) ;
146
153
// `context.getFilename()` was deprecated in ESLint v8.40.0 and replaced
147
154
// with the `filename` property.
148
155
// TODO: Only use property when our eslint peerDependency is >=8.40.0.
@@ -169,12 +176,12 @@ const eslintPluginPrettier = {
169
176
) ;
170
177
}
171
178
172
- /**
173
- * @type {PrettierOptions }
174
- */
179
+ /** @type {PrettierOptions } */
175
180
const eslintPrettierOptions = context . options [ 0 ] || { } ;
176
181
177
- const parser = context . languageOptions ?. parser ;
182
+ const parser = /** @type {Linter.Parser | undefined } */ (
183
+ context . languageOptions ?. parser
184
+ ) ;
178
185
179
186
// prettier.format() may throw a SyntaxError if it cannot parse the
180
187
// source code it is given. Usually for JS files this isn't a
@@ -184,9 +191,7 @@ const eslintPluginPrettier = {
184
191
// files throw an error if they contain unclosed elements, such as
185
192
// `<template><div></template>. In this case report an error at the
186
193
// point at which parsing failed.
187
- /**
188
- * @type {string }
189
- */
194
+ /** @type {string } */
190
195
let prettierSource ;
191
196
try {
192
197
prettierSource = prettierFormat (
@@ -213,10 +218,12 @@ const eslintPluginPrettier = {
213
218
214
219
let message = 'Parsing error: ' + err . message ;
215
220
216
- const error =
217
- /** @type {SyntaxError & {codeFrame: string; loc?: SourceLocation} } */ (
218
- err
219
- ) ;
221
+ const error = /**
222
+ * @type {SyntaxError & {
223
+ * codeFrame: string;
224
+ * loc?: AST.SourceLocation;
225
+ * }}
226
+ */ ( err ) ;
220
227
221
228
// Prettier's message contains a codeframe style preview of the
222
229
// invalid code and the line/column at which the error occurred.
@@ -243,7 +250,10 @@ const eslintPluginPrettier = {
243
250
const differences = generateDifferences ( source , prettierSource ) ;
244
251
245
252
for ( const difference of differences ) {
246
- reportDifference ( context , difference ) ;
253
+ reportDifference (
254
+ /** @type {Rule.RuleContext } */ ( context ) ,
255
+ difference ,
256
+ ) ;
247
257
}
248
258
}
249
259
} ,
0 commit comments