Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the context object to inject the stdin-filename into the command #332

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,30 @@ class ESLint(NodeLinter):
"""Provides an interface to the eslint executable."""

missing_config_regex = re.compile(
r'^(.*?)\r?\n\w*(ESLint couldn\'t find a configuration file.)',
r"^(.*?)\r?\n\w*(ESLint couldn't find a configuration file.)",
re.DOTALL
)
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