Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Roy-Orbison opened this issue Nov 9, 2022 · 5 comments · May be fixed by #4230
Open

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

Roy-Orbison opened this issue Nov 9, 2022 · 5 comments · May be fixed by #4230
Labels
bug help welcome Could use help from community language

Comments

@Roy-Orbison
Copy link

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 Roy-Orbison added bug help welcome Could use help from community language labels Nov 9, 2022
@wkania
Copy link
Contributor

wkania commented Oct 3, 2024

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

Will look at it.

@hashar
Copy link

hashar commented Mar 21, 2025

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

hashar added a commit to hashar/highlight.js that referenced this issue Mar 21, 2025
functions and arrow functions parameters can contain 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;

Parameters of functions and arrow functions solely had
C_BLOCK_COMMENT_MODE. Add C_LINE_COMMENT_MODE and HASH_COMMENT_MODE

The PHP 8 attributes do have C_BLOCK_COMMENT_MODE set, but since they
are one line definitions they can't contain C_LINE_COMMENT_MODE or
HASH_COMMENT_MODE as far as I understand it. I have left a comment to
make this clear.

Fixes: highlightjs#3655
Fixes: https://phabricator.wikimedia.org/T372404
@joshgoebel
Copy link
Member

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

@Roy-Orbison
Copy link
Author

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?

hashar added a commit to hashar/highlight.js that referenced this issue Mar 25, 2025
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: highlightjs#3655
Fixes: https://phabricator.wikimedia.org/T372404
@hashar
Copy link

hashar commented Mar 25, 2025

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants