react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / convert-source-map / test / convert-source-map.js
80556 views'use strict';1/*jshint asi: true */23var test = require('tap').test4, generator = require('inline-source-map')5, convert = require('..')67var gen = generator()8.addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })9.addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 })1011, base64 = gen.base64Encode()12, comment = gen.inlineMappingUrl()13, json = gen.toString()14, obj = JSON.parse(json)1516test('different formats', function (t) {1718t.equal(convert.fromComment(comment).toComment(), comment, 'comment -> comment')19t.equal(convert.fromComment(comment).toBase64(), base64, 'comment -> base64')20t.equal(convert.fromComment(comment).toJSON(), json, 'comment -> json')21t.deepEqual(convert.fromComment(comment).toObject(), obj, 'comment -> object')2223t.equal(convert.fromBase64(base64).toBase64(), base64, 'base64 -> base64')24t.equal(convert.fromBase64(base64).toComment(), comment, 'base64 -> comment')25t.equal(convert.fromBase64(base64).toJSON(), json, 'base64 -> json')26t.deepEqual(convert.fromBase64(base64).toObject(), obj, 'base64 -> object')2728t.equal(convert.fromJSON(json).toJSON(), json, 'json -> json')29t.equal(convert.fromJSON(json).toBase64(), base64, 'json -> base64')30t.equal(convert.fromJSON(json).toComment(), comment, 'json -> comment')31t.deepEqual(convert.fromJSON(json).toObject(), obj, 'json -> object')32t.end()33})3435test('to object returns a copy', function (t) {36var c = convert.fromJSON(json)37var o = c.toObject()38o.version = '99';39t.equal(c.toObject().version, 3, 'setting property on returned object does not affect original')40t.end()41})4243test('from source', function (t) {44var foo = [45'function foo() {'46, ' console.log("hello I am foo");'47, ' console.log("who are you");'48, '}'49, ''50, 'foo();'51, ''52].join('\n')53, map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'54, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'5556function getComment(src) {57var map = convert.fromSource(src);58return map ? map.toComment() : null;59}6061t.equal(getComment(foo), null, 'no comment returns null')62t.equal(getComment(foo + map), map, 'beginning of last line')63t.equal(getComment(foo + ' ' + map), map, 'indented of last line')64t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')65t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')66t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')67t.end()68})6970test('remove comments', function (t) {71var 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// this one is old spec on purpose81, map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'82, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'83, extraCode = '\nconsole.log("more code");\nfoo()\n'8485t.equal(convert.removeComments(foo + map), foo, 'from last line')86t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')87t.equal(convert.removeComments(foo + otherMap + extraCode + map + map), foo + extraCode, 'multiple comments from the middle of code')88t.end()89})9091test('remove map file comments', function (t) {92var foo = [93'function foo() {'94, ' console.log("hello I am foo");'95, ' console.log("who are you");'96, '}'97, ''98, 'foo();'99, ''100].join('\n')101, fileMap1 = '//# sourceMappingURL=foo.js.map'102, fileMap2 = '/*# sourceMappingURL=foo.js.map */';103104t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')105t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')106t.end()107})108109test('pretty json', function (t) {110var mod = convert.fromJSON(json).toJSON(2)111, expected = JSON.stringify(obj, null, 2);112113t.equal(114mod115, expected116, 'pretty prints json when space is given')117t.end()118})119120test('adding properties', function (t) {121var mod = convert122.fromJSON(json)123.addProperty('foo', 'bar')124.toJSON()125, expected = JSON.parse(json);126expected.foo = 'bar';127t.equal(128mod129, JSON.stringify(expected)130, 'includes added property'131)132t.end()133})134135test('setting properties', function (t) {136var mod = convert137.fromJSON(json)138.setProperty('version', '2')139.setProperty('mappings', ';;;UACG')140.setProperty('should add', 'this')141.toJSON()142, expected = JSON.parse(json);143expected.version = '2';144expected.mappings = ';;;UACG';145expected['should add'] = 'this';146t.equal(147mod148, JSON.stringify(expected)149, 'includes new property and changes existing properties'150)151t.end()152})153154test('getting properties', function (t) {155var sm = convert.fromJSON(json)156157t.equal(sm.getProperty('version'), 3, 'gets version')158t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')159t.end()160})161162163