forked from palantir/python-language-server
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmccabe_lint.py
More file actions
40 lines (32 loc) · 1.27 KB
/
Copy pathmccabe_lint.py
File metadata and controls
40 lines (32 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright 2017 Palantir Technologies, Inc.
import ast
import logging
import mccabe
from pyls import hookimpl, lsp
log = logging.getLogger(__name__)
THRESHOLD = 'threshold'
DEFAULT_THRESHOLD = 15
@hookimpl
def pyls_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", 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 None
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 - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})
return diags