forked from mozilla/source-map
/
test-nested-consumer-usage.js
80 lines (75 loc) · 2.05 KB
/
test-nested-consumer-usage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const {SourceMapConsumer, SourceMapGenerator} = require('../')
const tsMap = {
version: 3,
file: "blah.js",
sourceRoot: "",
sources: ["blah.tsx"],
names: [],
mappings:
";;AAKA;IACE,MAAM,CAAC,EAAC,MAAM,EAAE,SAAS,EAAC,CAAC;AAC7B,CAAC;AAFD,yBAEC",
sourcesContent: [
"\ntype Cheese = {\n readonly cheese: string\n}\n\nexport default function Cheese(): Cheese {\n return {cheese: 'stilton'};\n}\n"
]
};
const babelMap = {
version: 3,
sources: ["blah.tsx"],
names: [
"Object",
"defineProperty",
"exports",
"value",
"Cheese",
"cheese",
"default"
],
mappings:
"AAAA;;AACAA,OAAOC,cAAP,CAAsBC,OAAtB,EAA+B,YAA/B,EAA6C,EAAEC,OAAO,IAAT,EAA7C;AACA,SAASC,MAAT,GAAkB;AACd,WAAO,EAAEC,QAAQ,SAAV,EAAP;AACH;AACDH,QAAQI,OAAR,GAAkBF,MAAlB",
sourcesContent: [
'"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });\nfunction Cheese() {\n return { cheese: \'stilton\' };\n}\nexports.default = Cheese;\n//# sourceMappingURL=blah.js.map'
]
};
async function composeSourceMaps(
tsMap,
babelMap,
tsFileName,
) {
const tsConsumer = await new SourceMapConsumer(tsMap)
const babelConsumer = await new SourceMapConsumer(babelMap)
const map = new SourceMapGenerator()
babelConsumer.eachMapping(
({
source,
generatedLine,
generatedColumn,
originalLine,
originalColumn,
name,
}) => {
if (originalLine) {
const original = tsConsumer.originalPositionFor({
line: originalLine,
column: originalColumn,
})
if (original.line) {
map.addMapping({
generated: {
line: generatedLine,
column: generatedColumn,
},
original: {
line: original.line,
column: original.column,
},
source: tsFileName,
name: name,
})
}
}
}
)
return map.toJSON()
}
exports["test nested consumer usage"] = async function (assert) {
await composeSourceMaps(tsMap, babelMap, 'blah.tsx')
};