Skip to content

Commit d864744

Browse files
hasharjoshgoebel
andauthored
enh(php) add // and # comments in functions and attributes (#4230)
functions, arrow functions parameters can contain nested comments such as: header( /* cache age */ 'Age: 60' ); fn($x /* something */) => $x; Those are C blocks comments. However PHP also support single line comments either C-style (`//`) or using hash (`#`): header( # Set caching header 'Age: 60' // seconds ); fn( # We like magic $x // something ) => $x; PHP 8 attributes can also have such comments: #[\Deprecated( # A comment since: "2025-03-25" // C line comment #)] Parameters of functions, arrow functions and PHP 8 attributes solely had `C_BLOCK_COMMENT_MODE`. Add `C_LINE_COMMENT_MODE` and `HASH_COMMENT_MODE` to recognize the single line comments. Fixes: #3655 Fixes: https://phabricator.wikimedia.org/T372404 Co-authored-by: Josh Goebel <[email protected]>
1 parent d21e285 commit d864744

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ Core Grammars:
1010
- fix(javascript) correctly highlight 'for await' again [wolfgang42][]
1111
- enh(csp) add missing directives / keywords from MDN (7 more) [Max Liashuk][]
1212
- enh(ada) add new `parallel` keyword, allow `[]` for Ada 2022 [Max Reznik][]
13+
- enh(php) support single line and hash comments in attributes, constructor and functions [Antoine Musso][]
1314

1415
CONTRIBUTORS
1516

1617
[Josh Marchand]: https://github.com/yHSJ
1718
[Max Liashuk]: https://github.com/probil
1819
[Max Reznik]: https://github.com/reznikmm
20+
[Antoine Musso]: https://github.com/hashar
1921
[Chester Moses]: https://github.com/Chester-Moses-HCL
2022

23+
2124
## Version 11.11.1
2225

2326
- Fixes regression with Rust grammar.

src/languages/php.js

+6
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ export default function(hljs) {
414414
VARIABLE,
415415
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
416416
hljs.C_BLOCK_COMMENT_MODE,
417+
hljs.C_LINE_COMMENT_MODE,
418+
hljs.HASH_COMMENT_MODE,
417419
STRING,
418420
NUMBER,
419421
CONSTRUCTOR_CALL,
@@ -438,6 +440,8 @@ export default function(hljs) {
438440
NAMED_ARGUMENT,
439441
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
440442
hljs.C_BLOCK_COMMENT_MODE,
443+
hljs.C_LINE_COMMENT_MODE,
444+
hljs.HASH_COMMENT_MODE,
441445
STRING,
442446
NUMBER,
443447
CONSTRUCTOR_CALL,
@@ -566,6 +570,8 @@ export default function(hljs) {
566570
VARIABLE,
567571
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
568572
hljs.C_BLOCK_COMMENT_MODE,
573+
hljs.C_LINE_COMMENT_MODE,
574+
hljs.HASH_COMMENT_MODE,
569575
STRING,
570576
NUMBER
571577
]

test/markup/php/attributes.expect.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
<span class="hljs-attr">fields</span>: [
4545
<span class="hljs-string">&#x27;authorEmail&#x27;</span> =&gt; <span class="hljs-keyword">new</span> <span class="hljs-title class_">Assert\Email</span>,
4646
<span class="hljs-string">&#x27;shortDesc&#x27;</span> =&gt; [
47-
<span class="hljs-comment">/*something*/</span>
47+
<span class="hljs-comment">// C line comment</span>
4848
<span class="hljs-keyword">new</span> <span class="hljs-title class_">Assert\NotBlank</span>,
49+
<span class="hljs-comment">/* C block comment */</span>
4950
<span class="hljs-keyword">new</span> <span class="hljs-title class_">Assert\Length</span>(
5051
<span class="hljs-attr">max</span>: <span class="hljs-number">200</span>,
5152
<span class="hljs-attr">maxMessage</span>: <span class="hljs-string">&#x27;Your short desc is too long&#x27;</span>
5253
)
54+
<span class="hljs-comment"># Hash comment</span>
5355
],
5456
],
5557
<span class="hljs-attr">allowMissingFields</span>: <span class="hljs-literal">true</span>,

test/markup/php/attributes.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ class Book
4444
fields: [
4545
'authorEmail' => new Assert\Email,
4646
'shortDesc' => [
47-
/*something*/
47+
// C line comment
4848
new Assert\NotBlank,
49+
/* C block comment */
4950
new Assert\Length(
5051
max: 200,
5152
maxMessage: 'Your short desc is too long'
5253
)
54+
# Hash comment
5355
],
5456
],
5557
allowMissingFields: true,

test/markup/php/functions.expect.txt

+12
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@
4949
<span class="hljs-attr">label</span>: <span class="hljs-string">&#x27;foo&#x27;</span>,
5050
<span class="hljs-attr">time</span>:<span class="hljs-title function_ invoke__">time</span>() + <span class="hljs-keyword">array</span>(<span class="hljs-number">5</span>)[<span class="hljs-number">0</span>] + <span class="hljs-title class_">Foo</span>::<span class="hljs-variable constant_">HOUR</span>,
5151
);
52+
53+
<span class="hljs-title function_ invoke__">header</span>(
54+
<span class="hljs-comment">// Contained C line comment</span>
55+
<span class="hljs-comment">/* Contained C block comment */</span>
56+
<span class="hljs-comment"># Contained hash comment</span>
57+
);
58+
59+
<span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">
60+
<span class="hljs-variable">$x</span>, <span class="hljs-comment">// C line comment</span>
61+
<span class="hljs-comment">/* C block comment */</span>
62+
<span class="hljs-variable">$y</span>, <span class="hljs-comment"># Contained hash comment</span>
63+
</span>) =&gt;</span> <span class="hljs-variable">$x</span> / <span class="hljs-variable">$y</span>;

test/markup/php/functions.txt

+12
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ setAlarm(
4949
label: 'foo',
5050
time:time() + array(5)[0] + Foo::HOUR,
5151
);
52+
53+
header(
54+
// Contained C line comment
55+
/* Contained C block comment */
56+
# Contained hash comment
57+
);
58+
59+
fn(
60+
$x, // C line comment
61+
/* C block comment */
62+
$y, # Contained hash comment
63+
) => $x / $y;

0 commit comments

Comments
 (0)