convert-source-map.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 'use strict';
  2. /*jshint asi: true */
  3. var test = require('tap').test
  4. , generator = require('inline-source-map')
  5. , convert = require('..')
  6. var gen = generator()
  7. .addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })
  8. .addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 })
  9. , base64 = gen.base64Encode()
  10. , comment = gen.inlineMappingUrl()
  11. , json = gen.toString()
  12. , obj = JSON.parse(json)
  13. test('different formats', function (t) {
  14. t.equal(convert.fromComment(comment).toComment(), comment, 'comment -> comment')
  15. t.equal(convert.fromComment(comment).toBase64(), base64, 'comment -> base64')
  16. t.equal(convert.fromComment(comment).toJSON(), json, 'comment -> json')
  17. t.deepEqual(convert.fromComment(comment).toObject(), obj, 'comment -> object')
  18. t.equal(convert.fromBase64(base64).toBase64(), base64, 'base64 -> base64')
  19. t.equal(convert.fromBase64(base64).toComment(), comment, 'base64 -> comment')
  20. t.equal(convert.fromBase64(base64).toJSON(), json, 'base64 -> json')
  21. t.deepEqual(convert.fromBase64(base64).toObject(), obj, 'base64 -> object')
  22. t.equal(convert.fromJSON(json).toJSON(), json, 'json -> json')
  23. t.equal(convert.fromJSON(json).toBase64(), base64, 'json -> base64')
  24. t.equal(convert.fromJSON(json).toComment(), comment, 'json -> comment')
  25. t.deepEqual(convert.fromJSON(json).toObject(), obj, 'json -> object')
  26. t.end()
  27. })
  28. test('to object returns a copy', function (t) {
  29. var c = convert.fromJSON(json)
  30. var o = c.toObject()
  31. o.version = '99';
  32. t.equal(c.toObject().version, 3, 'setting property on returned object does not affect original')
  33. t.end()
  34. })
  35. test('to multi-line map', function (t) {
  36. var c = convert.fromObject(obj);
  37. var s = c.toComment({ multiline: true });
  38. t.similar(s, /^\/\*# sourceMappingURL=.+ \*\/$/);
  39. t.end();
  40. })
  41. test('to map file comment', function (t) {
  42. t.equal(convert.generateMapFileComment('index.js.map'), '//# sourceMappingURL=index.js.map');
  43. t.equal(convert.generateMapFileComment('index.css.map', { multiline: true }), '/*# sourceMappingURL=index.css.map */');
  44. t.end();
  45. })
  46. test('from source', function (t) {
  47. var foo = [
  48. 'function foo() {'
  49. , ' console.log("hello I am foo");'
  50. , ' console.log("who are you");'
  51. , '}'
  52. , ''
  53. , 'foo();'
  54. , ''
  55. ].join('\n')
  56. , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  57. , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  58. function getComment(src) {
  59. var map = convert.fromSource(src);
  60. return map ? map.toComment() : null;
  61. }
  62. t.equal(getComment(foo), null, 'no comment returns null')
  63. t.equal(getComment(foo + map), map, 'beginning of last line')
  64. t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
  65. t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
  66. t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
  67. t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
  68. t.end()
  69. })
  70. test('from source with a large source', function (t) {
  71. var foo = [
  72. 'function foo() {'
  73. , ' console.log("hello I am foo");'
  74. , ' console.log("who are you");'
  75. , '}'
  76. , ''
  77. , 'foo();'
  78. , ''
  79. ].join('\n')
  80. , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  81. , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  82. function getComment(src) {
  83. var map = convert.fromSource(src, true);
  84. return map ? map.toComment() : null;
  85. }
  86. t.equal(getComment(foo), null, 'no comment returns null')
  87. t.equal(getComment(foo + map), map, 'beginning of last line')
  88. t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
  89. t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
  90. t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
  91. t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
  92. t.end()
  93. })
  94. test('remove comments', function (t) {
  95. var foo = [
  96. 'function foo() {'
  97. , ' console.log("hello I am foo");'
  98. , ' console.log("who are you");'
  99. , '}'
  100. , ''
  101. , 'foo();'
  102. , ''
  103. ].join('\n')
  104. // this one is old spec on purpose
  105. , map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  106. , otherMap = '//# sourceMappingURL=data:application/json;base64,ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  107. , extraCode = '\nconsole.log("more code");\nfoo()\n'
  108. t.equal(convert.removeComments(foo + map), foo, 'from last line')
  109. t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')
  110. t.equal(convert.removeComments(foo + otherMap + extraCode + map), foo + extraCode, 'multiple comments from the middle of code')
  111. t.end()
  112. })
  113. test('remove map file comments', function (t) {
  114. var foo = [
  115. 'function foo() {'
  116. , ' console.log("hello I am foo");'
  117. , ' console.log("who are you");'
  118. , '}'
  119. , ''
  120. , 'foo();'
  121. , ''
  122. ].join('\n')
  123. , fileMap1 = '//# sourceMappingURL=foo.js.map'
  124. , fileMap2 = '/*# sourceMappingURL=foo.js.map */';
  125. t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')
  126. t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')
  127. t.end()
  128. })
  129. test('pretty json', function (t) {
  130. var mod = convert.fromJSON(json).toJSON(2)
  131. , expected = JSON.stringify(obj, null, 2);
  132. t.equal(
  133. mod
  134. , expected
  135. , 'pretty prints json when space is given')
  136. t.end()
  137. })
  138. test('adding properties', function (t) {
  139. var mod = convert
  140. .fromJSON(json)
  141. .addProperty('foo', 'bar')
  142. .toJSON()
  143. , expected = JSON.parse(json);
  144. expected.foo = 'bar';
  145. t.equal(
  146. mod
  147. , JSON.stringify(expected)
  148. , 'includes added property'
  149. )
  150. t.end()
  151. })
  152. test('setting properties', function (t) {
  153. var mod = convert
  154. .fromJSON(json)
  155. .setProperty('version', '2')
  156. .setProperty('mappings', ';;;UACG')
  157. .setProperty('should add', 'this')
  158. .toJSON()
  159. , expected = JSON.parse(json);
  160. expected.version = '2';
  161. expected.mappings = ';;;UACG';
  162. expected['should add'] = 'this';
  163. t.equal(
  164. mod
  165. , JSON.stringify(expected)
  166. , 'includes new property and changes existing properties'
  167. )
  168. t.end()
  169. })
  170. test('getting properties', function (t) {
  171. var sm = convert.fromJSON(json)
  172. t.equal(sm.getProperty('version'), 3, 'gets version')
  173. t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')
  174. t.end()
  175. })
  176. test('return null fromSource when largeSource is true', function(t) {
  177. var mod = convert.fromSource('', true)
  178. , expected = null;
  179. t.equal(
  180. mod
  181. , expected
  182. , 'return value should be null'
  183. )
  184. t.end()
  185. })