Skip to content

Commit

Permalink
enable selector-specific stdin filename for buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
dndrsn committed Dec 29, 2022
1 parent 4740af8 commit 380be4c
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

MYPY = False
if MYPY:
from typing import List, Union
from typing import List, Union # noqa: F401


logger = logging.getLogger('SublimeLinter.plugin.eslint')
Expand All @@ -37,9 +37,23 @@
'@angular-eslint/eslint-plugin': 'text.html',
'@typescript-eslint/parser': 'source.ts, source.tsx',
'tsdx': 'source.ts, source.tsx',
'eslint-plugin-yml': 'source.yaml',
'eslint-plugin-yaml': 'source.yaml',
}
OPTIMISTIC_SELECTOR = ', '.join({STANDARD_SELECTOR} | set(PLUGINS.values()))

BUFFER_FILE_STEM = '__buffer__'
BUFFER_FILE_EXTENSIONS = {
'source.js': 'js',
'source.jsx': 'jsx',
'text.html': 'html',
'text.html.vue': 'vue',
'source.ts': 'ts',
'source.tsx': 'tsx',
'source.json': 'json',
'source.yaml': 'yaml',
}


class ESLint(NodeLinter):
"""Provides an interface to the eslint executable."""
Expand All @@ -53,7 +67,6 @@ class ESLint(NodeLinter):
line_col_base = (1, 1)
defaults = {
'selector': OPTIMISTIC_SELECTOR,
'--stdin-filename': '${file}',
'prefer_eslint_d': True,
}

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

self.ensure_plugin_installed()

stdin_filename = self.get_stdin_filename()
if stdin_filename:
cmd.append('--stdin-filename=' + stdin_filename)

return super().run(cmd, code)

def ensure_plugin_installed(self) -> bool:
Expand Down Expand Up @@ -120,6 +138,15 @@ def ensure_plugin_installed(self) -> bool:
self.notify_unassign() # Abort linting without popping error dialog
raise PermanentError()

def get_stdin_filename(self) -> str:
filename = self.view.file_name()
if filename is None:
for selector in BUFFER_FILE_EXTENSIONS.keys():
if self.view.match_selector(0, selector):
filename = '.'.join([BUFFER_FILE_STEM, BUFFER_FILE_EXTENSIONS[selector]])
break
return filename

def find_local_executable(self, start_dir, npm_name):
# type: (str, str) -> Union[None, str, List[str]]
"""Automatically switch to `eslint_d` if available (and wanted)."""
Expand Down Expand Up @@ -177,6 +204,8 @@ def find_errors(self, output):
filename = entry.get('filePath', None)
if filename == '<text>':
filename = 'stdin'
elif filename.split(os.path.sep)[-1].split('.')[0] == BUFFER_FILE_STEM:
filename = 'stdin'

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

0 comments on commit 380be4c

Please sign in to comment.