

def CheckToken(self, token, state):
    'Checks a token, given the current parser_state, for warnings and errors.\n\n    Args:\n      token: The current token under consideration\n      state: parser_state object that indicates the current state in the page\n    '
    super(JavaScriptLintRules, self).CheckToken(token, state)
    namespaces_info = self._namespaces_info
    if error_check.ShouldCheck(Rule.UNUSED_LOCAL_VARIABLES):
        self._CheckUnusedLocalVariables(token, state)
    if error_check.ShouldCheck(Rule.UNUSED_PRIVATE_MEMBERS):
        if (token.type == Type.SIMPLE_LVALUE):
            identifier = token.string
            if (identifier.endswith('_') and (not identifier.endswith('__'))):
                doc_comment = state.GetDocComment()
                suppressed = (doc_comment and (('underscore' in doc_comment.suppressions) or ('unusedPrivateMembers' in doc_comment.suppressions)))
                if (not suppressed):
                    if namespaces_info:
                        namespace = namespaces_info.GetClosurizedNamespace(identifier)
                        provided_namespaces = namespaces_info.GetProvidedNamespaces()
                    else:
                        namespace = None
                        provided_namespaces = set()
                    regex = re.compile('^this\\.[a-zA-Z_]+$')
                    if ((namespace in provided_namespaces) or regex.match(identifier)):
                        variable = identifier.split('.')[(- 1)]
                        self._declared_private_member_tokens[variable] = token
                        self._declared_private_members.add(variable)
            elif (not identifier.endswith('__')):
                for piece in identifier.split('.'):
                    if piece.endswith('_'):
                        self._used_private_members.add(piece)
        if (token.type == Type.IDENTIFIER):
            for piece in token.string.split('.'):
                if piece.endswith('_'):
                    self._used_private_members.add(piece)
    if (token.type == Type.DOC_FLAG):
        flag = token.attached_object
        if ((flag.flag_type == 'param') and (flag.name_token is not None)):
            self._CheckForMissingSpaceBeforeToken(token.attached_object.name_token)
            if ((flag.type is not None) and (flag.name is not None)):
                if error_check.ShouldCheck(Rule.VARIABLE_ARG_MARKER):
                    if (flag.jstype.IsVarArgsType() and (flag.name != 'var_args')):
                        self._HandleError(errors.JSDOC_MISSING_VAR_ARGS_NAME, ('Variable length argument %s must be renamed to var_args.' % flag.name), token)
                    elif ((not flag.jstype.IsVarArgsType()) and (flag.name == 'var_args')):
                        self._HandleError(errors.JSDOC_MISSING_VAR_ARGS_TYPE, ("Variable length argument %s type must start with '...'." % flag.name), token)
                if error_check.ShouldCheck(Rule.OPTIONAL_TYPE_MARKER):
                    if (flag.jstype.opt_arg and (not flag.name.startswith('opt_'))):
                        self._HandleError(errors.JSDOC_MISSING_OPTIONAL_PREFIX, ('Optional parameter name %s must be prefixed with opt_.' % flag.name), token)
                    elif ((not flag.jstype.opt_arg) and flag.name.startswith('opt_')):
                        self._HandleError(errors.JSDOC_MISSING_OPTIONAL_TYPE, ('Optional parameter %s type must end with =.' % flag.name), token)
        if (flag.flag_type in state.GetDocFlag().HAS_TYPE):
            if ((flag.flag_type not in state.GetDocFlag().CAN_OMIT_TYPE) and ((not flag.jstype) or flag.jstype.IsEmpty())):
                self._HandleError(errors.MISSING_JSDOC_TAG_TYPE, ('Missing type in %s tag' % token.string), token)
            elif (flag.name_token and flag.type_end_token and (tokenutil.Compare(flag.type_end_token, flag.name_token) > 0)):
                self._HandleError(errors.OUT_OF_ORDER_JSDOC_TAG_TYPE, ('Type should be immediately after %s tag' % token.string), token)
    elif (token.type == Type.DOUBLE_QUOTE_STRING_START):
        next_token = token.next
        while (next_token.type == Type.STRING_TEXT):
            if javascripttokenizer.JavaScriptTokenizer.SINGLE_QUOTE.search(next_token.string):
                break
            next_token = next_token.next
        else:
            self._HandleError(errors.UNNECESSARY_DOUBLE_QUOTED_STRING, 'Single-quoted string preferred over double-quoted string.', token, position=Position.All(token.string))
    elif (token.type == Type.END_DOC_COMMENT):
        doc_comment = state.GetDocComment()
        if (doc_comment.HasFlag('fileoverview') and doc_comment.HasFlag('externs')):
            self._SetLimitedDocChecks(True)
        if (error_check.ShouldCheck(Rule.BLANK_LINES_AT_TOP_LEVEL) and (not self._is_html) and state.InTopLevel() and (not state.InNonScopeBlock())):
            is_constructor = (doc_comment.HasFlag('constructor') or doc_comment.HasFlag('interface'))
            is_file_level_comment = (doc_comment.HasFlag('fileoverview') or (not doc_comment.start_token.previous))
            next_token = token.next
            if ((not next_token) or ((not is_file_level_comment) and (next_token.type in Type.NON_CODE_TYPES))):
                return
            if (doc_comment.SuppressionOnly() and (next_token.type == Type.IDENTIFIER) and (next_token.string in ['goog.provide', 'goog.require'])):
                return
            block_start = doc_comment.start_token
            if (not is_file_level_comment):
                token = block_start.previous
                while (token and (token.type in Type.COMMENT_TYPES)):
                    block_start = token
                    token = token.previous
            blank_lines = 0
            token = block_start.previous
            while (token and (token.type in [Type.WHITESPACE, Type.BLANK_LINE])):
                if (token.type == Type.BLANK_LINE):
                    blank_lines += 1
                elif ((token.type == Type.WHITESPACE) and (not token.line.strip())):
                    blank_lines += 1
                token = token.previous
            error_message = False
            expected_blank_lines = 0
            if (is_file_level_comment and (blank_lines == 0) and block_start.previous):
                error_message = 'Should have a blank line before a file overview.'
                expected_blank_lines = 1
            elif (is_constructor and (blank_lines != 3)):
                error_message = 'Should have 3 blank lines before a constructor/interface.'
                expected_blank_lines = 3
            elif ((not is_file_level_comment) and (not is_constructor) and (blank_lines != 2)):
                error_message = 'Should have 2 blank lines between top-level blocks.'
                expected_blank_lines = 2
            if error_message:
                self._HandleError(errors.WRONG_BLANK_LINE_COUNT, error_message, block_start, position=Position.AtBeginning(), fix_data=(expected_blank_lines - blank_lines))
    elif (token.type == Type.END_BLOCK):
        if (state.InFunction() and state.IsFunctionClose()):
            is_immediately_called = (token.next and (token.next.type == Type.START_PAREN))
            function = state.GetFunction()
            if (not self._limited_doc_checks):
                if (function.has_return and function.doc and (not is_immediately_called) and (not function.doc.HasFlag('return')) and (not function.doc.InheritsDocumentation()) and (not function.doc.HasFlag('constructor'))):
                    self._HandleError(errors.MISSING_RETURN_DOCUMENTATION, 'Missing @return JsDoc in function with non-trivial return', function.doc.end_token, position=Position.AtBeginning())
                elif ((not function.has_return) and (not function.has_throw) and function.doc and function.doc.HasFlag('return') and (not state.InInterfaceMethod())):
                    flag = function.doc.GetFlag('return')
                    valid_no_return_names = ['undefined', 'void', '*']
                    invalid_return = ((flag.jstype is None) or (not any(((sub_type.identifier in valid_no_return_names) for sub_type in flag.jstype.IterTypeGroup()))))
                    if invalid_return:
                        self._HandleError(errors.UNNECESSARY_RETURN_DOCUMENTATION, 'Found @return JsDoc on function that returns nothing', flag.flag_token, position=Position.AtBeginning())
            prototype_object_literal = False
            block_start = None
            previous_code = None
            previous_previous_code = None
            if state.InObjectLiteral():
                block_start = state.GetCurrentBlockStart()
            if block_start:
                previous_code = tokenutil.SearchExcept(block_start, Type.NON_CODE_TYPES, reverse=True)
            if (previous_code and previous_code.IsOperator('=')):
                previous_previous_code = tokenutil.SearchExcept(previous_code, Type.NON_CODE_TYPES, reverse=True)
            prototype_object_literal = (previous_previous_code and previous_previous_code.string.endswith('.prototype'))
            if (function.has_this and function.doc and (not function.doc.HasFlag('this')) and (not function.is_constructor) and (not function.is_interface) and ('.prototype.' not in function.name) and (not prototype_object_literal)):
                self._HandleError(errors.MISSING_JSDOC_TAG_THIS, 'Missing @this JsDoc in function referencing "this". (this usually means you are trying to reference "this" in a static function, or you have forgotten to mark a constructor with @constructor)', function.doc.end_token, position=Position.AtBeginning())
    elif (token.type == Type.IDENTIFIER):
        if ((token.string == 'goog.inherits') and (not state.InFunction())):
            if (state.GetLastNonSpaceToken().line_number == token.line_number):
                self._HandleError(errors.MISSING_LINE, 'Missing newline between constructor and goog.inherits', token, position=Position.AtBeginning())
            extra_space = state.GetLastNonSpaceToken().next
            while (extra_space != token):
                if (extra_space.type == Type.BLANK_LINE):
                    self._HandleError(errors.EXTRA_LINE, 'Extra line between constructor and goog.inherits', extra_space)
                extra_space = extra_space.next
        elif ((token.string == 'goog.provide') and (not state.InFunction()) and (namespaces_info is not None)):
            namespace = tokenutil.GetStringAfterToken(token)
            if ((not namespace) or namespaces_info.IsExtraProvide(token)):
                if (not namespace):
                    msg = 'Empty namespace in goog.provide'
                else:
                    msg = ('Unnecessary goog.provide: ' + namespace)
                    if namespace.endswith('Test'):
                        msg += ' *Test namespaces must be mentioned in the goog.setTestOnly() call'
                self._HandleError(errors.EXTRA_GOOG_PROVIDE, msg, token, position=Position.AtBeginning())
            if namespaces_info.IsLastProvide(token):
                missing_provides = namespaces_info.GetMissingProvides()
                if missing_provides:
                    self._ReportMissingProvides(missing_provides, tokenutil.GetLastTokenInSameLine(token).next, False)
                if (not namespaces_info.GetRequiredNamespaces()):
                    (missing_requires, illegal_alias_statements) = namespaces_info.GetMissingRequires()
                    if missing_requires:
                        self._ReportMissingRequires(missing_requires, tokenutil.GetLastTokenInSameLine(token).next, True)
                    if illegal_alias_statements:
                        self._ReportIllegalAliasStatement(illegal_alias_statements)
        elif ((token.string == 'goog.require') and (not state.InFunction()) and (namespaces_info is not None)):
            namespace = tokenutil.GetStringAfterToken(token)
            if (namespaces_info.IsFirstRequire(token) and (not namespaces_info.GetProvidedNamespaces())):
                missing_provides = namespaces_info.GetMissingProvides()
                if missing_provides:
                    self._ReportMissingProvides(missing_provides, tokenutil.GetFirstTokenInSameLine(token), True)
            if ((not namespace) or namespaces_info.IsExtraRequire(token)):
                if (not namespace):
                    msg = 'Empty namespace in goog.require'
                else:
                    msg = ('Unnecessary goog.require: ' + namespace)
                self._HandleError(errors.EXTRA_GOOG_REQUIRE, msg, token, position=Position.AtBeginning())
            if namespaces_info.IsLastRequire(token):
                (missing_requires, illegal_alias_statements) = namespaces_info.GetMissingRequires()
                if missing_requires:
                    self._ReportMissingRequires(missing_requires, tokenutil.GetLastTokenInSameLine(token).next, False)
                if illegal_alias_statements:
                    self._ReportIllegalAliasStatement(illegal_alias_statements)
    elif (token.type == Type.OPERATOR):
        last_in_line = token.IsLastInLine()
        if ((not token.metadata.IsUnaryOperator()) and (not last_in_line) and (not token.next.IsComment()) and (not token.next.IsOperator(',')) and (not tokenutil.IsDot(token)) and (token.next.type not in (Type.WHITESPACE, Type.END_PAREN, Type.END_BRACKET, Type.SEMICOLON, Type.START_BRACKET))):
            self._HandleError(errors.MISSING_SPACE, ('Missing space after "%s"' % token.string), token, position=Position.AtEnd(token.string))
    elif (token.type == Type.WHITESPACE):
        first_in_line = token.IsFirstInLine()
        last_in_line = token.IsLastInLine()
        if ((not last_in_line) and (not first_in_line) and (not token.next.IsComment())):
            if ((token.previous.type in (Type.START_PAREN, Type.START_BRACKET, Type.FUNCTION_NAME)) or (token.next.type == Type.START_PARAMETERS)):
                self._HandleError(errors.EXTRA_SPACE, ('Extra space after "%s"' % token.previous.string), token, position=Position.All(token.string))
    elif (token.type == Type.SEMICOLON):
        previous_token = tokenutil.SearchExcept(token, Type.NON_CODE_TYPES, reverse=True)
        if (not previous_token):
            self._HandleError(errors.REDUNDANT_SEMICOLON, 'Semicolon without any statement', token, position=Position.AtEnd(token.string))
        elif ((previous_token.type == Type.KEYWORD) and (previous_token.string not in ['break', 'continue', 'return'])):
            self._HandleError(errors.REDUNDANT_SEMICOLON, ("Semicolon after '%s' without any statement. Looks like an error." % previous_token.string), token, position=Position.AtEnd(token.string))

dataset/ETHPy150Open google/closure-linter/closure_linter/javascriptlintrules.py JavaScriptLintRules.CheckToken/original