Skip to content

Commit 97c86d6

Browse files
ajainarayananjohnnyreilly
authored andcommitted
[Feature][#67] Adds tslint autofix as a flag to ForkTsCheckerWebpackPlugin plugin (#174)
* Adds tslintAutoFix flag to ForkTsCheckerWebpackPlugin * Updates Vue integration test based on IncrementalChecker class update * Adds integration testing for tslintAutoFix flag * Updates README file to add description about tslintAutoFix flag * Removes unnecessary tslint checks * Adds a check to verify formatted file contents * Adds seperate tslint autofix json to avoid formatting files other than autofix files in integration test * Bumps package.json and updates CHANGELOG file for tslintAutoFix option * Updates README for tslintAutoFix with default value as false
1 parent 7967402 commit 97c86d6

11 files changed

+146
-6
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./test/integration/project/lintingError.ts

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.4.15
2+
3+
* [Add `tslintAutoFix` option to be passed on to tslint to auto format typescript files](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/174) (#174)
4+
15
## v0.4.14
26

37
* [Add support for `reportFiles` option](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/179) (#179)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Allows overriding TypeScript options. Should be specified in the same format as
6666
* **tslint** `string | true`:
6767
Path to *tslint.json* file or `true`. If `true`, uses `path.resolve(compiler.options.context, './tslint.json')`. Default: `undefined`.
6868

69+
* **tslintAutoFix** `boolean `:
70+
Passes on `--fix` flag while running `tslint` to auto fix linting errors. Default: false.
71+
6972
* **watch** `string | string[]`:
7073
Directories or files to watch by service. Not necessary but improves performance (reduces number of `fs.stat` calls).
7174

23.6 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "0.4.14",
3+
"version": "0.4.15",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"types": "lib/types/index.d.ts",

src/IncrementalChecker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class IncrementalChecker {
2424
programConfigFile: string;
2525
compilerOptions: object;
2626
linterConfigFile: string | false;
27+
linterAutoFix: boolean;
2728
watchPaths: string[];
2829
workNumber: number;
2930
workDivision: number;
@@ -44,6 +45,7 @@ export class IncrementalChecker {
4445
programConfigFile: string,
4546
compilerOptions: object,
4647
linterConfigFile: string | false,
48+
linterAutoFix: boolean,
4749
watchPaths: string[],
4850
workNumber: number,
4951
workDivision: number,
@@ -53,6 +55,7 @@ export class IncrementalChecker {
5355
this.programConfigFile = programConfigFile;
5456
this.compilerOptions = compilerOptions;
5557
this.linterConfigFile = linterConfigFile;
58+
this.linterAutoFix = linterAutoFix;
5659
this.watchPaths = watchPaths;
5760
this.workNumber = workNumber || 0;
5861
this.workDivision = workDivision || 1;
@@ -137,10 +140,10 @@ export class IncrementalChecker {
137140
);
138141
}
139142

140-
static createLinter(program: ts.Program) {
143+
createLinter(program: ts.Program) {
141144
const tslint = require('tslint');
142145

143-
return new tslint.Linter({ fix: false }, program);
146+
return new tslint.Linter({ fix: this.linterAutoFix }, program);
144147
}
145148

146149
static isFileExcluded(
@@ -192,7 +195,7 @@ export class IncrementalChecker {
192195
this.program = this.vue ? this.loadVueProgram() : this.loadDefaultProgram();
193196

194197
if (this.linterConfig) {
195-
this.linter = IncrementalChecker.createLinter(this.program);
198+
this.linter = this.createLinter(this.program);
196199
}
197200
}
198201

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ interface Options {
4242
tsconfig: string;
4343
compilerOptions: object;
4444
tslint: string | true;
45+
tslintAutoFix: boolean;
4546
watch: string | string[];
4647
async: boolean;
4748
ignoreDiagnostics: number[];
@@ -76,6 +77,7 @@ class ForkTsCheckerWebpackPlugin {
7677
tsconfig: string;
7778
compilerOptions: object;
7879
tslint: string | true;
80+
tslintAutoFix: boolean;
7981
watch: string[];
8082
ignoreDiagnostics: number[];
8183
ignoreLints: string[];
@@ -128,6 +130,7 @@ class ForkTsCheckerWebpackPlugin {
128130
? './tslint.json'
129131
: options.tslint
130132
: undefined;
133+
this.tslintAutoFix = options.tslintAutoFix || false;
131134
this.watch = isString(options.watch)
132135
? [options.watch]
133136
: options.watch || [];
@@ -566,6 +569,7 @@ class ForkTsCheckerWebpackPlugin {
566569
TSCONFIG: this.tsconfigPath,
567570
COMPILER_OPTIONS: JSON.stringify(this.compilerOptions),
568571
TSLINT: this.tslintPath || '',
572+
TSLINTAUTOFIX: this.tslintAutoFix,
569573
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
570574
WORK_DIVISION: Math.max(1, this.workersNumber),
571575
MEMORY_LIMIT: this.memoryLimit,

src/service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const checker = new IncrementalChecker(
88
process.env.TSCONFIG,
99
JSON.parse(process.env.COMPILER_OPTIONS),
1010
process.env.TSLINT === '' ? false : process.env.TSLINT,
11+
process.env.TSLINTAUTOFIX === 'true',
1112
process.env.WATCH === '' ? [] : process.env.WATCH.split('|'),
1213
parseInt(process.env.WORK_NUMBER, 10),
1314
parseInt(process.env.WORK_DIVISION, 10),

test/integration/index.spec.js

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var fs = require('fs');
12
var describe = require('mocha').describe;
23
var it = require('mocha').it;
34
var chai = require('chai');
@@ -9,12 +10,31 @@ chai.config.truncateThreshold = 0;
910
var expect = chai.expect;
1011

1112
var webpackMajorVersion = require('./webpackVersion')();
13+
const writeContentsToLintingErrorFile = (fileName, data) => {
14+
const promise = new Promise((resolve, reject) => {
15+
try {
16+
fs.writeFileSync(
17+
path.resolve(__dirname, `./project/src/${fileName}.ts`),
18+
data,
19+
{ flag: 'w' }
20+
);
21+
} catch (e) {
22+
return reject(e);
23+
}
24+
return resolve();
25+
});
26+
return promise;
27+
};
1228

1329
describe('[INTEGRATION] index', function() {
1430
this.timeout(60000);
1531
var plugin;
1632

17-
function createCompiler(options, happyPackMode) {
33+
function createCompiler(
34+
options,
35+
happyPackMode,
36+
entryPoint = './src/index.ts'
37+
) {
1838
plugin = new ForkTsCheckerWebpackPlugin(
1939
Object.assign({}, options, { silent: true })
2040
);
@@ -26,7 +46,7 @@ describe('[INTEGRATION] index', function() {
2646
return webpack({
2747
...(webpackMajorVersion >= 4 ? { mode: 'development' } : {}),
2848
context: path.resolve(__dirname, './project'),
29-
entry: './src/index.ts',
49+
entry: entryPoint,
3050
output: {
3151
path: path.resolve(__dirname, '../../tmp')
3252
},
@@ -92,6 +112,95 @@ describe('[INTEGRATION] index', function() {
92112
});
93113
});
94114

115+
it('should fix linting errors with tslintAutofix flag set to true', function(callback) {
116+
const lintErrorFileContents = `function someFunctionName(param1,param2){return param1+param2};
117+
`;
118+
const formattedFileContents = `function someFunctionName(param1, param2) {return param1 + param2; }
119+
`;
120+
const fileName = 'lintingError1';
121+
writeContentsToLintingErrorFile(fileName, lintErrorFileContents).then(
122+
() => {
123+
var compiler = createCompiler(
124+
{
125+
tslintAutoFix: true,
126+
tslint: path.resolve(__dirname, './project/tslint.autofix.json'),
127+
tsconfig: false
128+
},
129+
false,
130+
`./src/${fileName}.ts`
131+
);
132+
const deleteFile = () =>
133+
fs.unlinkSync(
134+
path.resolve(__dirname, `./project/src/${fileName}.ts`)
135+
);
136+
compiler.run(function(err, stats) {
137+
expect(stats.compilation.warnings.length).to.be.eq(0);
138+
let fileContents;
139+
try {
140+
fileContents = fs.readFileSync(
141+
path.resolve(__dirname, `./project/src/${fileName}.ts`),
142+
{
143+
encoding: 'utf-8'
144+
}
145+
);
146+
} catch (e) {
147+
throw e;
148+
}
149+
/*
150+
Helpful to wrap this in a try catch.
151+
If the assertion fails we still need to cleanup
152+
the temporary file created as part of the test
153+
*/
154+
try {
155+
expect(fileContents).to.be.eq(formattedFileContents);
156+
} catch (e) {
157+
deleteFile();
158+
throw e;
159+
}
160+
deleteFile();
161+
callback();
162+
});
163+
},
164+
err => {
165+
throw err;
166+
}
167+
);
168+
});
169+
it('should not fix linting by default', function(callback) {
170+
const lintErrorFileContents = `function someFunctionName(param1,param2){return param1+param2};
171+
`;
172+
const fileName = 'lintingError2';
173+
const deleteFile = () =>
174+
fs.unlinkSync(path.resolve(__dirname, `./project/src/${fileName}.ts`));
175+
writeContentsToLintingErrorFile(fileName, lintErrorFileContents).then(
176+
() => {
177+
var compiler = createCompiler(
178+
{ tslint: true },
179+
false,
180+
`./src/${fileName}.ts`
181+
);
182+
compiler.run(function(err, stats) {
183+
/*
184+
Helpful to wrap this in a try catch.
185+
If the assertion fails we still need to cleanup
186+
the temporary file created as part of the test
187+
*/
188+
try {
189+
expect(stats.compilation.warnings.length).to.be.eq(7);
190+
} catch (e) {
191+
deleteFile();
192+
throw e;
193+
}
194+
deleteFile();
195+
callback();
196+
});
197+
},
198+
err => {
199+
throw err;
200+
}
201+
);
202+
});
203+
95204
it('should block emit on build mode', function(callback) {
96205
var compiler = createCompiler();
97206

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"defaultSeverity": "warning",
3+
"extends": [
4+
"tslint:recommended"
5+
],
6+
"linterOptions": {
7+
"exclude": [
8+
"src/index.ts"
9+
]
10+
},
11+
"jsRules": {},
12+
"rules": {},
13+
"rulesDirectory": []
14+
}

test/integration/vue.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe('[INTEGRATION] vue', function() {
7878
plugin.tsconfigPath,
7979
{},
8080
plugin.tslintPath || false,
81+
plugin.tslintAutoFix || false,
8182
[compiler.context],
8283
ForkTsCheckerWebpackPlugin.ONE_CPU,
8384
1,

0 commit comments

Comments
 (0)