Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita48f8c2

Browse files
authored
feat: add typeFormatterFunction, updateLoadedFormatter (#18872)
* feat: add type `FormatterFunction`, update `LoadedFormatter`* add TypeDoc for `FormatterFunction`* mark second argument of `FormatterFunction` as required, update TypeDoc
1 parentd594ddd commita48f8c2

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
exportnamespaceESLint{
@@ -1555,14 +1555,38 @@ export namespace ESLint {
15551555
replacedBy:string[];
15561556
}
15571557

1558-
interfaceFormatter{
1559-
format(results:LintResult[],data?:LintResultData):string|Promise<string>;
1558+
interfaceResultsMeta{
1559+
maxWarningsExceeded?:MaxWarningsExceeded|undefined;
1560+
}
1561+
1562+
/** The type of an object resolved by {@link ESLint.loadFormatter}. */
1563+
interfaceLoadedFormatter{
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+
typeFormatter=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+
typeFormatterFunction=
1586+
(results:LintResult[],context:LintResultData)=>string|Promise<string>;
1587+
15621588
// Docs reference the types by those name
15631589
typeEditInfo=Rule.Fix;
1564-
typeLoadedFormatter=Formatter;
1565-
typeResultsMeta=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
constcustomFormatter1:ESLint.Formatter={format:()=>"ok"};
10041004
constcustomFormatter2:ESLint.Formatter={format:()=>Promise.resolve("ok")};
10051005

1006-
letdata:ESLint.LintResultData;
1006+
letresultsMeta:ESLint.ResultsMeta;
10071007
constmeta: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
constversion:string=ESLint.version;
10251025

10261026
(async()=>{
10271027
constresults:ESLint.LintResult[]=awaitresultsPromise;
10281028
constformatter=awaitformatterPromise;
10291029

1030-
constoutput:string=awaitformatter.format(results,data);
1030+
constoutput:string=awaitformatter.format(results,resultsMeta);
10311031

10321032
eslint.getRulesMetaForResults(results);
10331033

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

1134-
letdata:ESLint.LintResultData;
1134+
letresultsMeta:ESLint.ResultsMeta;
11351135
constmeta: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
constversion:string=LegacyESLint.version;
11531153

11541154
(async()=>{
11551155
constresults:ESLint.LintResult[]=awaitresultsPromise;
11561156
constformatter=awaitformatterPromise;
11571157

1158-
constoutput:string=awaitformatter.format(results,data);
1158+
constoutput:string=awaitformatter.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+
functionjsonFormatter(results:ESLint.LintResult[]){
1175+
returnJSON.stringify(results,null,2);
1176+
};
1177+
1178+
constcustomFormatter:ESLint.FormatterFunction=jsonFormatter;
1179+
1180+
functionwrapperFormatter(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
letresults!: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)

[8]ページ先頭

©2009-2025 Movatter.jp