[Pkg-javascript-commits] [node-convert-source-map] 01/09: Imported Upstream version 1.0.0

Ross Gammon ross-guest at moszumanska.debian.org
Wed Apr 1 18:23:58 UTC 2015


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

ross-guest pushed a commit to branch master
in repository node-convert-source-map.

commit 2f4b31368953618bcaaa7ff041d9f3b20977ec4f
Author: Ross Gammon <rossgammon at mail.dk>
Date:   Wed Apr 1 19:22:16 2015 +0200

    Imported Upstream version 1.0.0
---
 .gitignore                                      |  16 ++
 .travis.yml                                     |   5 +
 LICENSE                                         |  23 +++
 README.md                                       | 121 +++++++++++++++
 example/comment-to-json.js                      |  15 ++
 index.js                                        | 148 ++++++++++++++++++
 package.json                                    |  36 +++++
 test/comment-regex.js                           | 133 ++++++++++++++++
 test/convert-source-map.js                      | 195 ++++++++++++++++++++++++
 test/fixtures/map-file-comment-double-slash.css |  14 ++
 test/fixtures/map-file-comment-inline.css       |  14 ++
 test/fixtures/map-file-comment.css              |  14 ++
 test/fixtures/map-file-comment.css.map          |   6 +
 test/map-file-comment.js                        |  70 +++++++++
 14 files changed, 810 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..de78e27
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,16 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
+tmp
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..a55b235
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - 0.8
+  - 0.10
+  - 0.11
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..41702c5
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,23 @@
+Copyright 2013 Thorsten Lorenz. 
+All rights reserved.
+
+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..e415fec
--- /dev/null
+++ b/README.md
@@ -0,0 +1,121 @@
+# convert-source-map [![build status](https://secure.travis-ci.org/thlorenz/convert-source-map.png)](http://travis-ci.org/thlorenz/convert-source-map)
+
+[![NPM](https://nodei.co/npm/convert-source-map.png?downloads=true&stars=true)](https://nodei.co/npm/convert-source-map/)
+
+Converts a source-map from/to  different formats and allows adding/changing properties.
+
+```js
+var convert = require('convert-source-map');
+
+var json = convert
+  .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+  .toJSON();
+
+var modified = convert
+  .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+  .setProperty('sources', [ 'CONSOLE.LOG("HI");' ])
+  .toJSON();
+
+console.log(json);
+console.log(modified);
+```
+
+```json
+{"version":3,"file":"foo.js","sources":["console.log(\"hi\");"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
+{"version":3,"file":"foo.js","sources":["CONSOLE.LOG(\"HI\");"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
+```
+
+## API
+
+### fromObject(obj)
+
+Returns source map converter from given object.
+
+### fromJSON(json)
+
+Returns source map converter from given json string.
+
+### fromBase64(base64)
+
+Returns source map converter from given base64 encoded json string.
+
+### fromComment(comment)
+
+Returns source map converter from given base64 encoded json string prefixed with `//# sourceMappingURL=...`.
+
+### fromMapFileComment(comment, mapFileDir)
+
+Returns source map converter from given `filename` by parsing `//# sourceMappingURL=filename`.
+
+`filename` must point to a file that is found inside the `mapFileDir`. Most tools store this file right next to the
+generated file, i.e. the one containing the source map.
+
+### fromSource(source[, largeSource])
+
+Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was found.
+
+If `largeSource` is set to `true`, an algorithm that does not use regex is applied to find the source map. This is faster and especially useful if you're running into "call stack size exceeded" errors with the default algorithm.
+
+However, it is less accurate and may match content that isn't a source map comment.
+
+### fromMapFileSource(source, mapFileDir)
+
+Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was
+found.
+
+The sourcemap will be read from the map file found by parsing `# sourceMappingURL=file` comment. For more info see
+fromMapFileComment.
+
+### toObject()
+
+Returns a copy of the underlying source map.
+
+### toJSON([space])
+
+Converts source map to json string. If `space` is given (optional), this will be passed to
+[JSON.stringify](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify) when the
+JSON string is generated.
+
+### toBase64()
+
+Converts source map to base64 encoded json string.
+
+### toComment([options])
+
+Converts source map to an inline comment that can be appended to the source-file.
+
+By default, the comment is formatted like: `//# sourceMappingURL=...`, which you would
+normally see in a JS source file.
+
+When `options.multiline == true`, the comment is formatted like: `/*# sourceMappingURL=... */`, which you would find in a CSS source file.
+
+### addProperty(key, value)
+
+Adds given property to the source map. Throws an error if property already exists.
+
+### setProperty(key, value)
+
+Sets given property to the source map. If property doesn't exist it is added, otherwise its value is updated.
+
+### getProperty(key)
+
+Gets given property of the source map.
+
+### removeComments(src)
+
+Returns `src` with all source map comments removed
+
+### removeMapFileComments(src)
+
+Returns `src` with all source map comments pointing to map files removed.
+
+### commentRegex
+
+Returns the regex used to find source map comments.
+
+### mapFileCommentRegex
+
+Returns the regex used to find source map comments pointing to map files.
+
+
+[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/thlorenz/convert-source-map/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
diff --git a/example/comment-to-json.js b/example/comment-to-json.js
new file mode 100644
index 0000000..dfab186
--- /dev/null
+++ b/example/comment-to-json.js
@@ -0,0 +1,15 @@
+'use strict';
+
+var convert = require('..');
+
+var json = convert
+  .fromComment('//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+  .toJSON();
+
+var modified = convert
+  .fromComment('//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9vLmpzIiwic291cmNlcyI6WyJjb25zb2xlLmxvZyhcImhpXCIpOyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
+  .setProperty('sources', [ 'CONSOLE.LOG("HI");' ])
+  .toJSON();
+
+console.log(json);
+console.log(modified);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..c3abefe
--- /dev/null
+++ b/index.js
@@ -0,0 +1,148 @@
+'use strict';
+var fs = require('fs');
+var path = require('path');
+
+var commentRx = /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+;)?base64,(.*)$/mg;
+var mapFileCommentRx =
+  // //# sourceMappingURL=foo.js.map                       /*# sourceMappingURL=foo.js.map */
+  /(?:\/\/[@#][ \t]+sourceMappingURL=(.+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg
+
+function decodeBase64(base64) {
+  return new Buffer(base64, 'base64').toString();
+}
+
+function stripComment(sm) {
+  return sm.split(',').pop();
+}
+
+function readFromFileMap(sm, dir) {
+  // NOTE: this will only work on the server since it attempts to read the map file
+
+  var r = mapFileCommentRx.exec(sm);
+  mapFileCommentRx.lastIndex = 0;
+  
+  // for some odd reason //# .. captures in 1 and /* .. */ in 2
+  var filename = r[1] || r[2];
+  var filepath = path.join(dir, filename);
+
+  try {
+    return fs.readFileSync(filepath, 'utf8');
+  } catch (e) {
+    throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
+  }
+}
+
+function Converter (sm, opts) {
+  opts = opts || {};
+
+  if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);
+  if (opts.hasComment) sm = stripComment(sm);
+  if (opts.isEncoded) sm = decodeBase64(sm);
+  if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);
+
+  this.sourcemap = sm;
+}
+
+function convertFromLargeSource(content){
+  var lines = content.split('\n');
+  var line;
+  // find first line which contains a source map starting at end of content 
+  for (var i = lines.length - 1; i > 0; i--) {
+    line = lines[i]
+    if (~line.indexOf('sourceMappingURL=data:')) return exports.fromComment(line);
+  }
+}
+
+Converter.prototype.toJSON = function (space) {
+  return JSON.stringify(this.sourcemap, null, space);
+};
+
+Converter.prototype.toBase64 = function () {
+  var json = this.toJSON();
+  return new Buffer(json).toString('base64');
+};
+
+Converter.prototype.toComment = function (options) {
+  var base64 = this.toBase64();
+  var data = 'sourceMappingURL=data:application/json;base64,' + base64;
+  return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
+};
+
+// returns copy instead of original
+Converter.prototype.toObject = function () {
+  return JSON.parse(this.toJSON());
+};
+
+Converter.prototype.addProperty = function (key, value) {
+  if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead');
+  return this.setProperty(key, value);
+};
+
+Converter.prototype.setProperty = function (key, value) {
+  this.sourcemap[key] = value;
+  return this;
+};
+
+Converter.prototype.getProperty = function (key) {
+  return this.sourcemap[key];
+};
+
+exports.fromObject = function (obj) {
+  return new Converter(obj);
+};
+
+exports.fromJSON = function (json) {
+  return new Converter(json, { isJSON: true });
+};
+
+exports.fromBase64 = function (base64) {
+  return new Converter(base64, { isEncoded: true });
+};
+
+exports.fromComment = function (comment) {
+  comment = comment
+    .replace(/^\/\*/g, '//')
+    .replace(/\*\/$/g, '');
+
+  return new Converter(comment, { isEncoded: true, hasComment: true });
+};
+
+exports.fromMapFileComment = function (comment, dir) {
+  return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
+};
+
+// Finds last sourcemap comment in file or returns null if none was found
+exports.fromSource = function (content, largeSource) {
+  if (largeSource) return convertFromLargeSource(content);
+
+  var m = content.match(commentRx);
+  commentRx.lastIndex = 0;
+  return m ? exports.fromComment(m.pop()) : null;
+};
+
+// Finds last sourcemap comment in file or returns null if none was found
+exports.fromMapFileSource = function (content, dir) {
+  var m = content.match(mapFileCommentRx);
+  mapFileCommentRx.lastIndex = 0;
+  return m ? exports.fromMapFileComment(m.pop(), dir) : null;
+};
+
+exports.removeComments = function (src) {
+  commentRx.lastIndex = 0;
+  return src.replace(commentRx, '');
+};
+
+exports.removeMapFileComments = function (src) {
+  mapFileCommentRx.lastIndex = 0;
+  return src.replace(mapFileCommentRx, '');
+};
+
+exports.__defineGetter__('commentRegex', function () {
+  commentRx.lastIndex = 0;
+  return commentRx; 
+});
+
+exports.__defineGetter__('mapFileCommentRegex', function () {
+  mapFileCommentRx.lastIndex = 0;
+  return mapFileCommentRx; 
+});
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..5ba1005
--- /dev/null
+++ b/package.json
@@ -0,0 +1,36 @@
+{
+  "name": "convert-source-map",
+  "version": "1.0.0",
+  "description": "Converts a source-map from/to  different formats and allows adding/changing properties.",
+  "main": "index.js",
+  "scripts": {
+    "test": "tap test/*.js"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/thlorenz/convert-source-map.git"
+  },
+  "homepage": "https://github.com/thlorenz/convert-source-map",
+  "dependencies": {},
+  "devDependencies": {
+    "inline-source-map": "~0.3.1",
+    "tap": "~0.4.13"
+  },
+  "keywords": [
+    "convert",
+    "sourcemap",
+    "source",
+    "map",
+    "browser",
+    "debug"
+  ],
+  "author": {
+    "name": "Thorsten Lorenz",
+    "email": "thlorenz at gmx.de",
+    "url": "http://thlorenz.com"
+  },
+  "license": "MIT",
+  "engine": {
+    "node": ">=0.6"
+  }
+}
diff --git a/test/comment-regex.js b/test/comment-regex.js
new file mode 100644
index 0000000..1dbdec1
--- /dev/null
+++ b/test/comment-regex.js
@@ -0,0 +1,133 @@
+'use strict';
+/*jshint asi: true */
+
+var test = require('tap').test
+  , generator = require('inline-source-map')
+  , rx = require('..').commentRegex
+  , mapFileRx = require('..').mapFileCommentRegex
+
+function comment(prefix, suffix) {
+  rx.lastIndex = 0;
+  return rx.test(prefix + 'sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9' + suffix)
+}
+
+function commentWithCharSet(prefix, suffix, sep) {
+  sep = sep || ':';
+  rx.lastIndex = 0;
+  return rx.test(prefix + 'sourceMappingURL=data:application/json;charset' + sep +'utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9' + suffix)
+}
+
+// Source Map v2 Tests
+test('comment regex old spec - @', function (t) {
+  [ 
+    '//@ ',
+    '  //@ ', // with leading space
+    '\t//@ ', // with leading tab
+    '//@ ',   // with leading text
+    '/*@ ',   // multi line style
+    '  /*@ ', // multi line style with leading spaces
+    '\t/*@ ', // multi line style with leading tab
+    '/*@ ',   // multi line style with leading text
+  ].forEach(function (x) { 
+    t.ok(comment(x, ''), 'matches ' + x) 
+    t.ok(commentWithCharSet(x, ''), 'matches ' + x + ' with charset') 
+    t.ok(commentWithCharSet(x, '', '='), 'matches ' + x + ' with charset')
+  });
+
+  [
+    ' @// @',
+    ' @/* @',
+  ].forEach(function (x) { t.ok(!comment(x, ''), 'should not match ' + x) })
+
+  t.end()
+})
+
+test('comment regex new spec - #', function (t) {
+  [ 
+    '  //# ', // with leading spaces
+    '\t//# ', // with leading tab
+    '//# ',   // with leading text
+    '/*# ',   // multi line style
+    '  /*# ', // multi line style with leading spaces
+    '\t/*# ', // multi line style with leading tab
+    '/*# ',   // multi line style with leading text
+  ].forEach(function (x) { 
+    t.ok(comment(x, ''), 'matches ' + x) 
+    t.ok(commentWithCharSet(x, ''), 'matches ' + x + ' with charset') 
+    t.ok(commentWithCharSet(x, '', '='), 'matches ' + x + ' with charset')
+  });
+  
+  [ 
+    ' #// #',
+    ' #/* #',
+  ].forEach(function (x) { t.ok(!comment(x, ''), 'should not match ' + x) })
+
+  t.end()
+})
+
+function mapFileComment(s) {
+  mapFileRx.lastIndex = 0;
+  return mapFileRx.test(s + 'sourceMappingURL=foo.js.map')
+}
+
+test('mapFileComment regex old spec - @', function (t) {
+
+  [ 
+    '//@ ',
+    '  //@ ',
+    '\t//@ ',
+    '///@ ',
+  ].forEach(function (x) { t.ok(mapFileComment(x), 'matches ' + x) });
+
+  [ 
+    ' @// @',
+  ].forEach(function (x) { t.ok(!mapFileComment(x), 'does not match ' + x) })
+  t.end()
+})
+
+test('mapFileComment regex new spec - #', function (t) {
+  [ 
+    '//@ ',
+    '  //@ ', // with leading space
+    '\t//@ ', // with leading tab
+    '//@ ', // with leading text
+  ].forEach(function (x) { t.ok(mapFileComment(x), 'matches ' + x) });
+
+  [ 
+    ' #// #',
+  ].forEach(function (x) { t.ok(!mapFileComment(x), 'does not match ' + x) })
+  t.end()
+})
+
+function mapFileCommentWrap(s1, s2) {
+  mapFileRx.lastIndex = 0;
+  return mapFileRx.test(s1 + 'sourceMappingURL=foo.js.map' + s2)
+}
+
+test('mapFileComment regex /* */ old spec - @', function (t) {
+  [ [ '/*@ ', '*/' ]
+  , ['  /*@ ', '  */ ' ]            // with leading spaces
+  , [ '\t/*@ ', ' \t*/\t ']         // with a leading tab
+  , [ 'leading string/*@ ', '*/' ]  // with a leading string
+  , [ '/*@ ', ' \t*/\t ']           // with trailing whitespace
+  ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
+
+  [ ['/*@ ', ' */ */ ' ],       // not the last thing on its line 
+    ['/*@ ', ' */ more text ' ] // not the last thing on its line 
+  ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
+  t.end()
+})
+
+test('mapFileComment regex /* */ new spec - #', function (t) {
+  [ [ '/*# ', '*/' ]
+  , ['  /*# ', '  */ ' ]            // with leading spaces
+  , [ '\t/*# ', ' \t*/\t ']         // with a leading tab
+  , [ 'leading string/*# ', '*/' ]  // with a leading string
+  , [ '/*# ', ' \t*/\t ']           // with trailing whitespace
+  ].forEach(function (x) { t.ok(mapFileCommentWrap(x[0], x[1]), 'matches ' + x.join(' :: ')) });
+
+  [ ['/*# ', ' */ */ ' ],       // not the last thing on its line 
+    ['/*# ', ' */ more text ' ] // not the last thing on its line 
+  ].forEach(function (x) { t.ok(!mapFileCommentWrap(x[0], x[1]), 'does not match ' + x.join(' :: ')) });
+  t.end()
+})
diff --git a/test/convert-source-map.js b/test/convert-source-map.js
new file mode 100644
index 0000000..5263d1f
--- /dev/null
+++ b/test/convert-source-map.js
@@ -0,0 +1,195 @@
+'use strict';
+/*jshint asi: true */
+
+var test = require('tap').test
+  , generator = require('inline-source-map')
+  , convert = require('..')
+
+var gen = generator()
+    .addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })
+    .addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 })
+
+  , base64 = gen.base64Encode()
+  , comment = gen.inlineMappingUrl()
+  , json = gen.toString()
+  , obj = JSON.parse(json)
+
+test('different formats', function (t) {
+
+  t.equal(convert.fromComment(comment).toComment(), comment, 'comment -> comment')
+  t.equal(convert.fromComment(comment).toBase64(), base64, 'comment -> base64')
+  t.equal(convert.fromComment(comment).toJSON(), json, 'comment -> json')
+  t.deepEqual(convert.fromComment(comment).toObject(), obj, 'comment -> object')
+
+  t.equal(convert.fromBase64(base64).toBase64(), base64, 'base64 -> base64')
+  t.equal(convert.fromBase64(base64).toComment(), comment, 'base64 -> comment')
+  t.equal(convert.fromBase64(base64).toJSON(), json, 'base64 -> json')
+  t.deepEqual(convert.fromBase64(base64).toObject(), obj, 'base64 -> object')
+
+  t.equal(convert.fromJSON(json).toJSON(), json, 'json -> json')
+  t.equal(convert.fromJSON(json).toBase64(), base64, 'json -> base64')
+  t.equal(convert.fromJSON(json).toComment(), comment, 'json -> comment')
+  t.deepEqual(convert.fromJSON(json).toObject(), obj, 'json -> object')
+  t.end()
+})
+
+test('to object returns a copy', function (t) {
+  var c = convert.fromJSON(json)
+  var o = c.toObject()
+  o.version = '99';
+  t.equal(c.toObject().version, 3, 'setting property on returned object does not affect original')
+  t.end()
+})
+
+test('to multi-line map', function (t) {
+  var c = convert.fromObject(obj);
+  var s = c.toComment({ multiline: true });
+  t.similar(s, /^\/\*# sourceMappingURL=.+ \*\/$/);
+  t.end();
+})
+
+test('from source', function (t) {
+  var foo = [
+      'function foo() {'
+    , ' console.log("hello I am foo");'
+    , ' console.log("who are you");'
+    , '}'
+    , ''
+    , 'foo();'
+    , ''
+    ].join('\n')
+  , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+  , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+
+  function getComment(src) {
+    var map = convert.fromSource(src);
+    return map ? map.toComment() : null;
+  }
+
+  t.equal(getComment(foo), null, 'no comment returns null')
+  t.equal(getComment(foo + map), map, 'beginning of last line')
+  t.equal(getComment(foo + '    ' +  map), map, 'indented of last line')
+  t.equal(getComment(foo + '   ' + map + '\n\n'), map, 'indented on last non empty line')
+  t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
+  t.equal(getComment(foo + otherMap + '\n' +  map), map, 'finds last map in source')
+  t.end()
+})
+
+test('from source with a large source', function (t) {
+  var foo = [
+      'function foo() {'
+    , ' console.log("hello I am foo");'
+    , ' console.log("who are you");'
+    , '}'
+    , ''
+    , 'foo();'
+    , ''
+    ].join('\n')
+  , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+  , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+
+  function getComment(src) {
+    var map = convert.fromSource(src, true);
+    return map ? map.toComment() : null;
+  }
+
+  t.equal(getComment(foo), null, 'no comment returns null')
+  t.equal(getComment(foo + map), map, 'beginning of last line')
+  t.equal(getComment(foo + '    ' +  map), map, 'indented of last line')
+  t.equal(getComment(foo + '   ' + map + '\n\n'), map, 'indented on last non empty line')
+  t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
+  t.equal(getComment(foo + otherMap + '\n' +  map), map, 'finds last map in source')
+  t.end()
+})
+
+test('remove comments', function (t) {
+  var foo = [
+      'function foo() {'
+    , ' console.log("hello I am foo");'
+    , ' console.log("who are you");'
+    , '}'
+    , ''
+    , 'foo();'
+    , ''
+    ].join('\n')
+    // this one is old spec on purpose
+  , map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+  , otherMap = '//# sourceMappingURL=data:application/json;base64,ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
+  , extraCode = '\nconsole.log("more code");\nfoo()\n'
+
+  t.equal(convert.removeComments(foo + map), foo, 'from last line')
+  t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')
+  t.equal(convert.removeComments(foo + otherMap + extraCode + map), foo + extraCode, 'multiple comments from the middle of code')
+  t.end()
+})
+
+test('remove map file comments', function (t) {
+  var foo = [
+      'function foo() {'
+    , ' console.log("hello I am foo");'
+    , ' console.log("who are you");'
+    , '}'
+    , ''
+    , 'foo();'
+    , ''
+    ].join('\n')
+  , fileMap1 = '//# sourceMappingURL=foo.js.map'
+  , fileMap2 = '/*# sourceMappingURL=foo.js.map */';
+
+  t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')
+  t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')
+  t.end()
+})
+
+test('pretty json', function (t) {
+  var mod = convert.fromJSON(json).toJSON(2)
+    , expected = JSON.stringify(obj, null, 2);
+
+  t.equal(
+      mod
+    , expected
+    , 'pretty prints json when space is given')
+  t.end()
+})
+
+test('adding properties', function (t) {
+  var mod = convert
+    .fromJSON(json)
+    .addProperty('foo', 'bar')
+    .toJSON()
+    , expected = JSON.parse(json);
+    expected.foo = 'bar';
+  t.equal(
+      mod
+    , JSON.stringify(expected)
+    , 'includes added property'
+  )
+  t.end()
+})
+
+test('setting properties', function (t) {
+  var mod = convert
+    .fromJSON(json)
+    .setProperty('version', '2')
+    .setProperty('mappings', ';;;UACG')
+    .setProperty('should add', 'this')
+    .toJSON()
+    , expected = JSON.parse(json);
+    expected.version = '2';
+    expected.mappings = ';;;UACG';
+    expected['should add'] = 'this';
+  t.equal(
+      mod
+    , JSON.stringify(expected)
+    , 'includes new property and changes existing properties'
+  )
+  t.end()
+})
+
+test('getting properties', function (t) {
+  var sm = convert.fromJSON(json)
+
+  t.equal(sm.getProperty('version'), 3, 'gets version')
+  t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')
+  t.end()
+})
diff --git a/test/fixtures/map-file-comment-double-slash.css b/test/fixtures/map-file-comment-double-slash.css
new file mode 100644
index 0000000..e777991
--- /dev/null
+++ b/test/fixtures/map-file-comment-double-slash.css
@@ -0,0 +1,14 @@
+.header {
+  background: #444;
+  border: solid;
+  padding: 10px;
+  border-radius: 10px 5px 10px 5px;
+  color: #b4b472; }
+
+#main li {
+  color: green;
+  margin: 10px;
+  padding: 10px;
+  font-size: 18px; }
+
+//# sourceMappingURL=map-file-comment.css.map
diff --git a/test/fixtures/map-file-comment-inline.css b/test/fixtures/map-file-comment-inline.css
new file mode 100644
index 0000000..1e61b24
--- /dev/null
+++ b/test/fixtures/map-file-comment-inline.css
@@ -0,0 +1,14 @@
+.header {
+  background: #444;
+  border: solid;
+  padding: 10px;
+  border-radius: 10px 5px 10px 5px;
+  color: #b4b472; }
+
+#main li {
+  color: green;
+  margin: 10px;
+  padding: 10px;
+  font-size: 18px; }
+
+/*# sourceMappingURL=data:application/json;base64,ewoidmVyc2lvbiI6ICIzIiwKIm1hcHBpbmdzIjogIkFBQUEsd0JBQXlCO0VBQ3ZCLFVBQVUsRUFBRSxJQUFJO0VBQ2hCLE1BQU0sRUFBRSxLQUFLO0VBQ2IsT0FBTyxFQUFFLElBQUk7RUFDYixhQUFhLEVBQUUsaUJBQWlCO0VBQ2hDLEtBQUssRUFBRSxPQUFrQjs7QUFHM0Isd0JBQXlCO0VBQ3ZCLE9BQU8sRUFBRSxJQUFJOztBQ1RmLGdCQUFpQjtFQUNmLFVBQVUsRUFBRSxJQUFJO0VBQ2hCLEtBQUssRUFBRSxNQUFNOztBQUdmLGtCQUFtQjtFQUNqQixNQUFNLEVBQUUsSUFBSTtFQUNaLE9BQU8sRUFBRSxJQUFJO0VBQ2IsVUFBVSxFQUFFLEtBQUs7RUFDakIsYUFBYSxFQUFFLEdBQU [...]
diff --git a/test/fixtures/map-file-comment.css b/test/fixtures/map-file-comment.css
new file mode 100644
index 0000000..8b28268
--- /dev/null
+++ b/test/fixtures/map-file-comment.css
@@ -0,0 +1,14 @@
+.header {
+  background: #444;
+  border: solid;
+  padding: 10px;
+  border-radius: 10px 5px 10px 5px;
+  color: #b4b472; }
+
+#main li {
+  color: green;
+  margin: 10px;
+  padding: 10px;
+  font-size: 18px; }
+
+/*# sourceMappingURL=map-file-comment.css.map */
diff --git a/test/fixtures/map-file-comment.css.map b/test/fixtures/map-file-comment.css.map
new file mode 100644
index 0000000..25950ea
--- /dev/null
+++ b/test/fixtures/map-file-comment.css.map
@@ -0,0 +1,6 @@
+{
+"version": "3",
+"mappings": "AAAA,wBAAyB;EACvB,UAAU,EAAE,IAAI;EAChB,MAAM,EAAE,KAAK;EACb,OAAO,EAAE,IAAI;EACb,aAAa,EAAE,iBAAiB;EAChC,KAAK,EAAE,OAAkB;;AAG3B,wBAAyB;EACvB,OAAO,EAAE,IAAI;;ACTf,gBAAiB;EACf,UAAU,EAAE,IAAI;EAChB,KAAK,EAAE,MAAM;;AAGf,kBAAmB;EACjB,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,UAAU,EAAE,KAAK;EACjB,aAAa,EAAE,GAAG;EAClB,KAAK,EAAE,KAAK;;AAEd,kBAAmB;EACjB,KAAK,EAAE,KAAK;;AAGd,mBAAoB;EAClB,KAAK,EAAE,KAAK;EACZ,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,SAAS,EAAE,IAAI",
+"sources": ["./client/sass/core.scss","./client/sass/main.scss"],
+"file": "map-file-comment.css"
+}
diff --git a/test/map-file-comment.js b/test/map-file-comment.js
new file mode 100644
index 0000000..b416787
--- /dev/null
+++ b/test/map-file-comment.js
@@ -0,0 +1,70 @@
+'use strict';
+/*jshint asi: true */
+
+var test = require('tap').test
+  , rx = require('..')
+  , fs = require('fs')
+  , convert = require('..')
+
+test('\nresolving a "/*# sourceMappingURL=map-file-comment.css.map*/" style comment inside a given css content', function (t) {
+  var css = fs.readFileSync(__dirname + '/fixtures/map-file-comment.css', 'utf8')
+  var conv = convert.fromMapFileSource(css, __dirname + '/fixtures');
+  var sm = conv.toObject();
+
+  t.deepEqual(
+      sm.sources
+    , [ './client/sass/core.scss',
+        './client/sass/main.scss' ]
+    , 'resolves paths of original sources'
+  )
+
+  t.equal(sm.file, 'map-file-comment.css', 'includes filename of generated file')
+  t.equal(
+      sm.mappings
+    , 'AAAA,wBAAyB;EACvB,UAAU,EAAE,IAAI;EAChB,MAAM,EAAE,KAAK;EACb,OAAO,EAAE,IAAI;EACb,aAAa,EAAE,iBAAiB;EAChC,KAAK,EAAE,OAAkB;;AAG3B,wBAAyB;EACvB,OAAO,EAAE,IAAI;;ACTf,gBAAiB;EACf,UAAU,EAAE,IAAI;EAChB,KAAK,EAAE,MAAM;;AAGf,kBAAmB;EACjB,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,UAAU,EAAE,KAAK;EACjB,aAAa,EAAE,GAAG;EAClB,KAAK,EAAE,KAAK;;AAEd,kBAAmB;EACjB,KAAK,EAAE,KAAK;;AAGd,mBAAoB;EAClB,KAAK,EAAE,KAAK;EACZ,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,SAAS,EAAE,IAAI'
+    , 'includes mappings'
+  )
+  t.end()
+})
+
+test('\nresolving a "//# sourceMappingURL=map-file-comment.css.map" style comment inside a given css content', function (t) {
+  var css = fs.readFileSync(__dirname + '/fixtures/map-file-comment-double-slash.css', 'utf8')
+  var conv = convert.fromMapFileSource(css, __dirname + '/fixtures');
+  var sm = conv.toObject();
+
+  t.deepEqual(
+      sm.sources
+    , [ './client/sass/core.scss',
+        './client/sass/main.scss' ]
+    , 'resolves paths of original sources'
+  )
+
+  t.equal(sm.file, 'map-file-comment.css', 'includes filename of generated file')
+  t.equal(
+      sm.mappings
+    , 'AAAA,wBAAyB;EACvB,UAAU,EAAE,IAAI;EAChB,MAAM,EAAE,KAAK;EACb,OAAO,EAAE,IAAI;EACb,aAAa,EAAE,iBAAiB;EAChC,KAAK,EAAE,OAAkB;;AAG3B,wBAAyB;EACvB,OAAO,EAAE,IAAI;;ACTf,gBAAiB;EACf,UAAU,EAAE,IAAI;EAChB,KAAK,EAAE,MAAM;;AAGf,kBAAmB;EACjB,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,UAAU,EAAE,KAAK;EACjB,aAAa,EAAE,GAAG;EAClB,KAAK,EAAE,KAAK;;AAEd,kBAAmB;EACjB,KAAK,EAAE,KAAK;;AAGd,mBAAoB;EAClB,KAAK,EAAE,KAAK;EACZ,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,SAAS,EAAE,IAAI'
+    , 'includes mappings'
+  )
+  t.end()
+})
+
+test('\nresolving a /*# sourceMappingURL=data:application/json;base64,... */ style comment inside a given css content', function(t) {
+  var css = fs.readFileSync(__dirname + '/fixtures/map-file-comment-inline.css', 'utf8')
+  var conv = convert.fromSource(css, __dirname + '/fixtures')
+  var sm = conv.toObject()
+
+  t.deepEqual(
+      sm.sources
+    , [ './client/sass/core.scss',
+        './client/sass/main.scss' ]
+    , 'resolves paths of original sources'
+  )
+
+  t.equal(sm.file, 'map-file-comment.css', 'includes filename of generated file')
+  t.equal(
+      sm.mappings
+    , 'AAAA,wBAAyB;EACvB,UAAU,EAAE,IAAI;EAChB,MAAM,EAAE,KAAK;EACb,OAAO,EAAE,IAAI;EACb,aAAa,EAAE,iBAAiB;EAChC,KAAK,EAAE,OAAkB;;AAG3B,wBAAyB;EACvB,OAAO,EAAE,IAAI;;ACTf,gBAAiB;EACf,UAAU,EAAE,IAAI;EAChB,KAAK,EAAE,MAAM;;AAGf,kBAAmB;EACjB,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,UAAU,EAAE,KAAK;EACjB,aAAa,EAAE,GAAG;EAClB,KAAK,EAAE,KAAK;;AAEd,kBAAmB;EACjB,KAAK,EAAE,KAAK;;AAGd,mBAAoB;EAClB,KAAK,EAAE,KAAK;EACZ,MAAM,EAAE,IAAI;EACZ,OAAO,EAAE,IAAI;EACb,SAAS,EAAE,IAAI'
+    , 'includes mappings'
+  )
+  t.end()
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-convert-source-map.git



More information about the Pkg-javascript-commits mailing list