Home | History | Annotate | Download | only in closure_linter
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2010 The Closure Linter Authors. All Rights Reserved.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS-IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 """Linter error rules class for Closure Linter."""
     18 
     19 __author__ = 'robbyw (at] google.com (Robert Walker)'
     20 
     21 import gflags as flags
     22 from closure_linter import errors
     23 
     24 
     25 FLAGS = flags.FLAGS
     26 flags.DEFINE_boolean('jsdoc', True,
     27                      'Whether to report errors for missing JsDoc.')
     28 flags.DEFINE_list('disable', None,
     29                   'Disable specific error. Usage Ex.: gjslint --disable 1,'
     30                   '0011 foo.js.')
     31 flags.DEFINE_integer('max_line_length', 80, 'Maximum line length allowed '
     32                      'without warning.', lower_bound=1)
     33 
     34 disabled_error_nums = None
     35 
     36 
     37 def GetMaxLineLength():
     38   """Returns allowed maximum length of line.
     39 
     40   Returns:
     41     Length of line allowed without any warning.
     42   """
     43   return FLAGS.max_line_length
     44 
     45 
     46 def ShouldReportError(error):
     47   """Whether the given error should be reported.
     48 
     49   Returns:
     50     True for all errors except missing documentation errors and disabled
     51     errors.  For missing documentation, it returns the value of the
     52     jsdoc flag.
     53   """
     54   global disabled_error_nums
     55   if disabled_error_nums is None:
     56     disabled_error_nums = []
     57     if FLAGS.disable:
     58       for error_str in FLAGS.disable:
     59         error_num = 0
     60         try:
     61           error_num = int(error_str)
     62         except ValueError:
     63           pass
     64         disabled_error_nums.append(error_num)
     65 
     66   return ((FLAGS.jsdoc or error not in (
     67       errors.MISSING_PARAMETER_DOCUMENTATION,
     68       errors.MISSING_RETURN_DOCUMENTATION,
     69       errors.MISSING_MEMBER_DOCUMENTATION,
     70       errors.MISSING_PRIVATE,
     71       errors.MISSING_JSDOC_TAG_THIS)) and
     72           (not FLAGS.disable or error not in disabled_error_nums))
     73