Skip to content

Commit 2d54479

Browse files
authored
Merge pull request #332 from SublimeLinter/do-it-right
2 parents 9771add + b8b4d1d commit 2d54479

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

linter.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,30 @@ class ESLint(NodeLinter):
5959
"""Provides an interface to the eslint executable."""
6060

6161
missing_config_regex = re.compile(
62-
r'^(.*?)\r?\n\w*(ESLint couldn\'t find a configuration file.)',
62+
r"^(.*?)\r?\n\w*(ESLint couldn't find a configuration file.)",
6363
re.DOTALL
6464
)
6565
line_col_base = (1, 1)
6666
defaults = {
6767
'selector': OPTIMISTIC_SELECTOR,
68+
'--stdin-filename': '${file:fallback_filename}',
6869
'prefer_eslint_d': True,
6970
}
7071

7172
def cmd(self):
72-
cmd = ['eslint', '--format=json', '--stdin']
73-
stdin_filename = self.get_stdin_filename()
74-
if stdin_filename:
75-
cmd.append('--stdin-filename=' + stdin_filename.replace('$', '\\$'))
76-
return cmd
73+
if not self.context.get('file'):
74+
fallback_filename = self.compute_fallback_filename()
75+
if fallback_filename:
76+
self.context['fallback_filename'] = fallback_filename
77+
return ['eslint', '--format=json', '--stdin']
78+
79+
def compute_fallback_filename(self):
80+
# type: () -> Optional[str]
81+
view_selectors = set(self.view.scope_name(0).split(' '))
82+
for selector in BUFFER_FILE_EXTENSIONS.keys():
83+
if selector in view_selectors:
84+
return '.'.join([BUFFER_FILE_STEM, BUFFER_FILE_EXTENSIONS[selector]])
85+
return None
7786

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

141-
def get_stdin_filename(self):
142-
# type: () -> Optional[str]
143-
filename = self.view.file_name()
144-
if filename is None:
145-
view_selectors = set(self.view.scope_name(0).split(' '))
146-
for selector in BUFFER_FILE_EXTENSIONS.keys():
147-
if selector in view_selectors:
148-
filename = '.'.join([BUFFER_FILE_STEM, BUFFER_FILE_EXTENSIONS[selector]])
149-
break
150-
return filename
151-
152150
def find_local_executable(self, start_dir, npm_name):
153151
# type: (str, str) -> Union[None, str, List[str]]
154152
"""Automatically switch to `eslint_d` if available (and wanted)."""

0 commit comments

Comments
 (0)