Skip to content

Commit 128563c

Browse files
authored
Merge pull request #326 from dndrsn/selector-specific-buffer-filename
2 parents ea67491 + ad8dbe1 commit 128563c

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

linter.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
MYPY = False
2424
if MYPY:
25-
from typing import List, Union
25+
from typing import List, Optional, Union
2626

2727

2828
logger = logging.getLogger('SublimeLinter.plugin.eslint')
@@ -37,26 +37,44 @@
3737
'@angular-eslint/eslint-plugin': 'text.html',
3838
'@typescript-eslint/parser': 'source.ts, source.tsx',
3939
'tsdx': 'source.ts, source.tsx',
40+
'eslint-plugin-yml': 'source.yaml',
41+
'eslint-plugin-yaml': 'source.yaml',
4042
}
4143
OPTIMISTIC_SELECTOR = ', '.join({STANDARD_SELECTOR} | set(PLUGINS.values()))
4244

45+
BUFFER_FILE_STEM = '__buffer__'
46+
BUFFER_FILE_EXTENSIONS = {
47+
'source.js': 'js',
48+
'source.jsx': 'jsx',
49+
'text.html': 'html',
50+
'text.html.vue': 'vue',
51+
'source.ts': 'ts',
52+
'source.tsx': 'tsx',
53+
'source.json': 'json',
54+
'source.yaml': 'yaml',
55+
}
56+
4357

4458
class ESLint(NodeLinter):
4559
"""Provides an interface to the eslint executable."""
4660

47-
cmd = 'eslint --format json --stdin'
48-
4961
missing_config_regex = re.compile(
5062
r'^(.*?)\r?\n\w*(ESLint couldn\'t find a configuration file.)',
5163
re.DOTALL
5264
)
5365
line_col_base = (1, 1)
5466
defaults = {
5567
'selector': OPTIMISTIC_SELECTOR,
56-
'--stdin-filename': '${file}',
5768
'prefer_eslint_d': True,
5869
}
5970

71+
def cmd(self):
72+
cmd = ['eslint', '--format=json', '--stdin']
73+
stdin_filename = self.get_stdin_filename()
74+
if stdin_filename:
75+
cmd.append('--stdin-filename=' + stdin_filename)
76+
return cmd
77+
6078
def run(self, cmd, code):
6179
# Workaround eslint bug https://github.com/eslint/eslint/issues/9515
6280
# Fixed in eslint 4.10.0
@@ -120,6 +138,17 @@ def ensure_plugin_installed(self) -> bool:
120138
self.notify_unassign() # Abort linting without popping error dialog
121139
raise PermanentError()
122140

141+
def get_stdin_filename(self):
142+
# type: () -> Optional[str]
143+
filename = self.view.file_name()
144+
if filename is None:
145+
view_selectors = set(self.view.scope_name(0).split(' '))
146+
for selector in BUFFER_FILE_EXTENSIONS.keys():
147+
if selector in view_selectors:
148+
filename = '.'.join([BUFFER_FILE_STEM, BUFFER_FILE_EXTENSIONS[selector]])
149+
break
150+
return filename
151+
123152
def find_local_executable(self, start_dir, npm_name):
124153
# type: (str, str) -> Union[None, str, List[str]]
125154
"""Automatically switch to `eslint_d` if available (and wanted)."""
@@ -177,6 +206,8 @@ def find_errors(self, output):
177206
filename = entry.get('filePath', None)
178207
if filename == '<text>':
179208
filename = 'stdin'
209+
elif filename and os.path.basename(filename).startswith(BUFFER_FILE_STEM + '.'):
210+
filename = 'stdin'
180211

181212
for match in entry['messages']:
182213
if match['message'].startswith('File ignored'):

0 commit comments

Comments
 (0)