Skip to content

Commit 46f9825

Browse files
committed
Add source diagnostic suppressions
1 parent 36c9677 commit 46f9825

33 files changed

Lines changed: 898 additions & 94 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Suppress code-qualified diagnostics with source comments
56
- Honor router request context parameters in route diagnostics
67
- Report Symfony branches outside the live supported range
78
- Ignore Twig macro declarations that share a name with custom functions

docs/features/headless-diagnostics.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,50 @@ with:
180180
$ symfony-lsp check --list-codes
181181
$ symfony-lsp check --format=json --list-codes
182182
183+
Suppressing Intentional Diagnostics
184+
-----------------------------------
185+
186+
When source code intentionally triggers a diagnostic, add a code-qualified
187+
suppression in a native PHP, Twig, YAML or XML comment. The editor and
188+
``symfony-lsp check`` apply the same suppressions:
189+
190+
.. code-block:: php
191+
192+
/* @symfony-lsp-ignore template.not_found (intentional missing template) */
193+
$this->render('test/does_not_exist.html.twig');
194+
195+
.. code-block:: twig
196+
197+
{# @symfony-lsp-ignore template.not_found (intentional missing template) #}
198+
{{ include('test/does_not_exist.html.twig') }}
199+
200+
.. code-block:: yaml
201+
202+
# @symfony-lsp-ignore config.unknown_key (compatibility fixture)
203+
framework:
204+
unsupported_option: true
205+
206+
.. code-block:: xml
207+
208+
<!-- @symfony-lsp-ignore config.unknown_key (compatibility fixture) -->
209+
<framework:unsupported-option>true</framework:unsupported-option>
210+
211+
A standalone comment targets diagnostics whose ranges start on the next
212+
physical line. A comment that shares a line with source code targets that line.
213+
Blank lines aren't skipped.
214+
215+
The directive accepts a comma-separated list of exact diagnostic codes. Each
216+
listed code suppresses one occurrence, so repeat a code to suppress several
217+
matching diagnostics on the same line. An optional parenthesized reason can
218+
follow the codes. Bare directives, malformed directives and unknown codes
219+
produce a ``suppression.invalid`` warning instead of suppressing diagnostics.
220+
221+
Only native comments are recognized. Directive-shaped text in strings, Twig
222+
verbatim content, YAML block scalars and XML CDATA sections has no effect.
223+
Suppressed diagnostics are omitted from editor publications and checker
224+
reports, and they aren't written to new baselines. A matching entry in an
225+
existing baseline becomes stale.
226+
183227
Using a Baseline
184228
----------------
185229

resources/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Lsp\Feature\DiagnosticCollector;
2020
use Symfony\Lsp\Feature\DiagnosticProviderInterface;
2121
use Symfony\Lsp\Feature\DiagnosticProviderRegistry;
22+
use Symfony\Lsp\Feature\DiagnosticSuppressor;
2223
use Symfony\Lsp\Feature\Doctrine\DoctrineRelationshipCodeLensProvider;
2324
use Symfony\Lsp\Feature\DocumentLinkProviderInterface;
2425
use Symfony\Lsp\Feature\DocumentLinkProviderRegistry;
@@ -135,6 +136,7 @@
135136

136137
$services->load('Symfony\\Lsp\\Feature\\', '../src/Feature/*Registry.php');
137138
$services->set(DiagnosticCollector::class);
139+
$services->set(DiagnosticSuppressor::class);
138140
$featureGroups = [
139141
'Route' => [],
140142
'DependencyInjection' => [],

src/Check/BaselineCodec.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Lsp\Check;
44

55
use Symfony\Component\Filesystem\Path;
6+
use Symfony\Lsp\Feature\DiagnosticCodeRegistry;
67
use Symfony\Lsp\Project\InvalidConfigurationException;
78

89
final class BaselineCodec

src/Check/CheckCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Symfony\Lsp\Check;
44

5+
use Symfony\Lsp\Feature\DiagnosticCodeRegistry;
56
use Symfony\Lsp\Project\InvalidConfigurationException;
67
use Symfony\Lsp\Server\SensitiveDataRedactor;
78

src/Check/CheckDiagnosticExecutor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Lsp\Document\Document;
77
use Symfony\Lsp\Document\DocumentStore;
88
use Symfony\Lsp\Document\PositionConverter;
9+
use Symfony\Lsp\Feature\DiagnosticCodeRegistry;
910
use Symfony\Lsp\Feature\DiagnosticCollector;
1011
use Symfony\Lsp\Index\ApplicationSourceScanner;
1112
use Symfony\Lsp\Project\ProjectConfiguration;

src/Check/CheckOptionsParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Symfony\Lsp\Check;
44

5+
use Symfony\Lsp\Feature\DiagnosticCodeRegistry;
56
use Symfony\Lsp\Project\AnalysisSettings;
67
use Symfony\Lsp\Project\InvalidConfigurationException;
78

src/Check/SarifCheckReporter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Symfony\Lsp\Check;
44

5+
use Symfony\Lsp\Feature\DiagnosticCodeRegistry;
6+
57
final class SarifCheckReporter
68
{
79
private const SCHEMA = 'https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json';
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Symfony\Lsp\Check;
3+
namespace Symfony\Lsp\Feature;
44

55
final class DiagnosticCodeRegistry
66
{
@@ -28,6 +28,7 @@ final class DiagnosticCodeRegistry
2828
'security.unknown_provider',
2929
'service.not_found',
3030
'stimulus.unknown_controller',
31+
'suppression.invalid',
3132
'template.not_found',
3233
'translation.domain_not_found',
3334
'translation.not_found',

src/Feature/DiagnosticCollector.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function __construct(
1818
private readonly ProjectPathResolver $pathResolver,
1919
private readonly ProjectFileScopeRegistry $fileScope,
2020
private readonly UriToPathConverter $uriToPathConverter,
21+
private readonly DiagnosticSuppressor $suppressor,
2122
private readonly iterable $providers,
2223
) {
2324
}
@@ -54,7 +55,9 @@ public function collect(array $params, bool $includeExcluded = false): ?array
5455
array_push($diagnostics, ...$providedDiagnostics);
5556
}
5657

57-
return $matched ? $diagnostics : null;
58+
$diagnostics = $this->suppressor->suppress($document, $diagnostics);
59+
60+
return $matched || [] !== $diagnostics ? $diagnostics : null;
5861
}
5962

6063
/**
@@ -105,7 +108,9 @@ public function collectDetailed(array $params, bool $includeExcluded = false): ?
105108
array_push($diagnostics, ...$provided);
106109
}
107110

108-
return new DetailedDiagnosticCollection($matched, $diagnostics, $failures);
111+
$diagnostics = $this->suppressor->suppressCollected($document, $diagnostics);
112+
113+
return new DetailedDiagnosticCollection($matched || [] !== $diagnostics, $diagnostics, $failures);
109114
}
110115

111116
private function collectedDiagnostic(string $provider, mixed $diagnostic): CollectedDiagnostic

0 commit comments

Comments
 (0)