-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
There was a problem hiding this 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'; |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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';
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
Thank you @smnandre. |
4927b8e
to
41a3489
Compare
The following code was wrongly treated as one line of comment
This PR updates the regexp to ignore comment lines (and fix #53849)
Also added a comment explaining how the regexp works for future maintenance.