Failed Conditions
Pull Request — master (#1438)
by Sudheesh
01:40
created

bears.js.ESLintBear   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %
Metric Value
dl 0
loc 53
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 2
B _process_issues() 0 31 6
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
The loop variable lintLines might not be defined here.
Loading history...
57
                file=filename,
58
                diffs={filename: diff},
59
                severity=self.severity_map[str(lintLines['severity'])],
0 ignored issues
show
Bug introduced by
The loop variable lintLines might not be defined here.
Loading history...
60
                line=lintLines['line'])
0 ignored issues
show
Bug introduced by
The loop variable lintLines might not be defined here.
Loading history...
61
62
        except:
63
            output = {}
64