Skip to content

Commit f2c2d35

Browse files
authored
docs: disambiguate types FormatterFunction and LoadedFormatter (#15727)
* chore: disambiguate types `FormatterFunction` and `LoadedFormatter` Fixes #15654 * Code review update * Remove redundant `LintResult` type import * Remove note about `context` argument of `FormatterFunction` from JSDoc * docs: Revert to using variable name `formatter` in code examples
1 parent d36f12f commit f2c2d35

5 files changed

Lines changed: 39 additions & 29 deletions

File tree

docs/developer-guide/nodejs-api.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ While ESLint is designed to be run on the command line, it's possible to use ESL
2121
* [LintMessage type][lintmessage]
2222
* [SuppressedLintMessage type][suppressedlintmessage]
2323
* [EditInfo type][editinfo]
24-
* [Formatter type][formatter]
24+
* [LoadedFormatter type][loadedformatter]
2525
* [SourceCode](#sourcecode)
2626
* [splitLines()](#sourcecodesplitlines)
2727
* [Linter](#linter)
@@ -287,8 +287,8 @@ This method loads a formatter. Formatters convert lint results to a human- or ma
287287

288288
#### Return Value
289289

290-
* (`Promise<Formatter>`)<br>
291-
The promise that will be fulfilled with a [Formatter] object.
290+
* (`Promise<LoadedFormatter>`)<br>
291+
The promise that will be fulfilled with a [LoadedFormatter] object.
292292

293293
### ◆ ESLint.version
294294

@@ -430,9 +430,9 @@ The `EditInfo` value is information to edit text. The `fix` and `suggestions` pr
430430

431431
This edit information means replacing the range of the `range` property by the `text` property value. It's like `sourceCodeText.slice(0, edit.range[0]) + edit.text + sourceCodeText.slice(edit.range[1])`. Therefore, it's an add if the `range[0]` and `range[1]` property values are the same value, and it's removal if the `text` property value is empty string.
432432

433-
### Formatter type
433+
### LoadedFormatter type
434434

435-
The `Formatter` value is the object to convert the [LintResult] objects to text. The [eslint.loadFormatter()][eslint-loadformatter] method returns it. It has the following method:
435+
The `LoadedFormatter` value is the object to convert the [LintResult] objects to text. The [eslint.loadFormatter()][eslint-loadformatter] method returns it. It has the following method:
436436

437437
* `format` (`(results: LintResult[]) => string | Promise<string>`)<br>
438438
The method to convert the [LintResult] objects to text.
@@ -960,5 +960,5 @@ ruleTester.run("my-rule", myRule, {
960960
[lintmessage]: #-lintmessage-type
961961
[suppressedlintmessage]: #-suppressedlintmessage-type
962962
[editinfo]: #-editinfo-type
963-
[formatter]: #-formatter-type
963+
[loadedformatter]: #-loadedformatter-type
964964
[linter]: #linter

docs/developer-guide/working-with-custom-formatters.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
While ESLint has some built-in formatters available to format the linting results, it's also possible to create and distribute your own custom formatters. You can include custom formatters in your project directly or create an npm package to distribute them separately.
44

5-
Each formatter is just a function that receives a `results` object and returns a string. For example, the following is how the `json` built-in formatter is implemented:
5+
Each formatter is just a function that receives a `results` object and a `context` and returns a string. For example, the following is how the `json` built-in formatter is implemented:
66

77
```js
88
//my-awesome-formatter.js
@@ -11,7 +11,7 @@ module.exports = function(results, context) {
1111
};
1212
```
1313

14-
Formatter can also be an async function (from ESLint v8.4.0), the following shows a simple example:
14+
A formatter can also be an async function (from ESLint v8.4.0), the following shows a simple example:
1515

1616
```js
1717
//my-awesome-formatter.js
@@ -280,7 +280,7 @@ warning no-unused-vars (https://eslint.org/docs/rules/no-unused-vars)
280280

281281
## Passing Arguments to Formatters
282282

283-
While custom formatter do not receive arguments in addition to the results object, it is possible to pass additional data into formatters.
283+
While formatter functions do not receive arguments in addition to the results object and the context, it is possible to pass additional data into custom formatters using the methods described below.
284284

285285
## Using Environment Variables
286286

lib/cli-engine/cli-engine.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const validFixTypes = new Set(["directive", "problem", "suggestion", "layout"]);
5656
/** @typedef {import("../shared/types").Plugin} Plugin */
5757
/** @typedef {import("../shared/types").RuleConf} RuleConf */
5858
/** @typedef {import("../shared/types").Rule} Rule */
59+
/** @typedef {import("../shared/types").FormatterFunction} FormatterFunction */
5960
/** @typedef {ReturnType<CascadingConfigArrayFactory.getConfigArrayForFile>} ConfigArray */
6061
/** @typedef {ReturnType<ConfigArray.extractConfig>} ExtractedConfig */
6162

@@ -1002,7 +1003,7 @@ class CLIEngine {
10021003
* @param {string} [format] The name of the format to load or the path to a
10031004
* custom formatter.
10041005
* @throws {any} As may be thrown by requiring of formatter
1005-
* @returns {(Function|null)} The formatter function or null if the `format` is not a string.
1006+
* @returns {(FormatterFunction|null)} The formatter function or null if the `format` is not a string.
10061007
*/
10071008
getFormatter(format) {
10081009

lib/eslint/eslint.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ const { version } = require("../../package.json");
3535
/** @typedef {import("../shared/types").SuppressedLintMessage} SuppressedLintMessage */
3636
/** @typedef {import("../shared/types").Plugin} Plugin */
3737
/** @typedef {import("../shared/types").Rule} Rule */
38+
/** @typedef {import("../shared/types").LintResult} LintResult */
3839

3940
/**
4041
* The main formatter object.
41-
* @typedef Formatter
42+
* @typedef LoadedFormatter
4243
* @property {function(LintResult[]): string | Promise<string>} format format function.
4344
*/
4445

@@ -74,22 +75,6 @@ const { version } = require("../../package.json");
7475
* @property {Object} definition The plugin definition.
7576
*/
7677

77-
/**
78-
* A linting result.
79-
* @typedef {Object} LintResult
80-
* @property {string} filePath The path to the file that was linted.
81-
* @property {LintMessage[]} messages All of the messages for the result.
82-
* @property {SuppressedLintMessage[]} suppressedMessages All of the suppressed messages for the result.
83-
* @property {number} errorCount Number of errors for the result.
84-
* @property {number} fatalErrorCount Number of fatal errors for the result.
85-
* @property {number} warningCount Number of warnings for the result.
86-
* @property {number} fixableErrorCount Number of fixable errors for the result.
87-
* @property {number} fixableWarningCount Number of fixable warnings for the result.
88-
* @property {string} [source] The source code of the file that was linted.
89-
* @property {string} [output] The source code of the file that was linted, with as many fixes applied as possible.
90-
* @property {DeprecatedRuleInfo[]} usedDeprecatedRules The list of used deprecated rules.
91-
*/
92-
9378
/**
9479
* Private members for the `ESLint` instance.
9580
* @typedef {Object} ESLintPrivateMembers
@@ -619,7 +604,7 @@ class ESLint {
619604
* - `@foo` → `@foo/eslint-formatter`
620605
* - `@foo/bar` → `@foo/eslint-formatter-bar`
621606
* - A file path ... Load the file.
622-
* @returns {Promise<Formatter>} A promise resolving to the formatter object.
607+
* @returns {Promise<LoadedFormatter>} A promise resolving to the formatter object.
623608
* This promise will be rejected if the given formatter was not found or not
624609
* a function.
625610
*/
@@ -639,7 +624,7 @@ class ESLint {
639624

640625
/**
641626
* The main formatter method.
642-
* @param {LintResults[]} results The lint results to format.
627+
* @param {LintResult[]} results The lint results to format.
643628
* @returns {string | Promise<string>} The formatted lint results.
644629
*/
645630
format(results) {

lib/shared/types.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,27 @@ module.exports = {};
173173
* @property {string} ruleId The rule ID.
174174
* @property {string[]} replacedBy The rule IDs that replace this deprecated rule.
175175
*/
176+
177+
/**
178+
* A linting result.
179+
* @typedef {Object} LintResult
180+
* @property {string} filePath The path to the file that was linted.
181+
* @property {LintMessage[]} messages All of the messages for the result.
182+
* @property {SuppressedLintMessage[]} suppressedMessages All of the suppressed messages for the result.
183+
* @property {number} errorCount Number of errors for the result.
184+
* @property {number} fatalErrorCount Number of fatal errors for the result.
185+
* @property {number} warningCount Number of warnings for the result.
186+
* @property {number} fixableErrorCount Number of fixable errors for the result.
187+
* @property {number} fixableWarningCount Number of fixable warnings for the result.
188+
* @property {string} [source] The source code of the file that was linted.
189+
* @property {string} [output] The source code of the file that was linted, with as many fixes applied as possible.
190+
* @property {DeprecatedRuleInfo[]} usedDeprecatedRules The list of used deprecated rules.
191+
*/
192+
193+
/**
194+
* A formatter function.
195+
* @callback FormatterFunction
196+
* @param {LintResult[]} results The list of linting results.
197+
* @param {{cwd: string, rulesMeta: Record<string, RuleMeta>}} [context] A context object.
198+
* @returns {string | Promise<string>} Formatted text.
199+
*/

0 commit comments

Comments
 (0)