Skip to content

Commit

Permalink
Add support for .jshintignore files
Browse files Browse the repository at this point in the history
Search for `.jshintignore` files in parent directories and
evaluate ignore patterns by utilizing the python pathspec library

This resolves victorporof#59 and resolves victorporof#113
  • Loading branch information
jhinzmann committed Oct 18, 2015
1 parent 8c13bc6 commit c987b41
Show file tree
Hide file tree
Showing 20 changed files with 2,003 additions and 0 deletions.
51 changes: 51 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,45 @@ 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()

for ignorefile in reversed(ignore_files):
with open(ignorefile) as jshintignore:
spec = pathspec.PathSpec.from_lines('gitignore', jshintignore)
matches = spec.match_tree(os.path.dirname(ignorefile))
for match in matches:
abs_match = os.path.join(os.path.dirname(ignorefile),match)
if abs_match == self.view.file_name():
if PluginUtils.get_pref('print_diagnostics'):
print(abs_match + " ignored in " + ignorefile)
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

0 comments on commit c987b41

Please sign in to comment.