highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

(PHP) Syntax parsing breaks inside parameters of some function calls

Open Roy-Orbison opened this issue 3 years ago • 1 comments

Describe the issue When a lambda function is used as a parameter, it breaks down if it has a use section after its parameters.

Comments break inside object method calls (but apparently not regular function calls).

Which language seems to have the issue? PHP

Are you using highlight or highlightAuto? I noticed it on Stack Overflow, and confirmed with highlightElement in your demo pages.

Sample Code to Reproduce As highlighted on your demo page

Expected behavior

funky(
	'string',
	function(array $x) use ($y) {
		# $y above fails to parse as a var, and this comment is broken
		$x[] = $y
		return $x;
	}
);

$o->m([
	'foo',
	# this comment highlights keywords like include and 'string' literals
	'bar',
]);

Roy-Orbison avatar Nov 09 '22 03:11 Roy-Orbison

Yes the comments are somehow wrongly parsed. I noticed it on ChatGPT answer.

Will look at it.

wkania avatar Oct 03 '24 10:10 wkania

With #4230 I am fixing the C line comments (// comment). But I guess the patch should also fixes the comments starting with # :)

hashar avatar Mar 21 '25 14:03 hashar

Yes, let us know if #4230 does the trick.

joshgoebel avatar Mar 22 '25 18:03 joshgoebel

Do you have a sandbox to test in? If not, can you just paste the snippet above into your build to see if it's fixed?

Roy-Orbison avatar Mar 23 '25 23:03 Roy-Orbison

With #4230 I am fixing the C line comments (// comment). But I guess the patch should also fixes the comments starting with # :)

After I have left that comment above, I have amended my pull request to add support for hash comments. The diff can be seen at https://github.com/highlightjs/highlight.js/compare/80f3fa7bee289cbdb21277da1f0e0e216559d6bd..6060d5c68794ed43f2e036d0607404b7e33264b8 and it adds a test to validate that hash comment contained within a function is rendered as a comment span :)

hashar avatar Mar 25 '25 10:03 hashar