Skip to content

Commit

Permalink
Merge pull request #318 from SublimeLinter/auto-eslint_d
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste authored Aug 5, 2022
2 parents 2bbb711 + cc320fa commit 4740af8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ For [Svelte](https://svelte.dev/) `.svelte` files, using [`eslint-plugin-svelte3

To find the `selector` value for a particular file type, place the cursor at the start of the file and use the command **Tools** ➡️ **Developer** ➡️ **Show Scope Name**.

## Using eslint_d

This plugin will automatically prefer a `eslint_d` installation if present.
You can change this behavior by setting `prefer_eslint_d` to `false` either
globally or per project. E.g. for the global setting:

```json
"linters": {
"eslint": {
"prefer_eslint_d": false
}
}
```

## Settings

- SublimeLinter settings: http://sublimelinter.com/en/latest/settings.html
Expand Down
24 changes: 23 additions & 1 deletion linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
import logging
import os
import re
import shutil
from SublimeLinter.lint import LintMatch, NodeLinter, PermanentError

from SublimeLinter.lint.base_linter.node_linter import read_json_file


MYPY = False
if MYPY:
from typing import List, Union


logger = logging.getLogger('SublimeLinter.plugin.eslint')


Expand Down Expand Up @@ -47,7 +53,8 @@ class ESLint(NodeLinter):
line_col_base = (1, 1)
defaults = {
'selector': OPTIMISTIC_SELECTOR,
'--stdin-filename': '${file}'
'--stdin-filename': '${file}',
'prefer_eslint_d': True,
}

def run(self, cmd, code):
Expand Down Expand Up @@ -113,6 +120,21 @@ def ensure_plugin_installed(self) -> bool:
self.notify_unassign() # Abort linting without popping error dialog
raise PermanentError()

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)."""
executable = super().find_local_executable(start_dir, npm_name)
if self.settings.get('prefer_eslint_d') and isinstance(executable, str):
basedir = os.path.dirname(executable)
daemonized_name = '{}_d'.format(npm_name)
return (
(shutil.which(daemonized_name, path=basedir) if basedir else None) # local
or self.which(daemonized_name) # global
or executable # keep it
)

return executable

def on_stderr(self, stderr):
# Demote 'annoying' config is missing error to a warning.
if self.missing_config_regex.match(stderr):
Expand Down
3 changes: 2 additions & 1 deletion messages.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"install": "messages/install.txt",
"4.2.0": "messages/4.2.0.txt"
"4.2.0": "messages/4.2.0.txt",
"4.3.0": "messages/4.3.0.txt"
}
16 changes: 16 additions & 0 deletions messages/4.3.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SublimeLinter-eslint 4.3.0
--------------------------

SublimeLinter will now prefer a possible `eslint_d` installation (globally or
locally) over the slower `eslint` binary automatically.

You can control this behavior with the `prefer_eslint_d` setting. For example,
to turn this off in the global SublimeLinter settings:

```json
"linters": {
"eslint": {
"prefer_eslint_d": false
}
}
```

0 comments on commit 4740af8

Please sign in to comment.