Skip to content

Commit 9f19aa8

Browse files
tckKrinkle
authored andcommitted
Fix "PHP Warning: Undefined array key currentUri" when using @import (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 https://github.com/wikimedia/less.php/pull/121. Bug: T380641 Change-Id: I2e04e33facd6f900c147c124e9931ffc2cac5551
1 parent d9c2671 commit 9f19aa8

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ insert_final_newline = true
1010
[test/phpunit/ParserTest.php]
1111
trim_trailing_whitespace = false
1212

13+
[test/**/*.map]
14+
insert_final_newline = false
15+
1316
# Markdown:
1417
# - Editors should not change leading spaces to tabs (e.g. multi-line list items)
1518
# YAML:

lib/Less/Output/Mapped.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) {
6363
$sourceLines = [];
6464
$sourceColumns = ' ';
6565

66-
if ( $fileInfo ) {
67-
66+
if ( isset( $fileInfo['currentUri'] ) ) {
6867
$url = $fileInfo['currentUri'];
6968

7069
if ( isset( $this->contentsMap[$url] ) ) {

lib/Less/SourceMap/Generator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,12 @@ public function addMapping( $generatedLine, $generatedColumn, $originalLine, $or
204204
'generated_column' => $generatedColumn,
205205
'original_line' => $originalLine,
206206
'original_column' => $originalColumn,
207-
'source_file' => $fileInfo['currentUri']
207+
'source_file' => $fileInfo['currentUri'] ?? null
208208
];
209209

210-
$this->sources[$fileInfo['currentUri']] = $fileInfo['filename'];
210+
if ( isset( $fileInfo['currentUri'] ) ) {
211+
$this->sources[$fileInfo['currentUri']] = $fileInfo['filename'];
212+
}
211213
}
212214

213215
/**

test/Fixtures/less.php/css/T380641-sourcemap-import-inline.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import (inline, css) url("imports/a.less");

test/phpunit/MapTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,34 @@ public function testMap() {
77
$expectedFile = self::$fixturesDir . '/bootstrap3-sourcemap/expected/bootstrap.map';
88
$mapDestination = self::$cacheDir . '/bootstrap.map';
99

10-
$options['sourceMap'] = true;
11-
$options['sourceMapWriteTo'] = $mapDestination;
12-
$options['sourceMapURL'] = '/';
13-
$options['sourceMapBasepath'] = dirname( dirname( $lessFile ) );
14-
$options['math'] = "always";
10+
$parser = new Less_Parser( [
11+
'sourceMap' => true,
12+
'sourceMapURL' => '/',
13+
'sourceMapBasepath' => dirname( dirname( $lessFile ) ),
14+
'sourceMapWriteTo' => $mapDestination,
15+
'math' => 'always',
16+
] );
17+
$parser->parseFile( $lessFile );
18+
$parser->getCss();
19+
20+
$expected = file_get_contents( $expectedFile );
21+
$generated = file_get_contents( $mapDestination );
22+
$this->assertEquals( $expected, $generated );
23+
}
24+
25+
public function testImportInline() {
26+
$lessFile = self::$fixturesDir . '/less.php/less/T380641-sourcemap-import-inline.less';
27+
$expectedFile = self::$fixturesDir . '/less.php/css/T380641-sourcemap-import-inline.map';
28+
$mapDestination = self::$cacheDir . '/import-inline.map';
1529

16-
$parser = new Less_Parser( $options );
30+
$parser = new Less_Parser( [
31+
'sourceMap' => true,
32+
'sourceMapURL' => '/',
33+
'sourceMapBasepath' => dirname( $lessFile ),
34+
'sourceMapWriteTo' => $mapDestination,
35+
] );
1736
$parser->parseFile( $lessFile );
18-
$css = $parser->getCss();
37+
$parser->getCss();
1938

2039
$expected = file_get_contents( $expectedFile );
2140
$generated = file_get_contents( $mapDestination );

0 commit comments

Comments
 (0)