Skip to content

Commit

Permalink
Fixes for latest SublimeLinter
Browse files Browse the repository at this point in the history
Includes some regex fixes for latest PHP versions and some improved
error message filtering.
  • Loading branch information
gerardroche committed Jul 21, 2019
1 parent 804a20f commit b1b02d1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/tests/
28 changes: 21 additions & 7 deletions linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@
from SublimeLinter.lint import Linter, util


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


def _filter_message(message):
if not message:
message = 'parse error'
else:
message = message.replace('Standard input code', '')
message = message.replace(' on ', '')
message = message.replace(' in -', '')
message = message.replace(' in ', '')
message = message.strip()

return message


class PHP(Linter):
Expand All @@ -24,18 +37,19 @@ class PHP(Linter):
'selector': 'source.php, text.html.basic'
}
regex = (
r'^(?:Parse|Fatal) (?P<error>error):(\s*(?P<type>parse|syntax) error,?)?\s*'
r'(?P<message>(?:unexpected \'(?P<near>[^\']+)\')?.*) (?:in - )?on line (?P<line>\d+)'
r'^(?P<error>Parse|Fatal) error:\s*'
r'(?P<message>((?:parse|syntax) error,?)?\s*(?:unexpected \'(?P<near>[^\']+)\')?.*) '
r'(?:in - )?on line (?P<line>\d+)'
)
error_stream = util.STREAM_STDOUT

def split_match(self, match):
"""Return the components of the error."""
match, line, col, error, warning, message, near = super().split_match(match)
result = super().split_match(match)

match, line, col, error, warning, message, near = result

# message might be empty, we have to supply a value
if match and match.group('type') == 'parse' and not message:
message = 'parse error'
message = _filter_message(message)

return match, line, col, error, warning, message, near

Expand Down
67 changes: 67 additions & 0 deletions tests/test_regex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# flake8: noqa
import unittest
import re
import importlib

# Damn you dash separated module names!!!
LinterModule = importlib.import_module('SublimeLinter-php.linter')
Linter = LinterModule.PHP
_filter_message = LinterModule._filter_message
regex = Linter.regex


class TestRegex(unittest.TestCase):

def assertMatch(self, string, expected):
match = re.match(regex, string)
self.assertIsNotNone(match)

# Hack to test message filtering.
actual = match.groupdict()
if 'message' in actual:
actual['message'] = _filter_message(actual['message'])

self.assertEqual(actual, expected)

def assertNoMatch(self, string):
self.assertIsNone(re.match(regex, string))

def test_no_errors(self):
self.assertNoMatch('No syntax errors detected in - ')
self.assertNoMatch('No syntax errors detected in - file.php')
self.assertNoMatch('No syntax errors detected in - /path/to/file.php')
self.assertNoMatch('No syntax errors detected in Standard input code')

def test_errors(self):
self.assertMatch(
'Parse error: syntax error, unexpected \'$this\' (T_VARIABLE) in - on line 14', {
'error': 'Parse',
'line': '14',
'message': 'syntax error, unexpected \'$this\' (T_VARIABLE)',
'near': '$this'
})

self.assertMatch(
'Parse error: syntax error, unexpected end of file in - on line 23', {
'error': 'Parse',
'line': '23',
'message': 'syntax error, unexpected end of file',
'near': None
})

def test_issue_29(self):
self.assertMatch(
'Parse error: syntax error, unexpected \'endwhile\' (T_ENDWHILE), expecting end of file in Standard input code on line 16', {
'error': 'Parse',
'line': '16',
'message': 'syntax error, unexpected \'endwhile\' (T_ENDWHILE), expecting end of file',
'near': 'endwhile'
})

self.assertMatch(
'Parse error: parse error in - on line 16', {
'error': 'Parse',
'line': '16',
'message': 'parse error',
'near': None
})
9 changes: 9 additions & 0 deletions unittesting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests_dir" : "tests",
"pattern" : "test*.py",
"async": false,
"deferred": true,
"verbosity": 2,
"capture_console": true,
"output": null
}

0 comments on commit b1b02d1

Please sign in to comment.