Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typings support #509

Merged
merged 3 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-cups-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-prettier": minor
---

feat: add typings support
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"@1stg",
"plugin:eslint-plugin/recommended"
],
"parserOptions": {
"project": null
},
"rules": {
"eslint-plugin/report-message-format": [
"error",
Expand Down
4 changes: 2 additions & 2 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github:
- JounQin
- 1stG
- rxts
- unts
- rx-ts
- un-ts
patreon: 1stG
open_collective: prettier
custom:
Expand Down
5 changes: 5 additions & 0 deletions eslint-plugin-prettier.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ESLint } from 'eslint';

declare const eslintPluginPrettier: ESLint.Plugin;

export = eslintPluginPrettier;
39 changes: 31 additions & 8 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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

// Lazily-loaded Prettier.
/**
* @type {import('prettier')}
* @type {typeof import('prettier')}
*/
let prettier;

Expand All @@ -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),
);
Expand All @@ -63,7 +71,10 @@ function reportDifference(context, difference) {
// Module Definition
// ------------------------------------------------------------------------------

module.exports = {
/**
* @type {Plugin}
*/
const eslintPluginPrettier = {
configs: {
recommended: {
extends: ['prettier'],
Expand Down Expand Up @@ -241,7 +252,9 @@ module.exports = {
'angular',
'svelte',
];
if (parserBlocklist.includes(inferredParser)) {
if (
parserBlocklist.includes(/** @type {string} */ (inferredParser))
) {
return;
}
}
Expand All @@ -261,6 +274,9 @@ module.exports = {
// files throw an error if they contain unclosed elements, such as
// `<template><div></template>. In this case report an error at the
// point at which parsing failed.
/**
* @type {string}
*/
let prettierSource;
try {
prettierSource = prettier.format(source, prettierOptions);
Expand All @@ -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;
}
Expand All @@ -300,3 +321,5 @@ module.exports = {
},
},
};

module.exports = eslintPluginPrettier;
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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",
Expand All @@ -33,10 +36,14 @@
"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
}
Expand All @@ -50,6 +57,9 @@
"@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",
Expand All @@ -66,6 +76,6 @@
},
"resolutions": {
"eslint-plugin-prettier": "link:.",
"prettier": "^2.7.1"
"prettier": "^2.0.0"
}
}
22 changes: 20 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down