Skip to content

Commit 54a51c7

Browse files
authored
feat: Support ESLint fix option
Closes: #323
1 parent d083fc8 commit 54a51c7

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ There's a good explanation on setting up TypeScript ESLint support by Robert Coo
114114

115115
- **eslintOptions** `object`:
116116

117-
- Options that can be used to initialise ESLint. See https://eslint.org/docs/1.0.0/developer-guide/nodejs-api#cliengine
117+
- Options that can be used to initialise ESLint. See https://eslint.org/docs/developer-guide/nodejs-api#cliengine
118118

119119
- **async** `boolean`:
120120
True by default - `async: false` can block webpack's emit to wait for type checker/linter and to add errors to the webpack's compilation.

src/createEslinter.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as path from 'path';
22

3-
import { LintReport } from './types/eslint';
3+
import { LintReport, Options as EslintOptions } from './types/eslint';
44
import { throwIfIsInvalidSourceFileError } from './FsHelper';
55

6-
export function createEslinter(eslintOptions: object) {
6+
export function createEslinter(eslintOptions: EslintOptions) {
77
// eslint-disable-next-line @typescript-eslint/no-var-requires
88
const { CLIEngine } = require('eslint');
99

10-
// See https://eslint.org/docs/1.0.0/developer-guide/nodejs-api#cliengine
10+
// See https://eslint.org/docs/developer-guide/nodejs-api#cliengine
1111
const eslinter = new CLIEngine(eslintOptions);
1212

1313
function getReport(filepath: string): LintReport | undefined {
@@ -21,7 +21,13 @@ export function createEslinter(eslintOptions: object) {
2121
return undefined;
2222
}
2323

24-
return eslinter.executeOnFiles([filepath]);
24+
const lintReport = eslinter.executeOnFiles([filepath]);
25+
26+
if (eslintOptions && eslintOptions.fix) {
27+
eslinter.outputFixes(lintReport);
28+
}
29+
30+
return lintReport;
2531
} catch (e) {
2632
throwIfIsInvalidSourceFileError(filepath, e);
2733
}

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { getForkTsCheckerWebpackPluginHooks } from './hooks';
2121
import { RUN, RunPayload, RunResult } from './RpcTypes';
2222
import { Issue, IssueSeverity } from './issue';
2323
import { VueOptions } from './types/vue-options';
24+
import { Options as EslintOptions } from './types/eslint';
2425

2526
const checkerPluginName = 'fork-ts-checker-webpack-plugin';
2627

@@ -39,8 +40,8 @@ namespace ForkTsCheckerWebpackPlugin {
3940
tsconfig: string;
4041
compilerOptions: object;
4142
eslint: boolean;
42-
/** Options to supply to eslint https://eslint.org/docs/1.0.0/developer-guide/nodejs-api#cliengine */
43-
eslintOptions: object;
43+
/** Options to supply to eslint https://eslint.org/docs/developer-guide/nodejs-api#cliengine */
44+
eslintOptions: EslintOptions;
4445
async: boolean;
4546
ignoreDiagnostics: number[];
4647
ignoreLints: string[];
@@ -79,7 +80,7 @@ class ForkTsCheckerWebpackPlugin {
7980
private tsconfig: string;
8081
private compilerOptions: object;
8182
private eslint = false;
82-
private eslintOptions: object = {};
83+
private eslintOptions: EslintOptions = {};
8384
private ignoreDiagnostics: number[];
8485
private ignoreLints: string[];
8586
private ignoreLintWarnings: boolean;

src/types/eslint.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ export interface LintReport {
3434
fixableErrorCount: number;
3535
fixableWarningCount: number;
3636
}
37+
38+
export interface Options {
39+
fix?: boolean;
40+
41+
// The rest of the properties are not specified here because they are not
42+
// directly used by this package and are instead just passed to eslint.
43+
// We do this in order to avoid a dependency on @types/eslint (since the use
44+
// of eslint is optional) and to avoid copying types from @types/eslint.
45+
[key: string]: any;
46+
}

0 commit comments

Comments
 (0)