Skip to content

Commit

Permalink
work on evanw#184: instrumentation uses original line and column from…
Browse files Browse the repository at this point in the history
… sourcemaps
  • Loading branch information
stoand committed Mar 13, 2023
1 parent 8477fe6 commit 622f502
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
17 changes: 6 additions & 11 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func (p *printer) printJSXTag(tagOrNil js_ast.Expr) {
}

type printer struct {
file string
symbols js_ast.SymbolMap
isUnbound func(js_ast.Ref) bool
renamer renamer.Renamer
Expand Down Expand Up @@ -1759,17 +1760,9 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla

// findme
// instrument
// p.print(fmt.Sprintf("__INST(%d, null, ", int(expr.Loc.Start)))

// p.print(fmt.Sprintf("__INST(%07d, null, ", int(expr.Loc.Start)+4*len(p.js)+22-12))

p.addSourceMapping(expr.Loc)
// p.builder.UpdateGeneratedLineAndColumn(p.js)

var fileName = strings.Split(string(p.js), "\n")[0]

p.print(fmt.Sprintf("__INST(%d,%d, %s, ", p.builder.GetPrevGeneratedLine(),
p.builder.GetPrevGeneratedColumn(), fileName))
p.print(fmt.Sprintf("__INST(%d,%d,null,null,'%s',", p.builder.GetOriginalLine(),
p.builder.GetOriginalColumn(), p.file))

// If syntax compression is enabled, do a pre-pass over unary and binary
// operators to inline bitwise operations of cross-module inlined constants.
Expand Down Expand Up @@ -4548,8 +4541,10 @@ type PrintResult struct {
SourceMapChunk sourcemap.Chunk
}

func Print(tree js_ast.AST, symbols js_ast.SymbolMap, r renamer.Renamer, options Options) PrintResult {
// findme print
func Print(tree js_ast.AST, symbols js_ast.SymbolMap, r renamer.Renamer, options Options, file string) PrintResult {
p := &printer{
file: file,
symbols: symbols,
renamer: r,
importRecords: tree.ImportRecords,
Expand Down
8 changes: 4 additions & 4 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4118,7 +4118,7 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
tree.Directive = "" // This is handled elsewhere
tree.Parts = []js_ast.Part{{Stmts: stmts}}
*result = compileResultJS{
PrintResult: js_printer.Print(tree, c.graph.Symbols, r, printOptions),
PrintResult: js_printer.Print(tree, c.graph.Symbols, r, printOptions, file.InputFile.Source.PrettyPath),
sourceIndex: partRange.sourceIndex,
}

Expand Down Expand Up @@ -4435,7 +4435,7 @@ func (c *linkerContext) generateEntryPointTailJS(
RequireOrImportMetaForSource: c.requireOrImportMetaForSource,
MangledProps: c.mangledProps,
}
result.PrintResult = js_printer.Print(tree, c.graph.Symbols, r, printOptions)
result.PrintResult = js_printer.Print(tree, c.graph.Symbols, r, printOptions, file.InputFile.Source.PrettyPath)
return
}

Expand Down Expand Up @@ -4761,12 +4761,12 @@ func (c *linkerContext) generateChunkJS(chunkIndex int, chunkWaitGroup *sync.Wai
crossChunkResult := js_printer.Print(js_ast.AST{
ImportRecords: crossChunkImportRecords,
Parts: []js_ast.Part{{Stmts: chunkRepr.crossChunkPrefixStmts}},
}, c.graph.Symbols, r, printOptions)
}, c.graph.Symbols, r, printOptions, "")
crossChunkPrefix = crossChunkResult.JS
jsonMetadataImports = crossChunkResult.JSONMetadataImports
crossChunkSuffix = js_printer.Print(js_ast.AST{
Parts: []js_ast.Part{{Stmts: chunkRepr.crossChunkSuffixStmts}},
}, c.graph.Symbols, r, printOptions).JS
}, c.graph.Symbols, r, printOptions, "").JS
}

// Generate the exports for the entry point, if there are any
Expand Down
13 changes: 4 additions & 9 deletions internal/sourcemap/sourcemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,19 +726,14 @@ func (b *ChunkBuilder) GenerateChunk(output []byte) Chunk {
}
}

func (b *ChunkBuilder) GetPrevGeneratedLine() int {
return b.prevState.GeneratedLine
func (b *ChunkBuilder) GetOriginalLine() int {
return int(b.prevState.OriginalLine)
}

func (b *ChunkBuilder) GetPrevGeneratedColumn() int {
return b.prevState.GeneratedColumn
func (b *ChunkBuilder) GetOriginalColumn() int {
return int(b.prevState.OriginalColumn)
}

func (b *ChunkBuilder) GetPrevOriginalName() string {
return b.prevOriginalName
}


// Scan over the printed text since the last source mapping and update the
// generated line and column numbers
func (b *ChunkBuilder) updateGeneratedLineAndColumn(output []byte) {
Expand Down

0 comments on commit 622f502

Please sign in to comment.