Skip to content

Commit

Permalink
Fix "PHP Warning: Undefined array key currentUri" when using `@import…
Browse files Browse the repository at this point in the history
… (inline)`

When enabling source maps and an input file uses `@import (inline)`,
then the source map generation causes this error because not all
chunks have file info metadata associated in that case.

This matches upstream behaviour at
* Output Mapped:
  https://github.com/less/less.js/blob/v3.13.1/packages/less/src/less/source-map-output.js#L89
* Generator:
  https://github.com/mozilla/source-map/blob/v0.7.4/lib/source-map-generator.js#L110-L129

Without the changes in lib/, the new test case fails with:

```
1) MapTest::testImportInline
Undefined array key "currentUri"

/less.php/lib/Less/Output/Mapped.php:68
…
/less.php/lib/Less/SourceMap/Generator.php:118
/less.php/lib/Less/Parser.php:235
/less.php/test/phpunit/MapTest.php:37
```

Ref #109.
Closes #121.

Bug: T380641
Change-Id: I2e04e33facd6f900c147c124e9931ffc2cac5551
  • Loading branch information
tck authored and Krinkle committed Nov 22, 2024
1 parent d9c2671 commit 9f19aa8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ insert_final_newline = true
[test/phpunit/ParserTest.php]
trim_trailing_whitespace = false

[test/**/*.map]
insert_final_newline = false

# Markdown:
# - Editors should not change leading spaces to tabs (e.g. multi-line list items)
# YAML:
Expand Down
3 changes: 1 addition & 2 deletions lib/Less/Output/Mapped.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) {
$sourceLines = [];
$sourceColumns = ' ';

if ( $fileInfo ) {

if ( isset( $fileInfo['currentUri'] ) ) {
$url = $fileInfo['currentUri'];

if ( isset( $this->contentsMap[$url] ) ) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Less/SourceMap/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ public function addMapping( $generatedLine, $generatedColumn, $originalLine, $or
'generated_column' => $generatedColumn,
'original_line' => $originalLine,
'original_column' => $originalColumn,
'source_file' => $fileInfo['currentUri']
'source_file' => $fileInfo['currentUri'] ?? null
];

$this->sources[$fileInfo['currentUri']] = $fileInfo['filename'];
if ( isset( $fileInfo['currentUri'] ) ) {
$this->sources[$fileInfo['currentUri']] = $fileInfo['filename'];
}
}

/**
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import (inline, css) url("imports/a.less");
33 changes: 26 additions & 7 deletions test/phpunit/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,34 @@ public function testMap() {
$expectedFile = self::$fixturesDir . '/bootstrap3-sourcemap/expected/bootstrap.map';
$mapDestination = self::$cacheDir . '/bootstrap.map';

$options['sourceMap'] = true;
$options['sourceMapWriteTo'] = $mapDestination;
$options['sourceMapURL'] = '/';
$options['sourceMapBasepath'] = dirname( dirname( $lessFile ) );
$options['math'] = "always";
$parser = new Less_Parser( [
'sourceMap' => true,
'sourceMapURL' => '/',
'sourceMapBasepath' => dirname( dirname( $lessFile ) ),
'sourceMapWriteTo' => $mapDestination,
'math' => 'always',
] );
$parser->parseFile( $lessFile );
$parser->getCss();

$expected = file_get_contents( $expectedFile );
$generated = file_get_contents( $mapDestination );
$this->assertEquals( $expected, $generated );
}

public function testImportInline() {
$lessFile = self::$fixturesDir . '/less.php/less/T380641-sourcemap-import-inline.less';
$expectedFile = self::$fixturesDir . '/less.php/css/T380641-sourcemap-import-inline.map';
$mapDestination = self::$cacheDir . '/import-inline.map';

$parser = new Less_Parser( $options );
$parser = new Less_Parser( [
'sourceMap' => true,
'sourceMapURL' => '/',
'sourceMapBasepath' => dirname( $lessFile ),
'sourceMapWriteTo' => $mapDestination,
] );
$parser->parseFile( $lessFile );
$css = $parser->getCss();
$parser->getCss();

$expected = file_get_contents( $expectedFile );
$generated = file_get_contents( $mapDestination );
Expand Down

0 comments on commit 9f19aa8

Please sign in to comment.