From 673cef7d92e4acceb0bdf7866280d3f7b7ac336b Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 31 Aug 2022 21:39:12 +0800 Subject: [PATCH 1/3] feat: add typings support close #490 --- .eslintrc.json | 3 +++ .github/FUNDING.yml | 4 ++-- eslint-plugin-prettier.d.ts | 5 +++++ eslint-plugin-prettier.js | 39 +++++++++++++++++++++++++++++-------- package.json | 15 ++++++++++++-- yarn.lock | 24 ++++++++++++++++++++--- 6 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 eslint-plugin-prettier.d.ts diff --git a/.eslintrc.json b/.eslintrc.json index b2104c5a..a7ac6675 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,6 +7,9 @@ "@1stg", "plugin:eslint-plugin/recommended" ], + "parserOptions": { + "project": null + }, "rules": { "eslint-plugin/report-message-format": [ "error", diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index b2ae54de..60fd76ca 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,8 +1,8 @@ github: - JounQin - 1stG - - rxts - - unts + - rx-ts + - un-ts patreon: 1stG open_collective: prettier custom: diff --git a/eslint-plugin-prettier.d.ts b/eslint-plugin-prettier.d.ts new file mode 100644 index 00000000..eafdb4a1 --- /dev/null +++ b/eslint-plugin-prettier.d.ts @@ -0,0 +1,5 @@ +import { ESLint } from 'eslint'; + +declare const eslintPluginPrettier: ESLint.Plugin; + +export = eslintPluginPrettier; diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 3b5179f9..dab66579 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -3,6 +3,14 @@ * @author Andres Suarez */ +// @ts-check + +/** + * @typedef {import('eslint').AST.Range} Range + * @typedef {import('eslint').AST.SourceLocation} SourceLocation + * @typedef {import('eslint').ESLint.Plugin} Plugin + */ + 'use strict'; // ------------------------------------------------------------------------------ @@ -26,7 +34,7 @@ const { INSERT, DELETE, REPLACE } = generateDifferences; // Lazily-loaded Prettier. /** - * @type {import('prettier')} + * @type {typeof import('prettier')} */ let prettier; @@ -43,7 +51,7 @@ let prettier; */ function reportDifference(context, difference) { const { operation, offset, deleteText = '', insertText = '' } = difference; - const range = [offset, offset + deleteText.length]; + const range = /** @type {Range} */ ([offset, offset + deleteText.length]); const [start, end] = range.map(index => context.getSourceCode().getLocFromIndex(index), ); @@ -63,7 +71,10 @@ function reportDifference(context, difference) { // Module Definition // ------------------------------------------------------------------------------ -module.exports = { +/** + * @type {Plugin} + */ +const eslintPluginPrettier = { configs: { recommended: { extends: ['prettier'], @@ -241,7 +252,9 @@ module.exports = { 'angular', 'svelte', ]; - if (parserBlocklist.includes(inferredParser)) { + if ( + parserBlocklist.includes(/** @type {string} */ (inferredParser)) + ) { return; } } @@ -261,6 +274,9 @@ module.exports = { // files throw an error if they contain unclosed elements, such as // `. In this case report an error at the // point at which parsing failed. + /** + * @type {string} + */ let prettierSource; try { prettierSource = prettier.format(source, prettierOptions); @@ -271,18 +287,23 @@ module.exports = { let message = 'Parsing error: ' + err.message; + const error = + /** @type {SyntaxError & {codeFrame: string; loc: SourceLocation}} */ ( + err + ); + // Prettier's message contains a codeframe style preview of the // invalid code and the line/column at which the error occurred. // ESLint shows those pieces of information elsewhere already so // remove them from the message - if (err.codeFrame) { - message = message.replace(`\n${err.codeFrame}`, ''); + if (error.codeFrame) { + message = message.replace(`\n${error.codeFrame}`, ''); } - if (err.loc) { + if (error.loc) { message = message.replace(/ \(\d+:\d+\)$/, ''); } - context.report({ message, loc: err.loc }); + context.report({ message, loc: error.loc }); return; } @@ -300,3 +321,5 @@ module.exports = { }, }, }; + +module.exports = eslintPluginPrettier; diff --git a/package.json b/package.json index 1d6771dd..9254e6ee 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ "node": "^14.18.0 || >=16.0.0" }, "main": "eslint-plugin-prettier.js", + "types": "eslint-plugin-prettier.d.ts", "files": [ + "eslint-plugin-prettier.d.ts", "eslint-plugin-prettier.js" ], "keywords": [ @@ -25,6 +27,7 @@ "prettier" ], "scripts": { + "build": "tsc -b", "format": "yarn prettier '**/*.{js,json,md,yml}' --write && yarn lint --fix", "lint": "eslint . --cache -f friendly --max-warnings 10", "prepare": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0", @@ -33,23 +36,31 @@ "test": "yarn lint && mocha" }, "peerDependencies": { + "@types/eslint": ">=8.0.0", "eslint": ">=8.0.0", "prettier": ">=2.0.0" }, "peerDependenciesMeta": { + "@types/eslint": { + "optional": true + }, "eslint-config-prettier": { "optional": true } }, "dependencies": { "eslint-plugin-prettier": "link:.", - "prettier-linter-helpers": "^1.0.0" + "prettier-linter-helpers": "^1.0.0", + "synckit": "^0.8.4" }, "devDependencies": { "@1stg/common-config": "^7.1.1", "@changesets/changelog-github": "^0.4.6", "@changesets/cli": "^2.24.4", "@graphql-eslint/eslint-plugin": "^3.10.7", + "@types/eslint": "^8.4.6", + "@types/prettier": "^2.7.0", + "@types/prettier-linter-helpers": "^1.0.1", "@typescript-eslint/parser": "^5.36.1", "eslint-config-prettier": "^8.5.0", "eslint-mdx": "^2.0.2", @@ -66,6 +77,6 @@ }, "resolutions": { "eslint-plugin-prettier": "link:.", - "prettier": "^2.7.1" + "prettier": "^2.0.0" } } diff --git a/yarn.lock b/yarn.lock index 17d1ff28..316c28c3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2353,6 +2353,14 @@ dependencies: "@types/ms" "*" +"@types/eslint@^8.4.6": + version "8.4.6" + resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.4.6.tgz#7976f054c1bccfcf514bff0564c0c41df5c08207" + integrity sha512-/fqTbjxyFUaYNO7VcW5g+4npmqVACz1bB7RTHYuLj+PRjw9hrCwrUXVQFpChUS0JsyEFvMZ7U/PfmvWgxJhI9g== + dependencies: + "@types/estree" "*" + "@types/json-schema" "*" + "@types/estree-jsx@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/estree-jsx/-/estree-jsx-1.0.0.tgz#7bfc979ab9f692b492017df42520f7f765e98df1" @@ -2384,7 +2392,7 @@ resolved "https://registry.yarnpkg.com/@types/is-empty/-/is-empty-1.2.1.tgz#18d7256a73e43ec51f8b75c25fbdc31350be52a6" integrity sha512-a3xgqnFTuNJDm1fjsTjHocYJ40Cz3t8utYpi5GNaxzrJC2HSD08ym+whIL7fNqiqBCdM9bcqD1H/tORWAFXoZw== -"@types/json-schema@^7.0.9": +"@types/json-schema@*", "@types/json-schema@^7.0.9": version "7.0.11" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3" integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ== @@ -2431,6 +2439,16 @@ resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA== +"@types/prettier-linter-helpers@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@types/prettier-linter-helpers/-/prettier-linter-helpers-1.0.1.tgz#463c97a9e386fe2781026852fa97bca3dfd42625" + integrity sha512-3O6c5261s6dNZDSZ2jQM57Mw7tGIREc3IjN7WJjWYqPKzzIYW9ujri2O1gRGVhFlfLTJl/cU/Ju26S0Otm5cog== + +"@types/prettier@^2.7.0": + version "2.7.0" + resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.0.tgz#ea03e9f0376a4446f44797ca19d9c46c36e352dc" + integrity sha512-RI1L7N4JnW5gQw2spvL7Sllfuf1SaHdrZpCHiBlCXjIlufi1SMNnbu2teze3/QE67Fg2tBlH7W+mi4hVNk4p0A== + "@types/semver@^6.0.0": version "6.2.3" resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.2.3.tgz#5798ecf1bec94eaa64db39ee52808ec0693315aa" @@ -7311,7 +7329,7 @@ prettier-plugin-toml@^0.3.1: "@toml-tools/parser" "^0.3.1" prettier "^1.16.0" -prettier@>=2.3, prettier@>=2.3.0, prettier@>=2.4.0, prettier@^1.16.0, prettier@^2.6.2, prettier@^2.7.1: +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: version "2.7.1" resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64" integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g== @@ -9028,7 +9046,7 @@ svg-tags@^1.0.0: resolved "https://registry.yarnpkg.com/svg-tags/-/svg-tags-1.0.0.tgz#58f71cee3bd519b59d4b2a843b6c7de64ac04764" integrity sha512-ovssysQTa+luh7A5Weu3Rta6FJlFBBbInjOh722LIt6klpU2/HtdUbszju/G4devcvk8PGt7FCLv5wftu3THUA== -synckit@^0.8.0, synckit@^0.8.1, synckit@^0.8.3: +synckit@^0.8.0, synckit@^0.8.1, synckit@^0.8.3, synckit@^0.8.4: version "0.8.4" resolved "https://registry.yarnpkg.com/synckit/-/synckit-0.8.4.tgz#0e6b392b73fafdafcde56692e3352500261d64ec" integrity sha512-Dn2ZkzMdSX827QbowGbU/4yjWuvNaCoScLLoMo/yKbu+P4GBR6cRGKZH27k6a9bRzdqcyd1DE96pQtQ6uNkmyw== From ebce81a41836bcd26fdae32a33fbf7a0e43571b7 Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 31 Aug 2022 21:42:29 +0800 Subject: [PATCH 2/3] Create quiet-cups-pull.md --- .changeset/quiet-cups-pull.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/quiet-cups-pull.md diff --git a/.changeset/quiet-cups-pull.md b/.changeset/quiet-cups-pull.md new file mode 100644 index 00000000..a3da6175 --- /dev/null +++ b/.changeset/quiet-cups-pull.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-prettier": minor +--- + +feat: add typings support From 368a45afe139764ac60e79f956294607f63a7a66 Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 31 Aug 2022 21:44:45 +0800 Subject: [PATCH 3/3] chore: remove unused synckit for now --- package.json | 3 +-- yarn.lock | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 9254e6ee..39360bf9 100644 --- a/package.json +++ b/package.json @@ -50,8 +50,7 @@ }, "dependencies": { "eslint-plugin-prettier": "link:.", - "prettier-linter-helpers": "^1.0.0", - "synckit": "^0.8.4" + "prettier-linter-helpers": "^1.0.0" }, "devDependencies": { "@1stg/common-config": "^7.1.1", diff --git a/yarn.lock b/yarn.lock index 316c28c3..c7f79977 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9046,7 +9046,7 @@ svg-tags@^1.0.0: resolved "https://registry.yarnpkg.com/svg-tags/-/svg-tags-1.0.0.tgz#58f71cee3bd519b59d4b2a843b6c7de64ac04764" integrity sha512-ovssysQTa+luh7A5Weu3Rta6FJlFBBbInjOh722LIt6klpU2/HtdUbszju/G4devcvk8PGt7FCLv5wftu3THUA== -synckit@^0.8.0, synckit@^0.8.1, synckit@^0.8.3, synckit@^0.8.4: +synckit@^0.8.0, synckit@^0.8.1, synckit@^0.8.3: version "0.8.4" resolved "https://registry.yarnpkg.com/synckit/-/synckit-0.8.4.tgz#0e6b392b73fafdafcde56692e3352500261d64ec" integrity sha512-Dn2ZkzMdSX827QbowGbU/4yjWuvNaCoScLLoMo/yKbu+P4GBR6cRGKZH27k6a9bRzdqcyd1DE96pQtQ6uNkmyw==