66// @ts -check
77
88/**
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
1728 */
1829
1930'use strict' ;
@@ -39,9 +50,7 @@ const { INSERT, DELETE, REPLACE } = generateDifferences;
3950// ------------------------------------------------------------------------------
4051
4152// Lazily-loaded Prettier.
42- /**
43- * @type {PrettierFormat }
44- */
53+ /** @type {PrettierFormat } */
4554let prettierFormat ;
4655
4756// ------------------------------------------------------------------------------
@@ -51,13 +60,14 @@ let prettierFormat;
5160/**
5261 * Reports a difference.
5362 *
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.
5665 * @returns {void }
5766 */
5867function reportDifference ( context , difference ) {
5968 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 ] ;
6171 // `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
6272 // with the `sourceCode` property.
6373 // TODO: Only use property when our eslint peerDependency is >=8.40.0.
@@ -80,9 +90,7 @@ function reportDifference(context, difference) {
8090// Module Definition
8191// ------------------------------------------------------------------------------
8292
83- /**
84- * @type {Plugin }
85- */
93+ /** @type {ESLint.Plugin } */
8694const eslintPluginPrettier = {
8795 meta : { name, version } ,
8896 configs : {
@@ -131,18 +139,17 @@ const eslintPluginPrettier = {
131139 } ,
132140 } ,
133141 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 || { } ;
141146
142147 // `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
143148 // with the `sourceCode` property.
144149 // 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+ ) ;
146153 // `context.getFilename()` was deprecated in ESLint v8.40.0 and replaced
147154 // with the `filename` property.
148155 // TODO: Only use property when our eslint peerDependency is >=8.40.0.
@@ -169,12 +176,12 @@ const eslintPluginPrettier = {
169176 ) ;
170177 }
171178
172- /**
173- * @type {PrettierOptions }
174- */
179+ /** @type {PrettierOptions } */
175180 const eslintPrettierOptions = context . options [ 0 ] || { } ;
176181
177- const parser = context . languageOptions ?. parser ;
182+ const parser = /** @type {Linter.Parser | undefined } */ (
183+ context . languageOptions ?. parser
184+ ) ;
178185
179186 // prettier.format() may throw a SyntaxError if it cannot parse the
180187 // source code it is given. Usually for JS files this isn't a
@@ -184,9 +191,7 @@ const eslintPluginPrettier = {
184191 // files throw an error if they contain unclosed elements, such as
185192 // `<template><div></template>. In this case report an error at the
186193 // point at which parsing failed.
187- /**
188- * @type {string }
189- */
194+ /** @type {string } */
190195 let prettierSource ;
191196 try {
192197 prettierSource = prettierFormat (
@@ -213,10 +218,12 @@ const eslintPluginPrettier = {
213218
214219 let message = 'Parsing error: ' + err . message ;
215220
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 ) ;
220227
221228 // Prettier's message contains a codeframe style preview of the
222229 // invalid code and the line/column at which the error occurred.
@@ -243,7 +250,10 @@ const eslintPluginPrettier = {
243250 const differences = generateDifferences ( source , prettierSource ) ;
244251
245252 for ( const difference of differences ) {
246- reportDifference ( context , difference ) ;
253+ reportDifference (
254+ /** @type {Rule.RuleContext } */ ( context ) ,
255+ difference ,
256+ ) ;
247257 }
248258 }
249259 } ,
0 commit comments