Skip to content

Commit 1da2605

Browse files
authored
Add support for SourceMap emit and baselines (#773)
1 parent 353bc12 commit 1da2605

File tree

950 files changed

+229355
-1165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

950 files changed

+229355
-1165
lines changed

internal/ast/ast.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9684,7 +9684,7 @@ type SourceFile struct {
96849684

96859685
// Fields set by NewSourceFile
96869686

9687-
Text string
9687+
text string
96889688
fileName string
96899689
path tspath.Path
96909690
Statements *NodeList // NodeList[*Statement]
@@ -9750,14 +9750,18 @@ func (f *NodeFactory) NewSourceFile(text string, fileName string, path tspath.Pa
97509750
}
97519751

97529752
data := &SourceFile{}
9753-
data.Text = text
9753+
data.text = text
97549754
data.fileName = fileName
97559755
data.path = path
97569756
data.Statements = statements
97579757
data.LanguageVersion = core.ScriptTargetLatest
97589758
return newNode(KindSourceFile, data, f.hooks)
97599759
}
97609760

9761+
func (node *SourceFile) Text() string {
9762+
return node.text
9763+
}
9764+
97619765
func (node *SourceFile) FileName() string {
97629766
return node.fileName
97639767
}
@@ -9830,7 +9834,7 @@ func (node *SourceFile) copyFrom(other *SourceFile) {
98309834
}
98319835

98329836
func (node *SourceFile) Clone(f *NodeFactory) *Node {
9833-
updated := f.NewSourceFile(node.Text, node.FileName(), node.Path(), node.Statements)
9837+
updated := f.NewSourceFile(node.Text(), node.FileName(), node.Path(), node.Statements)
98349838
newFile := updated.AsSourceFile()
98359839
newFile.copyFrom(node)
98369840
return cloneNode(updated, node.AsNode(), f.hooks)
@@ -9842,7 +9846,7 @@ func (node *SourceFile) computeSubtreeFacts() SubtreeFacts {
98429846

98439847
func (f *NodeFactory) UpdateSourceFile(node *SourceFile, statements *StatementList) *Node {
98449848
if statements != node.Statements {
9845-
updated := f.NewSourceFile(node.Text, node.fileName, node.path, statements).AsSourceFile()
9849+
updated := f.NewSourceFile(node.Text(), node.fileName, node.path, statements).AsSourceFile()
98469850
updated.copyFrom(node)
98479851
return updateNode(updated.AsNode(), node.AsNode(), f.hooks)
98489852
}
@@ -9858,7 +9862,7 @@ func (node *SourceFile) LineMap() []core.TextPos {
98589862
defer node.lineMapMu.Unlock()
98599863
lineMap = node.lineMap
98609864
if lineMap == nil {
9861-
lineMap = core.ComputeLineStarts(node.Text)
9865+
lineMap = core.ComputeLineStarts(node.Text())
98629866
node.lineMap = lineMap
98639867
}
98649868
}
@@ -9909,6 +9913,11 @@ func IsSourceFile(node *Node) bool {
99099913
return node.Kind == KindSourceFile
99109914
}
99119915

9916+
type SourceFileLike interface {
9917+
Text() string
9918+
LineMap() []core.TextPos
9919+
}
9920+
99129921
type CommentRange struct {
99139922
core.TextRange
99149923
Kind Kind

internal/ast/utilities.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,10 @@ func IsJsonSourceFile(file *SourceFile) bool {
16751675
return file.ScriptKind == core.ScriptKindJSON
16761676
}
16771677

1678+
func IsInJsonFile(node *Node) bool {
1679+
return node.Flags&NodeFlagsJsonFile != 0
1680+
}
1681+
16781682
func GetExternalModuleName(node *Node) *Expression {
16791683
switch node.Kind {
16801684
case KindImportDeclaration:
@@ -2534,7 +2538,7 @@ func ForEachDynamicImportOrRequireCall(
25342538
cb func(node *Node, argument *Expression) bool,
25352539
) bool {
25362540
isJavaScriptFile := IsInJSFile(file.AsNode())
2537-
lastIndex, size := findImportOrRequire(file.Text, 0)
2541+
lastIndex, size := findImportOrRequire(file.Text(), 0)
25382542
for lastIndex >= 0 {
25392543
node := GetNodeAtPosition(file, lastIndex, isJavaScriptFile && includeTypeSpaceImports)
25402544
if isJavaScriptFile && IsRequireCall(node, requireStringLiteralLikeArgument) {
@@ -2559,7 +2563,7 @@ func ForEachDynamicImportOrRequireCall(
25592563
}
25602564
// skip past import/require
25612565
lastIndex += size
2562-
lastIndex, size = findImportOrRequire(file.Text, lastIndex)
2566+
lastIndex, size = findImportOrRequire(file.Text(), lastIndex)
25632567
}
25642568
return false
25652569
}

internal/astnav/tokens_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func writeRangeDiff(output *strings.Builder, file *ast.SourceFile, diff tokenDif
261261
line = skipTo
262262
}
263263
output.WriteString(fmt.Sprintf("%*d │", digits, line+1))
264-
end := len(file.Text) + 1
264+
end := len(file.Text()) + 1
265265
if line < len(lines)-1 {
266266
end = int(lines[line+1])
267267
}
@@ -286,8 +286,8 @@ func writeRangeDiff(output *strings.Builder, file *ast.SourceFile, diff tokenDif
286286
output.WriteString("〚")
287287
}
288288

289-
if pos < len(file.Text) {
290-
output.WriteByte(file.Text[pos])
289+
if pos < len(file.Text()) {
290+
output.WriteByte(file.Text()[pos])
291291
}
292292
}
293293
}

internal/binder/binder.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,7 +2786,7 @@ func isEffectiveModuleDeclaration(node *ast.Node) bool {
27862786
}
27872787

27882788
func getErrorRangeForArrowFunction(sourceFile *ast.SourceFile, node *ast.Node) core.TextRange {
2789-
pos := scanner.SkipTrivia(sourceFile.Text, node.Pos())
2789+
pos := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
27902790
body := node.AsArrowFunction().Body
27912791
if body != nil && body.Kind == ast.KindBlock {
27922792
startLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, body.Pos())
@@ -2804,8 +2804,8 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
28042804
errorNode := node
28052805
switch node.Kind {
28062806
case ast.KindSourceFile:
2807-
pos := scanner.SkipTrivia(sourceFile.Text, 0)
2808-
if pos == len(sourceFile.Text) {
2807+
pos := scanner.SkipTrivia(sourceFile.Text(), 0)
2808+
if pos == len(sourceFile.Text()) {
28092809
return core.NewTextRange(0, 0)
28102810
}
28112811
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
@@ -2818,18 +2818,18 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
28182818
case ast.KindArrowFunction:
28192819
return getErrorRangeForArrowFunction(sourceFile, node)
28202820
case ast.KindCaseClause, ast.KindDefaultClause:
2821-
start := scanner.SkipTrivia(sourceFile.Text, node.Pos())
2821+
start := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
28222822
end := node.End()
28232823
statements := node.AsCaseOrDefaultClause().Statements.Nodes
28242824
if len(statements) != 0 {
28252825
end = statements[0].Pos()
28262826
}
28272827
return core.NewTextRange(start, end)
28282828
case ast.KindReturnStatement, ast.KindYieldExpression:
2829-
pos := scanner.SkipTrivia(sourceFile.Text, node.Pos())
2829+
pos := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
28302830
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
28312831
case ast.KindSatisfiesExpression:
2832-
pos := scanner.SkipTrivia(sourceFile.Text, node.AsSatisfiesExpression().Expression.End())
2832+
pos := scanner.SkipTrivia(sourceFile.Text(), node.AsSatisfiesExpression().Expression.End())
28332833
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
28342834
case ast.KindConstructor:
28352835
scanner := scanner.GetScannerForSourceFile(sourceFile, node.Pos())
@@ -2840,7 +2840,7 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
28402840
return core.NewTextRange(start, scanner.TokenEnd())
28412841
// !!!
28422842
// case KindJSDocSatisfiesTag:
2843-
// pos := scanner.SkipTrivia(sourceFile.text, node.tagName.pos)
2843+
// pos := scanner.SkipTrivia(sourceFile.Text(), node.tagName.pos)
28442844
// return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
28452845
}
28462846
if errorNode == nil {
@@ -2850,7 +2850,7 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
28502850
}
28512851
pos := errorNode.Pos()
28522852
if !ast.NodeIsMissing(errorNode) {
2853-
pos = scanner.SkipTrivia(sourceFile.Text, pos)
2853+
pos = scanner.SkipTrivia(sourceFile.Text(), pos)
28542854
}
28552855
return core.NewTextRange(pos, errorNode.End())
28562856
}

internal/checker/checker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,7 +2691,7 @@ func (c *Checker) checkTypeReferenceNode(node *ast.Node) {
26912691
// If there was a token between the type name and the type arguments, check if it was a DotToken
26922692
sourceFile := ast.GetSourceFileOfNode(node)
26932693
if scanner.ScanTokenAtPosition(sourceFile, data.TypeName.End()) == ast.KindDotToken {
2694-
c.grammarErrorAtPos(node, scanner.SkipTrivia(sourceFile.Text, data.TypeName.End()), 1, diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments)
2694+
c.grammarErrorAtPos(node, scanner.SkipTrivia(sourceFile.Text(), data.TypeName.End()), 1, diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments)
26952695
}
26962696
}
26972697
}
@@ -8035,7 +8035,7 @@ func (c *Checker) resolveCallExpression(node *ast.Node, candidatesOutArray *[]*S
80358035
} else {
80368036
var relatedInformation *ast.Diagnostic
80378037
if len(node.Arguments()) == 1 {
8038-
text := ast.GetSourceFileOfNode(node).Text
8038+
text := ast.GetSourceFileOfNode(node).Text()
80398039
options := scanner.SkipTriviaOptions{StopAfterLineBreak: true}
80408040
if stringutil.IsLineBreak(rune(text[scanner.SkipTriviaEx(text, node.Expression().End(), &options)-1])) {
80418041
relatedInformation = createDiagnosticForNode(node.Expression(), diagnostics.Are_you_missing_a_semicolon)
@@ -9292,7 +9292,7 @@ func (c *Checker) getArgumentArityError(node *ast.Node, signatures []*Signature,
92929292
return diagnostic
92939293
default:
92949294
sourceFile := ast.GetSourceFileOfNode(node)
9295-
pos := scanner.SkipTrivia(sourceFile.Text, args[maxCount].Pos())
9295+
pos := scanner.SkipTrivia(sourceFile.Text(), args[maxCount].Pos())
92969296
end := args[len(args)-1].End()
92979297
if end == pos {
92989298
end++
@@ -10039,7 +10039,7 @@ func (c *Checker) getInstantiationExpressionType(exprType *Type, node *ast.Node)
1003910039
}
1004010040
if errorType != nil {
1004110041
sourceFile := ast.GetSourceFileOfNode(node)
10042-
loc := core.NewTextRange(scanner.SkipTrivia(sourceFile.Text, typeArguments.Pos()), typeArguments.End())
10042+
loc := core.NewTextRange(scanner.SkipTrivia(sourceFile.Text(), typeArguments.Pos()), typeArguments.End())
1004310043
c.diagnostics.Add(ast.NewDiagnostic(sourceFile, loc, diagnostics.Type_0_has_no_signatures_for_which_the_type_argument_list_is_applicable, c.TypeToString(errorType)))
1004410044
}
1004510045
return result
@@ -11773,7 +11773,7 @@ func (c *Checker) checkBinaryLikeExpression(left *ast.Node, operatorToken *ast.N
1177311773
case ast.KindCommaToken:
1177411774
if !c.compilerOptions.AllowUnreachableCode.IsTrue() && c.isSideEffectFree(left) && !c.isIndirectCall(left.Parent) {
1177511775
sf := ast.GetSourceFileOfNode(left)
11776-
start := scanner.SkipTrivia(sf.Text, left.Pos())
11776+
start := scanner.SkipTrivia(sf.Text(), left.Pos())
1177711777
isInDiag2657 := core.Some(sf.Diagnostics(), func(d *ast.Diagnostic) bool {
1177811778
if d.Code() != diagnostics.JSX_expressions_must_have_one_parent_element.Code() {
1177911779
return false

internal/checker/grammarchecks.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ func (c *Checker) checkGrammarForDisallowedTrailingComma(list *ast.NodeList, dia
687687
func (c *Checker) checkGrammarTypeParameterList(typeParameters *ast.NodeList, file *ast.SourceFile) bool {
688688
if typeParameters != nil && len(typeParameters.Nodes) == 0 {
689689
start := typeParameters.Pos() - len("<")
690-
end := scanner.SkipTrivia(file.Text, typeParameters.End()) + len(">")
690+
end := scanner.SkipTrivia(file.Text(), typeParameters.End()) + len(">")
691691
return c.grammarErrorAtPos(file.AsNode(), start, end-start, diagnostics.Type_parameter_list_cannot_be_empty)
692692
}
693693
return false
@@ -857,7 +857,7 @@ func (c *Checker) checkGrammarForAtLeastOneTypeArgument(node *ast.Node, typeArgu
857857
if typeArguments != nil && len(typeArguments.Nodes) == 0 {
858858
sourceFile := ast.GetSourceFileOfNode(node)
859859
start := typeArguments.Pos() - len("<")
860-
end := scanner.SkipTrivia(sourceFile.Text, typeArguments.End()) + len(">")
860+
end := scanner.SkipTrivia(sourceFile.Text(), typeArguments.End()) + len(">")
861861
return c.grammarErrorAtPos(sourceFile.AsNode(), start, end-start, diagnostics.Type_argument_list_cannot_be_empty)
862862
}
863863
return false
@@ -1855,7 +1855,7 @@ func (c *Checker) checkGrammarConstructorTypeParameters(node *ast.ConstructorDec
18551855
if range_.Pos() == range_.End() {
18561856
pos = range_.Pos()
18571857
} else {
1858-
pos = scanner.SkipTrivia(ast.GetSourceFileOfNode(node.AsNode()).Text, range_.Pos())
1858+
pos = scanner.SkipTrivia(ast.GetSourceFileOfNode(node.AsNode()).Text(), range_.Pos())
18591859
}
18601860
return c.grammarErrorAtPos(node.AsNode(), pos, range_.End()-pos, diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration)
18611861
}

internal/checker/printer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ func (p *Printer) printSourceFileWithTypes(sourceFile *ast.SourceFile) {
647647
var typesPrinted bool
648648
lineStarts := scanner.GetLineStarts(sourceFile)
649649
printLinesBefore := func(node *ast.Node) {
650-
line := scanner.ComputeLineOfPosition(lineStarts, scanner.SkipTrivia(sourceFile.Text, node.Pos()))
650+
line := scanner.ComputeLineOfPosition(lineStarts, scanner.SkipTrivia(sourceFile.Text(), node.Pos()))
651651
var nextLineStart int
652652
if line+1 < len(lineStarts) {
653653
nextLineStart = int(lineStarts[line+1])
@@ -658,7 +658,7 @@ func (p *Printer) printSourceFileWithTypes(sourceFile *ast.SourceFile) {
658658
if typesPrinted {
659659
p.print("\n")
660660
}
661-
p.print(sourceFile.Text[pos:nextLineStart])
661+
p.print(sourceFile.Text()[pos:nextLineStart])
662662
pos = nextLineStart
663663
typesPrinted = false
664664
}
@@ -681,7 +681,7 @@ func (p *Printer) printSourceFileWithTypes(sourceFile *ast.SourceFile) {
681681
return node.ForEachChild(visit)
682682
}
683683
visit(sourceFile.AsNode())
684-
p.print(sourceFile.Text[pos:sourceFile.End()])
684+
p.print(sourceFile.Text()[pos:sourceFile.End()])
685685
}
686686

687687
func (c *Checker) getTextAndTypeOfNode(node *ast.Node) (string, *Type, bool) {

internal/checker/utilities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ var getFeatureMap = sync.OnceValue(func() map[string][]FeatureMapEntry {
20902090
})
20912091

20922092
func rangeOfTypeParameters(sourceFile *ast.SourceFile, typeParameters *ast.NodeList) core.TextRange {
2093-
return core.NewTextRange(typeParameters.Pos()-1, min(len(sourceFile.Text), scanner.SkipTrivia(sourceFile.Text, typeParameters.End())+1))
2093+
return core.NewTextRange(typeParameters.Pos()-1, min(len(sourceFile.Text()), scanner.SkipTrivia(sourceFile.Text(), typeParameters.End())+1))
20942094
}
20952095

20962096
func tryGetPropertyAccessOrIdentifierToString(expr *ast.Node) string {

0 commit comments

Comments
 (0)