| 1 |
const test = require('tape') |
| 2 |
|
| 3 |
const merge = require('../') |
| 4 |
|
| 5 |
const fs = require('fs') |
| 6 |
const resolve = require('path').resolve |
| 7 |
|
| 8 |
const babel = require('babel-core') |
| 9 |
const SourceMapConsumer = require('source-map').SourceMapConsumer |
| 10 |
|
| 11 |
const doubleLineTransform = require('./transforms/tr-double-line') |
| 12 |
const insertEmptyStatementTransform = require('./transforms/tr-insert-empty-statement') |
| 13 |
|
| 14 |
test('undefined args should not effect results', function(t) { |
| 15 |
const code = 'y = x => x * 2' |
| 16 |
const transformed = babel.transform(code, { |
| 17 |
filename: 'source.js', |
| 18 |
plugins: ['transform-es2015-spread'], |
| 19 |
sourceMaps: true |
| 20 |
}) |
| 21 |
|
| 22 |
const map = transformed.map |
| 23 |
t.equal(map, merge(undefined, map)) |
| 24 |
t.equal(map, merge(map, undefined)) |
| 25 |
t.end() |
| 26 |
}) |
| 27 |
|
| 28 |
test('multi transform with CoffeeScript and doubleLineTransform', function(t) { |
| 29 |
// first transform from CofeeeScript to JavaScript (assumed pre-processed) |
| 30 |
const coffeeCompiledCode = fs.readFileSync(resolve(__dirname, './fixtures/coffee-script/expected.js'), 'utf-8') |
| 31 |
const coffeeCompiledMap = fs.readFileSync(resolve(__dirname, './fixtures/coffee-script/expected.js.map'), 'utf-8') |
| 32 |
|
| 33 |
// second transform with `doubleLineTransform` |
| 34 |
const doubleLineTransformed = doubleLineTransform(coffeeCompiledCode, 'coffeeCompiled.js') |
| 35 |
|
| 36 |
// exercise: merge maps of `coffeeCompiled` and `doubleLineTransformed` |
| 37 |
const mergedMap = merge(coffeeCompiledMap, doubleLineTransformed.map.toJSON()) |
| 38 |
|
| 39 |
// logging... |
| 40 |
// console.log('# 1') |
| 41 |
// new SourceMapConsumer(coffeeCompiledMap).eachMapping(m => console.log(JSON.stringify(m))) |
| 42 |
// console.log('# 2') |
| 43 |
// new SourceMapConsumer(doubleLineTransformed.map.toJSON()).eachMapping(m => console.log(JSON.stringify(m))) |
| 44 |
// console.log('# 3') |
| 45 |
// new SourceMapConsumer(mergedMap).eachMapping(m => console.log(JSON.stringify(m))) |
| 46 |
|
| 47 |
// verify |
| 48 |
t.equal(doubleLineTransformed.code, fs.readFileSync(resolve(__dirname, './fixtures/coffee-script/actual.js'), 'utf-8')) |
| 49 |
|
| 50 |
const con = new SourceMapConsumer(mergedMap) |
| 51 |
for (var i = 0; i < 2; i++) { |
| 52 |
const offset = i * 17 |
| 53 |
t.deepEqual( |
| 54 |
con.originalPositionFor({ line: 2 + offset, column: 8 }), |
| 55 |
{ column: 0, line: 1, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 56 |
'pos of `var *a* = b`' |
| 57 |
) |
| 58 |
t.deepEqual( |
| 59 |
con.originalPositionFor({ line: 2 + offset, column: 11 }), |
| 60 |
{ column: 0, line: 1, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 61 |
'pos of `var *a* = b`' |
| 62 |
) |
| 63 |
t.deepEqual( |
| 64 |
con.originalPositionFor({ line: 3 + offset, column: 4 }), |
| 65 |
{ column: 0, line: 1, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 66 |
'pos of `var *a* = b`' |
| 67 |
) |
| 68 |
t.deepEqual( |
| 69 |
con.originalPositionFor({ line: 4 + offset, column: 4 }), |
| 70 |
{ column: 0, line: 1, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 71 |
'pos of `var *a* = b`' |
| 72 |
) |
| 73 |
t.deepEqual( |
| 74 |
con.originalPositionFor({ line: 5 + offset, column: 4 }), |
| 75 |
{ column: 0, line: 2, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 76 |
'pos of `*b* = function (arg1, arg2)`' |
| 77 |
) |
| 78 |
t.deepEqual( |
| 79 |
con.originalPositionFor({ line: 5 + offset, column: 18 }), |
| 80 |
{ column: 5, line: 2, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 81 |
'pos of `b = function (*arg1*, arg2)`' |
| 82 |
) |
| 83 |
t.deepEqual( |
| 84 |
con.originalPositionFor({ line: 6 + offset, column: 8 }), |
| 85 |
{ column: 2, line: 3, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 86 |
'pos of `*console*.log(arg1)`' |
| 87 |
) |
| 88 |
t.deepEqual( |
| 89 |
con.originalPositionFor({ line: 6 + offset, column: 20 }), |
| 90 |
{ column: 14, line: 3, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 91 |
'pos of `*console*.log(arg1)`' |
| 92 |
) |
| 93 |
t.deepEqual( |
| 94 |
con.originalPositionFor({ line: 7 + offset, column: 8 }), |
| 95 |
{ column: 2, line: 3, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 96 |
'pos of `*console*.log(arg1)`' |
| 97 |
) |
| 98 |
t.deepEqual( |
| 99 |
con.originalPositionFor({ line: 7 + offset, column: 20 }), |
| 100 |
{ column: 14, line: 3, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 101 |
'pos of `*console*.log(arg1)`' |
| 102 |
) |
| 103 |
t.deepEqual( |
| 104 |
con.originalPositionFor({ line: 8 + offset, column: 8 }), |
| 105 |
{ column: 2, line: 5, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 106 |
'pos of `*console*.log(arg1)`' |
| 107 |
) |
| 108 |
t.deepEqual( |
| 109 |
con.originalPositionFor({ line: 8 + offset, column: 20 }), |
| 110 |
{ column: 14, line: 5, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 111 |
'pos of `*console*.log(arg1)`' |
| 112 |
) |
| 113 |
t.deepEqual( |
| 114 |
con.originalPositionFor({ line: 9 + offset, column: 8 }), |
| 115 |
{ column: 2, line: 5, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 116 |
'pos of `*console*.log(arg1)`' |
| 117 |
) |
| 118 |
t.deepEqual( |
| 119 |
con.originalPositionFor({ line: 9 + offset, column: 20 }), |
| 120 |
{ column: 14, line: 5, name: null, source: '../../../test/fixtures/coffee-script/expected.coffee' }, |
| 121 |
'pos of `*console*.log(arg1)`' |
| 122 |
) |
| 123 |
} |
| 124 |
t.end() |
| 125 |
}) |
| 126 |
|
| 127 |
test('multi transform with es2015-arrow and es2015-spread on babel', function(t) { |
| 128 |
|
| 129 |
// source.js -> resultOfArrow.js |
| 130 |
const resultOfArrow = babel.transform(fs.readFileSync(resolve(__dirname, './fixtures/es2015/expected.js')), { |
| 131 |
filename: 'source.js', |
| 132 |
plugins: ['transform-es2015-arrow-functions'], |
| 133 |
sourceMaps: true, |
| 134 |
}) |
| 135 |
// logging... |
| 136 |
// console.log('# arrow') |
| 137 |
// const arrowMapConsumer = new SourceMapConsumer(resultOfArrow.map) |
| 138 |
// arrowMapConsumer.eachMapping(m => console.log(JSON.stringify(m))) |
| 139 |
// console.log(resultOfArrow.code) |
| 140 |
|
| 141 |
// resultOfArrow.js -> resultOfAdditionalSpread.js |
| 142 |
const resultOfAdditionalSpread = babel.transform(resultOfArrow.code, { |
| 143 |
filename: 'resultOfArrow.js', |
| 144 |
plugins: ['transform-es2015-spread'], |
| 145 |
sourceMaps: true |
| 146 |
}) |
| 147 |
// logging... |
| 148 |
// console.log('# additionalSpread') |
| 149 |
// const additionalSpreadMapConsumer = new SourceMapConsumer(resultOfAdditionalSpread.map) |
| 150 |
// additionalSpreadMapConsumer.eachMapping(m => console.log(JSON.stringify(m))) |
| 151 |
// console.log(resultOfAdditionalSpread.code) |
| 152 |
|
| 153 |
// exercise: merge maps of `resultOfArrow` and `resultOfAdditionalSpread |
| 154 |
const mergedMap = merge(resultOfArrow.map, resultOfAdditionalSpread.map) |
| 155 |
const mergedMapConsumer = new SourceMapConsumer(mergedMap) |
| 156 |
|
| 157 |
// logging... |
| 158 |
// console.log('# merged') |
| 159 |
// mergedMapConsumer.eachMapping(m => console.log(JSON.stringify(m))) |
| 160 |
|
| 161 |
// actual map with babel in same time transform |
| 162 |
const resultOfArrowAndSpreadAtSameTime = babel.transform(fs.readFileSync(resolve(__dirname, './fixtures/es2015/expected.js')), { |
| 163 |
filename: 'source.js', |
| 164 |
plugins: ['transform-es2015-arrow-functions', 'transform-es2015-spread'], |
| 165 |
sourceMaps: true |
| 166 |
}) |
| 167 |
const sameTimeMapConsumer = new SourceMapConsumer(resultOfArrowAndSpreadAtSameTime.map) |
| 168 |
// logging... |
| 169 |
// console.log('# actual with babel') |
| 170 |
// sameTimeMapConsumer.eachMapping(m => console.log(JSON.stringify(m))) |
| 171 |
// console.log(resultOfArrowAndSpreadAtSameTime.code) |
| 172 |
|
| 173 |
// verify |
| 174 |
const actualCode = fs.readFileSync(resolve(__dirname, './fixtures/es2015/actual.js'), 'utf-8') |
| 175 |
t.equal(resultOfAdditionalSpread.code, actualCode) |
| 176 |
t.equal(resultOfArrowAndSpreadAtSameTime.code, actualCode) |
| 177 |
|
| 178 |
// mappings by merge-soource-source-map, filtered by having `name` property |
| 179 |
const mergedMappings = [] |
| 180 |
mergedMapConsumer.eachMapping(function(m) { m.name && mergedMappings.push(m) }) |
| 181 |
|
| 182 |
// mappings by babel, filtered by having `name` property |
| 183 |
const sameTimeMappings = [] |
| 184 |
sameTimeMapConsumer.eachMapping(function(m) { m.name && sameTimeMappings.push(m) }) |
| 185 |
|
| 186 |
t.deepEqual(mergedMappings, sameTimeMappings) |
| 187 |
t.end() |
| 188 |
}) |
| 189 |
|
| 190 |
test('handle original position of code that does not have an origin', function(t) { |
| 191 |
const origCode = 'a = b' |
| 192 |
|
| 193 |
// transform to insert empty statement |
| 194 |
const transformed1 = insertEmptyStatementTransform(origCode, 'source.js') |
| 195 |
t.equal(transformed1.code, 'a = b;\n;') |
| 196 |
|
| 197 |
// transform to insert empty statement (x2) |
| 198 |
const transformed2 = insertEmptyStatementTransform(transformed1.code, 'transformed1.js') |
| 199 |
t.equal(transformed2.code, 'a = b;\n;\n;') |
| 200 |
|
| 201 |
// verify |
| 202 |
const mergedMap = merge(transformed1.map.toJSON(), transformed2.map.toJSON()) |
| 203 |
const con = new SourceMapConsumer(mergedMap) |
| 204 |
var origPos |
| 205 |
|
| 206 |
// pos for variable `a` |
| 207 |
origPos = con.originalPositionFor({line: 1, column: 0}) |
| 208 |
t.deepEqual(origPos, {line: 1, column: 0, name: 'a', source: 'source.js'}) |
| 209 |
|
| 210 |
// pos for variable `b` |
| 211 |
origPos = con.originalPositionFor({line: 1, column: 4}) |
| 212 |
t.deepEqual(origPos, {line: 1, column: 4, name: 'b', source: 'source.js'}) |
| 213 |
|
| 214 |
t.end() |
| 215 |
}) |
| 216 |
|
| 217 |
test('handle original position of code that does not have an origin (with compact format)', function(t) { |
| 218 |
const origCode = 'a = b' |
| 219 |
|
| 220 |
// transform to insert empty statement |
| 221 |
const transformed1 = insertEmptyStatementTransform(origCode, 'source.js', true) |
| 222 |
t.equal(transformed1.code, 'a=b;;') |
| 223 |
|
| 224 |
// transform to insert empty statement (x2) |
| 225 |
const transformed2 = insertEmptyStatementTransform(transformed1.code, 'transformed1.js', true) |
| 226 |
t.equal(transformed2.code, 'a=b;;;') |
| 227 |
|
| 228 |
const mergedMap = merge(transformed1.map.toJSON(), transformed2.map.toJSON()) |
| 229 |
const con = new SourceMapConsumer(mergedMap) |
| 230 |
var origPos |
| 231 |
|
| 232 |
// pos for variable `a` |
| 233 |
origPos = con.originalPositionFor({line: 1, column: 0}) |
| 234 |
t.deepEqual(origPos, {line: 1, column: 0, name: 'a', source: 'source.js'}) |
| 235 |
|
| 236 |
// pos for variable `b` |
| 237 |
origPos = con.originalPositionFor({line: 1, column: 2}) |
| 238 |
t.deepEqual(origPos, {line: 1, column: 4, name: 'b', source: 'source.js'}) |
| 239 |
|
| 240 |
t.end() |
| 241 |
}) |