Refactor JS sourcemap generator (#21053)

* Parse the new line format

* Fix pattern

Didn't have space after the path

* Remove duplicate sources

Sources would sometimes double up with a new line at the end

* Remove unused variable

* Refactor sourcemap.nim

Removes the multiple translations needed, now goes from single high level type to the final SourceMap

Adds documentation for procs

* Line numbers line up properly now

Files aren't linking correctly though

* Files now link up correctly

Lines are sometimes off but overall seems pretty good

Just need to implement parser

* Add column info to output

Add sourceMappingURL to rope directly to prevent copy

* Properly handle columns

* Remove debug lines

* Add testcase

* Finish testcase

* Use the outdir folder instead of the folder the test is in to find the sourcemap

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
Jake Leahy 2022-12-13 15:21:51 +11:00 • committed by GitHub
commit 1fefb8e92a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 283 additions and 361 deletions

View file

@ -724,8 +724,9 @@ proc hasFrameInfo(p: PProc): bool =
((p.prc == nil) or not (sfPure in p.prc.flags))
proc lineDir(config: ConfigRef, info: TLineInfo, line: int): Rope =
ropes.`%`("/* line $2 \"$1\" */$n",
[rope(toFullPath(config, info)), rope(line)])
"/* line $2:$3 \"$1\" */$n" % [
rope(toFullPath(config, info)), rope(line), rope(info.toColumn)
]
proc genLineDir(p: PProc, n: PNode) =
let line = toLinenumber(n.info)
@ -2885,7 +2886,8 @@ proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
# Generate an optional source map.
if optSourcemap in m.config.globalOptions:
var map: SourceMap
(code, map) = genSourceMap($(code), outFile.string)
map = genSourceMap($code, outFile.string)
code &= "\n//# sourceMappingURL=$#.map" % [outFile.string]
writeFile(outFile.string & ".map", $(%map))
# Check if the generated JS code matches the output file, or else
# write it to the file.