8000 New: Exit on fatal error (fixes #13711) (#14730) · eslint/eslint@1bfbefd · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bfbefd

Browse files
A-KatopodisA-Katopodis
andauthored
New: Exit on fatal error (fixes #13711) (#14730)
* Docs: Updated docs for exit-on-fatal-error * New: Added exit-on-fatal-error feature * Addressed PR feedback * Made description consistent * Updated dev docs for nodejs-api * Updated cmd docs * Added test case for 1 exit code * Fixed typos Co-authored-by: A-Katopodis <ankatopo@microsoft.com>
1 parent ed007c8 commit 1bfbefd

File tree

12 files changed

+120
-11
lines changed

12 files changed

+120
-11
lines changed

docs/developer-guide/nodejs-api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ The `LintResult` value is the information of the linting result of each file. Th
355355
* `fixableWarningCount` (`number`)<br>
356356
The number of warnings that can be fixed automatically by the `fix` constructor option.
357357
* `errorCount` (`number`)<br>
358-
The number of errors. This includes fixable errors.
358+
The number of errors. This includes fixable errors and fatal errors.
359+
* `fatalErrorCount` (`number`)<br>
360+
The number of fatal errors.
359361
* `warningCount` (`number`)<br>
360362
The number of warnings. This includes fixable warnings.
361363
* `output` (`string | undefined`)<br>

docs/user-guide/command-line-interface.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Miscellaneous:
8181
--init Run config initialization wizard - default: false
8282
--env-info Output execution environment information - default: false
8383
--no-error-on-unmatched-pattern Prevent errors when pattern is unmatched - default: false
84+
--exit-on-fatal-error Exit with exit code 2 in case of fatal error - default: false
8485
--debug Output debugging information
8586
-h, --help Show help
8687
-v, --version Output the version number
@@ -467,6 +468,10 @@ This option outputs information about the execution environment, including the v
467468

468469
This option prevents errors when a quoted glob pattern or `--ext` is unmatched. This will not prevent errors when your shell can't match a glob.
469470

471+
#### `--exit-on-fatal-error`
472+
473+
This option causes ESLint to exit with exit code 2 if one or more fatal parsing errors occur. Without this option, fatal parsing errors are reported as rule violations.
474+
470475
#### `--debug`
471476

472477
This option outputs debugging information to the console. This information is useful when you're seeing a problem and having a hard time pinpointing it. The ESLint team may ask for this debugging information to help solve bugs.

lib/cli-engine/cli-engine.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ function calculateStatsPerFile(messages) {
156156
return messages.reduce((stat, message) => {
157157
if (message.fatal || message.severity === 2) {
158158
stat.errorCount++;
159+
if (message.fatal) {
160+
stat.fatalErrorCount++;
161+
}
159162
if (message.fix) {
160163
stat.fixableErrorCount++;
161164
}
@@ -168,6 +171,7 @@ function calculateStatsPerFile(messages) {
168171
return stat;
169172
}, {
170173
errorCount: 0,
174+
fatalErrorCount: 0,
171175
warningCount: 0,
172176
fixableErrorCount: 0,
173177
fixableWarningCount: 0
@@ -183,12 +187,14 @@ function calculateStatsPerFile(messages) {
183187
function calculateStatsPerRun(results) {
184188
return results.reduce((stat, result) => {
185189
stat.errorCount += result.errorCount;
190+
stat.fatalErrorCount += result.fatalErrorCount;
186191
stat.warningCount += result.warningCount;
187192
stat.fixableErrorCount += result.fixableErrorCount;
188193
stat.fixableWarningCount += result.fixableWarningCount;
189194
return stat;
190195
}, {
191196
errorCount: 0,
197+
fatalErrorCount: 0,
192198
warningCount: 0,
193199
fixableErrorCount: 0,
194200
fixableWarningCount: 0

lib/cli.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,16 @@ function translateOptions({
131131
*/
132132
function countErrors(results) {
133133
let errorCount = 0;
134+
let fatalErrorCount = 0;
134135
let warningCount = 0;
135136

136137
for (const result of results) {
137138
errorCount += result.errorCount;
139+
fatalErrorCount += result.fatalErrorCount;
138140
warningCount += result.warningCount;
139141
}
140142

141-
return { errorCount, warningCount };
143+
return { errorCount, fatalErrorCount, warningCount };
142144
}
143145

144146
/**
@@ -314,9 +316,12 @@ const cli = {
314316
if (await printResults(engine, resultsToPrint, options.format, options.outputFile)) {
315317

316318
// Errors and warnings from the original unfiltered results should determine the exit code
317-
const { errorCount, warningCount } = countErrors(results);
319+
const { errorCount, fatalErrorCount, warningCount } = countErrors(results);
320+
318321
const tooManyWarnings =
319322
options.maxWarnings >= 0 && warningCount > options.maxWarnings;
323+
const shouldExitForFatalErrors =
324+
options.exitOnFatalError && fatalErrorCount > 0;
320325

321326
if (!errorCount && tooManyWarnings) {
322327
log.error(
@@ -325,6 +330,10 @@ const cli = {
325330
);
326331
}
327332

333+
if (shouldExitForFatalErrors) {
334+
return 2;
335+
}
336+
328337
return (errorCount || tooManyWarnings) ? 1 : 0;
329338
}
330339

lib/options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ module.exports = optionator({
290290
default: "true",
291291
description: "Prevent errors when pattern is unmatched"
292292
},
293+
{
294+
option: "exit-on-fatal-error",
295+
type: "Boolean",
296+
default: "false" FEE1 ;,
297+
description: "Exit with exit code 2 in case of fatal error"
298+
},
293299
{
294300
option: "debug",
295301
type: "Boolean",

tests/bin/eslint.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe("bin/eslint.js", () => {
8989
filePath: "<text>",
9090
messages: [],
9191
errorCount: 0,
92+
fatalErrorCount: 0,
9293
warningCount: 0,
9394
fixableErrorCount: 0,
9495
fixableWarningCount: 0,
@@ -117,6 +118,14 @@ describe("bin/eslint.js", () => {
117118
return assertExitCode(child, 1);
118119
});
119120

121+
it("has exit code 2 if a syntax error is thrown when exit-on-fatal-error is true", () => {
122+
const child = runESLint(["--stdin", "--no-eslintrc", "--exit-on-fatal-error"]);
123+
124+
child.stdin.write("This is not valid JS syntax.\n");
125+
child.stdin.end();
126+
return assertExitCode(child, 2);
127+
});
128+
120129
it("has exit code 1 if a linting error occurs", () => {
121130
const child = runESLint(["--stdin", "--no-eslintrc", "--rule", "semi:2"]);
122131

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*eslint no-unused-vars: "error"*/
2+
3+
var foo = 1 F438 ;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = 1;

tests/lib/cli-engine/cli-engine.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ describe("CLIEngine", () => {
144144
assert.strictEqual(report.results.length, 1);
145145
assert.strictEqual(report.errorCount, 5);
146146
assert.strictEqual(report.warningCount, 0);
147+
assert.strictEqual(report.fatalErrorCount, 0);
147148
assert.strictEqual(report.fixableErrorCount, 3);
148149
assert.strictEqual(report.fixableWarningCount, 0);
149150
assert.strictEqual(report.results[0].messages.length, 5);
@@ -310,13 +311,15 @@ describe("CLIEngine", () => {
310311
messages: [],
311312
errorCount: 0,
312313
warningCount: 0,
314+
fatalErrorCount: 0,
313315
fixableErrorCount: 0,
314316
fixableWarningCount: 0,
315317
output: "var bar = foo;"
316318
}
317319
],
318320
errorCount: 0,
319321
warningCount: 0,
322+
fatalErrorCount: 0,
320323
fixableErrorCount: 0,
321324
fixableWarningCount: 0,
322325
usedDeprecatedRules: []
@@ -519,13 +522,15 @@ describe("CLIEngine", () => {
519522
],
520523
errorCount: 1,
521524
warningCount: 0,
525+
fatalErrorCount: 0,
522526
fixableErrorCount: 0,
523527
fixableWarningCount: 0,
524528
source: "var bar = foo"
525529
}
526530
],
527531
errorCount: 1,
528532
warningCount: 0,
533+
fatalErrorCount: 0,
529534
fixableErrorCount: 0,
530535
fixableWarningCount: 0,
531536
usedDeprecatedRules: []
@@ -562,13 +567,15 @@ describe("CLIEngine", () => {
562567
],
563568
errorCount: 1,
564569
warningCount: 0,
570+
fatalErrorCount: 1,
565571
fixableErrorCount: 0,
566572
fixableWarningCount: 0,
567573
output: "var bar = foothis is a syntax error."
568574
}
569575
],
570576
errorCount: 1,
571577
warningCount: 0,
578+
fatalErrorCount: 1,
572579
fixableErrorCount: 0,
573580
fixableWarningCount: 0,
574581
usedDeprecatedRules: []
@@ -604,13 +611,15 @@ describe("CLIEngine", () => {
604611
],
605612
errorCount: 1,
606613
warningCount: 0,
614+
fatalErrorCount: 1,
607615
fixableErrorCount: 0,
608616
fixableWarningCount: 0,
609617
source: "var bar ="
610618
}
611619
],
612620
errorCount: 1,
613621
warningCount: 0,
622+
fatalErrorCount: 1,
614623
fixableErrorCount: 0,
615624
fixableWarningCount: 0,
616625
usedDeprecatedRules: []
@@ -692,13 +701,15 @@ describe("CLIEngine", () => {
692701
],
693702
errorCount: 1,
694703
warningCount: 0,
704+
fatalErrorCount: 1,
695705
fixableErrorCount: 0,
696706
fixableWarningCount: 0,
697707
source: "var bar = foothis is a syntax error.\n return bar;"
698708
}
699709
],
700710
errorCount: 1,
701711
warningCount: 0,
712+
fatalErrorCount: 1,
702713
fixableErrorCount: 0,
703714
fixableWarningCount: 0,
704715
usedDeprecatedRules: []
@@ -1684,6 +1695,7 @@ describe("CLIEngine", () => {
16841695
messages: [],
16851696
errorCount: 0,
16861697
warningCount: 0,
1698+
fatalErrorCount: 0,
16871699
fixableErrorCount: 0,
16881700
fixableWarningCount: 0,
16891701
output: "true ? \"yes\" : \"no\";\n"
@@ -1693,6 +1705,7 @@ describe("CLIEngine", () => {
16931705
messages: [],
16941706
errorCount: 0,
16951707
warningCount: 0,
1708+
fatalErrorCount: 0,
16961709
fixableErrorCount: 0,
16971710
fixableWarningCount: 0
16981711
},
@@ -1713,6 +1726,7 @@ describe("CLIEngine", () => {
17131726
],
17141727
errorCount: 1,
17151728
warningCount: 0,
1729+
fatalErrorCount: 0,
17161730
fixableErrorCount: 0,
17171731
fixableWarningCount: 0,
17181732
output: "var msg = \"hi\";\nif (msg == \"hi\") {\n\n}\n"
@@ -1734,6 +1748,7 @@ describe("CLIEngine", () => {
17341748
],
17351749
errorCount: 1,
17361750
warningCount: 0,
1751+
fatalErrorCount: 0,
17371752
fixableErrorCount: 0,
17381753
fixableWarningCount: 0,
17391754
output: "var msg = \"hi\" + foo;\n"
@@ -5077,13 +5092,15 @@ describe("CLIEngine", () => {
50775092
],
50785093
errorCount: 1,
50795094
warningCount: 0,
5095+
fatalErrorCount: 0,
50805096
fixableErrorCount: 0,
50815097
fixableWarningCount: 0,
50825098
source: "/* eslint-disable */"
50835099
}
50845100
],
50855101
errorCount: 1,
50865102
warningCount: 0,
5103+
fatalErrorCount: 0,
50875104
fixableErrorCount: 0,
50885105
fixableWarningCount: 0,
50895106
usedDeprecatedRules: []
@@ -6155,7 +6172,8 @@ describe("CLIEngine", () => {
61556172
}
61566173
],
61576174
source: "a == b",
6158-
warningCount: 0
6175+
warningCount: 0,
6176+
fatalErrorCount: 0
61596177
}
61606178
]);
61616179
});
@@ -6177,7 +6195,8 @@ describe("CLIEngine", () => {
61776195
fixableErrorCount: 0,
61786196
fixableWarningCount: 0,
61796197
messages: [],
6180-
warningCount: 0
6198+
warningCount: 0,
6199+
fatalErrorCount: 0
61816200
}
61826201
]);
61836202
});
@@ -6223,7 +6242,8 @@ describe("CLIEngine", () => {
62236242
fixableErrorCount: 0,
62246243
fixableWarningCount: 0,
62256244
messages: [],
6226-
warningCount: 0
6245+
warningCount: 0,
6246+
fatalErrorCount: 0
62276247
}
62286248
]);
62296249
});
@@ -6258,7 +6278,8 @@ describe("CLIEngine", () => {
62586278
}
62596279
],
62606280
source: "a == b",
6261-
warningCount: 0
6281+
warningCount: 0,
6282+
fatalErrorCount: 0
62626283
}
62636284
]);
62646285
});

0 commit comments

Comments
 (0)
0