[Pkg-javascript-commits] [node-sourcemap-codec] 01/02: New upstream version 1.3.0

Julien Puydt julien.puydt at laposte.net
Tue Nov 15 06:52:26 UTC 2016


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-sourcemap-codec.

commit 5d546d4e56a5df024cbb9200f939991363c13195
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Tue Nov 1 15:07:13 2016 +0100

    New upstream version 1.3.0
---
 .babelrc               |   3 ++
 .eslintrc              |  28 ++++++++++++
 .gitignore             |   5 +++
 .travis.yml            |  11 +++++
 CHANGELOG.md           |  24 ++++++++++
 LICENSE                |  21 +++++++++
 README.md              |  63 ++++++++++++++++++++++++++
 appveyor.yml           |  23 ++++++++++
 package.json           |  51 +++++++++++++++++++++
 rollup.config.js       |  12 +++++
 src/sourcemap-codec.js | 117 +++++++++++++++++++++++++++++++++++++++++++++++++
 test/test.js           |  61 ++++++++++++++++++++++++++
 12 files changed, 419 insertions(+)

diff --git a/.babelrc b/.babelrc
new file mode 100644
index 0000000..48f1c3a
--- /dev/null
+++ b/.babelrc
@@ -0,0 +1,3 @@
+{
+	"presets": [ "es2015-rollup" ]
+}
diff --git a/.eslintrc b/.eslintrc
new file mode 100644
index 0000000..af8d088
--- /dev/null
+++ b/.eslintrc
@@ -0,0 +1,28 @@
+{
+	"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
+	}
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f21b148
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+.DS_Store
+node_modules
+dist
+.gobbl*
+coverage
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..f5bf60c
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,11 @@
+sudo: false
+language: node_js
+node_js:
+  - "0.12"
+  - "4"
+  - "5"
+env:
+  global:
+    - BUILD_TIMEOUT=10000
+install: npm install
+script: npm test
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..a22967b
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,24 @@
+# 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
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a331065
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e4373aa
--- /dev/null
+++ b/README.md
@@ -0,0 +1,63 @@
+# 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
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 0000000..e326df6
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,23 @@
+# 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
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c1c2625
--- /dev/null
+++ b/package.json
@@ -0,0 +1,51 @@
+{
+  "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"
+  ]
+}
diff --git a/rollup.config.js b/rollup.config.js
new file mode 100644
index 0000000..2ae6879
--- /dev/null
+++ b/rollup.config.js
@@ -0,0 +1,12 @@
+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
+};
diff --git a/src/sourcemap-codec.js b/src/sourcemap-codec.js
new file mode 100644
index 0000000..e47c070
--- /dev/null
+++ b/src/sourcemap-codec.js
@@ -0,0 +1,117 @@
+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 );
+	}
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..b77188c
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,61 @@
+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 );
+			});
+		});
+	});
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-sourcemap-codec.git



More information about the Pkg-javascript-commits mailing list