diff --git a/linter.py b/linter.py index 1393739..c6f8556 100644 --- a/linter.py +++ b/linter.py @@ -11,21 +11,23 @@ """This module exports the PHP plugin class.""" import logging +import re from SublimeLinter.lint import Linter, util logger = logging.getLogger('SublimeLinter.plugin.php') +# PHP appends where it read the code from, e.g. `... in Standard input code` +# or `... in -`. Only that trailing part is noise; ` in ` and ` on ` inside the +# message itself (`Cannot use isset() on the result of an expression`) are not. +TRAILING_SOURCE_RE = re.compile(r'\s+in\s+(?:Standard input code|-)\s*$') + 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() + message = TRAILING_SOURCE_RE.sub('', message).strip() return message diff --git a/tests/test_regex.py b/tests/test_regex.py index 5542a50..bda56d1 100644 --- a/tests/test_regex.py +++ b/tests/test_regex.py @@ -69,3 +69,36 @@ def test_issue_29(self): 'near': None, }, ) + + def test_issue_55_message_filter_only_strips_trailing_source(self): + # " on " and " in " inside the message must survive + self.assertMatch( + 'Fatal error: Cannot use isset() on the result of an expression ' + '(you can use "null !== expression" instead) in Standard input code on line 2', + { + 'error': 'Fatal', + 'line': 1, + 'message': 'Cannot use isset() on the result of an expression ' + '(you can use "null !== expression" instead)', + }, + ) + + self.assertMatch( + "Fatal error: 'break' not in the 'loop' or 'switch' context " + 'in Standard input code on line 3', + { + 'error': 'Fatal', + 'line': 2, + 'message': "'break' not in the 'loop' or 'switch' context", + }, + ) + + self.assertMatch( + 'Fatal error: Cannot redeclare f() (previously declared in Standard input code:2) ' + 'in Standard input code on line 3', + { + 'error': 'Fatal', + 'line': 2, + 'message': 'Cannot redeclare f() (previously declared in Standard input code:2)', + }, + )