Skip to content

Commit 4a6c6ea

Browse files
committed
Merge remote-tracking branch 'origin/gabritto/renamelinemaps' into gabritto/def1
2 parents cef9f67 + c80e7a3 commit 4a6c6ea

Some content is hidden

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

53 files changed

+390
-279
lines changed

internal/ast/ast.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10724,10 +10724,10 @@ type SourceFile struct {
1072410724
ClassifiableNames collections.Set[string]
1072510725
PatternAmbientModules []*PatternAmbientModule
1072610726

10727-
// Fields set by LineMap
10727+
// Fields set by ECMALineMap
1072810728

10729-
lineMapMu sync.RWMutex
10730-
lineMap []core.TextPos
10729+
ecmaLineMapMu sync.RWMutex
10730+
ecmaLineMap []core.TextPos
1073110731

1073210732
// Fields set by language service
1073310733

@@ -10862,17 +10862,17 @@ func (f *NodeFactory) UpdateSourceFile(node *SourceFile, statements *StatementLi
1086210862
return node.AsNode()
1086310863
}
1086410864

10865-
func (node *SourceFile) LineMap() []core.TextPos {
10866-
node.lineMapMu.RLock()
10867-
lineMap := node.lineMap
10868-
node.lineMapMu.RUnlock()
10865+
func (node *SourceFile) ECMALineMap() []core.TextPos {
10866+
node.ecmaLineMapMu.RLock()
10867+
lineMap := node.ecmaLineMap
10868+
node.ecmaLineMapMu.RUnlock()
1086910869
if lineMap == nil {
10870-
node.lineMapMu.Lock()
10871-
defer node.lineMapMu.Unlock()
10872-
lineMap = node.lineMap
10870+
node.ecmaLineMapMu.Lock()
10871+
defer node.ecmaLineMapMu.Unlock()
10872+
lineMap = node.ecmaLineMap
1087310873
if lineMap == nil {
10874-
lineMap = core.ComputeLineStarts(node.Text())
10875-
node.lineMap = lineMap
10874+
lineMap = core.ComputeECMALineStarts(node.Text())
10875+
node.ecmaLineMap = lineMap
1087610876
}
1087710877
}
1087810878
return lineMap
@@ -11054,7 +11054,7 @@ func getDeclarationName(declaration *Node) string {
1105411054

1105511055
type SourceFileLike interface {
1105611056
Text() string
11057-
LineMap() []core.TextPos
11057+
ECMALineMap() []core.TextPos
1105811058
}
1105911059

1106011060
type CommentRange struct {

internal/astnav/tokens_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func tsGetTouchingPropertyName(t testing.TB, fileText string, positions []int) [
240240
}
241241

242242
func writeRangeDiff(output *strings.Builder, file *ast.SourceFile, diff tokenDiff, rng core.TextRange, position int) {
243-
lines := file.LineMap()
243+
lines := file.ECMALineMap()
244244

245245
tsTokenPos := position
246246
goTokenPos := position

internal/binder/binder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,12 +2243,12 @@ func (b *Binder) bindBinaryExpressionFlow(node *ast.Node) {
22432243
b.bind(expr.Left)
22442244
b.bind(expr.Type)
22452245
if operator == ast.KindCommaToken {
2246-
b.maybeBindExpressionFlowIfCall(node)
2246+
b.maybeBindExpressionFlowIfCall(expr.Left)
22472247
}
22482248
b.bind(expr.OperatorToken)
22492249
b.bind(expr.Right)
22502250
if operator == ast.KindCommaToken {
2251-
b.maybeBindExpressionFlowIfCall(node)
2251+
b.maybeBindExpressionFlowIfCall(expr.Right)
22522252
}
22532253
if ast.IsAssignmentOperator(operator) && !ast.IsAssignmentTarget(node) {
22542254
b.bindAssignmentTargetFlow(expr.Left)

internal/checker/checker.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18332,6 +18332,11 @@ func (c *Checker) getBaseTypes(t *Type) []*Type {
1833218332
}
1833318333
}
1833418334
}
18335+
// In general, base type resolution always precedes member resolution. However, it is possible
18336+
// for resolution of type parameter defaults to cause circularity errors, possibly leaving
18337+
// members partially resolved. Here we ensure any such partial resolution is reset.
18338+
// See https://github.com/microsoft/TypeScript/issues/16861 for an example.
18339+
t.objectFlags &^= ObjectFlagsMembersResolved
1833518340
data.baseTypesResolved = true
1833618341
}
1833718342
return data.resolvedBaseTypes
@@ -18395,14 +18400,6 @@ func (c *Checker) resolveBaseTypesOfClass(t *Type) {
1839518400
c.error(t.symbol.ValueDeclaration, diagnostics.Type_0_recursively_references_itself_as_a_base_type, c.TypeToString(t))
1839618401
return
1839718402
}
18398-
// !!! This logic is suspicious. We really shouldn't be un-resolving members after they've been resolved.
18399-
// if t.resolvedBaseTypes == resolvingEmptyArray {
18400-
// // Circular reference, likely through instantiation of default parameters
18401-
// // (otherwise there'd be an error from hasBaseType) - this is fine, but `.members` should be reset
18402-
// // as `getIndexedAccessType` via `instantiateType` via `getTypeFromClassOrInterfaceReference` forces a
18403-
// // partial instantiation of the members without the base types fully resolved
18404-
// t.members = nil
18405-
// }
1840618403
t.AsInterfaceType().resolvedBaseTypes = []*Type{reducedBaseType}
1840718404
}
1840818405

internal/checker/grammarchecks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ func (c *Checker) checkGrammarArrowFunction(node *ast.Node, file *ast.SourceFile
814814
}
815815

816816
equalsGreaterThanToken := arrowFunc.EqualsGreaterThanToken
817-
startLine, _ := scanner.GetLineAndCharacterOfPosition(file, equalsGreaterThanToken.Pos())
818-
endLine, _ := scanner.GetLineAndCharacterOfPosition(file, equalsGreaterThanToken.End())
817+
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.Pos())
818+
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.End())
819819
return startLine != endLine && c.grammarErrorOnNode(equalsGreaterThanToken, diagnostics.Line_terminator_not_permitted_before_arrow)
820820
}
821821

internal/compiler/program.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,10 @@ func (p *Program) getDiagnosticsWithPrecedingDirectives(sourceFile *ast.SourceFi
10741074
// Build map of directives by line number
10751075
directivesByLine := make(map[int]ast.CommentDirective)
10761076
for _, directive := range sourceFile.CommentDirectives {
1077-
line, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, directive.Loc.Pos())
1077+
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, directive.Loc.Pos())
10781078
directivesByLine[line] = directive
10791079
}
1080-
lineStarts := scanner.GetLineStarts(sourceFile)
1080+
lineStarts := scanner.GetECMALineStarts(sourceFile)
10811081
filtered := make([]*ast.Diagnostic, 0, len(diags))
10821082
for _, diagnostic := range diags {
10831083
ignoreDiagnostic := false
@@ -1225,7 +1225,7 @@ func (p *Program) getDiagnosticsHelper(ctx context.Context, sourceFile *ast.Sour
12251225
func (p *Program) LineCount() int {
12261226
var count int
12271227
for _, file := range p.files {
1228-
count += len(file.LineMap())
1228+
count += len(file.ECMALineMap())
12291229
}
12301230
return count
12311231
}

internal/core/core.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,12 @@ func Coalesce[T *U, U any](a T, b T) T {
362362
}
363363
}
364364

365-
func ComputeLineStarts(text string) []TextPos {
365+
func ComputeECMALineStarts(text string) []TextPos {
366366
result := make([]TextPos, 0, strings.Count(text, "\n")+1)
367-
return slices.AppendSeq(result, ComputeLineStartsSeq(text))
367+
return slices.AppendSeq(result, ComputeECMALineStartsSeq(text))
368368
}
369369

370-
func ComputeLineStartsSeq(text string) iter.Seq[TextPos] {
370+
func ComputeECMALineStartsSeq(text string) iter.Seq[TextPos] {
371371
return func(yield func(TextPos) bool) {
372372
textLen := TextPos(len(text))
373373
var pos TextPos

internal/diagnosticwriter/diagnosticwriter.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func FormatDiagnosticWithColorAndContext(output io.Writer, diagnostic *ast.Diagn
8484
}
8585

8686
func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, length int, squiggleColor string, indent string, formatOpts *FormattingOptions) {
87-
firstLine, firstLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start)
88-
lastLine, lastLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start+length)
87+
firstLine, firstLineChar := scanner.GetECMALineAndCharacterOfPosition(sourceFile, start)
88+
lastLine, lastLineChar := scanner.GetECMALineAndCharacterOfPosition(sourceFile, start+length)
8989
if length == 0 {
9090
lastLineChar++ // When length is zero, squiggle the character right after the start position.
9191
}
9292

93-
lastLineOfFile, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, len(sourceFile.Text()))
93+
lastLineOfFile, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, len(sourceFile.Text()))
9494

9595
hasMoreThanFiveLines := lastLine-firstLine >= 4
9696
gutterWidth := len(strconv.Itoa(lastLine + 1))
@@ -113,10 +113,10 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
113113
i = lastLine - 1
114114
}
115115

116-
lineStart := scanner.GetPositionOfLineAndCharacter(sourceFile, i, 0)
116+
lineStart := scanner.GetECMAPositionOfLineAndCharacter(sourceFile, i, 0)
117117
var lineEnd int
118118
if i < lastLineOfFile {
119-
lineEnd = scanner.GetPositionOfLineAndCharacter(sourceFile, i+1, 0)
119+
lineEnd = scanner.GetECMAPositionOfLineAndCharacter(sourceFile, i+1, 0)
120120
} else {
121121
lineEnd = sourceFile.Loc.End()
122122
}
@@ -216,7 +216,7 @@ func writeWithStyleAndReset(output io.Writer, text string, formatStyle string) {
216216
}
217217

218218
func WriteLocation(output io.Writer, file *ast.SourceFile, pos int, formatOpts *FormattingOptions, writeWithStyleAndReset FormattedWriter) {
219-
firstLine, firstChar := scanner.GetLineAndCharacterOfPosition(file, pos)
219+
firstLine, firstChar := scanner.GetECMALineAndCharacterOfPosition(file, pos)
220220
var relativeFileName string
221221
if formatOpts != nil {
222222
relativeFileName = tspath.ConvertToRelativePath(file.FileName(), formatOpts.ComparePathsOptions)
@@ -357,7 +357,7 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
357357
if file == nil || len(fileErrors) == 0 {
358358
return ""
359359
}
360-
line, _ := scanner.GetLineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
360+
line, _ := scanner.GetECMALineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
361361
fileName := file.FileName()
362362
if tspath.PathIsAbsolute(fileName) && tspath.PathIsAbsolute(formatOpts.CurrentDirectory) {
363363
fileName = tspath.ConvertToRelativePath(file.FileName(), formatOpts.ComparePathsOptions)
@@ -378,7 +378,7 @@ func WriteFormatDiagnostics(output io.Writer, diagnostics []*ast.Diagnostic, for
378378

379379
func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *FormattingOptions) {
380380
if diagnostic.File() != nil {
381-
line, character := scanner.GetLineAndCharacterOfPosition(diagnostic.File(), diagnostic.Loc().Pos())
381+
line, character := scanner.GetECMALineAndCharacterOfPosition(diagnostic.File(), diagnostic.Loc().Pos())
382382
fileName := diagnostic.File().FileName()
383383
relativeFileName := tspath.ConvertToRelativePath(fileName, formatOpts.ComparePathsOptions)
384384
fmt.Fprintf(output, "%s(%d,%d): ", relativeFileName, line+1, character+1)

internal/execute/tsc/statistics.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,8 @@ func (s *Statistics) Aggregate(stat *Statistics) {
150150
}
151151

152152
func (s *Statistics) SetTotalTime(totalTime time.Duration) {
153+
if s.compileTimes == nil {
154+
s.compileTimes = &CompileTimes{}
155+
}
153156
s.compileTimes.totalTime = totalTime
154157
}

internal/format/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ func FormatOnSemicolon(ctx context.Context, sourceFile *ast.SourceFile, position
146146
}
147147

148148
func FormatOnEnter(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
149-
line, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, position)
149+
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, position)
150150
if line == 0 {
151151
return nil
152152
}
153153
// get start position for the previous line
154-
startPos := int(scanner.GetLineStarts(sourceFile)[line-1])
154+
startPos := int(scanner.GetECMALineStarts(sourceFile)[line-1])
155155
// After the enter key, the cursor is now at a new line. The new line may or may not contain non-whitespace characters.
156156
// If the new line has only whitespaces, we won't want to format this line, because that would remove the indentation as
157157
// trailing whitespaces. So the end of the formatting span should be the later one between:
158158
// 1. the end of the previous line
159159
// 2. the last non-whitespace character in the current line
160-
endOfFormatSpan := scanner.GetEndLinePosition(sourceFile, line)
160+
endOfFormatSpan := scanner.GetECMAEndLinePosition(sourceFile, line)
161161
for endOfFormatSpan > startPos {
162162
ch, s := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
163163
if s == 0 || stringutil.IsWhiteSpaceSingleLine(ch) { // on multibyte character keep backing up

0 commit comments

Comments
 (0)