convert-source-map.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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('from source', function (t) {
  42. var foo = [
  43. 'function foo() {'
  44. , ' console.log("hello I am foo");'
  45. , ' console.log("who are you");'
  46. , '}'
  47. , ''
  48. , 'foo();'
  49. , ''
  50. ].join('\n')
  51. , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  52. , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  53. function getComment(src) {
  54. var map = convert.fromSource(src);
  55. return map ? map.toComment() : null;
  56. }
  57. t.equal(getComment(foo), null, 'no comment returns null')
  58. t.equal(getComment(foo + map), map, 'beginning of last line')
  59. t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
  60. t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
  61. t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
  62. t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
  63. t.end()
  64. })
  65. test('from source with a large source', function (t) {
  66. var foo = [
  67. 'function foo() {'
  68. , ' console.log("hello I am foo");'
  69. , ' console.log("who are you");'
  70. , '}'
  71. , ''
  72. , 'foo();'
  73. , ''
  74. ].join('\n')
  75. , map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  76. , otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  77. function getComment(src) {
  78. var map = convert.fromSource(src, true);
  79. return map ? map.toComment() : null;
  80. }
  81. t.equal(getComment(foo), null, 'no comment returns null')
  82. t.equal(getComment(foo + map), map, 'beginning of last line')
  83. t.equal(getComment(foo + ' ' + map), map, 'indented of last line')
  84. t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')
  85. t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')
  86. t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')
  87. t.end()
  88. })
  89. test('remove comments', function (t) {
  90. var foo = [
  91. 'function foo() {'
  92. , ' console.log("hello I am foo");'
  93. , ' console.log("who are you");'
  94. , '}'
  95. , ''
  96. , 'foo();'
  97. , ''
  98. ].join('\n')
  99. // this one is old spec on purpose
  100. , map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  101. , otherMap = '//# sourceMappingURL=data:application/json;base64,ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'
  102. , extraCode = '\nconsole.log("more code");\nfoo()\n'
  103. t.equal(convert.removeComments(foo + map), foo, 'from last line')
  104. t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')
  105. t.equal(convert.removeComments(foo + otherMap + extraCode + map), foo + extraCode, 'multiple comments from the middle of code')
  106. t.end()
  107. })
  108. test('remove map file comments', function (t) {
  109. var foo = [
  110. 'function foo() {'
  111. , ' console.log("hello I am foo");'
  112. , ' console.log("who are you");'
  113. , '}'
  114. , ''
  115. , 'foo();'
  116. , ''
  117. ].join('\n')
  118. , fileMap1 = '//# sourceMappingURL=foo.js.map'
  119. , fileMap2 = '/*# sourceMappingURL=foo.js.map */';
  120. t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')
  121. t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')
  122. t.end()
  123. })
  124. test('pretty json', function (t) {
  125. var mod = convert.fromJSON(json).toJSON(2)
  126. , expected = JSON.stringify(obj, null, 2);
  127. t.equal(
  128. mod
  129. , expected
  130. , 'pretty prints json when space is given')
  131. t.end()
  132. })
  133. test('adding properties', function (t) {
  134. var mod = convert
  135. .fromJSON(json)
  136. .addProperty('foo', 'bar')
  137. .toJSON()
  138. , expected = JSON.parse(json);
  139. expected.foo = 'bar';
  140. t.equal(
  141. mod
  142. , JSON.stringify(expected)
  143. , 'includes added property'
  144. )
  145. t.end()
  146. })
  147. test('setting properties', function (t) {
  148. var mod = convert
  149. .fromJSON(json)
  150. .setProperty('version', '2')
  151. .setProperty('mappings', ';;;UACG')
  152. .setProperty('should add', 'this')
  153. .toJSON()
  154. , expected = JSON.parse(json);
  155. expected.version = '2';
  156. expected.mappings = ';;;UACG';
  157. expected['should add'] = 'this';
  158. t.equal(
  159. mod
  160. , JSON.stringify(expected)
  161. , 'includes new property and changes existing properties'
  162. )
  163. t.end()
  164. })
  165. test('getting properties', function (t) {
  166. var sm = convert.fromJSON(json)
  167. t.equal(sm.getProperty('version'), 3, 'gets version')
  168. t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')
  169. t.end()
  170. })
  171. test('return null fromSource when largeSource is true', function(t) {
  172. var mod = convert.fromSource('', true)
  173. , expected = null;
  174. t.equal(
  175. mod
  176. , expected
  177. , 'return value should be null'
  178. )
  179. t.end()
  180. })