Skip to content

Commit 08f2c8d

Browse files
authored
feat: add typings support (#509)
1 parent dec70a9 commit 08f2c8d

File tree

7 files changed

+77
-13
lines changed

7 files changed

+77
-13
lines changed

.changeset/quiet-cups-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-prettier": minor
3+
---
4+
5+
feat: add typings support

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"@1stg",
88
"plugin:eslint-plugin/recommended"
99
],
10+
"parserOptions": {
11+
"project": null
12+
},
1013
"rules": {
1114
"eslint-plugin/report-message-format": [
1215
"error",

.github/FUNDING.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
github:
22
- JounQin
33
- 1stG
4-
- rxts
5-
- unts
4+
- rx-ts
5+
- un-ts
66
patreon: 1stG
77
open_collective: prettier
88
custom:

eslint-plugin-prettier.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ESLint } from 'eslint';
2+
3+
declare const eslintPluginPrettier: ESLint.Plugin;
4+
5+
export = eslintPluginPrettier;

eslint-plugin-prettier.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
* @author Andres Suarez
44
*/
55

6+
// @ts-check
7+
8+
/**
9+
* @typedef {import('eslint').AST.Range} Range
10+
* @typedef {import('eslint').AST.SourceLocation} SourceLocation
11+
* @typedef {import('eslint').ESLint.Plugin} Plugin
12+
*/
13+
614
'use strict';
715

816
// ------------------------------------------------------------------------------
@@ -26,7 +34,7 @@ const { INSERT, DELETE, REPLACE } = generateDifferences;
2634

2735
// Lazily-loaded Prettier.
2836
/**
29-
* @type {import('prettier')}
37+
* @type {typeof import('prettier')}
3038
*/
3139
let prettier;
3240

@@ -43,7 +51,7 @@ let prettier;
4351
*/
4452
function reportDifference(context, difference) {
4553
const { operation, offset, deleteText = '', insertText = '' } = difference;
46-
const range = [offset, offset + deleteText.length];
54+
const range = /** @type {Range} */ ([offset, offset + deleteText.length]);
4755
const [start, end] = range.map(index =>
4856
context.getSourceCode().getLocFromIndex(index),
4957
);
@@ -63,7 +71,10 @@ function reportDifference(context, difference) {
6371
// Module Definition
6472
// ------------------------------------------------------------------------------
6573

66-
module.exports = {
74+
/**
75+
* @type {Plugin}
76+
*/
77+
const eslintPluginPrettier = {
6778
configs: {
6879
recommended: {
6980
extends: ['prettier'],
@@ -241,7 +252,9 @@ module.exports = {
241252
'angular',
242253
'svelte',
243254
];
244-
if (parserBlocklist.includes(inferredParser)) {
255+
if (
256+
parserBlocklist.includes(/** @type {string} */ (inferredParser))
257+
) {
245258
return;
246259
}
247260
}
@@ -261,6 +274,9 @@ module.exports = {
261274
// files throw an error if they contain unclosed elements, such as
262275
// `<template><div></template>. In this case report an error at the
263276
// point at which parsing failed.
277+
/**
278+
* @type {string}
279+
*/
264280
let prettierSource;
265281
try {
266282
prettierSource = prettier.format(source, prettierOptions);
@@ -271,18 +287,23 @@ module.exports = {
271287

272288
let message = 'Parsing error: ' + err.message;
273289

290+
const error =
291+
/** @type {SyntaxError & {codeFrame: string; loc: SourceLocation}} */ (
292+
err
293+
);
294+
274295
// Prettier's message contains a codeframe style preview of the
275296
// invalid code and the line/column at which the error occurred.
276297
// ESLint shows those pieces of information elsewhere already so
277298
// remove them from the message
278-
if (err.codeFrame) {
279-
message = message.replace(`\n${err.codeFrame}`, '');
299+
if (error.codeFrame) {
300+
message = message.replace(`\n${error.codeFrame}`, '');
280301
}
281-
if (err.loc) {
302+
if (error.loc) {
282303
message = message.replace(/ \(\d+:\d+\)$/, '');
283304
}
284305

285-
context.report({ message, loc: err.loc });
306+
context.report({ message, loc: error.loc });
286307

287308
return;
288309
}
@@ -300,3 +321,5 @@ module.exports = {
300321
},
301322
},
302323
};
324+
325+
module.exports = eslintPluginPrettier;

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"node": "^14.18.0 || >=16.0.0"
1616
},
1717
"main": "eslint-plugin-prettier.js",
18+
"types": "eslint-plugin-prettier.d.ts",
1819
"files": [
20+
"eslint-plugin-prettier.d.ts",
1921
"eslint-plugin-prettier.js"
2022
],
2123
"keywords": [
@@ -25,6 +27,7 @@
2527
"prettier"
2628
],
2729
"scripts": {
30+
"build": "tsc -b",
2831
"format": "yarn prettier '**/*.{js,json,md,yml}' --write && yarn lint --fix",
2932
"lint": "eslint . --cache -f friendly --max-warnings 10",
3033
"prepare": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
@@ -33,10 +36,14 @@
3336
"test": "yarn lint && mocha"
3437
},
3538
"peerDependencies": {
39+
"@types/eslint": ">=8.0.0",
3640
"eslint": ">=8.0.0",
3741
"prettier": ">=2.0.0"
3842
},
3943
"peerDependenciesMeta": {
44+
"@types/eslint": {
45+
"optional": true
46+
},
4047
"eslint-config-prettier": {
4148
"optional": true
4249
}
@@ -50,6 +57,9 @@
5057
"@changesets/changelog-github": "^0.4.6",
5158
"@changesets/cli": "^2.24.4",
5259
"@graphql-eslint/eslint-plugin": "^3.10.7",
60+
"@types/eslint": "^8.4.6",
61+
"@types/prettier": "^2.7.0",
62+
"@types/prettier-linter-helpers": "^1.0.1",
5363
"@typescript-eslint/parser": "^5.36.1",
5464
"eslint-config-prettier": "^8.5.0",
5565
"eslint-mdx": "^2.0.2",
@@ -66,6 +76,6 @@
6676
},
6777
"resolutions": {
6878
"eslint-plugin-prettier": "link:.",
69-
"prettier": "^2.7.1"
79+
"prettier": "^2.0.0"
7080
}
7181
}

yarn.lock

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,14 @@
23532353
dependencies:
23542354
"@types/ms" "*"
23552355

2356+
"@types/eslint@^8.4.6":
2357+
version "8.4.6"
2358+
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.4.6.tgz#7976f054c1bccfcf514bff0564c0c41df5c08207"
2359+
integrity sha512-/fqTbjxyFUaYNO7VcW5g+4npmqVACz1bB7RTHYuLj+PRjw9hrCwrUXVQFpChUS0JsyEFvMZ7U/PfmvWgxJhI9g==
2360+
dependencies:
2361+
"@types/estree" "*"
2362+
"@types/json-schema" "*"
2363+
23562364
"@types/estree-jsx@^1.0.0":
23572365
version "1.0.0"
23582366
resolved "https://registry.yarnpkg.com/@types/estree-jsx/-/estree-jsx-1.0.0.tgz#7bfc979ab9f692b492017df42520f7f765e98df1"
@@ -2384,7 +2392,7 @@
23842392
resolved "https://registry.yarnpkg.com/@types/is-empty/-/is-empty-1.2.1.tgz#18d7256a73e43ec51f8b75c25fbdc31350be52a6"
23852393
integrity sha512-a3xgqnFTuNJDm1fjsTjHocYJ40Cz3t8utYpi5GNaxzrJC2HSD08ym+whIL7fNqiqBCdM9bcqD1H/tORWAFXoZw==
23862394

2387-
"@types/json-schema@^7.0.9":
2395+
"@types/json-schema@*", "@types/json-schema@^7.0.9":
23882396
version "7.0.11"
23892397
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
23902398
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
@@ -2431,6 +2439,16 @@
24312439
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
24322440
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
24332441

2442+
"@types/prettier-linter-helpers@^1.0.1":
2443+
version "1.0.1"
2444+
resolved "https://registry.yarnpkg.com/@types/prettier-linter-helpers/-/prettier-linter-helpers-1.0.1.tgz#463c97a9e386fe2781026852fa97bca3dfd42625"
2445+
integrity sha512-3O6c5261s6dNZDSZ2jQM57Mw7tGIREc3IjN7WJjWYqPKzzIYW9ujri2O1gRGVhFlfLTJl/cU/Ju26S0Otm5cog==
2446+
2447+
"@types/prettier@^2.7.0":
2448+
version "2.7.0"
2449+
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.0.tgz#ea03e9f0376a4446f44797ca19d9c46c36e352dc"
2450+
integrity sha512-RI1L7N4JnW5gQw2spvL7Sllfuf1SaHdrZpCHiBlCXjIlufi1SMNnbu2teze3/QE67Fg2tBlH7W+mi4hVNk4p0A==
2451+
24342452
"@types/semver@^6.0.0":
24352453
version "6.2.3"
24362454
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.2.3.tgz#5798ecf1bec94eaa64db39ee52808ec0693315aa"
@@ -7311,7 +7329,7 @@ prettier-plugin-toml@^0.3.1:
73117329
"@toml-tools/parser" "^0.3.1"
73127330
prettier "^1.16.0"
73137331

7314-
prettier@>=2.3, prettier@>=2.3.0, prettier@>=2.4.0, prettier@^1.16.0, prettier@^2.6.2, prettier@^2.7.1:
7332+
prettier@>=2.3, prettier@>=2.3.0, prettier@>=2.4.0, prettier@^1.16.0, prettier@^2.0.0, prettier@^2.6.2, prettier@^2.7.1:
73157333
version "2.7.1"
73167334
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
73177335
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==

0 commit comments

Comments
 (0)