Skip to content

Commit

Permalink
Merge pull request #50 from gerardroche/master
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste authored Jul 13, 2022
2 parents 804a20f + 15b00ec commit 1f0f5b5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 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/
29 changes: 20 additions & 9 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,20 +37,18 @@ 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)

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

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

def cmd(self):
"""Read cmd from inline settings."""
Expand Down
77 changes: 77 additions & 0 deletions tests/test_regex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# flake8: noqa
import unittest
import re
import importlib

import sublime

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


class TestRegex(unittest.TestCase):

def splitMatch(self, string):
linter = Linter(sublime.View(0), {})
match = re.match(regex, string)

if match is None:
return None

match = linter.split_match(match)

return {
'error': match['error'],
'line': match['line'],
'message': match['message'],
'near': match['near'],
}


def assertMatch(self, string, expected):
self.assertEqual(self.splitMatch(string), expected)

def assertNoMatch(self, string):
self.assertIsNone(self.splitMatch(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) on line 14', {
'error': 'Parse',
'line': 13,
'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': 22,
'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': 15,
'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': 15,
'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 1f0f5b5

Please sign in to comment.