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

enable selector-specific stdin filename for buffers #326

Merged
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
39 changes: 35 additions & 4 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, Optional, Union


logger = logging.getLogger('SublimeLinter.plugin.eslint')
Expand All @@ -37,26 +37,44 @@
'@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."""

cmd = 'eslint --format json --stdin'

missing_config_regex = re.compile(
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}',
'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)
return cmd

def run(self, cmd, code):
# Workaround eslint bug https://github.com/eslint/eslint/issues/9515
# Fixed in eslint 4.10.0
Expand Down Expand Up @@ -120,6 +138,17 @@ 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
dndrsn marked this conversation as resolved.
Show resolved Hide resolved

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 +206,8 @@ def find_errors(self, output):
filename = entry.get('filePath', None)
if filename == '<text>':
filename = 'stdin'
elif filename and os.path.basename(filename).startswith(BUFFER_FILE_STEM + '.'):
filename = 'stdin'

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