Skip to content

Commit 8828ff5

Browse files
committed
fix: highlightPhp misidentifies multiplication line as doc-comment
1 parent 2f1f4fd commit 8828ff5

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
2+
import { tmpdir } from 'node:os';
3+
import { join } from 'node:path';
4+
import styles from 'ansi-styles';
5+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
6+
import { AnsiStyle, DEFAULT_THEME } from './AnsiStyle';
7+
import { readSourceSnippet } from './SourceFileReader';
8+
9+
describe('readSourceSnippet highlightPhp', () => {
10+
const style = new AnsiStyle(DEFAULT_THEME);
11+
const dir = join(tmpdir(), 'source-reader-test');
12+
const file = join(dir, 'multiply.php');
13+
14+
beforeAll(() => {
15+
mkdirSync(dir, { recursive: true });
16+
writeFileSync(file, ['<?php', '$x = $a', ' * $b;', ''].join('\n'));
17+
});
18+
19+
afterAll(() => {
20+
rmSync(dir, { recursive: true, force: true });
21+
});
22+
23+
it('should not treat multiplication line as doc-comment', () => {
24+
const lines = readSourceSnippet(file, 3, style);
25+
26+
expect(lines).toBeDefined();
27+
// Line 3 is " * $b;" — the * should NOT be styled as a comment
28+
const line3 = lines?.find((l) => l.includes('$b'));
29+
expect(line3).toBeDefined();
30+
// $b should be styled as a variable (bold)
31+
expect(line3).toContain(`${styles.bold.open}$b${styles.bold.close}`);
32+
// The content after delimiter should NOT start with italic (comment style)
33+
const content = line3?.split('▕')[1] ?? '';
34+
expect(content).not.toContain(styles.italic.open);
35+
});
36+
});

packages/phpunit/src/Printer/SourceFileReader.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ function highlightPhp(line: string, style: AnsiStyle): string {
8888
continue;
8989
}
9090

91-
// Doc comment continuation: lines starting with *
91+
// Doc comment continuation: lines starting with * (but not *=, */, etc.)
9292
if (pos === 0 || line.slice(0, pos).trim() === '') {
93-
const docContMatch = line.slice(pos).match(/^\s*\*.*/);
94-
if (docContMatch && line.trim().startsWith('*')) {
93+
const docContMatch = line.slice(pos).match(/^\s*\*(?!\s*\$)(?!\s*\d).*/);
94+
if (docContMatch) {
9595
result += style.comment(docContMatch[0]);
9696
pos += docContMatch[0].length;
9797
continue;

0 commit comments

Comments
 (0)