From a4264be74e8035ee491a52ef731a2864c5eec3e7 Mon Sep 17 00:00:00 2001 From: dpc00 Date: Tue, 29 Sep 2026 11:34:44 -0600 Subject: [PATCH] Only strip the trailing source location from messages The filter removed every ' on ' and ' in ' in the message, turning 'Cannot use isset() on the result of an expression' into 'Cannot use isset()the result of an expression' and "'break' not in the 'loop' or 'switch' context" into "'break' notthe 'loop' or 'switch' context". Only the trailing ' in Standard input code' / ' in -' that PHP appends should go. Add tests for messages that contain ' on ' and ' in '. Fixes #55 Co-Authored-By: Claude Sonnet 5.5 --- linter.py | 12 +++++++----- tests/test_regex.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) 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)', + }, + )