Skip to content

Commit b684bb1

Browse files
authored
fix: make TimePass.parse optional in types and docs (#21313)
1 parent ac74e37 commit b684bb1

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

‎docs/src/extend/stats.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ The `Stats` value is the timing information of each lint run. The `stats` proper
3030
The number of times ESLint has applied at least one fix after linting.
3131
- `times` (`{ passes: TimePass[] }`)<br>
3232
The times spent on (parsing, fixing, linting) a file, where the linting refers to the timing information for each rule.
33-
- `TimePass` (`{ parse: ParseTime, rules?: Record<string, RuleTime>, fix: FixTime, total: number }`)<br>
33+
- `TimePass` (`{ parse?: ParseTime, rules?: Record<string, RuleTime>, fix: FixTime, total: number }`)<br>
3434
An object containing the times spent on (parsing, fixing, linting)
3535
- `ParseTime` (`{ total: number }`)<br>
36-
The total time that is spent when parsing a file.
36+
The total time that is spent when parsing a file. The `parse` property is omitted if no parsing was performed, for example, when a processor's `preprocess()` method returns an empty array.
3737
- `RuleTime` (`{ total: number }`)<br>
3838
The total time that is spent on a rule.
3939
- `FixTime` (`{ total: number }`)<br>

‎lib/types/index.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,9 @@ export namespace Linter {
11251125
interface TimePass {
11261126
/**
11271127
* The parse object containing all parse time information.
1128+
* If no parsing was performed, the property is omitted.
11281129
*/
1129-
parse: { total: number };
1130+
parse?: { total: number };
11301131

11311132
/**
11321133
* The rules object containing all lint time information for each rule.

‎tests/lib/eslint/eslint.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10644,6 +10644,47 @@ describe("ESLint", () => {
1064410644
true,
1064510645
);
1064610646
});
10647+
10648+
it("Should omit parse stats when a processor returns no code blocks", async () => {
10649+
const engine = new ESLint({
10650+
overrideConfigFile: true,
10651+
stats: true,
10652+
overrideConfig: {
10653+
files: ["**/*.txt"],
10654+
processor: {
10655+
preprocess() {
10656+
return [];
10657+
},
10658+
postprocess() {
10659+
return [];
10660+
},
10661+
},
10662+
},
10663+
});
10664+
const results = await engine.lintText("plain text", {
10665+
filePath: "empty.txt",
10666+
});
10667+
10668+
assert.deepStrictEqual(results[0].messages, []);
10669+
assert.strictEqual(results[0].stats.fixPasses, 0);
10670+
assert.strictEqual(results[0].stats.times.passes.length, 1);
10671+
assert.strictEqual(
10672+
Object.hasOwn(results[0].stats.times.passes[0], "parse"),
10673+
false,
10674+
);
10675+
assert.strictEqual(
10676+
Object.hasOwn(results[0].stats.times.passes[0], "rules"),
10677+
false,
10678+
);
10679+
assert.deepStrictEqual(results[0].stats.times.passes[0].fix, {
10680+
total: 0,
10681+
});
10682+
assert.strictEqual(
10683+
isNumber(results[0].stats.times.passes[0].total),
10684+
true,
10685+
);
10686+
assert.ok(results[0].stats.times.passes[0].total >= 0);
10687+
});
1064710688
});
1064810689

1064910690
describe("getRulesMetaForResults()", () => {

‎tests/lib/types/types.test.mts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,13 @@ linter.getFixPassCount(); // $ExpectType number
12941294
(index: number, ruleId: string) => {
12951295
const pass = linter.getTimes().passes[index];
12961296
pass.fix.total; // $ExpectType number
1297-
pass.parse.total; // $ExpectType number
1297+
pass.parse?.total; // $ExpectType number | undefined
1298+
// @ts-expect-error -- parse may be omitted
1299+
pass.parse.total;
1300+
if (pass.parse) {
1301+
pass.parse.total; // $ExpectType number
1302+
}
1303+
delete pass.parse;
12981304
pass.rules![ruleId].total; // $ExpectType number
12991305
delete pass.rules;
13001306
pass.total; // $ExpectType number
@@ -1799,7 +1805,7 @@ for (const result of results) {
17991805
result.output = "foo";
18001806

18011807
result.stats = {
1802-
fixPasses: 2,
1808+
fixPasses: 3,
18031809
times: {
18041810
passes: [
18051811
{
@@ -1813,6 +1819,10 @@ for (const result of results) {
18131819
fix: { total: 5 },
18141820
total: 9,
18151821
},
1822+
{
1823+
fix: { total: 0 },
1824+
total: 1,
1825+
},
18161826
],
18171827
},
18181828
};

0 commit comments

Comments
 (0)