Commit 5d546d4e authored by Julien Puydt's avatar Julien Puydt

New upstream version 1.3.0

parents
{
"presets": [ "es2015-rollup" ]
}
{
"rules": {
"indent": [ 2, "tab", { "SwitchCase": 1 }],
"quotes": [ 2, "single" ],
"linebreak-style": [ 2, "unix" ],
"semi": [ 2, "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"object-shorthand": [2, "always" ],
"no-const-assign": 2,
"no-class-assign": 2,
"no-this-before-super": 2,
"no-var": 2,
"quote-props": [ 2, "as-needed" ],
"one-var": [ 2, "never" ],
"prefer-arrow-callback": 2,
"arrow-spacing": 2,
"no-cond-assign": 0
},
"env": {
"es6": true,
"browser": true
},
"extends": "eslint:recommended",
"ecmaFeatures": {
"modules": true
}
}
.DS_Store
node_modules
dist
.gobbl*
coverage
sudo: false
language: node_js
node_js:
- "0.12"
- "4"
- "5"
env:
global:
- BUILD_TIMEOUT=10000
install: npm install
script: npm test
# sourcemap-codec changelog
## 1.3.0
* Update build process
## 1.2.1
* Add dist files to npm package
## 1.2.0
* Add ES6 build
* Update dependencies
* Add test coverage
## 1.1.0
* Fix bug with lines containing single-character segments
* Add tests
## 1.0.0
* First release
The MIT License
Copyright (c) 2015 Rich Harris
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# sourcemap-codec
Encode/decode the `mappings` property of a [sourcemap](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit).
## Why?
Sourcemaps are difficult to generate and manipulate, because the `mappings` property – the part that actually links the generated code back to the original source – is encoded using an obscure method called [Variable-length quantity](https://en.wikipedia.org/wiki/Variable-length_quantity). On top of that, each segment in the mapping contains offsets rather than absolute indices, which means that you can't look at a segment in isolation – you have to understand the whole sourcemap.
This package makes the process slightly easier.
## Installation
```bash
npm install sourcemap-codec
```
## Usage
```js
import { encode, decode } from 'sourcemap-codec';
var decoded = decode( ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' );
assert.deepEqual( decoded, [
// the first line (of the generated code) has no mappings,
// as shown by the starting semi-colon (which separates lines)
[],
// the second line contains four (comma-separated) segments
[
// segments are encoded as you'd expect:
// [ generatedCodeColumn, sourceIndex, sourceCodeLine, sourceCodeColumn, nameIndex ]
// i.e. the first segment begins at column 2, and maps back to the second column
// of the second line (both zero-based) of the 0th source, and uses the 0th
// name in the `map.names` array
[ 2, 0, 2, 2, 0 ],
// the remaining segments are 4-length rather than 5-length,
// because they don't map a name
[ 4, 0, 2, 4 ],
[ 6, 0, 2, 5 ],
[ 7, 0, 2, 7 ]
],
// the final line contains two segments
[
[ 2, 1, 10, 19 ],
[ 12, 1, 11, 20 ]
]
]);
var encoded = encode( decoded );
assert.equal( encoded, ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' );
```
# License
MIT
# Test against this version of Node.js
environment:
matrix:
# node.js
- nodejs_version: "5"
# Install scripts. (runs after repo cloning)
install:
# Get the latest stable version of Node.js or io.js
- ps: Install-Product node $env:nodejs_version
# install modules
- npm install
# Post-install test scripts.
test_script:
# Output useful info for debugging.
- node --version
- npm --version
# run tests
- npm test
# Don't actually build.
build: off
{
"name": "sourcemap-codec",
"version": "1.3.0",
"description": "Encode/decode sourcemap mappings",
"main": "dist/sourcemap-codec.umd.js",
"jsnext:main": "dist/sourcemap-codec.es6.js",
"scripts": {
"test": "mocha",
"build": "rm -rf dist && rollup -c -f umd -o dist/sourcemap-codec.umd.js && rollup -c -f es6 -o dist/sourcemap-codec.es6.js",
"pretest": "npm run build",
"prepublish": "npm test",
"pretest-coverage": "npm run build",
"test-coverage": "rm -rf coverage/* && istanbul cover --report json node_modules/.bin/_mocha -- -u exports -R spec test/test.js",
"posttest-coverage": "remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.json -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.lcov -t lcovonly -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped -t html -b dist",
"ci": "npm run test-coverage && codecov < coverage/coverage-remapped.lcov"
},
"repository": {
"type": "git",
"url": "https://github.com/Rich-Harris/sourcemap-codec"
},
"keywords": [
"sourcemap",
"vlq"
],
"author": "Rich Harris",
"license": "MIT",
"bugs": {
"url": "https://github.com/Rich-Harris/sourcemap-codec/issues"
},
"homepage": "https://github.com/Rich-Harris/sourcemap-codec",
"dependencies": {
"vlq": "^0.2.1"
},
"devDependencies": {
"babel-preset-es2015-rollup": "^1.0.0",
"codecov.io": "^0.1.6",
"console-group": "^0.1.2",
"eslint": "^1.6.0",
"istanbul": "^0.4.0",
"mocha": "^2.3.3",
"remap-istanbul": "^0.5.1",
"rollup": "^0.21.2",
"rollup-plugin-babel": "^2.2.0",
"rollup-plugin-npm": "^1.1.0"
},
"files": [
"src",
"dist",
"README.md"
]
}
import babel from 'rollup-plugin-babel';
import npm from 'rollup-plugin-npm';
export default {
entry: 'src/sourcemap-codec.js',
plugins: [
babel({ exclude: 'node_modules/**' }),
npm({ jsnext: true })
],
moduleName: 'sourcemapCodec',
sourceMap: true
};
import { decode as decodeVlq, encode as encodeVlq } from 'vlq';
function decodeSegments ( encodedSegments ) {
let i = encodedSegments.length;
let segments = new Array( i );
while ( i-- ) segments[i] = decodeVlq( encodedSegments[i] );
return segments;
}
export function decode ( mappings ) {
let sourceFileIndex = 0; // second field
let sourceCodeLine = 0; // third field
let sourceCodeColumn = 0; // fourth field
let nameIndex = 0; // fifth field
let lines = mappings.split( ';' );
let numLines = lines.length;
let decoded = new Array( numLines );
let i;
let j;
let line;
let generatedCodeColumn;
let decodedLine;
let segments;
let segment;
let result;
for ( i = 0; i < numLines; i += 1 ) {
line = lines[i];
generatedCodeColumn = 0; // first field - reset each time
decodedLine = [];
segments = decodeSegments( line.split( ',' ) );
for ( j = 0; j < segments.length; j += 1 ) {
segment = segments[j];
if ( !segment.length ) {
break;
}
generatedCodeColumn += segment[0];
result = [ generatedCodeColumn ];
decodedLine.push( result );
if ( segment.length === 1 ) {
// only one field!
continue;
}
sourceFileIndex += segment[1];
sourceCodeLine += segment[2];
sourceCodeColumn += segment[3];
result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn );
if ( segment.length === 5 ) {
nameIndex += segment[4];
result.push( nameIndex );
}
}
decoded[i] = decodedLine;
}
return decoded;
}
export function encode ( decoded ) {
let offsets = {
generatedCodeColumn: 0,
sourceFileIndex: 0, // second field
sourceCodeLine: 0, // third field
sourceCodeColumn: 0, // fourth field
nameIndex: 0 // fifth field
};
return decoded.map( line => {
offsets.generatedCodeColumn = 0; // first field - reset each time
return line.map( encodeSegment ).join( ',' );
}).join( ';' );
function encodeSegment ( segment ) {
if ( !segment.length ) {
return segment;
}
let result = new Array( segment.length );
result[0] = segment[0] - offsets.generatedCodeColumn;
offsets.generatedCodeColumn = segment[0];
if ( segment.length === 1 ) {
// only one field!
return encodeVlq( result );
}
result[1] = segment[1] - offsets.sourceFileIndex;
result[2] = segment[2] - offsets.sourceCodeLine;
result[3] = segment[3] - offsets.sourceCodeColumn;
offsets.sourceFileIndex = segment[1];
offsets.sourceCodeLine = segment[2];
offsets.sourceCodeColumn = segment[3];
if ( segment.length === 5 ) {
result[4] = segment[4] - offsets.nameIndex;
offsets.nameIndex = segment[4];
}
return encodeVlq( result );
}
}
var decode = require( '../' ).decode;
var encode = require( '../' ).encode;
var assert = require( 'assert' );
require( 'console-group' ).install();
describe( 'sourcemap-codec', function () {
// TODO more tests
var tests = [
{
encoded: 'AAAA',
decoded: [ [ [ 0, 0, 0, 0 ] ] ]
},
{
encoded: ';;;',
decoded: [ [], [], [], [] ]
},
{
encoded: 'A,AAAA;;AACDE;',
decoded: [
[ [ 0 ], [ 0, 0, 0, 0 ] ],
[],
[ [ 0, 0, 1, -1, 2 ] ],
[]
]
},
{
encoded: ';;;;EAEEA,EAAE,EAAC,CAAE;ECQY,UACC',
decoded: [
[],
[],
[],
[],
[ [ 2, 0, 2, 2, 0 ], [ 4, 0, 2, 4 ], [ 6, 0, 2, 5 ], [ 7, 0, 2, 7 ] ],
[ [ 2, 1, 10, 19 ], [ 12, 1, 11, 20 ] ]
]
}
];
var filtered = tests.filter( function ( test ) {
return test.solo;
});
tests = filtered.length ? filtered : tests;
describe( 'decode()', function () {
tests.forEach( function ( test, i ) {
it( 'decodes sample ' + i, function () {
assert.deepEqual( decode( test.encoded ), test.decoded );
});
});
});
describe( 'encode()', function () {
tests.forEach( function ( test, i ) {
it( 'encodes sample ' + i, function () {
assert.deepEqual( encode( test.decoded ), test.encoded );
});
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment