Skip to content

[AssetMapper] Ignore comment lines in JavaScriptImportPathCompiler #53893

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

Merged
merged 1 commit into from
Feb 11, 2024

Conversation

smnandre
Copy link
Member

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #53849
License MIT

The following code was wrongly treated as one line of comment

// import
import "foo.js"

This PR updates the regexp to ignore comment lines (and fix #53849)

Also added a comment explaining how the regexp works for future maintenance.

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very complex regex, as expected :). We’re living by the tests on this by necessity. Thanks for fixing this!

*
* @see https://regex101.com/r/1iBAIb/1
*/
private const IMPORT_PATTERN = '/^(?:\/\/.*)|(?:\'(?:[^\'\\\\]|\\\\.)*\'|"(?:[^"\\\\]|\\\\.)*")|(?:import\s*(?:(?:\*\s*as\s+\w+|\s+[\w\s{},*]+)\s*from\s*)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably inline the comments directly in the regex (and add some spaces/new lines) to make the regex more easily understandable by using /ms.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that better... or a bit too much ?

private const IMPORT_PATTERN = '/
    ^
        (?:\/\/.*)                     # Lines that start with comments
    |
        (?:
            \'(?:[^\'\\\\]|\\\\.)*\'   # Strings enclosed in single quotes
        |
            "(?:[^"\\\\]|\\\\.)*"      # Strings enclosed in double quotes
        )
    |
        (?:                            # Import statements (script captured)
            import\s*
                (?:
                    (?:\*\s*as\s+\w+|\s+[\w\s{},*]+)
                    \s*from\s*
                )?
        |
            \bimport\(
        )
        \s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]
    ?
/mx';

Copy link
Contributor

@AurelienPillevesse AurelienPillevesse Feb 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another solution can be :

private const PATTERN_LINES_START_WITH_COMMENTS = '(?:\/\/.*)';
private const PATTERN_STRINGS_ENCLOSED_SINGLE_OR_DOUBLE_QUOTES = '(?:
            \'(?:[^\'\\\\]|\\\\.)*\'   # Strings enclosed in single quotes
        |
            "(?:[^"\\\\]|\\\\.)*"      # Strings enclosed in double quotes
        )
';
// ...

And you concat everything in the IMPORT_PATTERN. What do you think about it ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I genuinely have no opinion here, just happy if the next person to work with this pattern does not lose time to understand why and how we made this regexp :) So you guys tell me

@fabpot
Copy link
Member

fabpot commented Feb 11, 2024

Thank you @smnandre.

@fabpot fabpot force-pushed the fix/js-compiler-regexp branch from 4927b8e to 41a3489 Compare February 11, 2024 14:11
@fabpot fabpot merged commit ca8c10d into symfony:6.4 Feb 11, 2024
This was referenced Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants