Skip to content

Commit d8eb4be

Browse files
frankdekkerCopilot
andauthored
Remove generics from typescript files before highlighting (#1158)
* Remove generics from typescript files before highlighting * Update src/Service/CodeHighlight/HighlightedFilePreprocessor.php Co-authored-by: Copilot <[email protected]> * Remove generics from typescript files before highlighting --------- Co-authored-by: Copilot <[email protected]>
1 parent 6fea4d1 commit d8eb4be

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DR\Review\Service\CodeHighlight;
5+
6+
use DR\Utils\Assert;
7+
8+
class HighlightedFilePreprocessor
9+
{
10+
public function process(string $language, string $content): string
11+
{
12+
if ($language === "typescript") {
13+
// currently 2025-08-21 highlightjs does not support typescript generics completely.
14+
$content = Assert::string(preg_replace('#<\w+\[]>#', '', $content));
15+
}
16+
return $content;
17+
}
18+
}

src/Service/CodeHighlight/HighlightedFileService.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class HighlightedFileService implements LoggerAwareInterface
2323
public function __construct(
2424
private readonly FilenameToLanguageTranslator $translator,
2525
private readonly HttpClientInterface $highlightjsClient,
26-
private readonly HighlightHtmlLineSplitter $splitter
26+
private readonly HighlightHtmlLineSplitter $splitter,
27+
private readonly HighlightedFilePreprocessor $preprocessor
2728
) {
2829
}
2930

@@ -37,10 +38,13 @@ public function fromDiffFile(DiffFile $diffFile): ?HighlightedFile
3738
return null;
3839
}
3940

40-
$lines = $diffFile->getLines();
41+
$content = implode("\n", $diffFile->getLines());
42+
43+
// preprocess certain contents that breaks the highlightjs formatter
44+
$content = $this->preprocessor->process($languageName, $content);
4145

4246
try {
43-
$response = $this->highlightjsClient->request('POST', '', ['query' => ['language' => $languageName], 'body' => implode("\n", $lines)]);
47+
$response = $this->highlightjsClient->request('POST', '', ['query' => ['language' => $languageName], 'body' => $content]);
4448
} catch (Throwable $exception) {
4549
$this->logger?->info('Failed to get code highlighting: ' . $exception->getMessage());
4650

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DR\Review\Tests\Unit\Service\CodeHighlight;
5+
6+
use DR\Review\Service\CodeHighlight\HighlightedFilePreprocessor;
7+
use DR\Review\Tests\AbstractTestCase;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
10+
#[CoversClass(HighlightedFilePreprocessor::class)]
11+
class HighlightedFilePreprocessorTest extends AbstractTestCase
12+
{
13+
private HighlightedFilePreprocessor $preprocessor;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->preprocessor = new HighlightedFilePreprocessor();
19+
}
20+
21+
public function testProcessShouldSkipOtherLanguages(): void
22+
{
23+
static::assertSame('test <test[]>test', $this->preprocessor->process('foo', 'test <test[]>test'));
24+
}
25+
26+
public function testProcessShouldRemoveTypescriptGenerics(): void
27+
{
28+
static::assertSame('test test', $this->preprocessor->process('typescript', 'test <test[]>test'));
29+
}
30+
}

tests/Unit/Service/CodeHighlight/HighlightedFileServiceTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DR\Review\Entity\Git\Diff\DiffFile;
99
use DR\Review\Entity\Git\Diff\DiffLine;
1010
use DR\Review\Service\CodeHighlight\FilenameToLanguageTranslator;
11+
use DR\Review\Service\CodeHighlight\HighlightedFilePreprocessor;
1112
use DR\Review\Service\CodeHighlight\HighlightedFileService;
1213
use DR\Review\Service\CodeHighlight\HighlightHtmlLineSplitter;
1314
use DR\Review\Tests\AbstractTestCase;
@@ -25,15 +26,17 @@ class HighlightedFileServiceTest extends AbstractTestCase
2526
private FilenameToLanguageTranslator&MockObject $translator;
2627
private HttpClientInterface&MockObject $httpClient;
2728
private HighlightHtmlLineSplitter&MockObject $splitter;
29+
private HighlightedFilePreprocessor&MockObject $preprocessor;
2830
private HighlightedFileService $service;
2931

3032
public function setUp(): void
3133
{
3234
parent::setUp();
33-
$this->translator = $this->createMock(FilenameToLanguageTranslator::class);
34-
$this->httpClient = $this->createMock(HttpClientInterface::class);
35-
$this->splitter = $this->createMock(HighlightHtmlLineSplitter::class);
36-
$this->service = new HighlightedFileService($this->translator, $this->httpClient, $this->splitter);
35+
$this->translator = $this->createMock(FilenameToLanguageTranslator::class);
36+
$this->httpClient = $this->createMock(HttpClientInterface::class);
37+
$this->splitter = $this->createMock(HighlightHtmlLineSplitter::class);
38+
$this->preprocessor = $this->createMock(HighlightedFilePreprocessor::class);
39+
$this->service = new HighlightedFileService($this->translator, $this->httpClient, $this->splitter, $this->preprocessor);
3740
}
3841

3942
/**
@@ -73,6 +76,7 @@ public function testGetHighlightedFile(): void
7376
->with('POST', '', ['query' => ['language' => 'xml'], 'body' => 'file-data'])
7477
->willReturn($response);
7578
$this->splitter->expects($this->once())->method('split')->with('highlighted-data')->willReturn(['highlighted', 'data']);
79+
$this->preprocessor->expects($this->once())->method('process')->with('xml', 'file-data')->willReturnArgument(1);
7680

7781
$actual = $this->service->fromDiffFile($file);
7882
static::assertNotNull($actual);
@@ -92,6 +96,7 @@ public function testGetHighlightedFileRequestFailure(): void
9296
$file->addBlock($block);
9397

9498
$this->translator->expects($this->once())->method('translate')->with('/path/to/file.xml')->willReturn('xml');
99+
$this->preprocessor->expects($this->once())->method('process')->with('xml', 'file-data')->willReturnArgument(1);
95100
$this->httpClient->expects($this->once())->method('request')->willThrowException(new RuntimeException('error'));
96101

97102
static::assertNull($this->service->fromDiffFile($file));

0 commit comments

Comments
 (0)