-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlinter.py
85 lines (65 loc) · 2.46 KB
/
linter.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by roadhump
# Copyright (c) 2015 roadhump
#
# License: MIT
#
"""This module exports the Eslint_d plugin class."""
import sublime
import os
import re
from SublimeLinter.lint import NodeLinter
class Eslint_d(NodeLinter):
"""Provides an interface to eslint_d."""
syntax = ('javascript', 'html', 'javascriptnext', 'javascript (babel)', 'javascript (jsx)', 'jsx-real')
npm_name = 'eslint_d'
cmd = ('eslint_d', '--format', 'compact', '--stdin', '--stdin-filename', '@')
executable = None
version_args = '--version'
version_re = r'eslint_d v(?P<version>\d+\.\d+\.\d+)'
version_requirement = '>= 2.3.1'
regex = (
r'^.+?: line (?P<line>\d+), col (?P<col>\d+), '
r'(?:(?P<error>Error)|(?P<warning>Warning)) - '
r'(?P<message>.+)'
)
config_fail_regex = re.compile(r'^Cannot read config file: .*\r?\n')
crash_regex = re.compile(
r'^(.*?)\r?\n\w*Error: \1',
re.MULTILINE
)
line_col_base = (1, 1)
selectors = {
'html': 'source.js.embedded.html'
}
config_file = ('--config', '.eslintrc', '~')
def find_errors(self, output):
"""
Parse errors from linter's output.
We override this method to handle parsing eslint crashes.
"""
match = self.config_fail_regex.match(output)
if match:
return [(match, 0, None, "Error", "", match.group(0), None)]
match = self.crash_regex.match(output)
if match:
msg = "ESLint crashed: %s" % match.group(1)
return [(match, 0, None, "Error", "", msg, None)]
return super().find_errors(output)
def communicate(self, cmd, code=None):
"""Run an external executable using stdin to pass code and return its output."""
if '__RELATIVE_TO_FOLDER__' in cmd:
relfilename = self.filename
window = self.view.window()
# can't get active folder, it will work only if there is one folder in project
if int(sublime.version()) >= 3080 and len(window.folders()) < 2:
vars = window.extract_variables()
if 'folder' in vars:
relfilename = os.path.relpath(self.filename, vars['folder'])
cmd[cmd.index('__RELATIVE_TO_FOLDER__')] = relfilename
elif not code:
cmd.append(self.filename)
return super().communicate(cmd, code)