Don't commit that (gulp-check-grep)

Rodrigo Lopez
August 30, 2017

This is part of Code quality enforcement series.

If you have been programming and using version control for some time, it's very likely that you at some point have (or almost have) committed debug code, comments left for yourself, or even ascii-art (not judging here).

Commit hooks come in very handy when validating your code before commiting something you might regret.

Basic usage

gulp-check-grep is a gulp plugin that will process your files searching for a specific pattern and raising an error if it's found.

For instance, if you want to check for ipdb breakpoints in python:

gulp.task('check', function () {
  return gulp.src('**/*.py'
    .pipe(gulpCheckGrep(/ipdb\.set_trace\(/g, {message: 'Found ipdb breakpoint'}))
    .pipe(gulpCheckGrep.failOnError());
    });

Pretty often print() calls are used for debugging purposes while programming in Django: .pipe(gulpCheckGrep(/print\(/g)) This line could save your career.

Not only print calls are used, let's fix that:

.pipe(gulpCheckGrep([/print\(/g, /stdout\.write\(/g,], {message: 'stdout write found'}))

Pass function

With the patterh matching you will be good for a while, but at some point you might actually want to write to stdout, the standard and expected procedure would be using # noqa and the end of the line for python.

You could change your regular expressions and use lookarouds to just ignore the lines ending in noqa, but that could make your regular expressions look messy, for instance:

checkGrep(/print\((.(?!noqa$))+$/gm, {message: 'print call'},

Luckily gulp-check-grep accepts an optional parameter pass which is a function.

.pipe(gulpCheckGrep(/print\(/g, {pass: (line, lineNumber, file) => line.endsWith('# noqa')}))

The pass function receives not only the matching line, but also the line number, and the File object. The file objects might come handy in case you want to skip based on other file contents, for instance, a global # noqa comment at the begining of the file:

.pipe(gulpCheckGrep(/print\(/g, {pass: (line, lineNumber, file) => file.contents.startsWith('# noqa')}))

The pass function must return a boolean value.

"Don't commit that (gulp-check-grep)" by Rodrigo Lopez is licensed under CC BY SA. Source code examples are licensed under MIT.

Photo by Matthew Fournier.

Categorized under open source.

We’d love to work with you.

We treat client projects as if they were our own, understanding the underlying needs and astonishing users with the results.