1#!/usr/bin/env python 2# 3# Copyright 2011 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 18"""Specific JSLint errors checker.""" 19 20 21 22import gflags as flags 23 24FLAGS = flags.FLAGS 25 26 27class Rule(object): 28 """Different rules to check.""" 29 30 # Documentations for specific rules goes in flag definition. 31 BLANK_LINES_AT_TOP_LEVEL = 'blank_lines_at_top_level' 32 INDENTATION = 'indentation' 33 WELL_FORMED_AUTHOR = 'well_formed_author' 34 NO_BRACES_AROUND_INHERIT_DOC = 'no_braces_around_inherit_doc' 35 BRACES_AROUND_TYPE = 'braces_around_type' 36 OPTIONAL_TYPE_MARKER = 'optional_type_marker' 37 VARIABLE_ARG_MARKER = 'variable_arg_marker' 38 UNUSED_PRIVATE_MEMBERS = 'unused_private_members' 39 UNUSED_LOCAL_VARIABLES = 'unused_local_variables' 40 41 # Rule to raise all known errors. 42 ALL = 'all' 43 44 # All rules that are to be checked when using the strict flag. E.g. the rules 45 # that are specific to the stricter Closure style. 46 CLOSURE_RULES = frozenset([BLANK_LINES_AT_TOP_LEVEL, 47 INDENTATION, 48 WELL_FORMED_AUTHOR, 49 NO_BRACES_AROUND_INHERIT_DOC, 50 BRACES_AROUND_TYPE, 51 OPTIONAL_TYPE_MARKER, 52 VARIABLE_ARG_MARKER]) 53 54 55flags.DEFINE_boolean('strict', False, 56 'Whether to validate against the stricter Closure style. ' 57 'This includes ' + (', '.join(Rule.CLOSURE_RULES)) + '.') 58flags.DEFINE_multistring('jslint_error', [], 59 'List of specific lint errors to check. Here is a list' 60 ' of accepted values:\n' 61 ' - ' + Rule.ALL + ': enables all following errors.\n' 62 ' - ' + Rule.BLANK_LINES_AT_TOP_LEVEL + ': validates' 63 'number of blank lines between blocks at top level.\n' 64 ' - ' + Rule.INDENTATION + ': checks correct ' 65 'indentation of code.\n' 66 ' - ' + Rule.WELL_FORMED_AUTHOR + ': validates the ' 67 '@author JsDoc tags.\n' 68 ' - ' + Rule.NO_BRACES_AROUND_INHERIT_DOC + ': ' 69 'forbids braces around @inheritdoc JsDoc tags.\n' 70 ' - ' + Rule.BRACES_AROUND_TYPE + ': enforces braces ' 71 'around types in JsDoc tags.\n' 72 ' - ' + Rule.OPTIONAL_TYPE_MARKER + ': checks correct ' 73 'use of optional marker = in param types.\n' 74 ' - ' + Rule.UNUSED_PRIVATE_MEMBERS + ': checks for ' 75 'unused private variables.\n' 76 ' - ' + Rule.UNUSED_LOCAL_VARIABLES + ': checks for ' 77 'unused local variables.\n') 78 79 80def ShouldCheck(rule): 81 """Returns whether the optional rule should be checked. 82 83 Computes different flags (strict, jslint_error, jslint_noerror) to find out if 84 this specific rule should be checked. 85 86 Args: 87 rule: Name of the rule (see Rule). 88 89 Returns: 90 True if the rule should be checked according to the flags, otherwise False. 91 """ 92 if 'no_' + rule in FLAGS.jslint_error: 93 return False 94 if rule in FLAGS.jslint_error or Rule.ALL in FLAGS.jslint_error: 95 return True 96 # Checks strict rules. 97 return FLAGS.strict and rule in Rule.CLOSURE_RULES 98