forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEslintRunner.ts
More file actions
130 lines (108 loc) · 3.67 KB
/
Copy pathEslintRunner.ts
File metadata and controls
130 lines (108 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import { ITerminalProvider } from '@rushstack/node-core-library';
import { CmdRunner } from './CmdRunner';
import { ToolPaths } from './ToolPaths';
import { ILintRunnerConfig } from './ILintRunnerConfig';
import {
RushStackCompilerBase,
WriteFileIssueFunction
} from './RushStackCompilerBase';
interface IEslintFileResult {
// Example: "/full/path/to/File.ts"
filePath: string;
// Full content of the source file
source: string;
messages: IEslintMessage[];
errorCount: number;
warningCount: number;
fixableErrorCount: number;
fixableWarningCount: number;
}
enum EslintSeverity {
Off = 0,
Warn = 1,
Error = 2
}
interface IEslintMessage {
// The line number starts at 1
line: number;
endLine: number;
// The column number starts at 1
column: number;
endColumn: number;
// Example: "no-bitwise"
ruleId: string;
// Example: "unexpected"
messageId: string;
// Example: "Unexpected use of '&'."
message: string;
severity: EslintSeverity;
// Example: "BinaryExpression"
nodeType: string | null;
}
export class EslintRunner extends RushStackCompilerBase<ILintRunnerConfig> {
private _cmdRunner: CmdRunner;
public constructor(taskOptions: ILintRunnerConfig, rootPath: string, terminalProvider: ITerminalProvider) {
super(taskOptions, rootPath, terminalProvider);
this._cmdRunner = new CmdRunner(
this._standardBuildFolders,
this._terminal,
{
packagePath: ToolPaths.eslintPackagePath,
packageJson: ToolPaths.eslintPackageJson,
packageBinPath: path.join('bin', 'eslint.js')
}
);
}
public invoke(): Promise<void> {
const args: string[] = [
'--format', 'json',
'src/**/*.{ts,tsx}'
];
const stdoutBuffer: string[] = [];
return this._cmdRunner.runCmd({
args: args,
// ESLint errors are logged to stdout
onError: (data: Buffer) => {
this._terminal.writeErrorLine(`Unexpected STDERR output from ESLint: ${data.toString()}`);
},
onData: (data: Buffer) => {
stdoutBuffer.push(data.toString());
},
onClose: (code: number, hasErrors: boolean, resolve: () => void, reject: (error: Error) => void) => {
const dataStr: string = stdoutBuffer.join('');
try {
const eslintFileResults: IEslintFileResult[] = JSON.parse(dataStr);
const eslintErrorLogFn: WriteFileIssueFunction = this._taskOptions.displayAsError
? this._taskOptions.fileError
: this._taskOptions.fileWarning;
for (const eslintFileResult of eslintFileResults) {
const pathFromRoot: string = path.relative(this._standardBuildFolders.projectFolderPath,
eslintFileResult.filePath);
for (const message of eslintFileResult.messages) {
eslintErrorLogFn(
pathFromRoot,
message.line,
message.column,
message.ruleId,
message.message
);
}
}
} catch (e) {
// If we fail to parse the JSON, it's likely ESLint encountered an error parsing the config file,
// or it experienced an inner error. In this case, log the output as an error regardless of the
// displayAsError value
this._terminal.writeErrorLine(dataStr);
}
if (this._taskOptions.displayAsError && (code !== 0 || hasErrors)) {
reject(new Error(`exited with code ${code}`));
} else {
resolve();
}
}
});
}
}