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

Add support for .jshintignore files #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions JSHint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import os, sys, subprocess, codecs, re, webbrowser
from threading import Timer

sys.path.append(os.path.join(os.path.dirname(__file__), "python-path-specification"))

import pathspec

try:
import commands
except ImportError:
pass

PLUGIN_FOLDER = os.path.dirname(os.path.realpath(__file__))
RC_FILE = ".jshintrc"
IGNORE_FILE = ".jshintignore"
SETTINGS_FILE = "JSHint.sublime-settings"
KEYMAP_FILE = "Default ($PLATFORM).sublime-keymap"
OUTPUT_VALID = b"*** JSHint output ***"
Expand All @@ -23,6 +28,13 @@ def run(self, edit, show_regions=True, show_panel=True):
if self.file_unsupported():
return

# Do not lint if file is ignored by a .jshintignore, reset if linted before
if self.file_ignored():
JshintGlobalStore.reset()
JshintEventListeners.reset()
self.view.erase_regions("jshint_errors")
return

# Get the current text in the buffer and save it in a temporary file.
# This allows for scratch buffers and dirty files to be linted as well.
temp_file_path = self.save_buffer_to_temp_file()
Expand Down Expand Up @@ -74,6 +86,46 @@ def file_unsupported(self):
has_json_syntax = bool(re.search("JSON", view_settings.get("syntax"), re.I))
return has_json_syntax or (not has_js_or_html_extension and not has_js_or_html_syntax)

def file_ignored(self):
"""Check if current file is matched by a .jshintignore file.
Use pathspec library to evaluate the .jshintignore patterns.
"""
ignore_files = self.find_jshintignore_files()

if ignore_files:
jshintignore_file = ignore_files[-1]
with open(jshintignore_file) as jshintignore:
spec = pathspec.PathSpec.from_lines('gitignore', jshintignore)
matches = spec.match_tree(os.path.dirname(jshintignore_file))
for match in matches:
abs_match = os.path.join(os.path.dirname(jshintignore_file),match)
if abs_match == self.view.file_name():
if PluginUtils.get_pref('print_diagnostics'):
print(abs_match + " ignored in " + jshintignore_file)
return True

return False

def find_jshintignore_files(self):
"""Search for .jshintignore files along the root path of the current file.
Return a list of paths to .jshintignore files found, sorted from root to current parent directory.
"""
parent_dir = os.path.abspath(os.path.join(self.view.file_name(), os.pardir))
root_path = os.path.abspath(os.sep)

ignore_files = []

if os.path.isfile(os.path.join(root_path, IGNORE_FILE)):
ignorefiles.append(os.path.join(root_path, IGNORE_FILE))

for path in parent_dir.split(os.sep):
root_path = os.path.join(root_path, path)
ignore_file_path = os.path.join(root_path, IGNORE_FILE)
if os.path.isfile(ignore_file_path):
ignore_files.append(ignore_file_path)

return ignore_files

def save_buffer_to_temp_file(self):
buffer_text = self.view.substr(sublime.Region(0, self.view.size()))
temp_file_name = ".__temp__"
Expand Down
34 changes: 34 additions & 0 deletions python-path-specification/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Compiled python files.
*.py[cod]

# C extensions.
*.so

# Packages.
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
lib
lib64
__pycache__
/MANIFEST
.tox

# Installer logs.
pip-log.txt

# Temporary files.
*~
*.swp

# Hidden files.
.*
!.gitignore
61 changes: 61 additions & 0 deletions python-path-specification/CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

Change History
==============


0.3.4 (2015-08-24)
------------------

- Issue #7: Fixed non-recursive links.
- Issue #8: Fixed edge cases in gitignore patterns.
- Issue #9: Fixed minor usage documentation.
- Fixed recursion detection.
- Fixed trivial incompatibility with Python 3.2.


0.3.3 (2014-11-21)
------------------

- Improved documentation.


0.3.2 (2014-11-08)
------------------

- Improved documentation.
- Issue #6: Fixed matching Windows paths.
- API change: `spec.match_tree` and `spec.match_files` now return iterators instead of sets


0.3.1 (2014-09-17)
------------------

- Updated README.


0.3.0 (2014-09-17)
------------------

- Added registered patterns.
- Issue #3: Fixed trailing slash in gitignore patterns.
- Issue #4: Fixed test for trailing slash in gitignore patterns.


0.2.2 (2013-12-17)
------------------

- Fixed setup.py


0.2.1 (2013-12-17)
------------------

- Added tests.
- Fixed comment gitignore patterns.
- Fixed relative path gitignore patterns.


0.2.0 (2013-12-07)
------------------

- Initial release.
Loading