-
Notifications
You must be signed in to change notification settings - Fork 680
fix(printer): map source maps to MostOriginal source to avoid out-of-range positions #1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+142
−22
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
package execute_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/execute" | ||
) | ||
|
||
// This test intentionally reproduces a panic in source-map emission during declaration emit. | ||
// It mirrors the blackbox invocation that crashes when declaration maps are enabled. | ||
// The test is expected to FAIL until the underlying bug is fixed. | ||
func TestDeclarationMap_DestructuredParam_BlackboxPanic(t *testing.T) { | ||
t.Parallel() | ||
|
||
files := FileMap{ | ||
"/home/src/workspaces/project/tsconfig.json": `{ | ||
"compilerOptions": { | ||
"declaration": true, | ||
"declarationMap": true, | ||
"emitDeclarationOnly": true, | ||
"strict": true | ||
} | ||
}`, | ||
"/home/src/workspaces/project/index.ts": `export const fn = ({ a, b }: { a: string; b: number }) => { console.log(a, b); };`, | ||
} | ||
|
||
sys := newTestSys(files, "") | ||
// Intentionally do not recover: we want the panic to crash the test to reproduce the bug. | ||
_ = execute.CommandLine(sys, []string{}, true) | ||
} | ||
|
||
|
This file contains hidden or 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 hidden or 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,66 @@ | ||
package printer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/ast" | ||
"github.com/microsoft/typescript-go/internal/core" | ||
"github.com/microsoft/typescript-go/internal/parser" | ||
"github.com/microsoft/typescript-go/internal/sourcemap" | ||
"github.com/microsoft/typescript-go/internal/tspath" | ||
) | ||
|
||
// This test intentionally reproduces the panic seen when the source map range | ||
// refers to a different file (or position space) than the file used for text lookups. | ||
// It constructs a small source file and forces a SourceMapRange with a huge Pos, | ||
// then triggers printing with source maps enabled. The buggy implementation slices | ||
// text[start:pos] with pos > len(text), panicking. | ||
func TestSourceMapMismatchPanics(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Small source text (to be the current file) | ||
sourceText := "export const x = 1;\n" | ||
sf := parser.ParseSourceFile(ast.SourceFileParseOptions{ | ||
FileName: "/home/src/workspaces/project/index.ts", | ||
Path: "/home/src/workspaces/project/index.ts", | ||
JSDocParsingMode: ast.JSDocParsingModeParseAll, | ||
}, sourceText, core.ScriptKindTS) | ||
|
||
// Choose a node to print (the first statement) | ||
if len(sf.Statements.Nodes) == 0 { | ||
t.Fatalf("expected at least one statement") | ||
} | ||
stmt := sf.Statements.Nodes[0] | ||
|
||
// Create a second, much longer file to act as the "original" mapping source | ||
longPrefix := make([]byte, 2500) | ||
for i := range longPrefix { | ||
longPrefix[i] = ' ' | ||
} | ||
longText := string(longPrefix) + "export const y = 2;\n" | ||
sf2 := parser.ParseSourceFile(ast.SourceFileParseOptions{ | ||
FileName: "/home/src/workspaces/project/long.ts", | ||
Path: "/home/src/workspaces/project/long.ts", | ||
JSDocParsingMode: ast.JSDocParsingModeParseAll, | ||
}, longText, core.ScriptKindTS) | ||
if len(sf2.Statements.Nodes) == 0 { | ||
t.Fatalf("expected at least one statement in long file") | ||
} | ||
stmt2 := sf2.Statements.Nodes[0] | ||
|
||
// Build a printer with source maps enabled | ||
emitCtx := NewEmitContext() | ||
p := NewPrinter(PrinterOptions{SourceMap: true}, PrintHandlers{}, emitCtx) | ||
writer := NewTextWriter("\n") | ||
gen := sourcemap.NewGenerator("index.js", "", "/home/src/workspaces/project", tspath.ComparePathsOptions{}) | ||
|
||
// Map the current node to the original node from the longer file. | ||
// This simulates transformation attaching original positions from a different file tree. | ||
emitCtx.SetOriginal(stmt, stmt2) | ||
emitCtx.SetSourceMapRange(stmt, stmt2.Loc) | ||
|
||
// This should not panic if source selection is correct (mapping against original file). | ||
p.Write(stmt, sf, writer, gen) | ||
} | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
os
import is added but not used in the visible code. This appears to be leftover debug code that should be removed.Copilot uses AI. Check for mistakes.