|
1
|
|
|
import json |
|
2
|
|
|
|
|
3
|
|
|
from coalib.bearlib.abstractions.Lint import Lint |
|
4
|
|
|
from coalib.bears.LocalBear import LocalBear |
|
5
|
|
|
from coalib.results.Diff import Diff |
|
6
|
|
|
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY |
|
7
|
|
|
from coalib.misc.Shell import escape_path_argument |
|
8
|
|
|
from coalib.results.Result import Result |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class ESLintBear(LocalBear, Lint): |
|
12
|
|
|
executable = 'eslint' |
|
13
|
|
|
arguments = '--no-ignore --no-color -f=json' |
|
14
|
|
|
severity_map = { |
|
15
|
|
|
"2": RESULT_SEVERITY.MAJOR, |
|
16
|
|
|
"1": RESULT_SEVERITY.NORMAL, |
|
17
|
|
|
"0": RESULT_SEVERITY.INFO |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
def run(self, filename, file, eslint_config: str=""): |
|
21
|
|
|
''' |
|
22
|
|
|
Checks the code with eslint. This will run eslint over each of the files |
|
23
|
|
|
seperately. |
|
24
|
|
|
|
|
25
|
|
|
:param eslint_config: The location of the .eslintrc config file. |
|
26
|
|
|
''' |
|
27
|
|
|
if eslint_config: |
|
28
|
|
|
self.arguments += (" --config " |
|
29
|
|
|
+ escape_path_argument(eslint_config)) |
|
30
|
|
|
|
|
31
|
|
|
return self.lint(filename) |
|
32
|
|
|
|
|
33
|
|
|
def _process_issues(self, output, filename): |
|
34
|
|
|
try: |
|
35
|
|
|
output = json.loads("".join(output)) |
|
36
|
|
|
lines = [line for line in open(filename)] |
|
37
|
|
|
lines = "".join(lines) |
|
38
|
|
|
newoutput = lines |
|
39
|
|
|
for lintLines in output[0]['messages']: |
|
40
|
|
|
if lintLines['severity'] == '0': |
|
41
|
|
|
continue |
|
42
|
|
|
if 'fix' in lintLines: |
|
43
|
|
|
fixStructure = lintLines['fix'] |
|
44
|
|
|
startingValue, endingValue = fixStructure['range'] |
|
45
|
|
|
replacementText = fixStructure['text'] |
|
46
|
|
|
newoutput = newoutput[ |
|
47
|
|
|
:int(startingValue)+1] + replacementText +\ |
|
48
|
|
|
newoutput[int(endingValue):] |
|
49
|
|
|
|
|
50
|
|
|
diff = Diff.from_string_arrays( |
|
51
|
|
|
lines.splitlines(True), newoutput.splitlines(True)) |
|
52
|
|
|
|
|
53
|
|
|
yield Result.from_values( |
|
54
|
|
|
origin=self, |
|
55
|
|
|
message=lintLines['message'], |
|
|
|
|
|
|
56
|
|
|
file=filename, |
|
57
|
|
|
diffs={filename: diff}, |
|
58
|
|
|
severity=self.severity_map[str(lintLines['severity'])], |
|
|
|
|
|
|
59
|
|
|
line=lintLines['line']) |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
except: |
|
62
|
|
|
output = {} |
|
63
|
|
|
|