Ejemplo n.º 1
0
def pyls_lint(config, document):
    threshold = config.plugin_settings('mccabe').get(THRESHOLD, DEFAULT_THRESHOLD)

    try:
        tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
    except SyntaxError:
        # We'll let the other linters point this one out
        return

    visitor = mccabe.PathGraphingAstVisitor()
    visitor.preorder(tree, visitor)

    diags = []
    for graph in visitor.graphs.values():
        if graph.complexity() >= threshold:
            diags.append({
                'source': 'mccabe',
                'range': {
                    'start': {'line': graph.lineno, 'character': graph.column},
                    'end': {'line': graph.lineno, 'character': len(document.lines[graph.lineno])},
                },
                'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
                'severity': lsp.DiagnosticSeverity.Warning
            })

    return diags
Ejemplo n.º 2
0
def process(py_source, max_complexity):
    code = py_source.text()
    tree = compile(code, py_source, "exec", ast.PyCF_ONLY_AST)
    visitor = mccabe.PathGraphingAstVisitor()
    visitor.preorder(tree, visitor)
    for graph in visitor.graphs.values():
        if graph.complexity() > max_complexity:
            text = "{}:{}:{} {} {}"
            return text.format(py_source, graph.lineno, graph.column, graph.entity,
                               graph.complexity())
Ejemplo n.º 3
0
def process(py_source, max_complexity):
    res = list()
    code = py_source.text()
    tree = compile(code, py_source, "exec", ast.PyCF_ONLY_AST)
    visitor = mccabe.PathGraphingAstVisitor()
    visitor.preorder(tree, visitor)
    for graph in visitor.graphs.values():
        if graph.complexity() > max_complexity:
            res.append((py_source, graph))
    return res
def get_node_mccabe_complexity(ast_node: ast.AST) -> int:
    visitor = mccabe.PathGraphingAstVisitor()
    visitor.preorder(ast_node, visitor)
    return list(visitor.graphs.values())[0].complexity()