Skip to content

Commit a48f8c2

Browse files
authored
feat: add type FormatterFunction, update LoadedFormatter (#18872)
* feat: add type `FormatterFunction`, update `LoadedFormatter` * add TypeDoc for `FormatterFunction` * mark second argument of `FormatterFunction` as required, update TypeDoc
1 parent d594ddd commit a48f8c2

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

lib/shared/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,6 @@ module.exports = {};
245245
* A formatter function.
246246
* @callback FormatterFunction
247247
* @param {LintResult[]} results The list of linting results.
248-
* @param {{cwd: string, maxWarningsExceeded?: MaxWarningsExceeded, rulesMeta: Record<string, RuleMeta>}} [context] A context object.
248+
* @param {{cwd: string, maxWarningsExceeded?: MaxWarningsExceeded, rulesMeta: Record<string, RuleMeta>}} context A context object.
249249
* @returns {string | Promise<string>} Formatted text.
250250
*/

lib/types/index.d.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ export class ESLint {
14111411

14121412
isPathIgnored(filePath: string): Promise<boolean>;
14131413

1414-
loadFormatter(nameOrPath?: string): Promise<ESLint.Formatter>;
1414+
loadFormatter(nameOrPath?: string): Promise<ESLint.LoadedFormatter>;
14151415
}
14161416

14171417
export namespace ESLint {
@@ -1555,14 +1555,38 @@ export namespace ESLint {
15551555
replacedBy: string[];
15561556
}
15571557

1558-
interface Formatter {
1559-
format(results: LintResult[], data?: LintResultData): string | Promise<string>;
1558+
interface ResultsMeta {
1559+
maxWarningsExceeded?: MaxWarningsExceeded | undefined;
1560+
}
1561+
1562+
/** The type of an object resolved by {@link ESLint.loadFormatter}. */
1563+
interface LoadedFormatter {
1564+
1565+
/**
1566+
* Used to call the underlying formatter.
1567+
* @param results An array of lint results to format.
1568+
* @param resultsMeta An object with an optional `maxWarningsExceeded` property that will be
1569+
* passed to the underlying formatter function along with other properties set by ESLint.
1570+
* This argument can be omitted if `maxWarningsExceeded` is not needed.
1571+
* @return The formatter output.
1572+
*/
1573+
format(results: LintResult[], resultsMeta?: ResultsMeta): string | Promise<string>;
15601574
}
15611575

1576+
// The documented type name is `LoadedFormatter`, but `Formatter` has been historically more used.
1577+
type Formatter = LoadedFormatter;
1578+
1579+
/**
1580+
* The expected signature of a custom formatter.
1581+
* @param results An array of lint results to format.
1582+
* @param context Additional information for the formatter.
1583+
* @return The formatter output.
1584+
*/
1585+
type FormatterFunction =
1586+
(results: LintResult[], context: LintResultData) => string | Promise<string>;
1587+
15621588
// Docs reference the types by those name
15631589
type EditInfo = Rule.Fix;
1564-
type LoadedFormatter = Formatter;
1565-
type ResultsMeta = LintResultData;
15661590
}
15671591

15681592
// #endregion

tests/lib/types/types.test.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ linterWithEslintrcConfig.getRules();
10031003
const customFormatter1: ESLint.Formatter = { format: () => "ok" };
10041004
const customFormatter2: ESLint.Formatter = { format: () => Promise.resolve("ok") };
10051005

1006-
let data: ESLint.LintResultData;
1006+
let resultsMeta: ESLint.ResultsMeta;
10071007
const meta: Rule.RuleMetaData = {
10081008
type: "suggestion",
10091009
docs: {
@@ -1019,15 +1019,15 @@ linterWithEslintrcConfig.getRules();
10191019
},
10201020
};
10211021

1022-
data = { cwd: "/foo/bar", rulesMeta: { "no-extra-semi": meta } };
1022+
resultsMeta = { maxWarningsExceeded: { maxWarnings: 42, foundWarnings: 43 } };
10231023

10241024
const version: string = ESLint.version;
10251025

10261026
(async () => {
10271027
const results: ESLint.LintResult[] = await resultsPromise;
10281028
const formatter = await formatterPromise;
10291029

1030-
const output: string = await formatter.format(results, data);
1030+
const output: string = await formatter.format(results, resultsMeta);
10311031

10321032
eslint.getRulesMetaForResults(results);
10331033

@@ -1131,7 +1131,7 @@ linterWithEslintrcConfig.getRules();
11311131
const customFormatter1: ESLint.Formatter = { format: () => "ok" };
11321132
const customFormatter2: ESLint.Formatter = { format: () => Promise.resolve("ok") };
11331133

1134-
let data: ESLint.LintResultData;
1134+
let resultsMeta: ESLint.ResultsMeta;
11351135
const meta: Rule.RuleMetaData = {
11361136
type: "suggestion",
11371137
docs: {
@@ -1147,15 +1147,15 @@ linterWithEslintrcConfig.getRules();
11471147
},
11481148
};
11491149

1150-
data = { cwd: "/foo/bar", rulesMeta: { "no-extra-semi": meta } };
1150+
resultsMeta = { maxWarningsExceeded: { maxWarnings: 42, foundWarnings: 43 } };
11511151

11521152
const version: string = LegacyESLint.version;
11531153

11541154
(async () => {
11551155
const results: ESLint.LintResult[] = await resultsPromise;
11561156
const formatter = await formatterPromise;
11571157

1158-
const output: string = await formatter.format(results, data);
1158+
const output: string = await formatter.format(results, resultsMeta);
11591159

11601160
eslint.getRulesMetaForResults(results);
11611161

@@ -1169,6 +1169,20 @@ linterWithEslintrcConfig.getRules();
11691169

11701170
// #endregion
11711171

1172+
// #region ESLint.Formatter
1173+
1174+
function jsonFormatter(results: ESLint.LintResult[]) {
1175+
return JSON.stringify(results, null, 2);
1176+
};
1177+
1178+
const customFormatter: ESLint.FormatterFunction = jsonFormatter;
1179+
1180+
function wrapperFormatter(results: ESLint.LintResult[], { cwd, maxWarningsExceeded, rulesMeta }: ESLint.LintResultData) {
1181+
customFormatter(results, { cwd, maxWarningsExceeded, rulesMeta });
1182+
}
1183+
1184+
// #endregion ESLint.Formatter
1185+
11721186
// #region ESLint.LintResult
11731187

11741188
let results!: ESLint.LintResult[];
@@ -1216,7 +1230,7 @@ for (const result of results) {
12161230
}
12171231
}
12181232

1219-
// #region ESLint.LintResult
1233+
// #endregion ESLint.LintResult
12201234

12211235
// #region ESLintRules
12221236

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy