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 d75c8c3
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,22 @@
'@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 +66,6 @@ class ESLint(NodeLinter):
line_col_base = (1, 1)
defaults = {
'selector': OPTIMISTIC_SELECTOR,
'--stdin-filename': '${file}',
'prefer_eslint_d': True,
}

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

self.ensure_plugin_installed()

stdin_filename = self.get_stdin_filename();
if stdin_filename:
cmd.append('--stdin-filename={}'.format(stdin_filename))

return super().run(cmd, code)

def ensure_plugin_installed(self) -> bool:
Expand Down Expand Up @@ -120,6 +137,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 == 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 +203,8 @@ def find_errors(self, output):
filename = entry.get('filePath', None)
if filename == '<text>':
filename = 'stdin'
elif filename.split('/')[-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 d75c8c3

Please sign in to comment.