react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / test / combine-source-map.js
80540 views'use strict';1/*jshint asi: true */23var test = require('tap').test;4var convert = require('convert-source-map');5var commentRegex = require('convert-source-map').commentRegex;6var combine = require('..');7var mappingsFromMap = require('../lib/mappings-from-map');89function checkMappings(foo, sm, lineOffset) {10function inspect(obj, depth) {11return require('util').inspect(obj, false, depth || 5, true);12}1314var fooMappings = mappingsFromMap(foo);15var mappings = mappingsFromMap(sm);1617var genLinesOffset = true;18var origLinesSame = true;19for (var i = 0; i < mappings.length; i++) {20var fooGen = fooMappings[i].generated;21var fooOrig = fooMappings[i].original;22var gen = mappings[i].generated23var orig = mappings[i].original;2425if (gen.column !== fooGen.column || gen.line !== (fooGen.line + lineOffset)) {26console.error(27'generated mapping at %s not offset properly:\ninput: [%s]\noutput:[%s]\n\n',28i ,29inspect(fooGen),30inspect(gen)31);32genLinesOffset = false;33}3435if (orig.column !== fooOrig.column || orig.line !== fooOrig.line) {36console.error(37'original mapping at %s is not the same as the genrated mapping:\ninput: [%s]\noutput:[%s]\n\n',38i ,39inspect(fooOrig),40inspect(orig)41);42origLinesSame = false;43}44}45return { genLinesOffset: genLinesOffset, origLinesSame: origLinesSame };46}4748var foo = {49version : 3,50file : 'foo.js',51sourceRoot : '',52sources : [ 'foo.coffee' ],53names : [],54mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ',55sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] };5657test('add one file with inlined source', function (t) {5859var mapComment = convert.fromObject(foo).toComment();60var file = {61id: 'xyz'62, source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment63, sourceFile: 'foo.js'64};6566var lineOffset = 367var base64 = combine.create()68.addFile(file, { line: lineOffset })69.base64()7071var sm = convert.fromBase64(base64).toObject();72var res = checkMappings(foo, sm, lineOffset);7374t.ok(res.genLinesOffset, 'all generated lines are offset properly and columns unchanged')75t.ok(res.origLinesSame, 'all original lines and columns are unchanged')76t.equal(sm.sourcesContent[0], foo.sourcesContent[0], 'includes the original source')77t.equal(sm.sources[0], 'foo.coffee', 'includes original filename')78t.end()79});808182test('add one file without inlined source', function (t) {8384var mapComment = convert85.fromObject(foo)86.setProperty('sourcesContent', [])87.toComment();8889var file = {90id: 'xyz'91, source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment92, sourceFile: 'foo.js'93};9495var lineOffset = 396var base64 = combine.create()97.addFile(file, { line: lineOffset })98.base64()99100var sm = convert.fromBase64(base64).toObject();101var mappings = mappingsFromMap(sm);102103t.equal(sm.sourcesContent[0], file.source, 'includes the generated source')104t.equal(sm.sources[0], 'foo.js', 'includes generated filename')105106t.deepEqual(107mappings108, [ { generated: { line: 4, column: 0 },109original: { line: 1, column: 0 },110source: 'foo.js', name: undefined },111{ generated: { line: 5, column: 0 },112original: { line: 2, column: 0 },113source: 'foo.js', name: undefined },114{ generated: { line: 6, column: 0 },115original: { line: 3, column: 0 },116source: 'foo.js', name: undefined },117{ generated: { line: 7, column: 0 },118original: { line: 4, column: 0 },119source: 'foo.js', name: undefined },120{ generated: { line: 8, column: 0 },121original: { line: 5, column: 0 },122source: 'foo.js', name: undefined },123{ generated: { line: 9, column: 0 },124original: { line: 6, column: 0 },125source: 'foo.js', name: undefined },126{ generated: { line: 10, column: 0 },127original: { line: 7, column: 0 },128source: 'foo.js', name: undefined } ]129, 'generates mappings offset by the given line'130)131t.end()132})133134test('remove comments', function (t) {135var mapComment = convert.fromObject(foo).toComment();136137function sourcemapComments(src) {138var matches = src.match(commentRegex);139return matches ? matches.length : 0;140}141142t.equal(sourcemapComments('var a = 1;\n' + mapComment), 1);143144[ ''145, 'var a = 1;\n' + mapComment146, 'var a = 1;\n' + mapComment + '\nvar b = 5;\n' + mapComment147] .forEach(function (x) {148var removed = combine.removeComments(x)149t.equal(sourcemapComments(removed), 0)150})151t.end()152})153154155