Exemplo n.º 1
0
def _classify_assignment(iToken, lTokens):

    oToken = lTokens[iToken]
    if oToken.get_value() == '=>':
        iOpenParen = 0
        iCloseParen = 0
        for iReturn, oToken in enumerate(lTokens[iToken:]):
            if isinstance(oToken, parser.open_parenthesis):
                iOpenParen += 1
            elif isinstance(oToken, parser.close_parenthesis):
                iEnd = iToken + iReturn
                iCloseParen += 1
                if iCloseParen > iOpenParen:
                    #                    print(lTokens[iToken:iEnd -1])
                    #                    rules_utils.print_debug(lTokens[iToken:iEnd + 1])
                    iEnd = utils.find_previous_non_whitespace_token(
                        iEnd - 1, lTokens)
                    return iEnd, True
                elif iOpenParen == iCloseParen:
                    #                    print(lTokens[iToken:iEnd])
                    #                    rules_utils.print_debug(lTokens[iToken:iEnd + 1])
                    return iEnd, True
            elif isinstance(oToken, parser.comma):
                if iOpenParen == iCloseParen:
                    iEnd = iToken + iReturn - 1
                    #                    print(lTokens[iToken:iEnd])
                    #                    rules_utils.print_debug(lTokens[iToken:iEnd + 1])
                    return iEnd, True
        return None
    return iToken, False
Exemplo n.º 2
0
def _check_assign_on_single_line(self, oToi):

    if self.assign_on_single_line == 'ignore':
        return

    iLine, lTokens = utils.get_toi_parameters(oToi)

    bSearch = False
    iOpenParen = 0
    iCloseParen = 0
    bAssignmentFound = False
    bOthersClause = False

    iToken = _find_assignment_operator(lTokens) + 1
    iStopIndex = len(lTokens)
    bFirstParenFound = False
    while iToken < iStopIndex:

        if bFirstParenFound:
            iToken, bOthersClause = _classify_others(iToken, lTokens)

            if bOthersClause:
                iToken += 1
                continue

            if lTokens[iToken].get_value() == '=>':
                iPreviousToken = utils.find_previous_non_whitespace_token(
                    iToken - 1, lTokens)
            iToken, bAssignmentFound = _classify_assignment(iToken, lTokens)

            if bAssignmentFound:
                #                rule_utils.print_debug(lTokens[iPreviousToken:iToken + 1])
                if rule_utils.number_of_carriage_returns(
                        lTokens[iPreviousToken:iToken]) > 0:
                    iErrorLine = rule_utils.number_of_carriage_returns(
                        lTokens[:iPreviousToken]) + iLine
                    sSolution = 'Remove carriage returns for assignments.'
                    oViolation = _create_violation(oToi, iErrorLine,
                                                   iPreviousToken, iToken,
                                                   'assign_on_single_line',
                                                   'remove', sSolution)
                    self.add_violation(oViolation)

        oToken = lTokens[iToken]

        if isinstance(oToken, parser.open_parenthesis):
            bFirstParenFound = True

        iToken += 1

    return None
Exemplo n.º 3
0
def _check_last_paren_new_line(self, oToi):

    if self.last_paren_new_line == 'ignore':
        return
    iLine, lTokens = utils.get_toi_parameters(oToi)
    lTokens.reverse()
    iLine = iLine + utils.count_carriage_returns(lTokens)
    bReturnFound = False
    bCommentFound = False
    for iToken, oToken in enumerate(lTokens):
        iLine = utils.decrement_line_number(iLine, oToken)
        if isinstance(oToken, parser.comment):
            bCommentFound = True
        if isinstance(oToken, parser.close_parenthesis):
            iEnd = len(lTokens) - iToken - 1
            if utils.are_next_consecutive_token_types(
                [parser.whitespace, parser.carriage_return], iToken + 1,
                    lTokens):
                bReturnFound = True
            elif utils.are_next_consecutive_token_types(
                [parser.carriage_return], iToken + 1, lTokens):
                bReturnFound = True

            lTokens.reverse()

            if self.last_paren_new_line == 'yes' and not bReturnFound:
                if self.move_last_comment == 'yes' and bCommentFound:
                    sSolution = 'Move parenthesis after assignment to the next line and trailing comment to previous line.'
                    oViolation = _create_violation(oToi, iLine, iEnd - 1,
                                                   len(lTokens) - 1,
                                                   'last_paren_new_line',
                                                   'insert_and_move_comment',
                                                   sSolution)
                    self.add_violation(oViolation)
                else:
                    sSolution = 'Move closing parenthesis to the next line.'
                    oViolation = _create_violation(oToi, iLine, iEnd - 1, iEnd,
                                                   'last_paren_new_line',
                                                   'insert', sSolution)
                    self.add_violation(oViolation)
            elif self.last_paren_new_line == 'no' and bReturnFound:
                sSolution = 'Move closing parenthesis to previous line.'
                iStart = utils.find_previous_non_whitespace_token(
                    iEnd - 1, lTokens)
                oViolation = _create_violation(oToi, iLine, iStart, iEnd,
                                               'last_paren_new_line', 'remove',
                                               sSolution)
                self.add_violation(oViolation)

            break
Exemplo n.º 4
0
def _check_close_paren_new_line(self, oToi):

    if self.close_paren_new_line == 'ignore':
        return

    iLine, lTokens = utils.get_toi_parameters(oToi)

    bAssignmentFound = False
    bOthersClause = False

    iToken = _find_assignment_operator(lTokens) + 1
    iStopIndex = _find_last_closing_paren(lTokens)
    bFirstParenFound = False
    while iToken < iStopIndex:

        if bFirstParenFound:
            iToken, bOthersClause = _classify_others(iToken, lTokens)

            if bOthersClause:
                iToken += 1
                continue

            iToken, bAssignmentFound = _classify_assignment(iToken, lTokens)

            if bAssignmentFound:
                iToken += 1
                continue

        oToken = lTokens[iToken]

        if isinstance(oToken, parser.open_parenthesis):
            bFirstParenFound = True

        if isinstance(oToken, parser.close_parenthesis):
            if utils.does_token_start_line(iToken, lTokens):
                if self.close_paren_new_line == 'no':
                    iStart = utils.find_previous_non_whitespace_token(
                        iToken - 1, lTokens)
                    iErrorLine = rules_utils.number_of_carriage_returns(
                        lTokens[:iToken]) + iLine
                    sSolution = 'Move closing parenthesis to previous line.'
                    oViolation = _create_violation(oToi, iErrorLine, iStart,
                                                   iToken,
                                                   'close_paren_new_line',
                                                   'remove', sSolution)
                    self.add_violation(oViolation)
            else:
                if self.close_paren_new_line == 'yes':
                    iStart = iToken - 1
                    iErrorLine = rules_utils.number_of_carriage_returns(
                        lTokens[:iToken]) + iLine
                    sSolution = 'Move closing parenthesis to the next line.'
                    oViolation = _create_violation(oToi, iErrorLine, iStart,
                                                   iToken,
                                                   'close_paren_new_line',
                                                   'insert', sSolution)
                    self.add_violation(oViolation)

        iToken += 1

    return None