Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4:
  [AssetMapper] Throw exception in Javascript compiler when PCRE error
  • Loading branch information
fabpot committed Mar 4, 2024
2 parents 24cf1eb + 5f78910 commit 3c39c04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -115,7 +115,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
$relativeImportPath = $this->makeRelativeForJavaScript($relativeImportPath);

return str_replace($importedModule, $relativeImportPath, $fullImportString);
}, $content, -1, $count, \PREG_OFFSET_CAPTURE);
}, $content, -1, $count, \PREG_OFFSET_CAPTURE) ?? throw new RuntimeException(sprintf('Failed to compile JavaScript import paths in "%s". Error: "%s".', $asset->sourcePath, preg_last_error_msg()));
}

public function supports(MappedAsset $asset): bool
Expand Down
Expand Up @@ -641,4 +641,23 @@ public function testErrorMessageAvoidsCircularException()
// should not be caught.
$this->assertSame($content, $compiled);
}

public function testCompilerThrowsExceptionOnPcreError()
{
$compiler = new JavaScriptImportPathCompiler($this->createMock(ImportMapConfigReader::class));
$content = str_repeat('foo "import * ', 50);
$javascriptAsset = new MappedAsset('app.js', '/project/assets/app.js', publicPathWithoutDigest: '/assets/app.js');
$assetMapper = $this->createMock(AssetMapperInterface::class);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to compile JavaScript import paths in "/project/assets/app.js". Error: "Backtrack limit exhausted".');

$limit = \ini_get('pcre.backtrack_limit');
ini_set('pcre.backtrack_limit', 10);
try {
$compiler->compile($content, $javascriptAsset, $assetMapper);
} finally {
ini_set('pcre.backtrack_limit', $limit);
}
}
}

0 comments on commit 3c39c04

Please sign in to comment.