panuhorsmalahti / gulp-tslint

TypeScript linter plugin for Gulp
MIT License
118 stars 44 forks source link

feature request: allow passing gulp plugin callback that gets called for each reported error #81

Closed atsu85 closed 7 years ago

atsu85 commented 7 years ago

I'd like to have a callback from gulp-tslint that would get called when the plugin reports errors, so I could show GUI popup message when gulp-tslint detects problems (and when they get fixed)

Note, that I don't want to create my own custom error formatter, I'd like to use whatever error formatter independently of the callback.

Here is an example of what I'd like:

const ErrorHelper = require('../ErrorHelper');

const errorHelper = new ErrorHelper("gulp lint-typescript"); // just a helper class for showing GUI messages
gulp.task('lint-typescript', function() {
  return gulp.src(paths.src.typescript)
    .pipe(tslint({
      formatter: "verbose" // use whatever tslint reporter
    }))
    .pipe(tslint.report({
      emitError: false, // make sure "end" event is emitted even in case of error
      onReportCallback: (info) => { //  <------------- THIS IS WHAT I NEED
        errorHelper.errorHandler(info.totalReported); // increments error counter by totalReported
      },
    }))
    .on('end', errorHelper.onEnd); // shows GUI notification with number of errors or notify that errors are fixed
});
atsu85 commented 7 years ago

I'll send a PR shortly

atsu85 commented 7 years ago

@panuhorsmalahti, I've created the PR - it is ready to be merged :)

panuhorsmalahti commented 7 years ago

Couldn't you alternatively use the .tsint property of each file?

https://github.com/panuhorsmalahti/gulp-tslint/blob/master/index.ts#L160

atsu85 commented 7 years ago

@panuhorsmalahti, thanks, I didn't think about it initially (I have no experience with Node streams and Gulp plugins). The solution i ended up with needed following change:

Instead of:

    .pipe(tslint.report({
      emitError: false, // make sure "end" event is emitted even in case of error
      onReportCallback: (info) => { //  <------------- THIS IS WHAT I NEED
        errorHelper.errorHandler(info.totalReported); // increments error counter by totalReported
      },
    }))

I used:

    .pipe(tslint.report({
      emitError: false,
    }))
    .on('data', function(data) {
      if (data.tslint.failureCount) {
        errorHelper.errorHandler();
      }
    })