error.py revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1#!/usr/bin/env python 2# 3# Copyright 2007 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"""Error object commonly used in linters.""" 18 19__author__ = ('robbyw@google.com (Robert Walker)', 20 'ajp@google.com (Andy Perelson)') 21 22 23class Error(object): 24 """Object representing a style error.""" 25 26 def __init__(self, code, message, token=None, position=None, fix_data=None): 27 """Initialize the error object. 28 29 Args: 30 code: The numeric error code. 31 message: The error message string. 32 token: The tokens.Token where the error occurred. 33 position: The position of the error within the token. 34 fix_data: Data to be used in autofixing. Codes with fix_data are: 35 GOOG_REQUIRES_NOT_ALPHABETIZED - List of string value tokens that are 36 class names in goog.requires calls. 37 """ 38 self.code = code 39 self.message = message 40 self.token = token 41 self.position = position 42 if token: 43 self.start_index = token.start_index 44 else: 45 self.start_index = 0 46 self.fix_data = fix_data 47 if self.position: 48 self.start_index += self.position.start 49 50 def Compare(a, b): 51 """Compare two error objects, by source code order. 52 53 Args: 54 a: First error object. 55 b: Second error object. 56 57 Returns: 58 A Negative/0/Positive number when a is before/the same as/after b. 59 """ 60 line_diff = a.token.line_number - b.token.line_number 61 if line_diff: 62 return line_diff 63 64 return a.start_index - b.start_index 65 Compare = staticmethod(Compare) 66