-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinter.py
103 lines (83 loc) · 3.15 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Kenneth Skovhus
# Copyright (c) 2015 Kenneth Skovhus
#
# License: MIT
#
"""This module exports the sass-lint plugin linter class."""
import re
from SublimeLinter.lint import NodeLinter
class Sass(NodeLinter):
"""Provides an interface to the sass-lint executable."""
cmd = ('sass-lint', '--verbose', '--no-exit', '--format', 'stylish')
regex = (
r'^\s+(?P<line>\d+):(?P<col>\d+)'
r'\s+((?P<error>error)|(?P<warning>warning))'
r'\s+(?P<message>.+)'
)
regex_error_config = re.compile(
r'^YAMLException: (?P<msg>.*)'
)
regex_error_sass_parse = re.compile(
r'^Error: Parsing error at .*:(?P<msg>.*#(?P<line>\d*))'
)
regex_error = re.compile(
r'^(\w*)Error: (?P<msg>.*)'
)
tempfile_suffix = {
'scss': 'scss',
'sass': 'sass'
}
defaults = {
'selector': 'source.sass, source.scss'
}
def find_errors(self, output):
"""
Parse errors from linter's output.
We override this method to improve sass-lint error handling.
"""
for line in output.splitlines():
match = self.regex_error_sass_parse.match(line)
if match:
items = match.groupdict()
try:
line = int(items['line']) - 1
except ValueError:
line = 0
return [(match, line, None, "Error", None, items['msg'], None)]
match = self.regex_error_config.match(line)
if match:
msg = 'Config file invalid: {}'.format(match.group('msg'))
return [(match, 0, None, "Error", "", msg, None)]
match = self.regex_error.match(line)
if match:
msg = 'sass-lint failed: {}'.format(match.group('msg'))
return [(match, 0, None, "Error", "", msg, None)]
return super().find_errors(output)
def tmpfile(self, cmd, code, suffix=None):
"""Run an external executable using a temp file to pass code and return its output."""
# check if <style> tag exists
match_style_open_tag = re.search(r'^\s*<style[^>]*>', code)
if match_style_open_tag:
open_tag = match_style_open_tag.group(0)
# determine `suffix` by `lang` attribute
match_lang = re.search(r'lang="([^"]+)"', open_tag)
if match_lang:
suffix = '.' + match_lang.group(1)
# remove style tags
code = re.sub(r'^\s*<style[^>]*>', '', code)
code = re.sub(r'</style>\s*$', '', code)
return super(Sass, self).tmpfile(cmd, code, suffix)
def split_match(self, match):
"""
Extract and return values from match.
We override this method to make the message more compact.
"""
match, line, col, error, warning, message, near = super().split_match(match)
if message:
words = message.split()
message = '{}. ({})'.format(' '.join(words[:-1]), words[-1])
return match, line, col, error, warning, message, near