Disable Specific Error Messages in the Closure Linter (gjslint, fixjsstyle)

Update: It seems Google has made this possible directly now, the feature request was marked fixed. Usage as follows:

--disable Disable specific error.
Usage Ex.: gjslint --disable 1,0011 foo.js.

--max_line_length Maximum line length allowed without warning.

This makes the following mostly obsolete, but I’m leaving it here anyway.


I have recently been working on ical.js, a library to parse rfc5545 calendar data. James Lal has been helping me outand has suggested to use a Javascript linter on a regular basis. We have agreed on using the Google Closure Linter.

One thing I didn’t like about the Closure Linter is that error messages are not configurable. There are some cases where I think 80 characters per line are just wrong, for example a for() statement that has 81 characters. Would you really want to break lines for just one character? What about URLs in comments that are just longer than 80 characters? Also, I’m not a fan of single quotes.

On the internet I found the Closure Linter’s feature request issue and a few discussion topics. While the feature request links a patch, I wanted a solution that does not modify the Closure Linter so new contributors don’t have to go around patching things just to check and fix the style. To do so I created a wrapper script around gjslint and fixjsstyle, as well as a common file for defining the errors to ignore.

First of all, the common file. Name it something like myerrorrules.py:

from closure_linter import errors
from closure_linter import errorrules

OriginalShouldReportError = None

def InjectErrorReporter():
 global OriginalShouldReportError
 OriginalShouldReportError = errorrules.ShouldReportError
 errorrules.ShouldReportError = MyShouldReportError

def MyShouldReportError(error):
 global OriginalShouldReportError
 return error not in (
   errors.UNNECESSARY_DOUBLE_QUOTED_STRING,
   errors.LINE_TOO_LONG,
 ) and OriginalShouldReportError(error)

Then you need a small wrapper around gjslint.py:

from closure_linter import gjslint
import myerrorrules

if __name__ == '__main__':
  myerrorrules.InjectErrorReporter()
  gjslint.main()

And quite the same for fixjsstyle.py:

from closure_linter import fixjsstyle
import myerrorrules

if __name__ == '__main__':
  myerrorrules.InjectErrorReporter()
  fixjsstyle.main()

That is all! Now you just have to start gjslint and fixjsstyle through these wrappers and you will have all the errors ignored that you like.

About these ads

4 thoughts on “Disable Specific Error Messages in the Closure Linter (gjslint, fixjsstyle)

  1. Thanks Philipp for this wrapper. I am still confused on how to call your wrapper from my python script though. I don’t understand how to easily pass arguments such as the js file path I want to parse for example. The only way I found is to overwrite the sys.argv (since passing argv directly does not seem to work in gjslint).

    And If I want to read the result of the parsing, I need to launch a subprocess and read the stdout.. which seems pretty weird as well.

    Did I miss something?
    thanks!

    • Hi Sophie,

      the original intent of these scripts was to call them from the command line, just like you would call the original closure linter.

      looing at gjslint.py, you can call the main function with your own argv, like so:


      from closure_linter import fixjsstyle
      import myerrorrules

      if __name__ == '__main__':
      myerrorrules.InjectErrorReporter()
      fixjsstyle.main(['/path/to/file.js'])

      If you want to read the results and process them programatically, then I’d suggest to instead make your own version of gjslint’s main() function, which calls _CheckPaths() or _MultiprocessCheckPaths().

    • I agree, ignoring them on the command line is useful too. The advantage to this solution is that you can define your own derived code style for a project once and then stick with it. If you push this to your repo, others contributing can stick to your style without creating their own alias that adds the errors to ignore.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s