convert-source-map.js 5.9 KB

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