Adding GJSLinter to WebStorm – Google-Closure-JSLinter JetBrains WebStorm Integration

1. install Python and GJSLinter.

(as explained here)

2. apply the patch, or download the already patched version of GJSLint

(as explained here)

3. install WebStorm6, open it, close any opened projects.

4. follow the images:

2013-04-01_202923

2013-04-01_203126

2013-04-01_203301

prepare an empty file name it config.ini

here is a sample content (values are “new line”-separated)

--strict
--ignore_errors=1,2,6,110,131,233
--jsdoc
--summary
--beep
--check_html
--nomultiprocess
--debug_indentation
--time

 

place the path for gjslint.exe, it will most probably be something like:  C:\Python27\Scripts\gjslint.exe
set the placement of the configuration file, and press OK.

2013-04-01_203910

you can run those scripts in command-line too,
but then it will be one line, and “=” will be replaced with one space.

you can change the config.ini while working in WebStorm, and the changes will be effective immediately.

 

you can also change the color of the GJSLinter notifications:
2013-04-01_204445

Python Patch – Ignore Some Of Google-Closure-JSLinter (GJSLint) Errors

By Running From Command-Line:

gjslint.exe --strict C:\work\trunk\someJSFile.js

I’m getting some important notes like:

:: Line 9, E:0010: Missing semicolon at end of line

some less interesting but still important to keep the GClosure parser happy

:: Line 6, E:0220: No docs found for member '........'
:: Line 44, E:0240: @param descriptions must end with valid punctuation such as a period.

some are just for conventions, but still important to keep our code unified in format:

:: Line 7, E:0131: Single-quoted string preferred over double-quoted string.
:: Line 10, E:0001: Extra space after "......"

but some are completely rubbish like:

:: Line 17, E:0006: Wrong indentation: expected any of {2, 14} but got 4
:: Line 73, E:0110: Line too long (89 characters).
:: Line 142, E:0233: Optional parameter name ..... must be prefixed with opt_.

I especially dislike the long-line notice and the opt_ prefix needed,
since I work in camelCase, I keep underscore to very few and specific cases.

and as long I am using “=” suffix in the annotation to mark the parameter optional

--->   *   @param {number=} myNumber

its ok!

 

Python Patch:

well, Python files are text based-on, but the Windows-Installation of GJSLinter
is compiling an executable file named gjslint.exe

1. download the latest version of the GJSLinter.

2. apply the following patch

Left file: \closure_linter\errorrules_original.py
Right file: \closure_linter\errorrules_patched.py

22from closure_linter import errors=22from closure_linter import errors
2323
2424
25FLAGS = flags.FLAGS25FLAGS = flags.FLAGS
26flags.DEFINE_boolean(‘jsdoc’, True,26flags.DEFINE_boolean(‘jsdoc’, True,
27                     ‘Whether to report errors for missing JsDoc.’)27                     ‘Whether to report errors for missing JsDoc.’)
28<>28flags.DEFINE_list(‘ignore_errors’, [], ‘List of error codes to ignore.’)
29=29
30def ShouldReportError(error):30def ShouldReportError(error):
31  “””Whether the given error should be reported.31  “””Whether the given error should be reported.
3232
33  Returns:33  Returns:
34    True for all errors except missing documentation errors.  For these,34    True for all errors except missing documentation errors.  For these,
35    it returns the value of the jsdoc flag.35    it returns the value of the jsdoc flag.
36  “””36  “””
37  return FLAGS.jsdoc or error not in (<>37  return (FLAGS.jsdoc or error not in (
38      errors.MISSING_PARAMETER_DOCUMENTATION,=38      errors.MISSING_PARAMETER_DOCUMENTATION,
39      errors.MISSING_RETURN_DOCUMENTATION,39      errors.MISSING_RETURN_DOCUMENTATION,
40      errors.MISSING_MEMBER_DOCUMENTATION,40      errors.MISSING_MEMBER_DOCUMENTATION,
41      errors.MISSING_PRIVATE,41      errors.MISSING_PRIVATE,
42      errors.MISSING_JSDOC_TAG_THIS)<>42      errors.MISSING_JSDOC_TAG_THIS)) and str(error) not in FLAGS.ignore_errors

 

or:

Before

#!/usr/bin/env python
#
# Copyright 2010 The Closure Linter Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Linter error rules class for Closure Linter."""

__author__ = 'robbyw@google.com (Robert Walker)'

import gflags as flags
from closure_linter import errors

FLAGS = flags.FLAGS
flags.DEFINE_boolean('jsdoc', True,
                     'Whether to report errors for missing JsDoc.')

def ShouldReportError(error):
  """Whether the given error should be reported.

  Returns:
    True for all errors except missing documentation errors.  For these,
    it returns the value of the jsdoc flag.
  """
  return FLAGS.jsdoc or error not in (
      errors.MISSING_PARAMETER_DOCUMENTATION,
      errors.MISSING_RETURN_DOCUMENTATION,
      errors.MISSING_MEMBER_DOCUMENTATION,
      errors.MISSING_PRIVATE,
      errors.MISSING_JSDOC_TAG_THIS)

and
after:

#!/usr/bin/env python
#
# Copyright 2010 The Closure Linter Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Linter error rules class for Closure Linter."""

__author__ = 'robbyw@google.com (Robert Walker)'

import gflags as flags
from closure_linter import errors

FLAGS = flags.FLAGS
flags.DEFINE_boolean('jsdoc', True,
                     'Whether to report errors for missing JsDoc.')
flags.DEFINE_list('ignore_errors', [], 'List of error codes to ignore.')

def ShouldReportError(error):
  """Whether the given error should be reported.

  Returns:
    True for all errors except missing documentation errors.  For these,
    it returns the value of the jsdoc flag.
  """
  return (FLAGS.jsdoc or error not in (
      errors.MISSING_PARAMETER_DOCUMENTATION,
      errors.MISSING_RETURN_DOCUMENTATION,
      errors.MISSING_MEMBER_DOCUMENTATION,
      errors.MISSING_PRIVATE,
      errors.MISSING_JSDOC_TAG_THIS)) and str(error) not in FLAGS.ignore_errors

View Changes Side-By-Side in Google-Code (By Great Ilia Mirkin)

Use it:
without:

gjslint.exe --strict C:\work\trunk\myFile.js

with:

gjslint.exe --strict --ignore_errors 6,110,233 C:\work\trunk\myFile.js

:: disable:
::         6:    Wrong indentation
::         110:  Line too long (the max is 80chars).
::         233:  Optional parameter name ..... must be prefixed with opt_.

Note:
it takes comma-separated list of the integer error numbers
for example “line too long” is marked as “E0110” so you should use integer value of: 110.

also!
put the –ignore_errors switch before the files path,
just as the example, you can combine all switches you would like to, even ones you’ll ignore,
it works nicely… :)

This will also works with JetBrains WebStorm IDE,
here is my CONFIG FILE:

--strict
--jslint_error=braces_around_type 
--jslint_error=unused_private_members
--jslint_error=optional_type_marker
--jslint_error=well_formed_author
--jsdoc
--summary
--beep
--check_html
--multiprocess
--debug_indentation
--time

you should add the switch to it

--strict
--ignore_errors=6
--ignore_errors=110
--ignore_errors=233
--jslint_error=braces_around_type 
--jslint_error=unused_private_members
--jslint_error=optional_type_marker
--jslint_error=well_formed_author
--jsdoc
--nosummary
--nobeep
--nocheck_html
--multiprocess
--nodebug_indentation
--notime

 

Download a Ready To Use Patched Version

closure_linter-latest_with_patch.tar.gz
closure_linter_2_3_9_with_patch.zip
closure_linter_2_3_9_with_patch.rar

Windows 7 – Install gjslint (Google Closure JSLinter) into Python

Download:
http://closure-linter.googlecode.com/files/closure_linter-latest.tar.gz

Use WinRAR or Universal Extractor

after extracting (twice),
you’ll be left with the “installation”:
2013-03-28_000728

open CMD, and navigate to this folder (shortcut — hold shift, click in the folder once removing selection from files, then right click on empty space and you’ll have ‘open command window here’ in the context menu — open CMD in this folder),

as explained in the README file,
run
python ./setup.py install
you will get a message
Installing fixjsstyle.exe script to C:\PYTHON27\Scripts
Installing gjslint.exe script to C:\PYTHON27\Scripts

now you can add the path
C:\Python27\Scripts
to your PATH Environment-variable, so you could use gjslint.exe from everywhere.

how to use:

run
gjslint.exe --strict file1.js
to get all the Google-Closure-JSlinter messages.
you don’t have to use the ‘–strict’ but it is probably better if you’ll do.
gjslint.exe file1.js

to skip checking the @param and other annotations in the JSDoc, you can add –nojsdoc
gjslint.exe --strict --nojsdoc file1.js

you can run recursively on a folder’s content, parsing without changing, the files, and writing the result to output.
gjslint.exe -r C:\work\trunk\jsfolder

fixjsstyle.exe file1.js
to automatically preform style and code changes (make sure to backup first in case you won’t like the result, it is not backing up by default, but changing the file directly).

the original document https://developers.google.com/closure/utilities/docs/linter_howto

the Google Closure page https://developers.google.com/closure/library/

Also check-out the “how to integrate with JetBrains WebStorm 6”. (soon to come)

you can also run
gjslint.exe --help to get the following list of command line switches:

USAGE: C:\Python27\Scripts\gjslint-script.py [flags]

flags:

closure_linter.checker:
--closurized_namespaces: Namespace prefixes, used for testing
ofgoog.provide/require
(default: '')
(a comma separated list)
--ignored_extra_namespaces: Fully qualified namespaces that should be not be
reported as extra by the linter.
(default: '')
(a comma separated list)

closure_linter.common.simplefileflags:
-e,--exclude_directories: Exclude the specified directories (only applicable
along with -r or --presubmit)
(default: '_demos')
(a comma separated list)
-x,--exclude_files: Exclude the specified files
(default: 'deps.js')
(a comma separated list)
-r,--recurse: Recurse in to the subdirectories of the given path;
repeat this option to specify a list of values

closure_linter.ecmalintrules:
--custom_jsdoc_tags: Extra jsdoc tags to allow
(default: '')
(a comma separated list)

closure_linter.error_check:
--jslint_error: List of specific lint errors to check. Here is a list of
accepted values:
- all: enables all following errors.
- blank_lines_at_top_level: validatesnumber of blank lines between blocks at
top level.
- indentation: checks correct indentation of code.
- well_formed_author: validates the @author JsDoc tags.
- no_braces_around_inherit_doc: forbids braces around @inheritdoc JsDoc
tags.
- braces_around_type: enforces braces around types in JsDoc tags.
- optional_type_marker: checks correct use of optional marker = in param
types.
- unused_private_members: checks for unused private variables.
;
repeat this option to specify a list of values
(default: '[]')
--[no]strict: Whether to validate against the stricter Closure style. This
includes optional_type_marker, well_formed_author,
no_braces_around_inherit_doc, indentation, braces_around_type,
blank_lines_at_top_level.
(default: 'false')

closure_linter.errorrules:
--[no]jsdoc: Whether to report errors for missing JsDoc.
(default: 'true')

closure_linter.gjslint:
--additional_extensions: List of additional file extensions (not js) that
should be treated as JavaScript files.
(a comma separated list)
--[no]beep: Whether to beep when errors are found.
(default: 'true')
--[no]check_html: Whether to check javascript in html files.
(default: 'false')
-?,--[no]help: show this help
--[no]helpshort: show usage only for this module
--[no]helpxml: like --help, but generates XML output
--[no]multiprocess: Whether to attempt parallelized linting using the
multiprocessing module. Enabled by default on Linux if the multiprocessing
module is present (Python 2.6+). Otherwise disabled by default. Disabling
may make debugging easier.
(default: 'false')
--[no]summary: Whether to show an error count summary.
(default: 'false')
--[no]time: Whether to emit timing statistics.
(default: 'false')
--[no]unix_mode: Whether to emit warnings in standard unix format.
(default: 'false')

closure_linter.indentation:
--[no]debug_indentation: Whether to print debugging information for
indentation.
(default: 'false')

closure_linter.runner:
--[no]error_trace: Whether to show error exceptions.
(default: 'false')
--limited_doc_files: List of files with relaxed documentation checks. Will not
report errors for missing documentation, some missing descriptions, or
methods whose @return tags don't have a matching return statement.
(default: 'dummy.js,externs.js')
(a comma separated list)

gflags:
--flagfile: Insert flag definitions from the given file into the command line.
(default: '')
--undefok: comma-separated list of flag names that it is okay to specify on
the command line even if the program does not define a flag with that name.
IMPORTANT: flags in this list that have arguments MUST use the --flag=value
format.
(default: '')

run fixjsstyle.exe --help to get the full command line switches or the “fix style” execute,

note that the HELP text is saying
USAGE: C:\Python27\Scripts\gjslint-script.py [flags]
USAGE: C:\Python27\Scripts\fixjsstyle-script.py [flags]

but for windows you can you the exe-files with the flags,

you can also use the python syntax,
assuming your python path has set, you can also enter
python C:\Python27\Scripts\gjslint-script.py [flags]
or
python C:\Python27\Scripts\fixjsstyle-script.py [flags]

which is the equivalent of running gjslint.exe or fixjsstyle.exe