Skip to content

Commit ca8c10d

Browse files
committed
bug #53893 [AssetMapper] Ignore comment lines in JavaScriptImportPathCompiler (smnandre)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [AssetMapper] Ignore comment lines in JavaScriptImportPathCompiler | 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 ```js // 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. Commits ------- 41a3489 [AssetMapper] Ignore comment lines in JavaScriptImportPathCompiler
2 parents cff8011 + 41a3489 commit ca8c10d

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/Symfony/Component/AssetMapper/Compiler/JavaScriptImportPathCompiler.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,31 @@
2727
*/
2828
final class JavaScriptImportPathCompiler implements AssetCompilerInterface
2929
{
30-
// https://regex101.com/r/qFoeoR/1
31-
private const IMPORT_PATTERN = '/(?:\'(?:[^\'\\\\]|\\\\.)*\'|"(?:[^"\\\\]|\\\\.)*")|(?:import\s*(?:(?:\*\s*as\s+\w+|[\w\s{},*]+)\s*from\s*)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m';
30+
/**
31+
* @see https://regex101.com/r/1iBAIb/1
32+
*/
33+
private const IMPORT_PATTERN = '/
34+
^
35+
(?:\/\/.*) # Lines that start with comments
36+
|
37+
(?:
38+
\'(?:[^\'\\\\]|\\\\.)*\' # Strings enclosed in single quotes
39+
|
40+
"(?:[^"\\\\]|\\\\.)*" # Strings enclosed in double quotes
41+
)
42+
|
43+
(?: # Import statements (script captured)
44+
import\s*
45+
(?:
46+
(?:\*\s*as\s+\w+|\s+[\w\s{},*]+)
47+
\s*from\s*
48+
)?
49+
|
50+
\bimport\(
51+
)
52+
\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]
53+
?
54+
/mx';
3255

3356
public function __construct(
3457
private readonly ImportMapConfigReader $importMapConfigReader,
@@ -42,7 +65,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
4265
return preg_replace_callback(self::IMPORT_PATTERN, function ($matches) use ($asset, $assetMapper, $content) {
4366
$fullImportString = $matches[0][0];
4467

45-
// Ignore enquoted strings (e.g. console.log("import 'foo';")
68+
// Ignore matches that did not capture import statements
4669
if (!isset($matches[1][0])) {
4770
return $fullImportString;
4871
}

src/Symfony/Component/AssetMapper/Tests/Compiler/JavaScriptImportPathCompilerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ public static function provideCompileTests(): iterable
177177
'expectedJavaScriptImports' => ['/assets/other.js' => ['lazy' => false, 'asset' => 'other.js', 'add' => true]],
178178
];
179179

180+
yield 'commented_import_on_one_line_then_module_name_on_next_is_not_ok' => [
181+
'input' => "// import \n './other.js';",
182+
'expectedJavaScriptImports' => [],
183+
];
184+
185+
yield 'commented_import_on_one_line_then_import_on_next_is_ok' => [
186+
'input' => "// import\nimport { Foo } from './other.js';",
187+
'expectedJavaScriptImports' => ['/assets/other.js' => ['lazy' => false, 'asset' => 'other.js', 'add' => true]],
188+
];
189+
180190
yield 'importing_a_css_file_is_included' => [
181191
'input' => "import './styles.css';",
182192
'expectedJavaScriptImports' => ['/assets/styles.css' => ['lazy' => false, 'asset' => 'styles.css', 'add' => true]],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy