|
| 1 | +import { getMetroTransformer } from "../getMetroTransformer" |
| 2 | +import { |
| 3 | + convertMetroRawSourceMapToStandardSourceMap, |
| 4 | + MetroRawSourceMap, |
| 5 | + composeSourceMaps, |
| 6 | +} from "../composeSourceMaps" |
| 7 | +import * as fs from "fs" |
| 8 | +import * as path from "path" |
| 9 | +import { SourceMapConsumer } from "source-map/source-map" |
| 10 | +import { getPositionOfSubstring } from "./getPositionOfSubstring" |
| 11 | +import * as ts from "typescript" |
| 12 | + |
| 13 | +const numberedLines = fs |
| 14 | + .readFileSync(require.resolve("./files/numberedLines.js")) |
| 15 | + .toString() |
| 16 | + |
| 17 | +describe("convertMetroRawSourceMapToStandardSourceMap", () => { |
| 18 | + it("takes a raw source map and converts it to a non-raw source map", () => { |
| 19 | + const transformer = getMetroTransformer(47) |
| 20 | + |
| 21 | + const { map, code } = transformer.transform({ |
| 22 | + filename: require.resolve("./files/numberedLines.js"), |
| 23 | + src: numberedLines, |
| 24 | + options: { |
| 25 | + retainLines: false, |
| 26 | + }, |
| 27 | + }) |
| 28 | + |
| 29 | + if (typeof code !== "string") { |
| 30 | + // use this rather than expect for typescript's sake |
| 31 | + throw new Error("code must be a string") |
| 32 | + } |
| 33 | + |
| 34 | + expect(Array.isArray(map)).toBe(true) |
| 35 | + expect(map).toMatchSnapshot() |
| 36 | + |
| 37 | + const standardMap = convertMetroRawSourceMapToStandardSourceMap( |
| 38 | + map as MetroRawSourceMap, |
| 39 | + path.join(__dirname, "numberedLines.js"), |
| 40 | + numberedLines, |
| 41 | + ) |
| 42 | + |
| 43 | + const standardMapConsumer = new SourceMapConsumer(standardMap as any) // upstream types are wrong |
| 44 | + |
| 45 | + for (const substring of ["line1", "line2", "line3", "line5"]) |
| 46 | + expect( |
| 47 | + standardMapConsumer.originalPositionFor( |
| 48 | + getPositionOfSubstring(code, substring)!, |
| 49 | + ), |
| 50 | + ).toMatchObject(getPositionOfSubstring(numberedLines, substring)!) |
| 51 | + |
| 52 | + expect(standardMap).toMatchSnapshot() |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe("composeSourceMaps", () => { |
| 57 | + it("composes two source maps together", () => { |
| 58 | + const filename = require.resolve("./files/hello.ts") |
| 59 | + const hello = fs.readFileSync(filename).toString() |
| 60 | + |
| 61 | + const tsTranspileResult = ts.transpileModule(hello, { |
| 62 | + fileName: filename, |
| 63 | + compilerOptions: { |
| 64 | + sourceMap: true, |
| 65 | + target: ts.ScriptTarget.ES2015, |
| 66 | + }, |
| 67 | + }) |
| 68 | + |
| 69 | + expect(tsTranspileResult.outputText).toMatchSnapshot() |
| 70 | + |
| 71 | + const upstreamTransformResult = getMetroTransformer(47).transform({ |
| 72 | + filename, |
| 73 | + src: tsTranspileResult.outputText, |
| 74 | + options: { |
| 75 | + retainLines: false, |
| 76 | + }, |
| 77 | + }) |
| 78 | + |
| 79 | + expect(upstreamTransformResult.code).toMatchSnapshot() |
| 80 | + |
| 81 | + const composedMap = new SourceMapConsumer(composeSourceMaps( |
| 82 | + tsTranspileResult.sourceMapText!, |
| 83 | + convertMetroRawSourceMapToStandardSourceMap( |
| 84 | + upstreamTransformResult.map as MetroRawSourceMap, |
| 85 | + filename, |
| 86 | + hello, |
| 87 | + ), |
| 88 | + filename, |
| 89 | + hello, |
| 90 | + ) as any) // upstream types are wrong |
| 91 | + |
| 92 | + expect( |
| 93 | + composedMap.originalPositionFor( |
| 94 | + getPositionOfSubstring(upstreamTransformResult.code!, "line6")!, |
| 95 | + ), |
| 96 | + ).toMatchObject({ line: 6 }) |
| 97 | + |
| 98 | + expect( |
| 99 | + composedMap.originalPositionFor( |
| 100 | + getPositionOfSubstring(upstreamTransformResult.code!, "line8")!, |
| 101 | + ), |
| 102 | + ).toMatchObject({ line: 8 }) |
| 103 | + }) |
| 104 | +}) |
0 commit comments