Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./test/integration/project/lintingError.ts
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Allows overriding TypeScript options. Should be specified in the same format as
* **tslint** `string | true`:
Path to *tslint.json* file or `true`. If `true`, uses `path.resolve(compiler.options.context, './tslint.json')`. Default: `undefined`.

* **tslintAutoFix** `boolean`:
Passes on `--fix` flag while running `tslint` to auto fix linting errors.

* **watch** `string | string[]`:
Directories or files to watch by service. Not necessary but improves performance (reduces number of `fs.stat` calls).

Expand Down
Binary file added fork-ts-checker-webpack-plugin-v0.4.11.tgz
Binary file not shown.
9 changes: 6 additions & 3 deletions src/IncrementalChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class IncrementalChecker {
programConfigFile: string;
compilerOptions: object;
linterConfigFile: string | false;
linterAutoFix: boolean;
watchPaths: string[];
workNumber: number;
workDivision: number;
Expand All @@ -44,6 +45,7 @@ export class IncrementalChecker {
programConfigFile: string,
compilerOptions: object,
linterConfigFile: string | false,
linterAutoFix: boolean,
watchPaths: string[],
workNumber: number,
workDivision: number,
Expand All @@ -53,6 +55,7 @@ export class IncrementalChecker {
this.programConfigFile = programConfigFile;
this.compilerOptions = compilerOptions;
this.linterConfigFile = linterConfigFile;
this.linterAutoFix = linterAutoFix;
this.watchPaths = watchPaths;
this.workNumber = workNumber || 0;
this.workDivision = workDivision || 1;
Expand Down Expand Up @@ -134,10 +137,10 @@ export class IncrementalChecker {
);
}

static createLinter(program: ts.Program) {
createLinter(program: ts.Program) {
const tslint = require('tslint');

return new tslint.Linter({ fix: false }, program);
return new tslint.Linter({ fix: this.linterAutoFix }, program);
}

static isFileExcluded(
Expand Down Expand Up @@ -189,7 +192,7 @@ export class IncrementalChecker {
this.program = this.vue ? this.loadVueProgram() : this.loadDefaultProgram();

if (this.linterConfig) {
this.linter = IncrementalChecker.createLinter(this.program);
this.linter = this.createLinter(this.program);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface Options {
tsconfig: string;
compilerOptions: object;
tslint: string | true;
tslintAutoFix: boolean;
watch: string | string[];
async: boolean;
ignoreDiagnostics: number[];
Expand Down Expand Up @@ -74,6 +75,7 @@ class ForkTsCheckerWebpackPlugin {
tsconfig: string;
compilerOptions: object;
tslint: string | true;
tslintAutoFix: boolean;
watch: string[];
ignoreDiagnostics: number[];
ignoreLints: string[];
Expand Down Expand Up @@ -125,6 +127,7 @@ class ForkTsCheckerWebpackPlugin {
? './tslint.json'
: options.tslint
: undefined;
this.tslintAutoFix = options.tslintAutoFix || false;
this.watch = isString(options.watch)
? [options.watch]
: options.watch || [];
Expand Down Expand Up @@ -562,6 +565,7 @@ class ForkTsCheckerWebpackPlugin {
TSCONFIG: this.tsconfigPath,
COMPILER_OPTIONS: JSON.stringify(this.compilerOptions),
TSLINT: this.tslintPath || '',
TSLINTAUTOFIX: this.tslintAutoFix,
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
WORK_DIVISION: Math.max(1, this.workersNumber),
MEMORY_LIMIT: this.memoryLimit,
Expand Down
1 change: 1 addition & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const checker = new IncrementalChecker(
process.env.TSCONFIG,
JSON.parse(process.env.COMPILER_OPTIONS),
process.env.TSLINT === '' ? false : process.env.TSLINT,
process.env.TSLINTAUTOFIX === 'true',
process.env.WATCH === '' ? [] : process.env.WATCH.split('|'),
parseInt(process.env.WORK_NUMBER, 10),
parseInt(process.env.WORK_DIVISION, 10),
Expand Down
76 changes: 74 additions & 2 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var fs = require('fs');
Comment thread
ajainarayanan marked this conversation as resolved.
var describe = require('mocha').describe;
var it = require('mocha').it;
var chai = require('chai');
Expand All @@ -9,12 +10,31 @@ chai.config.truncateThreshold = 0;
var expect = chai.expect;

var webpackMajorVersion = require('./webpackVersion')();
const writeContentsToLintingErrorFile = (fileName, data) => {
const promise = new Promise((resolve, reject) => {
try {
fs.writeFileSync(
path.resolve(__dirname, `./project/src/${fileName}.ts`),
data,
{ flag: 'w' }
);
} catch (e) {
return reject(e);
}
return resolve();
});
return promise;
};

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

function createCompiler(options, happyPackMode) {
function createCompiler(
options,
happyPackMode,
entryPoint = './src/index.ts'
) {
plugin = new ForkTsCheckerWebpackPlugin(
Object.assign({}, options, { silent: true })
);
Expand All @@ -26,7 +46,7 @@ describe('[INTEGRATION] index', function() {
return webpack({
...(webpackMajorVersion >= 4 ? { mode: 'development' } : {}),
context: path.resolve(__dirname, './project'),
entry: './src/index.ts',
entry: entryPoint,
output: {
path: path.resolve(__dirname, '../../tmp')
},
Expand Down Expand Up @@ -92,6 +112,58 @@ describe('[INTEGRATION] index', function() {
});
});

it('should fix linting errors with tslintAutofix flag set to true', function(callback) {
const lintErrorFileContents = `function someFunctionName(param1,param2){return param1+param2};
`;
const fileName = 'lintingError1';
writeContentsToLintingErrorFile(fileName, lintErrorFileContents).then(
() => {
var compiler = createCompiler(
{
tslintAutoFix: true,
tslint: true
},
false,
`./src/${fileName}.ts`
);
compiler.run(function(err, stats) {
expect(stats.compilation.warnings.length).to.be.eq(0);
Comment thread
ajainarayanan marked this conversation as resolved.
Outdated
fs.unlinkSync(
path.resolve(__dirname, `./project/src/${fileName}.ts`)
);
callback();
});
},
err => {
throw err;
}
);
});
it('should not fix linting by default', function(callback) {
const lintErrorFileContents = `function someFunctionName(param1,param2){return param1+param2};
`;
const fileName = 'lintingError2';
writeContentsToLintingErrorFile(fileName, lintErrorFileContents).then(
() => {
var compiler = createCompiler(
{ tslint: true },
false,
`./src/${fileName}.ts`
);
compiler.run(function(err, stats) {
expect(stats.compilation.warnings.length).to.be.eq(6);
fs.unlinkSync(
path.resolve(__dirname, `./project/src/${fileName}.ts`)
);
callback();
});
},
err => {
throw err;
}
);
});

it('should block emit on build mode', function(callback) {
var compiler = createCompiler();

Expand Down
1 change: 1 addition & 0 deletions test/integration/vue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('[INTEGRATION] vue', function() {
plugin.tsconfigPath,
{},
plugin.tslintPath || false,
plugin.tslintAutoFix || false,
[compiler.context],
ForkTsCheckerWebpackPlugin.ONE_CPU,
1,
Expand Down