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 UNUSED_PRIVATE_MEMBERS = 'unused_private_members' 38 39 # Rule to raise all known errors. 40 ALL = 'all' 41 42 # All rules that are to be checked when using the strict flag. E.g. the rules 43 # that are specific to the stricter Closure style. 44 CLOSURE_RULES = frozenset([BLANK_LINES_AT_TOP_LEVEL, 45 INDENTATION, 46 WELL_FORMED_AUTHOR, 47 NO_BRACES_AROUND_INHERIT_DOC, 48 BRACES_AROUND_TYPE, 49 OPTIONAL_TYPE_MARKER]) 50 51 52flags.DEFINE_boolean('strict', False, 53 'Whether to validate against the stricter Closure style. ' 54 'This includes ' + (', '.join(Rule.CLOSURE_RULES)) + '.') 55flags.DEFINE_multistring('jslint_error', [], 56 'List of specific lint errors to check. Here is a list' 57 ' of accepted values:\n' 58 ' - ' + Rule.ALL + ': enables all following errors.\n' 59 ' - ' + Rule.BLANK_LINES_AT_TOP_LEVEL + ': validates' 60 'number of blank lines between blocks at top level.\n' 61 ' - ' + Rule.INDENTATION + ': checks correct ' 62 'indentation of code.\n' 63 ' - ' + Rule.WELL_FORMED_AUTHOR + ': validates the ' 64 '@author JsDoc tags.\n' 65 ' - ' + Rule.NO_BRACES_AROUND_INHERIT_DOC + ': ' 66 'forbids braces around @inheritdoc JsDoc tags.\n' 67 ' - ' + Rule.BRACES_AROUND_TYPE + ': enforces braces ' 68 'around types in JsDoc tags.\n' 69 ' - ' + Rule.OPTIONAL_TYPE_MARKER + ': checks correct ' 70 'use of optional marker = in param types.\n' 71 ' - ' + Rule.UNUSED_PRIVATE_MEMBERS + ': checks for ' 72 'unused private variables.\n') 73 74 75def ShouldCheck(rule): 76 """Returns whether the optional rule should be checked. 77 78 Computes different flags (strict, jslint_error, jslint_noerror) to find out if 79 this specific rule should be checked. 80 81 Args: 82 rule: Name of the rule (see Rule). 83 84 Returns: 85 True if the rule should be checked according to the flags, otherwise False. 86 """ 87 if rule in FLAGS.jslint_error or Rule.ALL in FLAGS.jslint_error: 88 return True 89 # Checks strict rules. 90 return FLAGS.strict and rule in Rule.CLOSURE_RULES 91