Skip to content

Commit 140e691

Browse files
committed
enable selector-specific stdin filename for buffers
1 parent 4740af8 commit 140e691

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

linter.py

Lines changed: 31 additions & 2 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, Union # noqa: F401
2626

2727

2828
logger = logging.getLogger('SublimeLinter.plugin.eslint')
@@ -37,9 +37,23 @@
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."""
@@ -53,7 +67,6 @@ class ESLint(NodeLinter):
5367
line_col_base = (1, 1)
5468
defaults = {
5569
'selector': OPTIMISTIC_SELECTOR,
56-
'--stdin-filename': '${file}',
5770
'prefer_eslint_d': True,
5871
}
5972

@@ -64,6 +77,11 @@ def run(self, cmd, code):
6477
code = ' '
6578

6679
self.ensure_plugin_installed()
80+
81+
stdin_filename = self.get_stdin_filename()
82+
if stdin_filename:
83+
cmd.append('--stdin-filename={}'.format(stdin_filename))
84+
6785
return super().run(cmd, code)
6886

6987
def ensure_plugin_installed(self) -> bool:
@@ -120,6 +138,15 @@ 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) -> str:
142+
filename = self.view.file_name()
143+
if filename is None:
144+
for selector in BUFFER_FILE_EXTENSIONS.keys():
145+
if self.view.match_selector(0, selector):
146+
filename = '.'.join([BUFFER_FILE_STEM, BUFFER_FILE_EXTENSIONS[selector]])
147+
break
148+
return filename
149+
123150
def find_local_executable(self, start_dir, npm_name):
124151
# type: (str, str) -> Union[None, str, List[str]]
125152
"""Automatically switch to `eslint_d` if available (and wanted)."""
@@ -177,6 +204,8 @@ def find_errors(self, output):
177204
filename = entry.get('filePath', None)
178205
if filename == '<text>':
179206
filename = 'stdin'
207+
elif filename.split('/')[-1].split('.')[0] == BUFFER_FILE_STEM:
208+
filename = 'stdin'
180209

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

0 commit comments

Comments
 (0)