PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/handlebars/node_modules/uglify-js/node_modules/source-map/test/source-map/test-source-map-generator.js

https://gitlab.com/marvin1/soundcloud
JavaScript | 417 lines | 350 code | 55 blank | 12 comment | 2 complexity | 147134376e9dd98d260172847f2745fe MD5 | raw file
  1. /* -*- Mode: js; js-indent-level: 2; -*- */
  2. /*
  3. * Copyright 2011 Mozilla Foundation and contributors
  4. * Licensed under the New BSD license. See LICENSE or:
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. */
  7. if (typeof define !== 'function') {
  8. var define = require('amdefine')(module, require);
  9. }
  10. define(function (require, exports, module) {
  11. var SourceMapGenerator = require('../../lib/source-map/source-map-generator').SourceMapGenerator;
  12. var SourceMapConsumer = require('../../lib/source-map/source-map-consumer').SourceMapConsumer;
  13. var SourceNode = require('../../lib/source-map/source-node').SourceNode;
  14. var util = require('./util');
  15. exports['test some simple stuff'] = function (assert, util) {
  16. var map = new SourceMapGenerator({
  17. file: 'foo.js',
  18. sourceRoot: '.'
  19. });
  20. assert.ok(true);
  21. };
  22. exports['test JSON serialization'] = function (assert, util) {
  23. var map = new SourceMapGenerator({
  24. file: 'foo.js',
  25. sourceRoot: '.'
  26. });
  27. assert.equal(map.toString(), JSON.stringify(map));
  28. };
  29. exports['test adding mappings (case 1)'] = function (assert, util) {
  30. var map = new SourceMapGenerator({
  31. file: 'generated-foo.js',
  32. sourceRoot: '.'
  33. });
  34. assert.doesNotThrow(function () {
  35. map.addMapping({
  36. generated: { line: 1, column: 1 }
  37. });
  38. });
  39. };
  40. exports['test adding mappings (case 2)'] = function (assert, util) {
  41. var map = new SourceMapGenerator({
  42. file: 'generated-foo.js',
  43. sourceRoot: '.'
  44. });
  45. assert.doesNotThrow(function () {
  46. map.addMapping({
  47. generated: { line: 1, column: 1 },
  48. source: 'bar.js',
  49. original: { line: 1, column: 1 }
  50. });
  51. });
  52. };
  53. exports['test adding mappings (case 3)'] = function (assert, util) {
  54. var map = new SourceMapGenerator({
  55. file: 'generated-foo.js',
  56. sourceRoot: '.'
  57. });
  58. assert.doesNotThrow(function () {
  59. map.addMapping({
  60. generated: { line: 1, column: 1 },
  61. source: 'bar.js',
  62. original: { line: 1, column: 1 },
  63. name: 'someToken'
  64. });
  65. });
  66. };
  67. exports['test adding mappings (invalid)'] = function (assert, util) {
  68. var map = new SourceMapGenerator({
  69. file: 'generated-foo.js',
  70. sourceRoot: '.'
  71. });
  72. // Not enough info.
  73. assert.throws(function () {
  74. map.addMapping({});
  75. });
  76. // Original file position, but no source.
  77. assert.throws(function () {
  78. map.addMapping({
  79. generated: { line: 1, column: 1 },
  80. original: { line: 1, column: 1 }
  81. });
  82. });
  83. };
  84. exports['test that the correct mappings are being generated'] = function (assert, util) {
  85. var map = new SourceMapGenerator({
  86. file: 'min.js',
  87. sourceRoot: '/the/root'
  88. });
  89. map.addMapping({
  90. generated: { line: 1, column: 1 },
  91. original: { line: 1, column: 1 },
  92. source: 'one.js'
  93. });
  94. map.addMapping({
  95. generated: { line: 1, column: 5 },
  96. original: { line: 1, column: 5 },
  97. source: 'one.js'
  98. });
  99. map.addMapping({
  100. generated: { line: 1, column: 9 },
  101. original: { line: 1, column: 11 },
  102. source: 'one.js'
  103. });
  104. map.addMapping({
  105. generated: { line: 1, column: 18 },
  106. original: { line: 1, column: 21 },
  107. source: 'one.js',
  108. name: 'bar'
  109. });
  110. map.addMapping({
  111. generated: { line: 1, column: 21 },
  112. original: { line: 2, column: 3 },
  113. source: 'one.js'
  114. });
  115. map.addMapping({
  116. generated: { line: 1, column: 28 },
  117. original: { line: 2, column: 10 },
  118. source: 'one.js',
  119. name: 'baz'
  120. });
  121. map.addMapping({
  122. generated: { line: 1, column: 32 },
  123. original: { line: 2, column: 14 },
  124. source: 'one.js',
  125. name: 'bar'
  126. });
  127. map.addMapping({
  128. generated: { line: 2, column: 1 },
  129. original: { line: 1, column: 1 },
  130. source: 'two.js'
  131. });
  132. map.addMapping({
  133. generated: { line: 2, column: 5 },
  134. original: { line: 1, column: 5 },
  135. source: 'two.js'
  136. });
  137. map.addMapping({
  138. generated: { line: 2, column: 9 },
  139. original: { line: 1, column: 11 },
  140. source: 'two.js'
  141. });
  142. map.addMapping({
  143. generated: { line: 2, column: 18 },
  144. original: { line: 1, column: 21 },
  145. source: 'two.js',
  146. name: 'n'
  147. });
  148. map.addMapping({
  149. generated: { line: 2, column: 21 },
  150. original: { line: 2, column: 3 },
  151. source: 'two.js'
  152. });
  153. map.addMapping({
  154. generated: { line: 2, column: 28 },
  155. original: { line: 2, column: 10 },
  156. source: 'two.js',
  157. name: 'n'
  158. });
  159. map = JSON.parse(map.toString());
  160. util.assertEqualMaps(assert, map, util.testMap);
  161. };
  162. exports['test that source content can be set'] = function (assert, util) {
  163. var map = new SourceMapGenerator({
  164. file: 'min.js',
  165. sourceRoot: '/the/root'
  166. });
  167. map.addMapping({
  168. generated: { line: 1, column: 1 },
  169. original: { line: 1, column: 1 },
  170. source: 'one.js'
  171. });
  172. map.addMapping({
  173. generated: { line: 2, column: 1 },
  174. original: { line: 1, column: 1 },
  175. source: 'two.js'
  176. });
  177. map.setSourceContent('one.js', 'one file content');
  178. map = JSON.parse(map.toString());
  179. assert.equal(map.sources[0], 'one.js');
  180. assert.equal(map.sources[1], 'two.js');
  181. assert.equal(map.sourcesContent[0], 'one file content');
  182. assert.equal(map.sourcesContent[1], null);
  183. };
  184. exports['test .fromSourceMap'] = function (assert, util) {
  185. var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap));
  186. util.assertEqualMaps(assert, map.toJSON(), util.testMap);
  187. };
  188. exports['test .fromSourceMap with sourcesContent'] = function (assert, util) {
  189. var map = SourceMapGenerator.fromSourceMap(
  190. new SourceMapConsumer(util.testMapWithSourcesContent));
  191. util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent);
  192. };
  193. exports['test applySourceMap'] = function (assert, util) {
  194. var node = new SourceNode(null, null, null, [
  195. new SourceNode(2, 0, 'fileX', 'lineX2\n'),
  196. 'genA1\n',
  197. new SourceNode(2, 0, 'fileY', 'lineY2\n'),
  198. 'genA2\n',
  199. new SourceNode(1, 0, 'fileX', 'lineX1\n'),
  200. 'genA3\n',
  201. new SourceNode(1, 0, 'fileY', 'lineY1\n')
  202. ]);
  203. var mapStep1 = node.toStringWithSourceMap({
  204. file: 'fileA'
  205. }).map;
  206. mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n');
  207. mapStep1 = mapStep1.toJSON();
  208. node = new SourceNode(null, null, null, [
  209. 'gen1\n',
  210. new SourceNode(1, 0, 'fileA', 'lineA1\n'),
  211. new SourceNode(2, 0, 'fileA', 'lineA2\n'),
  212. new SourceNode(3, 0, 'fileA', 'lineA3\n'),
  213. new SourceNode(4, 0, 'fileA', 'lineA4\n'),
  214. new SourceNode(1, 0, 'fileB', 'lineB1\n'),
  215. new SourceNode(2, 0, 'fileB', 'lineB2\n'),
  216. 'gen2\n'
  217. ]);
  218. var mapStep2 = node.toStringWithSourceMap({
  219. file: 'fileGen'
  220. }).map;
  221. mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n');
  222. mapStep2 = mapStep2.toJSON();
  223. node = new SourceNode(null, null, null, [
  224. 'gen1\n',
  225. new SourceNode(2, 0, 'fileX', 'lineA1\n'),
  226. new SourceNode(2, 0, 'fileA', 'lineA2\n'),
  227. new SourceNode(2, 0, 'fileY', 'lineA3\n'),
  228. new SourceNode(4, 0, 'fileA', 'lineA4\n'),
  229. new SourceNode(1, 0, 'fileB', 'lineB1\n'),
  230. new SourceNode(2, 0, 'fileB', 'lineB2\n'),
  231. 'gen2\n'
  232. ]);
  233. var expectedMap = node.toStringWithSourceMap({
  234. file: 'fileGen'
  235. }).map;
  236. expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n');
  237. expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n');
  238. expectedMap = expectedMap.toJSON();
  239. // apply source map "mapStep1" to "mapStep2"
  240. var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));
  241. generator.applySourceMap(new SourceMapConsumer(mapStep1));
  242. var actualMap = generator.toJSON();
  243. util.assertEqualMaps(assert, actualMap, expectedMap);
  244. };
  245. exports['test sorting with duplicate generated mappings'] = function (assert, util) {
  246. var map = new SourceMapGenerator({
  247. file: 'test.js'
  248. });
  249. map.addMapping({
  250. generated: { line: 3, column: 0 },
  251. original: { line: 2, column: 0 },
  252. source: 'a.js'
  253. });
  254. map.addMapping({
  255. generated: { line: 2, column: 0 }
  256. });
  257. map.addMapping({
  258. generated: { line: 2, column: 0 }
  259. });
  260. map.addMapping({
  261. generated: { line: 1, column: 0 },
  262. original: { line: 1, column: 0 },
  263. source: 'a.js'
  264. });
  265. util.assertEqualMaps(assert, map.toJSON(), {
  266. version: 3,
  267. file: 'test.js',
  268. sources: ['a.js'],
  269. names: [],
  270. mappings: 'AAAA;A;AACA'
  271. });
  272. };
  273. exports['test ignore duplicate mappings.'] = function (assert, util) {
  274. var init = { file: 'min.js', sourceRoot: '/the/root' };
  275. var map1, map2;
  276. // null original source location
  277. var nullMapping1 = {
  278. generated: { line: 1, column: 0 }
  279. };
  280. var nullMapping2 = {
  281. generated: { line: 2, column: 2 }
  282. };
  283. map1 = new SourceMapGenerator(init);
  284. map2 = new SourceMapGenerator(init);
  285. map1.addMapping(nullMapping1);
  286. map1.addMapping(nullMapping1);
  287. map2.addMapping(nullMapping1);
  288. util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
  289. map1.addMapping(nullMapping2);
  290. map1.addMapping(nullMapping1);
  291. map2.addMapping(nullMapping2);
  292. util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
  293. // original source location
  294. var srcMapping1 = {
  295. generated: { line: 1, column: 0 },
  296. original: { line: 11, column: 0 },
  297. source: 'srcMapping1.js'
  298. };
  299. var srcMapping2 = {
  300. generated: { line: 2, column: 2 },
  301. original: { line: 11, column: 0 },
  302. source: 'srcMapping2.js'
  303. };
  304. map1 = new SourceMapGenerator(init);
  305. map2 = new SourceMapGenerator(init);
  306. map1.addMapping(srcMapping1);
  307. map1.addMapping(srcMapping1);
  308. map2.addMapping(srcMapping1);
  309. util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
  310. map1.addMapping(srcMapping2);
  311. map1.addMapping(srcMapping1);
  312. map2.addMapping(srcMapping2);
  313. util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
  314. // full original source and name information
  315. var fullMapping1 = {
  316. generated: { line: 1, column: 0 },
  317. original: { line: 11, column: 0 },
  318. source: 'fullMapping1.js',
  319. name: 'fullMapping1'
  320. };
  321. var fullMapping2 = {
  322. generated: { line: 2, column: 2 },
  323. original: { line: 11, column: 0 },
  324. source: 'fullMapping2.js',
  325. name: 'fullMapping2'
  326. };
  327. map1 = new SourceMapGenerator(init);
  328. map2 = new SourceMapGenerator(init);
  329. map1.addMapping(fullMapping1);
  330. map1.addMapping(fullMapping1);
  331. map2.addMapping(fullMapping1);
  332. util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
  333. map1.addMapping(fullMapping2);
  334. map1.addMapping(fullMapping1);
  335. map2.addMapping(fullMapping2);
  336. util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
  337. };
  338. exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) {
  339. var map = new SourceMapGenerator({
  340. file: 'test.js'
  341. });
  342. map.addMapping({
  343. generated: { line: 1, column: 1 },
  344. original: { line: 2, column: 2 },
  345. source: 'a.js',
  346. name: 'foo'
  347. });
  348. map.addMapping({
  349. generated: { line: 3, column: 3 },
  350. original: { line: 4, column: 4 },
  351. source: 'a.js',
  352. name: 'foo'
  353. });
  354. util.assertEqualMaps(assert, map.toJSON(), {
  355. version: 3,
  356. file: 'test.js',
  357. sources: ['a.js'],
  358. names: ['foo'],
  359. mappings: 'CACEA;;GAEEA'
  360. });
  361. };
  362. });