Skip to content

Commit

Permalink
Add support for reportFiles option
Browse files Browse the repository at this point in the history
  • Loading branch information
ianschmitz committed Oct 28, 2018
1 parent 458cd74 commit 011cc15
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@types/babel-code-frame": "^6.20.1",
"@types/chokidar": "^1.7.5",
"@types/lodash": "^4.14.117",
"@types/micromatch": "^3.1.0",
"@types/minimatch": "^3.0.1",
"@types/node": "^8.0.26",
"@types/resolve": "0.0.4",
Expand Down Expand Up @@ -88,6 +89,7 @@
"chalk": "^2.4.1",
"chokidar": "^2.0.4",
"lodash": "^4.17.11",
"micromatch": "^3.1.10",
"minimatch": "^3.0.4",
"resolve": "^1.5.0",
"tapable": "^1.0.0"
Expand Down
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as process from 'process';
import * as childProcess from 'child_process';
import chalk, { Chalk } from 'chalk';
import * as fs from 'fs';
import * as micromatch from 'micromatch';
import * as os from 'os';
import * as webpack from 'webpack';
import isString = require('lodash/isString');
Expand Down Expand Up @@ -45,6 +46,7 @@ interface Options {
async: boolean;
ignoreDiagnostics: number[];
ignoreLints: string[];
reportFiles: string[];
colors: boolean;
logger: Logger;
formatter: 'default' | 'codeframe' | Formatter;
Expand Down Expand Up @@ -77,6 +79,7 @@ class ForkTsCheckerWebpackPlugin {
watch: string[];
ignoreDiagnostics: number[];
ignoreLints: string[];
reportFiles: string[];
logger: Logger;
silent: boolean;
async: boolean;
Expand Down Expand Up @@ -130,6 +133,7 @@ class ForkTsCheckerWebpackPlugin {
: options.watch || [];
this.ignoreDiagnostics = options.ignoreDiagnostics || [];
this.ignoreLints = options.ignoreLints || [];
this.reportFiles = options.reportFiles || [];
this.logger = options.logger || console;
this.silent = options.silent === true; // default false
this.async = options.async !== false; // default true
Expand Down Expand Up @@ -674,6 +678,26 @@ class ForkTsCheckerWebpackPlugin {
);
}

if (this.reportFiles.length) {
const reportFilesPredicate = (diagnostic: NormalizedMessage): boolean => {
if (diagnostic.file) {
const relativeFileName = path.relative(
this.compiler.options.context,
diagnostic.file
);
const matchResult = micromatch([relativeFileName], this.reportFiles);

if (matchResult.length === 0) {
return false;
}
}
return true;
};

this.diagnostics = this.diagnostics.filter(reportFilesPredicate);
this.lints = this.lints.filter(reportFilesPredicate);
}

if ('hooks' in this.compiler) {
// webpack 4
this.compiler.hooks.forkTsCheckerReceive.call(
Expand Down
31 changes: 22 additions & 9 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,26 @@ describe('[INTEGRATION] index', function() {
it('should detect paths', function() {
var plugin = new ForkTsCheckerWebpackPlugin({ tslint: true });

expect(plugin.tsconfig).to.be.equal('./tsconfig.json');
expect(plugin.tslint).to.be.equal('./tslint.json');
expect(plugin.tsconfig).to.equal('./tsconfig.json');
expect(plugin.tslint).to.equal('./tslint.json');
});

it('should set logger to console by default', function() {
var plugin = new ForkTsCheckerWebpackPlugin({});

expect(plugin.logger).to.be.equal(console);
expect(plugin.logger).to.equal(console);
});

it('should set watch to empty array by default', function() {
var plugin = new ForkTsCheckerWebpackPlugin({});

expect(plugin.watch).to.be.deep.equal([]);
expect(plugin.watch).to.deep.equal([]);
});

it('should set watch to one element array for string', function() {
var plugin = new ForkTsCheckerWebpackPlugin({ watch: '/test' });

expect(plugin.watch).to.be.deep.equal(['/test']);
expect(plugin.watch).to.deep.equal(['/test']);
});

it('should work without configuration', function(callback) {
Expand Down Expand Up @@ -227,8 +227,8 @@ describe('[INTEGRATION] index', function() {
});

function compareResults() {
expect(errorsA).to.be.deep.equal(errorsB);
expect(warningsA).to.be.deep.equal(warningsB);
expect(errorsA).to.deep.equal(errorsB);
expect(warningsA).to.deep.equal(warningsB);
callback();
}
});
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('[INTEGRATION] index', function() {
var compiler = createCompiler({}, true);

compiler.run(function(error, stats) {
expect(stats.compilation.errors.length).to.be.equal(1);
expect(stats.compilation.errors.length).to.equal(1);
callback();
});
});
Expand All @@ -293,7 +293,20 @@ describe('[INTEGRATION] index', function() {
var compiler = createCompiler({ checkSyntacticErrors: true }, true);

compiler.run(function(error, stats) {
expect(stats.compilation.errors.length).to.be.equal(2);
expect(stats.compilation.errors.length).to.equal(2);
callback();
});
});

it('should only show errors matching paths specified in reportFiles when provided', function(callback) {
var compiler = createCompiler(
{ checkSyntacticErrors: true, reportFiles: ['**/index.ts'] },
true
);

compiler.run(function(error, stats) {
expect(stats.compilation.errors.length).to.equal(1);
expect(stats.compilation.errors[0].file.endsWith('index.ts')).to.be.true;
callback();
});
});
Expand Down

0 comments on commit 011cc15

Please sign in to comment.