[Pkg-javascript-commits] [node-source-map] 01/01: Imported Upstream version 0.1.34

Leo Iannacone l3on-guest at moszumanska.debian.org
Thu Jun 12 21:18:25 UTC 2014


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

l3on-guest pushed a commit to branch upstream
in repository node-source-map.

commit 838a41f646cafcc05b1226af313bd061bf54c802
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Thu Jun 12 22:54:37 2014 +0200

    Imported Upstream version 0.1.34
---
 CHANGELOG.md                                 |   9 +-
 lib/source-map/source-map-generator.js       |   3 +
 lib/source-map/source-node.js                |  37 ++++++---
 package.json                                 |   5 +-
 test/source-map/test-source-map-generator.js |   9 ++
 test/source-map/test-source-node.js          | 120 +++++++++++++++++++--------
 6 files changed, 132 insertions(+), 51 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a0e1b9c..240d54a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,12 @@
 # Change Log
 
+## 0.1.34
+
+* Make `SourceNode` work with windows style ("\r\n") newlines. Issue #103.
+
+* Fix bug involving source contents and the
+  `SourceMapGenerator.prototype.applySourceMap`. Issue #100.
+
 ## 0.1.33
 
 * Fix some edge cases surrounding path joining and URL resolution.
@@ -44,7 +51,7 @@
 ## 0.1.29
 
 * Allow duplicate entries in the `names` and `sources` arrays of source maps
-  (usually from TypeScript) we are parsing. Fixes github isse 72.
+  (usually from TypeScript) we are parsing. Fixes github issue 72.
 
 ## 0.1.28
 
diff --git a/lib/source-map/source-map-generator.js b/lib/source-map/source-map-generator.js
index d555f08..fb6d6c3 100644
--- a/lib/source-map/source-map-generator.js
+++ b/lib/source-map/source-map-generator.js
@@ -229,6 +229,9 @@ define(function (require, exports, module) {
       aSourceMapConsumer.sources.forEach(function (sourceFile) {
         var content = aSourceMapConsumer.sourceContentFor(sourceFile);
         if (content) {
+          if (aSourceMapPath) {
+            sourceFile = util.join(aSourceMapPath, sourceFile);
+          }
           if (sourceRoot) {
             sourceFile = util.relative(sourceRoot, sourceFile);
           }
diff --git a/lib/source-map/source-node.js b/lib/source-map/source-node.js
index 1d98123..66a2ebc 100644
--- a/lib/source-map/source-node.js
+++ b/lib/source-map/source-node.js
@@ -12,6 +12,13 @@ define(function (require, exports, module) {
   var SourceMapGenerator = require('./source-map-generator').SourceMapGenerator;
   var util = require('./util');
 
+  // Matches a Windows-style `\r\n` newline or a `\n` newline used by all other
+  // operating systems these days (capturing the result).
+  var REGEX_NEWLINE = /(\r?\n)/g;
+
+  // Matches a Windows-style newline, or any character.
+  var REGEX_CHARACTER = /\r\n|[\s\S]/g;
+
   /**
    * SourceNodes provide a way to abstract over interpolating/concatenating
    * snippets of generated JavaScript source code while maintaining the line and
@@ -46,9 +53,17 @@ define(function (require, exports, module) {
       // and the SourceMap
       var node = new SourceNode();
 
-      // The generated code
-      // Processed fragments are removed from this array.
-      var remainingLines = aGeneratedCode.split('\n');
+      // All even indices of this array are one line of the generated code,
+      // while all odd indices are the newlines between two adjacent lines
+      // (since `REGEX_NEWLINE` captures its match).
+      // Processed fragments are removed from this array, by calling `shiftNextLine`.
+      var remainingLines = aGeneratedCode.split(REGEX_NEWLINE);
+      var shiftNextLine = function() {
+        var lineContents = remainingLines.shift();
+        // The last line of a file might not have a newline.
+        var newLine = remainingLines.shift() || "";
+        return lineContents + newLine;
+      };
 
       // We need to remember the position of "remainingLines"
       var lastGeneratedLine = 1, lastGeneratedColumn = 0;
@@ -65,7 +80,7 @@ define(function (require, exports, module) {
           if (lastGeneratedLine < mapping.generatedLine) {
             var code = "";
             // Associate first line with "lastMapping"
-            addMappingWithCode(lastMapping, remainingLines.shift() + "\n");
+            addMappingWithCode(lastMapping, shiftNextLine());
             lastGeneratedLine++;
             lastGeneratedColumn = 0;
             // The remaining code is added without mapping
@@ -89,7 +104,7 @@ define(function (require, exports, module) {
         // to the SourceNode without any mapping.
         // Each line is added as separate string.
         while (lastGeneratedLine < mapping.generatedLine) {
-          node.add(remainingLines.shift() + "\n");
+          node.add(shiftNextLine());
           lastGeneratedLine++;
         }
         if (lastGeneratedColumn < mapping.generatedColumn) {
@@ -104,12 +119,10 @@ define(function (require, exports, module) {
       if (remainingLines.length > 0) {
         if (lastMapping) {
           // Associate the remaining code in the current line with "lastMapping"
-          var lastLine = remainingLines.shift();
-          if (remainingLines.length > 0) lastLine += "\n";
-          addMappingWithCode(lastMapping, lastLine);
+          addMappingWithCode(lastMapping, shiftNextLine());
         }
         // and add the remaining lines without any mapping
-        node.add(remainingLines.join("\n"));
+        node.add(remainingLines.join(""));
       }
 
       // Copy sourcesContent into SourceNode
@@ -348,8 +361,8 @@ define(function (require, exports, module) {
         lastOriginalSource = null;
         sourceMappingActive = false;
       }
-      chunk.split('').forEach(function (ch, idx, array) {
-        if (ch === '\n') {
+      chunk.match(REGEX_CHARACTER).forEach(function (ch, idx, array) {
+        if (REGEX_NEWLINE.test(ch)) {
           generated.line++;
           generated.column = 0;
           // Mappings end at eol
@@ -371,7 +384,7 @@ define(function (require, exports, module) {
             });
           }
         } else {
-          generated.column++;
+          generated.column += ch.length;
         }
       });
     });
diff --git a/package.json b/package.json
index 360678e..423a4e2 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
   "name": "source-map",
   "description": "Generates and consumes source maps",
-  "version": "0.1.33",
+  "version": "0.1.34",
   "homepage": "https://github.com/mozilla/source-map",
   "author": "Nick Fitzgerald <nfitzgerald at mozilla.com>",
   "contributors": [
@@ -24,7 +24,8 @@
     "Hugh Kennedy <hughskennedy at gmail.com>",
     "David Glasser <glasser at davidglasser.net>",
     "Simon Lydell <simon.lydell at gmail.com>",
-    "Jmeas Smith <jellyes2 at gmail.com>"
+    "Jmeas Smith <jellyes2 at gmail.com>",
+    "Michael Z Goddard <mzgoddard at gmail.com>"
   ],
   "repository": {
     "type": "git",
diff --git a/test/source-map/test-source-map-generator.js b/test/source-map/test-source-map-generator.js
index 16241dd..227140f 100644
--- a/test/source-map/test-source-map-generator.js
+++ b/test/source-map/test-source-map-generator.js
@@ -309,16 +309,22 @@ define(function (require, exports, module) {
       original: { line: 2, column: 2 },
       source: '../coffee/foo.coffee'
     });
+    bundleMap.setSourceContent('../coffee/foo.coffee', 'foo coffee');
     bundleMap.addMapping({
       generated: { line: 13, column: 13 },
       original: { line: 12, column: 12 },
       source: '/bar.coffee'
     });
+    bundleMap.setSourceContent('/bar.coffee', 'bar coffee');
     bundleMap.addMapping({
       generated: { line: 23, column: 23 },
       original: { line: 22, column: 22 },
       source: 'http://www.example.com/baz.coffee'
     });
+    bundleMap.setSourceContent(
+      'http://www.example.com/baz.coffee',
+      'baz coffee'
+    );
     bundleMap = new SourceMapConsumer(bundleMap.toJSON());
 
     var minifiedMap = new SourceMapGenerator({
@@ -352,16 +358,19 @@ define(function (require, exports, module) {
         original: { line: 2, column: 2 },
         source: sources[0]
       });
+      map.setSourceContent(sources[0], 'foo coffee');
       map.addMapping({
         generated: { line: 11, column: 11 },
         original: { line: 12, column: 12 },
         source: sources[1]
       });
+      map.setSourceContent(sources[1], 'bar coffee');
       map.addMapping({
         generated: { line: 21, column: 21 },
         original: { line: 22, column: 22 },
         source: sources[2]
       });
+      map.setSourceContent(sources[2], 'baz coffee');
       return map.toJSON();
     }
 
diff --git a/test/source-map/test-source-node.js b/test/source-map/test-source-node.js
index e7afc4e..d186521 100644
--- a/test/source-map/test-source-node.js
+++ b/test/source-map/test-source-node.js
@@ -13,6 +13,12 @@ define(function (require, exports, module) {
   var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
   var SourceNode = require('../../lib/source-map/source-node').SourceNode;
 
+  function forEachNewline(fn) {
+    return function (assert, util) {
+      ['\n', '\r\n'].forEach(fn.bind(null, assert, util));
+    }
+  }
+
   exports['test .add()'] = function (assert, util) {
     var node = new SourceNode(null, null, null);
 
@@ -128,18 +134,27 @@ define(function (require, exports, module) {
     assert.equal(node.toString(), 'hey sexy mama, want to watch Futurama?');
   };
 
-  exports['test .toStringWithSourceMap()'] = function (assert, util) {
+  exports['test .toStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
     var node = new SourceNode(null, null, null,
-                              ['(function () {\n',
+                              ['(function () {' + nl,
                                '  ',
                                  new SourceNode(1, 0, 'a.js', 'someCall', 'originalCall'),
                                  new SourceNode(1, 8, 'a.js', '()'),
-                                 ';\n',
-                               '  ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';\n',
+                                 ';' + nl,
+                               '  ', new SourceNode(2, 0, 'b.js', ['if (foo) bar()']), ';' + nl,
                                '}());']);
-    var map = node.toStringWithSourceMap({
+    var result = node.toStringWithSourceMap({
       file: 'foo.js'
-    }).map;
+    });
+
+    assert.equal(result.code, [
+      '(function () {',
+      '  someCall();',
+      '  if (foo) bar();',
+      '}());'
+    ].join(nl));
+
+    var map = result.map;
     var mapWithoutOptions = node.toStringWithSourceMap().map;
 
     assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
@@ -191,11 +206,12 @@ define(function (require, exports, module) {
     assert.equal(actual.source, null);
     assert.equal(actual.line, null);
     assert.equal(actual.column, null);
-  };
+  });
 
-  exports['test .fromStringWithSourceMap()'] = function (assert, util) {
+  exports['test .fromStringWithSourceMap()'] = forEachNewline(function (assert, util, nl) {
+    var testCode = util.testGeneratedCode.replace(/\n/g, nl);
     var node = SourceNode.fromStringWithSourceMap(
-                              util.testGeneratedCode,
+                              testCode,
                               new SourceMapConsumer(util.testMap));
 
     var result = node.toStringWithSourceMap({
@@ -204,17 +220,17 @@ define(function (require, exports, module) {
     var map = result.map;
     var code = result.code;
 
-    assert.equal(code, util.testGeneratedCode);
+    assert.equal(code, testCode);
     assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
     map = map.toJSON();
     assert.equal(map.version, util.testMap.version);
     assert.equal(map.file, util.testMap.file);
     assert.equal(map.mappings, util.testMap.mappings);
-  };
+  });
 
-  exports['test .fromStringWithSourceMap() empty map'] = function (assert, util) {
+  exports['test .fromStringWithSourceMap() empty map'] = forEachNewline(function (assert, util, nl) {
     var node = SourceNode.fromStringWithSourceMap(
-                              util.testGeneratedCode,
+                              util.testGeneratedCode.replace(/\n/g, nl),
                               new SourceMapConsumer(util.emptyMap));
     var result = node.toStringWithSourceMap({
       file: 'min.js'
@@ -222,22 +238,22 @@ define(function (require, exports, module) {
     var map = result.map;
     var code = result.code;
 
-    assert.equal(code, util.testGeneratedCode);
+    assert.equal(code, util.testGeneratedCode.replace(/\n/g, nl));
     assert.ok(map instanceof SourceMapGenerator, 'map instanceof SourceMapGenerator');
     map = map.toJSON();
     assert.equal(map.version, util.emptyMap.version);
     assert.equal(map.file, util.emptyMap.file);
     assert.equal(map.mappings.length, util.emptyMap.mappings.length);
     assert.equal(map.mappings, util.emptyMap.mappings);
-  };
+  });
 
-  exports['test .fromStringWithSourceMap() complex version'] = function (assert, util) {
+  exports['test .fromStringWithSourceMap() complex version'] = forEachNewline(function (assert, util, nl) {
     var input = new SourceNode(null, null, null, [
-      "(function() {\n",
-        "  var Test = {};\n",
-        "  ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };\n"),
-        "  ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), "\n",
-        "}());\n",
+      "(function() {" + nl,
+        "  var Test = {};" + nl,
+        "  ", new SourceNode(1, 0, "a.js", "Test.A = { value: 1234 };" + nl),
+        "  ", new SourceNode(2, 0, "a.js", "Test.A.x = 'xyz';"), nl,
+        "}());" + nl,
         "/* Generated Source */"]);
     input = input.toStringWithSourceMap({
       file: 'foo.js'
@@ -258,27 +274,35 @@ define(function (require, exports, module) {
     map = map.toJSON();
     var inputMap = input.map.toJSON();
     util.assertEqualMaps(assert, map, inputMap);
-  };
+  });
 
-  exports['test .toStringWithSourceMap() merging duplicate mappings'] = function (assert, util) {
+  exports['test .toStringWithSourceMap() merging duplicate mappings'] = forEachNewline(function (assert, util, nl) {
     var input = new SourceNode(null, null, null, [
       new SourceNode(1, 0, "a.js", "(function"),
-      new SourceNode(1, 0, "a.js", "() {\n"),
+      new SourceNode(1, 0, "a.js", "() {" + nl),
       "  ",
       new SourceNode(1, 0, "a.js", "var Test = "),
-      new SourceNode(1, 0, "b.js", "{};\n"),
+      new SourceNode(1, 0, "b.js", "{};" + nl),
       new SourceNode(2, 0, "b.js", "Test"),
       new SourceNode(2, 0, "b.js", ".A", "A"),
       new SourceNode(2, 20, "b.js", " = { value: ", "A"),
       "1234",
-      new SourceNode(2, 40, "b.js", " };\n", "A"),
-      "}());\n",
+      new SourceNode(2, 40, "b.js", " };" + nl, "A"),
+      "}());" + nl,
       "/* Generated Source */"
     ]);
     input = input.toStringWithSourceMap({
       file: 'foo.js'
     });
 
+    assert.equal(input.code, [
+      "(function() {",
+      "  var Test = {};",
+      "Test.A = { value: 1234 };",
+      "}());",
+      "/* Generated Source */"
+    ].join(nl))
+
     var correctMap = new SourceMapGenerator({
       file: 'foo.js'
     });
@@ -333,22 +357,40 @@ define(function (require, exports, module) {
     var inputMap = input.map.toJSON();
     correctMap = correctMap.toJSON();
     util.assertEqualMaps(assert, inputMap, correctMap);
-  };
+  });
 
-  exports['test .toStringWithSourceMap() multi-line SourceNodes'] = function (assert, util) {
+  exports['test .toStringWithSourceMap() multi-line SourceNodes'] = forEachNewline(function (assert, util, nl) {
     var input = new SourceNode(null, null, null, [
-      new SourceNode(1, 0, "a.js", "(function() {\nvar nextLine = 1;\nanotherLine();\n"),
-      new SourceNode(2, 2, "b.js", "Test.call(this, 123);\n"),
-      new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';\n"),
-      new SourceNode(2, 2, "b.js", "anotherLine();\n"),
-      "/*\nGenerated\nSource\n*/\n",
-      new SourceNode(3, 4, "c.js", "anotherLine();\n"),
-      "/*\nGenerated\nSource\n*/"
+      new SourceNode(1, 0, "a.js", "(function() {" + nl + "var nextLine = 1;" + nl + "anotherLine();" + nl),
+      new SourceNode(2, 2, "b.js", "Test.call(this, 123);" + nl),
+      new SourceNode(2, 2, "b.js", "this['stuff'] = 'v';" + nl),
+      new SourceNode(2, 2, "b.js", "anotherLine();" + nl),
+      "/*" + nl + "Generated" + nl + "Source" + nl + "*/" + nl,
+      new SourceNode(3, 4, "c.js", "anotherLine();" + nl),
+      "/*" + nl + "Generated" + nl + "Source" + nl + "*/"
     ]);
     input = input.toStringWithSourceMap({
       file: 'foo.js'
     });
 
+    assert.equal(input.code, [
+      "(function() {",
+      "var nextLine = 1;",
+      "anotherLine();",
+      "Test.call(this, 123);",
+      "this['stuff'] = 'v';",
+      "anotherLine();",
+      "/*",
+      "Generated",
+      "Source",
+      "*/",
+      "anotherLine();",
+      "/*",
+      "Generated",
+      "Source",
+      "*/"
+    ].join(nl));
+
     var correctMap = new SourceMapGenerator({
       file: 'foo.js'
     });
@@ -391,6 +433,12 @@ define(function (require, exports, module) {
     var inputMap = input.map.toJSON();
     correctMap = correctMap.toJSON();
     util.assertEqualMaps(assert, inputMap, correctMap);
+  });
+
+  exports['test .toStringWithSourceMap() with empty string'] = function (assert, util) {
+    var node = new SourceNode(1, 0, 'empty.js', '');
+    var result = node.toStringWithSourceMap();
+    assert.equal(result.code, '');
   };
 
   exports['test setSourceContent with toStringWithSourceMap'] = function (assert, util) {

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



More information about the Pkg-javascript-commits mailing list