Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to visualize source maps in tests
- Loading branch information
1 parent
2c07574
commit 9606dd1
Showing
7 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
packages/babel-core/test/fixtures/transformation/source-maps/arrow-function/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"plugins": ["transform-arrow-functions", "transform-function-name"], | ||
"sourceMaps": true | ||
"sourceMaps": true, | ||
"sourceMapVisual": true | ||
} |
41 changes: 41 additions & 0 deletions
41
.../babel-core/test/fixtures/transformation/source-maps/arrow-function/source-map-visual.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(1:0) var t <-- (1:0) var t | ||
^ ^ | ||
|
||
(1:4) var t = x <-- (1:4) var t = f | ||
^ ^ | ||
|
||
(1:5) ar t = x <-- (1:5) ar t = fu | ||
^ ^ | ||
|
||
(1:8) t = x => <-- (1:8) t = funct | ||
^ ^ | ||
|
||
(1:4) var t = x <-- (1:17) ion t(x) | ||
^ ^ | ||
|
||
(1:5) ar t = x <-- (1:18) on t(x) { | ||
^ ^ | ||
|
||
(1:8) t = x => <-- (1:19) n t(x) { | ||
^ ^ | ||
|
||
(1:9) = x => x <-- (1:20) t(x) { | ||
^ ^ | ||
|
||
(1:9) = x => x <-- (2:2) retur | ||
^ ^ | ||
|
||
(1:13) => x * x <-- (2:9) urn x * x | ||
^ ^ | ||
|
||
(1:14) => x * x; <-- (2:10) rn x * x; | ||
^ ^ | ||
|
||
(1:17) x * x; <-- (2:13) x * x; | ||
^ ^ | ||
|
||
(1:18) * x; <-- (2:14) * x; | ||
^ ^ | ||
|
||
(1:18) * x; <-- (3:0) }; | ||
^ ^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/babel-helper-transform-fixture-test-runner/src/source-map-visualizer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { TraceMap, eachMapping } from "@jridgewell/trace-mapping"; | ||
|
||
const CONTEXT_SIZE = 4; | ||
const MAX_LOC_SIZE = 10; | ||
|
||
function simpleCodeFrame(lines: string[], line: number, col: number) { | ||
const start = Math.max(col - CONTEXT_SIZE, 0); | ||
const end = Math.min(col + CONTEXT_SIZE + 1, lines[line - 1].length); | ||
const marker = col - start; | ||
const code = lines[line - 1].slice(start, end); | ||
const loc = `(${line}:${col}) `.padStart(MAX_LOC_SIZE, " "); | ||
return loc + code + "\n" + " ".repeat(marker + loc.length) + "^"; | ||
} | ||
|
||
function joinMultiline(left: string, right: string, leftLen?: number) { | ||
const leftLines = left.split("\n"); | ||
const rightLines = right.split("\n"); | ||
|
||
leftLen ??= leftLines.reduce((len, line) => Math.max(len, line.length), 0); | ||
|
||
const linesCount = Math.max(leftLines.length, rightLines.length); | ||
let res = ""; | ||
for (let i = 0; i < linesCount; i++) { | ||
if (res !== "") res += "\n"; | ||
if (i < leftLines.length) res += leftLines[i].padEnd(leftLen, " "); | ||
else res += " ".repeat(leftLen); | ||
if (i < rightLines.length) res += rightLines[i]; | ||
} | ||
return res; | ||
} | ||
|
||
export default function visualize(input: string, output: string, map: any) { | ||
const inputLines = input.split("\n"); | ||
const outputLines = output.split("\n"); | ||
|
||
const res: string[] = []; | ||
eachMapping(new TraceMap(map), mapping => { | ||
const input = simpleCodeFrame( | ||
inputLines, | ||
mapping.originalLine, | ||
mapping.originalColumn, | ||
); | ||
const output = simpleCodeFrame( | ||
outputLines, | ||
mapping.generatedLine, | ||
mapping.generatedColumn, | ||
); | ||
|
||
res.push( | ||
joinMultiline( | ||
joinMultiline(input, " <-- ", MAX_LOC_SIZE + CONTEXT_SIZE * 2 + 5), | ||
output, | ||
), | ||
); | ||
}); | ||
|
||
return res.join("\n\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters