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 config for lookup #271

Closed
wants to merge 4 commits into from
Closed
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
80 changes: 79 additions & 1 deletion linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import json
import logging
import re
from SublimeLinter.lint import NodeLinter
import os
import shutil
from SublimeLinter.lint import NodeLinter, util


logger = logging.getLogger('SublimeLinter.plugin.eslint')
Expand All @@ -34,6 +36,82 @@ class ESLint(NodeLinter):
'selector': 'source.js - meta.attribute-with-value'
}

def context_sensitive_executable_path(self, cmd):
"""
Use local eslint only if both local eslint executable and
local config is found. Fallback to global eslint if exists.
"""
start_dir = (
# absolute path of current file
os.path.abspath(os.path.dirname(self.view.file_name()))
# or current working directory
or self.get_working_dir(self.settings)
)
local_cmd = self.find_local_linter(start_dir)
if local_cmd:
return True, local_cmd

if self.get_view_settings().get('disable_if_not_dependency', False):
return True, None

global_cmd = util.which(cmd[0])
if global_cmd:
return True, global_cmd
else:
logger.warning('{} cannot locate \'{}\'\n'
'Please refer to the readme of this plugin and our troubleshooting guide: '
'http://www.sublimelinter.com/en/stable/troubleshooting.html'.format(self.name, cmd[0]))
return True, None

def paths_upwards(self, path):
while True:
yield path
next_path = os.path.dirname(path)
if next_path == path:
return
path = next_path

def has_local_config(self, path):
eslint_files = [
".eslintrc.js",
".eslintrc.yaml",
".eslintrc.yml",
".eslintrc.json",
".eslintrc",
]
# Check for existence of config files in current directory
for config_file in eslint_files:
if os.path.exists(os.path.join(path, config_file)):
return True
return False

def find_local_linter(self, start_dir, npm_name='eslint'):
"""
Find local installation of eslint executable
and return it only if a local config is found.
"""
config_found = False
# Check if we have eslint config or executable in package.json
if self.manifest_path:
pkg = self.get_manifest()
pkg_dir = os.path.dirname(self.manifest_path)
cmd = pkg['bin'][npm_name] if 'bin' in pkg and npm_name in pkg['bin'] else None
if cmd:
executable = os.path.normpath(os.path.join(pkg_dir, cmd))
if 'eslintConfig' in pkg:
config_found = True

for path in self.paths_upwards(start_dir):
node_modules_bin = os.path.join(path, 'node_modules', '.bin')
executable = shutil.which(npm_name, path=node_modules_bin)
if not config_found:
# Look for eslint config in current directory
config_found = self.has_local_config(path)
if executable:
return executable if config_found else None
else:
return None

def on_stderr(self, stderr):
# Demote 'annoying' config is missing error to a warning.
if self.missing_config_regex.match(stderr):
Expand Down