def analyze(self, oFile):

        lToi = oFile.get_tokens_bounded_by(self.left_token, self.right_token)
        for oToi in lToi:
            iLine, lTokens = utils.get_toi_parameters(oToi)

            if not utils.does_token_type_exist_in_list_of_tokens(parser.carriage_return, lTokens):
                continue

            if not utils.does_token_start_line(len(lTokens) - 1, lTokens):
                continue

            iStartIndex = oToi.get_start_index()
            iEndIndex = oToi.get_end_index()

            iRightColumn = oFile.get_column_of_token_index(iStartIndex)
            iLeftColumn = oFile.get_column_of_token_index(iEndIndex)

            if iRightColumn + 1 != iLeftColumn:
                iLineNumber = iLine + utils.count_token_types_in_list_of_tokens(parser.carriage_return, lTokens)
                sSolution = 'Move ' + lTokens[-1].get_value() + ' to column ' + str(iRightColumn)
                dAction = {}
                if iLeftColumn == 1:
                    dAction['action'] = 'insert'
                else:
                    dAction['action'] = 'adjust'
                dAction['column'] = iRightColumn
                oViolation = violation.New(iLineNumber, oToi, sSolution)
                oViolation.set_action(dAction)
                self.add_violation(oViolation)
Пример #2
0
def _build_structure_list(iLine, iColumn, lTokens):

    lStructure = []

    for iToken, oToken in enumerate(lTokens):

        iLine = utils.increment_line_number(iLine, oToken)

        if isinstance(oToken, parser.blank_line):
            continue

        if isinstance(oToken, parser.carriage_return):
            iColumn = 0
            dReturn = {}
            dReturn['type'] = 'return'
            dReturn['line'] = iLine
            lStructure.append(dReturn)
            continue

        iColumn += len(oToken.get_value())

        if isinstance(oToken, parser.close_parenthesis):
            dParen = {}
            dParen['type'] = 'close'
            dParen['line'] = iLine
            dParen['column'] = iColumn
            dParen['begin_line'] = utils.does_token_start_line(iToken, lTokens)
            lStructure.append(dParen)

        if isinstance(oToken, parser.open_parenthesis):
            dParen = {}
            dParen['type'] = 'open'
            dParen['line'] = iLine
            dParen['column'] = iColumn
            lStructure.append(dParen)

        if oToken.get_value().lower() == 'when':
            dWhen = {}
            dWhen['type'] = 'when'
            dWhen['line'] = iLine
            dWhen['column'] = iColumn - 4
            dWhen['iToken'] = iToken
            lStructure.append(dWhen)

        if oToken.get_value().lower() == 'else':
            dElse = {}
            dElse['type'] = 'else'
            dElse['line'] = iLine
            dElse['column'] = iColumn - 4
            dElse['iToken'] = iToken
            lStructure.append(dElse)

    return lStructure, iLine
    def analyze(self, oFile):
        lToi = self._get_tokens_of_interest(oFile)

        for oToi in lToi:
            iLine, lTokens = utils.get_toi_parameters(oToi)
            #            print('='*5 + str(iLine) + '='*70)

            iFirstLine, iFirstLineIndent = _get_first_line_info(iLine, oFile)

            iAssignColumn = oFile.get_column_of_token_index(
                oToi.get_start_index())
            iColumn = iAssignColumn

            dActualIndent = {}

            dActualIndent[iLine] = iFirstLineIndent
            lParens = []
            dIndex = {}

            bStartsWithParen = _starts_with_paren(lTokens)
            bSkipCommentLine = False

            for iToken, oToken in enumerate(lTokens):

                iLine = utils.increment_line_number(iLine, oToken)

                if isinstance(oToken, parser.blank_line):
                    continue

                if bSkipCommentLine:
                    if not isinstance(oToken, parser.carriage_return):
                        continue

                if isinstance(oToken, parser.carriage_return):
                    iColumn = 0
                    bSkipCommentLine = rules_utils.does_line_start_with_comment(
                        lTokens[iToken + 1:iToken + 3])
                    if bSkipCommentLine:
                        dActualIndent[iLine] = None
                    else:
                        dActualIndent[iLine] = _set_indent(iToken, lTokens)
                        dIndex[iLine] = iToken + 1
                    continue

                iColumn += len(oToken.get_value())

                if isinstance(oToken, parser.close_parenthesis):
                    dParen = {}
                    dParen['type'] = 'close'
                    dParen['line'] = iLine
                    dParen['column'] = iColumn
                    dParen['begin_line'] = utils.does_token_start_line(
                        iToken, lTokens)
                    lParens.append(dParen)

                if isinstance(oToken, parser.open_parenthesis):
                    dParen = {}
                    dParen['type'] = 'open'
                    dParen['line'] = iLine
                    dParen['column'] = iColumn
                    lParens.append(dParen)

            iLastLine = iLine

            if iFirstLine == iLastLine:
                continue

            iFirstTokenLength = len(lTokens[0].get_value())

            if self.align_paren == 'no' and self.align_left == 'yes':
                dExpectedIndent = _analyze_align_paren_no(
                    iFirstLine, iLastLine, lParens, self.indentSize,
                    dActualIndent, bStartsWithParen)
            if self.align_paren == 'yes' and self.align_left == 'no':
                dExpectedIndent = _analyze_align_paren_yes_align_left_no(
                    iFirstLine, iLastLine, lParens, dActualIndent,
                    self.indentSize, bStartsWithParen, iAssignColumn,
                    iFirstTokenLength)
            if self.align_paren == 'yes' and self.align_left == 'yes':
                dExpectedIndent = _analyze_align_paren_yes_align_left_yes(
                    iFirstLine, iLastLine, lParens, dActualIndent,
                    self.indentSize, bStartsWithParen, iAssignColumn)
            if self.align_paren == 'no' and self.align_left == 'no':
                dExpectedIndent = _analyze_align_paren_no_align_left_no(
                    iFirstLine, iLastLine, lParens, dActualIndent,
                    self.indentSize, bStartsWithParen, iAssignColumn)


#            print(f'Actual = {dActualIndent}')
#            print(f'Expect = {dExpectedIndent}')
#            print(f'Index  = {dIndex}')

            for iLine in range(iFirstLine, iLastLine + 1):
                if dActualIndent[iLine] is None:
                    continue
                if indents_match(dActualIndent[iLine], dExpectedIndent[iLine]):
                    continue

                oViolation = build_violation(iLine, oToi, iToken,
                                             dExpectedIndent, dIndex,
                                             dActualIndent)
                self.add_violation(oViolation)
Пример #4
0
    def analyze(self, oFile):
        lToi = self._get_tokens_of_interest(oFile)

        for oToi in lToi:
            iLine, lTokens = utils.get_toi_parameters(oToi)
            #            print('='*5 + str(iLine) + '='*70)

            iFirstLine, iFirstLineIndent = _get_first_line_info(iLine, oFile)

            iFirstColumn, iNextColumn, iLastColumn = _find_first_column(
                oFile, oToi, self.align_left, iFirstLineIndent,
                self.indentSize)
            iAssignColumn = oFile.get_column_of_token_index(
                oToi.get_start_index())
            iColumn = iAssignColumn

            dActualIndent = {}

            dActualIndent[iLine] = iFirstLineIndent
            lParens = []
            dIndex = {}

            bStartsWithParen = _starts_with_paren(lTokens)

            for iToken, oToken in enumerate(lTokens):

                iLine = utils.increment_line_number(iLine, oToken)

                if isinstance(oToken, parser.blank_line):
                    continue

                if isinstance(oToken, parser.carriage_return):
                    iColumn = 0
                    dActualIndent[iLine] = _set_indent(iToken, lTokens)
                    dIndex[iLine] = iToken + 1
                    continue

                iColumn += len(oToken.get_value())

                if isinstance(oToken, parser.close_parenthesis):
                    dParen = {}
                    dParen['type'] = 'close'
                    dParen['line'] = iLine
                    dParen['column'] = iColumn
                    dParen['begin_line'] = utils.does_token_start_line(
                        iToken, lTokens)
                    lParens.append(dParen)

                if isinstance(oToken, parser.open_parenthesis):
                    dParen = {}
                    dParen['type'] = 'open'
                    dParen['line'] = iLine
                    dParen['column'] = iColumn
                    lParens.append(dParen)

            iLastLine = iLine

            if iFirstLine == iLastLine:
                continue

            if not self.align_paren and self.align_left:
                dExpectedIndent = _analyze_align_paren_false(
                    iFirstLine, iLastLine, lParens, self.indentSize,
                    dActualIndent, bStartsWithParen)
            if self.align_paren and not self.align_left:
                dExpectedIndent = _analyze_align_paren_true(
                    iFirstLine, iLastLine, lParens, dActualIndent,
                    self.indentSize, bStartsWithParen, iAssignColumn)
            if self.align_paren and self.align_left:
                dExpectedIndent = _analyze_align_paren_true_align_left_true(
                    iFirstLine, iLastLine, lParens, dActualIndent,
                    self.indentSize, bStartsWithParen, iAssignColumn)


#            print(f'Actual = {dActualIndent}')
#            print(f'Expect = {dExpectedIndent}')
#            print(f'Index  = {dIndex}')

            for iLine in range(iFirstLine, iLastLine + 1):
                if dActualIndent[iLine] == dExpectedIndent[iLine]:
                    continue

                dAction = {}
                dAction['line'] = iLine
                dAction['column'] = dExpectedIndent[iLine]

                if dActualIndent[iLine] > 0:
                    dAction['action'] = 'adjust'
                else:
                    dAction['action'] = 'insert'

                sSolution = 'Adjust indent to column ' + str(
                    dExpectedIndent[iLine])
                iToken = dIndex[iLine]
                oViolation = violation.New(iLine,
                                           oToi.extract_tokens(iToken, iToken),
                                           sSolution)
                oViolation.set_action(dAction)
                self.add_violation(oViolation)
Пример #5
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