Skip to content

Commit

Permalink
Merge 58bdf12 into f022300
Browse files Browse the repository at this point in the history
  • Loading branch information
morganpackard committed Apr 15, 2018
2 parents f022300 + 58bdf12 commit 3e87893
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 31 deletions.
74 changes: 43 additions & 31 deletions SourceMap.php
Expand Up @@ -23,8 +23,8 @@ class SourceMap extends ParentClass
/**
* Loads a source map from a file
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/common.md documentation
* @param string $filename
* @link https://github.com/axypro/sourcemap/blob/master/doc/common.md documentation
* @param string $filename
* @return \axy\sourcemap\SourceMap
* @throws \axy\sourcemap\errors\IOError
* @throws \axy\sourcemap\errors\InvalidFormat
Expand All @@ -37,10 +37,10 @@ public static function loadFromFile($filename)
/**
* Saves the map file
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/common.md documentation
* @param string $filename [optional]
* the map file name (by default used outFileName)
* @param int $jsonFlag [optional]
* @link https://github.com/axypro/sourcemap/blob/master/doc/common.md documentation
* @param string $filename [optional]
* the map file name (by default used outFileName)
* @param int $jsonFlag [optional]
* @throws \axy\sourcemap\errors\IOError
* @throws \axy\sourcemap\errors\OutFileNotSpecified
*/
Expand All @@ -59,10 +59,10 @@ public function save($filename = null, $jsonFlag = JSON_UNESCAPED_SLASHES)
/**
* Returns a position map by a position in the generated source
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/search.md documentation
* @param int $line
* @link https://github.com/axypro/sourcemap/blob/master/doc/search.md documentation
* @param int $line
* zero-based line number in the generated source
* @param int $column
* @param int $column
* zero-bases column number is the line
* @return \axy\sourcemap\PosMap|null
* A position map or NULL if it is not found
Expand All @@ -75,10 +75,10 @@ public function getPosition($line, $column)
/**
* Finds a position in the source files
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/search.md documentation
* @param int $fileIndex
* @param int $line
* @param int $column
* @link https://github.com/axypro/sourcemap/blob/master/doc/search.md documentation
* @param int $fileIndex
* @param int $line
* @param int $column
* @return \axy\sourcemap\PosMap|null
* A position map or NULL if it is not found
*/
Expand All @@ -90,9 +90,9 @@ public function findPositionInSource($fileIndex, $line, $column)
/**
* Finds positions that match to a filter
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/search.md documentation
* @param \axy\sourcemap\PosMap|object|array $filter [optional]
* the filter (if not specified then returns all positions)
* @link https://github.com/axypro/sourcemap/blob/master/doc/search.md documentation
* @param \axy\sourcemap\PosMap|object|array $filter [optional]
* the filter (if not specified then returns all positions)
* @return \axy\sourcemap\PosMap[]
*/
public function find($filter = null)
Expand All @@ -106,9 +106,9 @@ public function find($filter = null)
/**
* Removes a position
*
* @param int $line
* @param int $line
* zero-based line number in the generated source
* @param int $column
* @param int $column
* zero-bases column number is the line
* @return bool
* TRUE if the position was found and removed
Expand All @@ -121,8 +121,8 @@ public function removePosition($line, $column)
/**
* Adds a position to the source map
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/build.md documentation
* @param \axy\sourcemap\PosMap|array|object $position
* @link https://github.com/axypro/sourcemap/blob/master/doc/build.md documentation
* @param \axy\sourcemap\PosMap|array|object $position
* @return \axy\sourcemap\PosMap
* @throws \axy\sourcemap\errors\InvalidIndexed
* @throws \axy\sourcemap\errors\IncompleteData
Expand Down Expand Up @@ -154,7 +154,7 @@ public function addPosition($position)
/**
* Inserts a block in the generated content
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/blocks.md documentation
* @link https://github.com/axypro/sourcemap/blob/master/doc/blocks.md documentation
* @param int $sLine
* the line of the block start
* @param int $sColumn
Expand All @@ -172,7 +172,7 @@ public function insertBlock($sLine, $sColumn, $eLine, $eColumn)
/**
* Removes a block from the generated content
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/blocks.md documentation
* @link https://github.com/axypro/sourcemap/blob/master/doc/blocks.md documentation
* @param int $sLine
* the line of the block start
* @param int $sColumn
Expand All @@ -190,13 +190,15 @@ public function removeBlock($sLine, $sColumn, $eLine, $eColumn)
/**
* Concatenates two maps (this and other)
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/concat.md documentation
* @param \axy\sourcemap\SourceMap|array|string $map
* @link https://github.com/axypro/sourcemap/blob/master/doc/concat.md documentation
* @param \axy\sourcemap\SourceMap|array|string $map
* the other map (an instance, a data array or a file name)
* @param int $line
* @param int $line
* a line number of begin the two map in the resulting file
* @param int $column [optional]
* a column number in the $line
* @param int $column [optional]
* a column
* number in
* the $line
* @throws \axy\sourcemap\errors\IOError
* @throws \axy\sourcemap\errors\InvalidFormat
* @throws \InvalidArgumentException
Expand All @@ -207,6 +209,10 @@ public function concat($map, $line, $column = 0)
$mSources = [];
foreach ($map->context->sources as $index => $name) {
$new = $this->sources->add($name);
$map_sources = $map->sources->getContents();
if (count($map_sources) > $index) {
$this->sources->setContent($name, $map_sources[$index]);
}
if ($new !== $index) {
$mSources[$index] = $new;
}
Expand All @@ -225,11 +231,17 @@ public function concat($map, $line, $column = 0)
/**
* Merges a map to the current map
*
* @link https://github.com/axypro/sourcemap/blob/master/doc/merge.md documentation
* @param \axy\sourcemap\SourceMap|array|string $map
* @link https://github.com/axypro/sourcemap/blob/master/doc/merge.md documentation
* @param \axy\sourcemap\SourceMap|array|string $map
* the other map (an instance, a data array or a file name)
* @param string $file [optional]
* file name in current sources (by default "file" from $map)
* @param string $file [optional]
* file name
* in current
* sources
* (by
* default
* "file"
* from $map)
* @return bool
* @throws \axy\sourcemap\errors\IOError
* @throws \axy\sourcemap\errors\InvalidFormat
Expand Down
16 changes: 16 additions & 0 deletions tests/classSourceMap/ConcatTest.php
Expand Up @@ -249,13 +249,29 @@ public function testConcatLine()
$this->assertEquals($expectedData, $map->getData());
}


/**
* covers ::concat
*
* @expectedException \axy\sourcemap\errors\InvalidJSON
*/
public function testConcatInvalidFormat()
{
$map = new SourceMap();
$map->concat(__DIR__.'/../tst/invalid.json.js.map', 0);
}

public function testConcatSourcesContent()
{
$file_path_a = __DIR__.'/../tst/concat/a_content.js.map';
$file_path_b = __DIR__.'/../tst/concat/bc_content.js.map';
$map = SourceMap::loadFromFile($file_path_a);
$map->concat($file_path_b, 1);
$raw_map_a = json_decode(file_get_contents($file_path_a));
$raw_map_b = json_decode(file_get_contents($file_path_b));
$all_sources_raw = array_merge($raw_map_a->sourcesContent, $raw_map_b->sourcesContent);
$serialized = json_encode($map);
$total_sources_content_count = count($raw_map_a->sourcesContent) + count($raw_map_b->sourcesContent);
$this->assertSame(json_decode($serialized)->sourcesContent, $all_sources_raw);
}
}
10 changes: 10 additions & 0 deletions tests/classSourceMap/SContentTest.php
Expand Up @@ -23,4 +23,14 @@ public function testSourceContentLoad()
$json = json_decode(file_get_contents($fn), true);
$this->assertSame(['new content', 'The second file'], $json['sourcesContent']);
}

public function testSourceContentJsonEncode()
{
$filePath = __DIR__.'/../tst/scontent.js.map';
$map = SourceMap::loadFromFile($filePath);
$raw_map = json_decode(file_get_contents($filePath));
$serialized = json_encode($map);
$this->assertSame(json_decode($serialized)->sourcesContent, $raw_map->sourcesContent);
echo $serialized;
}
}
1 change: 1 addition & 0 deletions tests/tst/concat/a_content.js.map

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

1 change: 1 addition & 0 deletions tests/tst/concat/bc_content.js.map

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

0 comments on commit 3e87893

Please sign in to comment.