Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
19 changes: 14 additions & 5 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9652,7 +9652,7 @@ type SourceFile struct {

// Fields set by NewSourceFile

Text string
text string
fileName string
path tspath.Path
Statements *NodeList // NodeList[*Statement]
Expand Down Expand Up @@ -9718,14 +9718,18 @@ func (f *NodeFactory) NewSourceFile(text string, fileName string, path tspath.Pa
}

data := &SourceFile{}
data.Text = text
data.text = text
data.fileName = fileName
data.path = path
data.Statements = statements
data.LanguageVersion = core.ScriptTargetLatest
return newNode(KindSourceFile, data, f.hooks)
}

func (node *SourceFile) Text() string {
return node.text
}

func (node *SourceFile) FileName() string {
return node.fileName
}
Expand Down Expand Up @@ -9798,7 +9802,7 @@ func (node *SourceFile) copyFrom(other *SourceFile) {
}

func (node *SourceFile) Clone(f *NodeFactory) *Node {
updated := f.NewSourceFile(node.Text, node.FileName(), node.Path(), node.Statements)
updated := f.NewSourceFile(node.Text(), node.FileName(), node.Path(), node.Statements)
newFile := updated.AsSourceFile()
newFile.copyFrom(node)
return cloneNode(updated, node.AsNode(), f.hooks)
Expand All @@ -9810,7 +9814,7 @@ func (node *SourceFile) computeSubtreeFacts() SubtreeFacts {

func (f *NodeFactory) UpdateSourceFile(node *SourceFile, statements *StatementList) *Node {
if statements != node.Statements {
updated := f.NewSourceFile(node.Text, node.fileName, node.path, statements).AsSourceFile()
updated := f.NewSourceFile(node.Text(), node.fileName, node.path, statements).AsSourceFile()
updated.copyFrom(node)
return updateNode(updated.AsNode(), node.AsNode(), f.hooks)
}
Expand All @@ -9826,7 +9830,7 @@ func (node *SourceFile) LineMap() []core.TextPos {
defer node.lineMapMu.Unlock()
lineMap = node.lineMap
if lineMap == nil {
lineMap = core.ComputeLineStarts(node.Text)
lineMap = core.ComputeLineStarts(node.Text())
node.lineMap = lineMap
}
}
Expand Down Expand Up @@ -9877,6 +9881,11 @@ func IsSourceFile(node *Node) bool {
return node.Kind == KindSourceFile
}

type SourceFileLike interface {
Text() string
LineMap() []core.TextPos
}

type CommentRange struct {
core.TextRange
Kind Kind
Expand Down
8 changes: 6 additions & 2 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,10 @@ func IsJsonSourceFile(file *SourceFile) bool {
return file.ScriptKind == core.ScriptKindJSON
}

func IsInJsonFile(node *Node) bool {
return node.Flags&NodeFlagsJsonFile != 0
}

func GetExternalModuleName(node *Node) *Expression {
switch node.Kind {
case KindImportDeclaration:
Expand Down Expand Up @@ -2532,7 +2536,7 @@ func ForEachDynamicImportOrRequireCall(
cb func(node *Node, argument *Expression) bool,
) bool {
isJavaScriptFile := IsInJSFile(file.AsNode())
lastIndex, size := findImportOrRequire(file.Text, 0)
lastIndex, size := findImportOrRequire(file.Text(), 0)
for lastIndex >= 0 {
node := GetNodeAtPosition(file, lastIndex, isJavaScriptFile && includeTypeSpaceImports)
if isJavaScriptFile && IsRequireCall(node, requireStringLiteralLikeArgument) {
Expand All @@ -2557,7 +2561,7 @@ func ForEachDynamicImportOrRequireCall(
}
// skip past import/require
lastIndex += size
lastIndex, size = findImportOrRequire(file.Text, lastIndex)
lastIndex, size = findImportOrRequire(file.Text(), lastIndex)
}
return false
}
Expand Down
6 changes: 3 additions & 3 deletions internal/astnav/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func writeRangeDiff(output *strings.Builder, file *ast.SourceFile, diff tokenDif
line = skipTo
}
output.WriteString(fmt.Sprintf("%*d │", digits, line+1))
end := len(file.Text) + 1
end := len(file.Text()) + 1
if line < len(lines)-1 {
end = int(lines[line+1])
}
Expand All @@ -286,8 +286,8 @@ func writeRangeDiff(output *strings.Builder, file *ast.SourceFile, diff tokenDif
output.WriteString("〚")
}

if pos < len(file.Text) {
output.WriteByte(file.Text[pos])
if pos < len(file.Text()) {
output.WriteByte(file.Text()[pos])
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2786,7 +2786,7 @@ func isEffectiveModuleDeclaration(node *ast.Node) bool {
}

func getErrorRangeForArrowFunction(sourceFile *ast.SourceFile, node *ast.Node) core.TextRange {
pos := scanner.SkipTrivia(sourceFile.Text, node.Pos())
pos := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
body := node.AsArrowFunction().Body
if body != nil && body.Kind == ast.KindBlock {
startLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, body.Pos())
Expand All @@ -2804,8 +2804,8 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
errorNode := node
switch node.Kind {
case ast.KindSourceFile:
pos := scanner.SkipTrivia(sourceFile.Text, 0)
if pos == len(sourceFile.Text) {
pos := scanner.SkipTrivia(sourceFile.Text(), 0)
if pos == len(sourceFile.Text()) {
return core.NewTextRange(0, 0)
}
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
Expand All @@ -2818,18 +2818,18 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
case ast.KindArrowFunction:
return getErrorRangeForArrowFunction(sourceFile, node)
case ast.KindCaseClause, ast.KindDefaultClause:
start := scanner.SkipTrivia(sourceFile.Text, node.Pos())
start := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
end := node.End()
statements := node.AsCaseOrDefaultClause().Statements.Nodes
if len(statements) != 0 {
end = statements[0].Pos()
}
return core.NewTextRange(start, end)
case ast.KindReturnStatement, ast.KindYieldExpression:
pos := scanner.SkipTrivia(sourceFile.Text, node.Pos())
pos := scanner.SkipTrivia(sourceFile.Text(), node.Pos())
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
case ast.KindSatisfiesExpression:
pos := scanner.SkipTrivia(sourceFile.Text, node.AsSatisfiesExpression().Expression.End())
pos := scanner.SkipTrivia(sourceFile.Text(), node.AsSatisfiesExpression().Expression.End())
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
case ast.KindConstructor:
scanner := scanner.GetScannerForSourceFile(sourceFile, node.Pos())
Expand All @@ -2840,7 +2840,7 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
return core.NewTextRange(start, scanner.TokenEnd())
// !!!
// case KindJSDocSatisfiesTag:
// pos := scanner.SkipTrivia(sourceFile.text, node.tagName.pos)
// pos := scanner.SkipTrivia(sourceFile.Text(), node.tagName.pos)
// return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
}
if errorNode == nil {
Expand All @@ -2850,7 +2850,7 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
}
pos := errorNode.Pos()
if !ast.NodeIsMissing(errorNode) {
pos = scanner.SkipTrivia(sourceFile.Text, pos)
pos = scanner.SkipTrivia(sourceFile.Text(), pos)
}
return core.NewTextRange(pos, errorNode.End())
}
10 changes: 5 additions & 5 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2690,7 +2690,7 @@ func (c *Checker) checkTypeReferenceNode(node *ast.Node) {
// If there was a token between the type name and the type arguments, check if it was a DotToken
sourceFile := ast.GetSourceFileOfNode(node)
if scanner.ScanTokenAtPosition(sourceFile, data.TypeName.End()) == ast.KindDotToken {
c.grammarErrorAtPos(node, scanner.SkipTrivia(sourceFile.Text, data.TypeName.End()), 1, diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments)
c.grammarErrorAtPos(node, scanner.SkipTrivia(sourceFile.Text(), data.TypeName.End()), 1, diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments)
}
}
}
Expand Down Expand Up @@ -8034,7 +8034,7 @@ func (c *Checker) resolveCallExpression(node *ast.Node, candidatesOutArray *[]*S
} else {
var relatedInformation *ast.Diagnostic
if len(node.Arguments()) == 1 {
text := ast.GetSourceFileOfNode(node).Text
text := ast.GetSourceFileOfNode(node).Text()
options := scanner.SkipTriviaOptions{StopAfterLineBreak: true}
if stringutil.IsLineBreak(rune(text[scanner.SkipTriviaEx(text, node.Expression().End(), &options)-1])) {
relatedInformation = createDiagnosticForNode(node.Expression(), diagnostics.Are_you_missing_a_semicolon)
Expand Down Expand Up @@ -9291,7 +9291,7 @@ func (c *Checker) getArgumentArityError(node *ast.Node, signatures []*Signature,
return diagnostic
default:
sourceFile := ast.GetSourceFileOfNode(node)
pos := scanner.SkipTrivia(sourceFile.Text, args[maxCount].Pos())
pos := scanner.SkipTrivia(sourceFile.Text(), args[maxCount].Pos())
end := args[len(args)-1].End()
if end == pos {
end++
Expand Down Expand Up @@ -10038,7 +10038,7 @@ func (c *Checker) getInstantiationExpressionType(exprType *Type, node *ast.Node)
}
if errorType != nil {
sourceFile := ast.GetSourceFileOfNode(node)
loc := core.NewTextRange(scanner.SkipTrivia(sourceFile.Text, typeArguments.Pos()), typeArguments.End())
loc := core.NewTextRange(scanner.SkipTrivia(sourceFile.Text(), typeArguments.Pos()), typeArguments.End())
c.diagnostics.Add(ast.NewDiagnostic(sourceFile, loc, diagnostics.Type_0_has_no_signatures_for_which_the_type_argument_list_is_applicable, c.TypeToString(errorType)))
}
return result
Expand Down Expand Up @@ -11772,7 +11772,7 @@ func (c *Checker) checkBinaryLikeExpression(left *ast.Node, operatorToken *ast.N
case ast.KindCommaToken:
if !c.compilerOptions.AllowUnreachableCode.IsTrue() && c.isSideEffectFree(left) && !c.isIndirectCall(left.Parent) {
sf := ast.GetSourceFileOfNode(left)
start := scanner.SkipTrivia(sf.Text, left.Pos())
start := scanner.SkipTrivia(sf.Text(), left.Pos())
isInDiag2657 := core.Some(sf.Diagnostics(), func(d *ast.Diagnostic) bool {
if d.Code() != diagnostics.JSX_expressions_must_have_one_parent_element.Code() {
return false
Expand Down
6 changes: 3 additions & 3 deletions internal/checker/grammarchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func (c *Checker) checkGrammarForDisallowedTrailingComma(list *ast.NodeList, dia
func (c *Checker) checkGrammarTypeParameterList(typeParameters *ast.NodeList, file *ast.SourceFile) bool {
if typeParameters != nil && len(typeParameters.Nodes) == 0 {
start := typeParameters.Pos() - len("<")
end := scanner.SkipTrivia(file.Text, typeParameters.End()) + len(">")
end := scanner.SkipTrivia(file.Text(), typeParameters.End()) + len(">")
return c.grammarErrorAtPos(file.AsNode(), start, end-start, diagnostics.Type_parameter_list_cannot_be_empty)
}
return false
Expand Down Expand Up @@ -856,7 +856,7 @@ func (c *Checker) checkGrammarForAtLeastOneTypeArgument(node *ast.Node, typeArgu
if typeArguments != nil && len(typeArguments.Nodes) == 0 {
sourceFile := ast.GetSourceFileOfNode(node)
start := typeArguments.Pos() - len("<")
end := scanner.SkipTrivia(sourceFile.Text, typeArguments.End()) + len(">")
end := scanner.SkipTrivia(sourceFile.Text(), typeArguments.End()) + len(">")
return c.grammarErrorAtPos(sourceFile.AsNode(), start, end-start, diagnostics.Type_argument_list_cannot_be_empty)
}
return false
Expand Down Expand Up @@ -1864,7 +1864,7 @@ func (c *Checker) checkGrammarConstructorTypeParameters(node *ast.ConstructorDec
if range_.Pos() == range_.End() {
pos = range_.Pos()
} else {
pos = scanner.SkipTrivia(ast.GetSourceFileOfNode(node.AsNode()).Text, range_.Pos())
pos = scanner.SkipTrivia(ast.GetSourceFileOfNode(node.AsNode()).Text(), range_.Pos())
}
return c.grammarErrorAtPos(node.AsNode(), pos, range_.End()-pos, diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/checker/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func (p *Printer) printSourceFileWithTypes(sourceFile *ast.SourceFile) {
var typesPrinted bool
lineStarts := scanner.GetLineStarts(sourceFile)
printLinesBefore := func(node *ast.Node) {
line := scanner.ComputeLineOfPosition(lineStarts, scanner.SkipTrivia(sourceFile.Text, node.Pos()))
line := scanner.ComputeLineOfPosition(lineStarts, scanner.SkipTrivia(sourceFile.Text(), node.Pos()))
var nextLineStart int
if line+1 < len(lineStarts) {
nextLineStart = int(lineStarts[line+1])
Expand All @@ -658,7 +658,7 @@ func (p *Printer) printSourceFileWithTypes(sourceFile *ast.SourceFile) {
if typesPrinted {
p.print("\n")
}
p.print(sourceFile.Text[pos:nextLineStart])
p.print(sourceFile.Text()[pos:nextLineStart])
pos = nextLineStart
typesPrinted = false
}
Expand All @@ -681,7 +681,7 @@ func (p *Printer) printSourceFileWithTypes(sourceFile *ast.SourceFile) {
return node.ForEachChild(visit)
}
visit(sourceFile.AsNode())
p.print(sourceFile.Text[pos:sourceFile.End()])
p.print(sourceFile.Text()[pos:sourceFile.End()])
}

func (c *Checker) getTextAndTypeOfNode(node *ast.Node) (string, *Type, bool) {
Expand Down
2 changes: 1 addition & 1 deletion internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2095,7 +2095,7 @@ var getFeatureMap = sync.OnceValue(func() map[string][]FeatureMapEntry {
})

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

func tryGetPropertyAccessOrIdentifierToString(expr *ast.Node) string {
Expand Down
Loading