Skip to content

Commit 9e37575

Browse files
yashtech00JounQin
andcommitted
chore: migrate @typedefjsdoc to @import (#729)
Co-authored-by: JounQin <[email protected]>
1 parent 27030dd commit 9e37575

File tree

6 files changed

+98
-54
lines changed

6 files changed

+98
-54
lines changed

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"singleQuote": true,
44
"plugins": [
55
"@prettier/plugin-pug",
6+
"prettier-plugin-jsdoc",
67
"prettier-plugin-pkg",
78
"prettier-plugin-svelte"
89
],

eslint-plugin-prettier.js

+47-37
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
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} */
4554
let 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
*/
5867
function 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} */
8694
const 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
},

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"lint-staged": "^15.5.0",
9393
"mocha": "^11.1.0",
9494
"prettier": "^3.5.3",
95+
"prettier-plugin-jsdoc": "^1.3.2",
9596
"prettier-plugin-pkg": "^0.19.0",
9697
"prettier-plugin-svelte": "^3.3.3",
9798
"simple-git-hooks": "^2.12.1",

pnpm-lock.yaml

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prettier.mjs

+13-10
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,7 @@ runFixture('*.mdx', [
291291
],
292292
]);
293293

294-
/**
295-
* @see https://github.com/sveltejs/svelte/blob/226bf419f9b9b5f1a6da33bd6403dd70afe58b52/packages/svelte/package.json#L73
296-
*/
294+
/** @see https://github.com/sveltejs/svelte/blob/226bf419f9b9b5f1a6da33bd6403dd70afe58b52/packages/svelte/package.json#L73 */
297295
const svelteUnsupported = +process.versions.node.split('.')[0] < 16;
298296

299297
runFixture(
@@ -387,10 +385,10 @@ runFixture('invalid-prettierrc/*', [
387385
// ------------------------------------------------------------------------------
388386

389387
/**
390-
* Reads a fixture file and returns an "invalid" case for use by RuleTester.
391-
* The fixture format aims to reduce the pain of debugging offsets by keeping
392-
* the lines and columns of the test code as close to what the rule will report
393-
* as possible.
388+
* Reads a fixture file and returns an "invalid" case for use by RuleTester. The
389+
* fixture format aims to reduce the pain of debugging offsets by keeping the
390+
* lines and columns of the test code as close to what the rule will report as
391+
* possible.
394392
*
395393
* @param {string} name - Fixture basename.
396394
* @returns {object} A {code, output, options, errors} test object.
@@ -421,7 +419,8 @@ function loadInvalidFixture(name) {
421419
}
422420

423421
/**
424-
* Builds a dummy javascript file path to trick prettier into resolving a specific .prettierrc file.
422+
* Builds a dummy javascript file path to trick prettier into resolving a
423+
* specific .prettierrc file.
425424
*
426425
* @param {string} dir - Prettierrc fixture basename.
427426
* @param {string} file
@@ -433,14 +432,18 @@ function getPrettierRcJsFilename(dir, file = 'dummy.js') {
433432
);
434433
}
435434

435+
/**
436+
* @type {ESLint}
437+
* @import {ESLint} from 'eslint'
438+
*/
436439
let eslint;
437440

438441
/**
439-
*
440442
* @param {string} pattern
441-
* @param {import('eslint').Linter.LintMessage[][]} asserts
443+
* @param {Linter.LintMessage[][]} asserts
442444
* @param {boolean} [skip]
443445
* @returns {Promise<void>}
446+
* @import {Linter} from 'eslint'
444447
*/
445448
async function runFixture(pattern, asserts, skip) {
446449
if (!eslint) {

worker.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
// @ts-check
22

33
/**
4-
* @typedef {import('prettier').FileInfoOptions} FileInfoOptions
5-
* @typedef {import('eslint').ESLint.ObjectMetaProperties} ObjectMetaProperties
6-
* @typedef {import('prettier').Options & { onDiskFilepath: string, parserMeta?: ObjectMetaProperties['meta'], parserPath?: string, usePrettierrc?: boolean }} Options
4+
* @typedef {PrettierOptions & {
5+
* onDiskFilepath: string;
6+
* parserMeta?: ESLint.ObjectMetaProperties['meta'];
7+
* parserPath?: string;
8+
* usePrettierrc?: boolean;
9+
* }} Options
10+
* @import {FileInfoOptions, Options as PrettierOptions} from 'prettier'
11+
* @import {ESLint} from 'eslint'
712
*/
813

914
const { runAsWorker } = require('synckit');
1015

1116
/**
12-
* @type {typeof import('prettier')}
17+
* @type {typeof Prettier}
18+
* @import * as Prettier from 'prettier'
1319
*/
1420
let prettier;
1521

@@ -176,9 +182,7 @@ runAsWorker(
176182
}
177183
}
178184

179-
/**
180-
* @type {import('prettier').Options}
181-
*/
185+
/** @type {PrettierOptions} */
182186
const prettierOptions = {
183187
...initialOptions,
184188
...prettierRcOptions,

0 commit comments

Comments
 (0)