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 'fix' in lintLines: |
||
41 | fixStructure = lintLines['fix'] |
||
42 | startingValue = fixStructure['range'][0] |
||
43 | endingValue = fixStructure['range'][1] |
||
44 | replacementText = fixStructure['text'] |
||
45 | newoutput = newoutput[ |
||
46 | :int(startingValue)+1] + replacementText +\ |
||
47 | newoutput[int(endingValue):] |
||
48 | if lintLines['severity'] == '0': |
||
49 | continue |
||
50 | |||
51 | diff = Diff.from_string_arrays( |
||
52 | lines.splitlines(True), newoutput.splitlines(True)) |
||
53 | |||
54 | yield Result.from_values( |
||
55 | origin=self, |
||
56 | message=lintLines['message'], |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
57 | file=filename, |
||
58 | diffs={filename: diff}, |
||
59 | severity=self.severity_map[str(lintLines['severity'])], |
||
0 ignored issues
–
show
|
|||
60 | line=lintLines['line']) |
||
0 ignored issues
–
show
|
|||
61 | |||
62 | except: |
||
63 | output = {} |
||
64 |