Skip to content

Commit

Permalink
Use the context object to inject the stdin-filename into the command
Browse files Browse the repository at this point in the history
If we use SublimeLinters standard way of injecting variables, we don't
need the watch out for `$` characters because SublineLinter will do.
This is what we did before #326 as well.

This is a follow-up of #331.
  • Loading branch information
kaste committed Nov 10, 2023
1 parent 9771add commit c8ed61c
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,24 @@ class ESLint(NodeLinter):
line_col_base = (1, 1)
defaults = {
'selector': OPTIMISTIC_SELECTOR,
'--stdin-filename': '${file:fallback_filename}',
'prefer_eslint_d': True,
}

def cmd(self):
cmd = ['eslint', '--format=json', '--stdin']
stdin_filename = self.get_stdin_filename()
if stdin_filename:
cmd.append('--stdin-filename=' + stdin_filename.replace('$', '\\$'))
return cmd
if not self.context.get('file'):
fallback_filename = self.compute_fallback_filename()
if fallback_filename:
self.context['fallback_filename'] = fallback_filename
return ['eslint', '--format=json', '--stdin']

def compute_fallback_filename(self):
# type: () -> Optional[str]
view_selectors = set(self.view.scope_name(0).split(' '))
for selector in BUFFER_FILE_EXTENSIONS.keys():
if selector in view_selectors:
return '.'.join([BUFFER_FILE_STEM, BUFFER_FILE_EXTENSIONS[selector]])
return None

def run(self, cmd, code):
# Workaround eslint bug https://github.com/eslint/eslint/issues/9515
Expand Down Expand Up @@ -138,17 +147,6 @@ def ensure_plugin_installed(self) -> bool:
self.notify_unassign() # Abort linting without popping error dialog
raise PermanentError()

def get_stdin_filename(self):
# type: () -> Optional[str]
filename = self.view.file_name()
if filename is None:
view_selectors = set(self.view.scope_name(0).split(' '))
for selector in BUFFER_FILE_EXTENSIONS.keys():
if selector in view_selectors:
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

0 comments on commit c8ed61c

Please sign in to comment.