eslint-plugin-prettier 5.2.6 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/eslint-plugin-prettier.js +47 -37
- package/package.json +2 -2
- package/recommended.d.ts +8 -1
- package/worker.js +11 -7
|
@@ -6,14 +6,25 @@
|
|
|
6
6
|
// @ts-check
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
11
|
-
* @
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* @typedef {PrettierOptions & {
|
|
16
|
-
*
|
|
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 {
|
|
55
|
-
* @param {
|
|
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
|
-
|
|
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
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
218
|
-
|
|
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(
|
|
253
|
+
reportDifference(
|
|
254
|
+
/** @type {Rule.RuleContext} */ (context),
|
|
255
|
+
difference,
|
|
256
|
+
);
|
|
247
257
|
}
|
|
248
258
|
}
|
|
249
259
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-prettier",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Runs prettier as an eslint rule",
|
|
5
5
|
"repository": "https://github.com/prettier/eslint-plugin-prettier.git",
|
|
6
6
|
"homepage": "https://github.com/prettier/eslint-plugin-prettier#readme",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"node": "^14.18.0 || >=16.0.0"
|
|
15
15
|
},
|
|
16
16
|
"main": "eslint-plugin-prettier.js",
|
|
17
|
+
"types": "eslint-plugin-prettier.d.ts",
|
|
17
18
|
"exports": {
|
|
18
19
|
".": {
|
|
19
20
|
"types": "./eslint-plugin-prettier.d.ts",
|
|
@@ -25,7 +26,6 @@
|
|
|
25
26
|
},
|
|
26
27
|
"./package.json": "./package.json"
|
|
27
28
|
},
|
|
28
|
-
"types": "eslint-plugin-prettier.d.ts",
|
|
29
29
|
"files": [
|
|
30
30
|
"eslint-plugin-prettier.d.ts",
|
|
31
31
|
"eslint-plugin-prettier.js",
|
package/recommended.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { Linter } from 'eslint';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// prettier-ignore
|
|
4
|
+
type IfEqual<A, B, X = A, Y = B> =
|
|
5
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
6
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
7
|
+
? X
|
|
8
|
+
: Y;
|
|
9
|
+
|
|
10
|
+
declare const recommendedConfig: IfEqual<Linter.Config, Linter.FlatConfig>;
|
|
4
11
|
|
|
5
12
|
export = recommendedConfig;
|
package/worker.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @typedef {
|
|
5
|
-
*
|
|
6
|
-
*
|
|
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'
|
|
7
12
|
*/
|
|
8
13
|
|
|
9
14
|
const { runAsWorker } = require('synckit');
|
|
10
15
|
|
|
11
16
|
/**
|
|
12
|
-
* @type {typeof
|
|
17
|
+
* @type {typeof Prettier}
|
|
18
|
+
* @import * as Prettier from 'prettier'
|
|
13
19
|
*/
|
|
14
20
|
let prettier;
|
|
15
21
|
|
|
@@ -176,9 +182,7 @@ runAsWorker(
|
|
|
176
182
|
}
|
|
177
183
|
}
|
|
178
184
|
|
|
179
|
-
/**
|
|
180
|
-
* @type {import('prettier').Options}
|
|
181
|
-
*/
|
|
185
|
+
/** @type {PrettierOptions} */
|
|
182
186
|
const prettierOptions = {
|
|
183
187
|
...initialOptions,
|
|
184
188
|
...prettierRcOptions,
|