Skip to content

Commit d7887e0

Browse files
authored
Porting of tsc -b -w (#1684)
1 parent 18a61f3 commit d7887e0

File tree

112 files changed

+33123
-824
lines changed

Some content is hidden

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

112 files changed

+33123
-824
lines changed

internal/checker/checker.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -875,14 +875,14 @@ func NewChecker(program Program) *Checker {
875875
c.legacyDecorators = c.compilerOptions.ExperimentalDecorators == core.TSTrue
876876
c.emitStandardClassFields = !c.compilerOptions.UseDefineForClassFields.IsFalse() && c.compilerOptions.GetEmitScriptTarget() >= core.ScriptTargetES2022
877877
c.allowSyntheticDefaultImports = c.compilerOptions.GetAllowSyntheticDefaultImports()
878-
c.strictNullChecks = c.getStrictOptionValue(c.compilerOptions.StrictNullChecks)
879-
c.strictFunctionTypes = c.getStrictOptionValue(c.compilerOptions.StrictFunctionTypes)
880-
c.strictBindCallApply = c.getStrictOptionValue(c.compilerOptions.StrictBindCallApply)
881-
c.strictPropertyInitialization = c.getStrictOptionValue(c.compilerOptions.StrictPropertyInitialization)
882-
c.strictBuiltinIteratorReturn = c.getStrictOptionValue(c.compilerOptions.StrictBuiltinIteratorReturn)
883-
c.noImplicitAny = c.getStrictOptionValue(c.compilerOptions.NoImplicitAny)
884-
c.noImplicitThis = c.getStrictOptionValue(c.compilerOptions.NoImplicitThis)
885-
c.useUnknownInCatchVariables = c.getStrictOptionValue(c.compilerOptions.UseUnknownInCatchVariables)
878+
c.strictNullChecks = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.StrictNullChecks)
879+
c.strictFunctionTypes = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.StrictFunctionTypes)
880+
c.strictBindCallApply = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.StrictBindCallApply)
881+
c.strictPropertyInitialization = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.StrictPropertyInitialization)
882+
c.strictBuiltinIteratorReturn = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.StrictBuiltinIteratorReturn)
883+
c.noImplicitAny = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.NoImplicitAny)
884+
c.noImplicitThis = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.NoImplicitThis)
885+
c.useUnknownInCatchVariables = c.compilerOptions.GetStrictOptionValue(c.compilerOptions.UseUnknownInCatchVariables)
886886
c.exactOptionalPropertyTypes = c.compilerOptions.ExactOptionalPropertyTypes == core.TSTrue
887887
c.canCollectSymbolAliasAccessibilityData = c.compilerOptions.VerbatimModuleSyntax.IsFalseOrUnknown()
888888
c.arrayVariances = []VarianceFlags{VarianceFlagsCovariant}
@@ -1106,13 +1106,6 @@ func (c *Checker) reportUnmeasurableWorker(t *Type) *Type {
11061106
return t
11071107
}
11081108

1109-
func (c *Checker) getStrictOptionValue(value core.Tristate) bool {
1110-
if value != core.TSUnknown {
1111-
return value == core.TSTrue
1112-
}
1113-
return c.compilerOptions.Strict == core.TSTrue
1114-
}
1115-
11161109
// Resolve to the global class or interface by the given name and arity, or emptyObjectType/emptyGenericType otherwise
11171110
func (c *Checker) getGlobalTypeResolver(name string, arity int, reportErrors bool) func() *Type {
11181111
return core.Memoize(func() *Type {

internal/collections/syncmap.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@ func (s *SyncMap[K, V]) Keys() iter.Seq[K] {
7373
})
7474
}
7575
}
76+
77+
func (s *SyncMap[K, V]) Clone() *SyncMap[K, V] {
78+
clone := &SyncMap[K, V]{}
79+
s.m.Range(func(key, value any) bool {
80+
clone.m.Store(key, value)
81+
return true
82+
})
83+
return clone
84+
}

internal/compiler/emitter.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
262262
// Write the source map
263263
if len(sourceMapFilePath) > 0 {
264264
sourceMap := sourceMapGenerator.String()
265-
err := e.host.WriteFile(sourceMapFilePath, sourceMap, false /*writeByteOrderMark*/)
265+
err := e.writeText(sourceMapFilePath, sourceMap, false /*writeByteOrderMark*/, nil)
266266
if err != nil {
267267
e.emitterDiagnostics.Add(ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, jsFilePath, err.Error()))
268268
} else {
@@ -275,18 +275,12 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
275275

276276
// Write the output file
277277
text := e.writer.String()
278-
var err error
279-
var skippedDtsWrite bool
280-
if e.writeFile == nil {
281-
err = e.host.WriteFile(jsFilePath, text, e.host.Options().EmitBOM.IsTrue())
282-
} else {
283-
data := &WriteFileData{
284-
SourceMapUrlPos: sourceMapUrlPos,
285-
Diagnostics: e.emitterDiagnostics.GetDiagnostics(),
286-
}
287-
err = e.writeFile(jsFilePath, text, e.host.Options().EmitBOM.IsTrue(), data)
288-
skippedDtsWrite = data.SkippedDtsWrite
278+
data := &WriteFileData{
279+
SourceMapUrlPos: sourceMapUrlPos,
280+
Diagnostics: e.emitterDiagnostics.GetDiagnostics(),
289281
}
282+
err := e.writeText(jsFilePath, text, options.EmitBOM.IsTrue(), data)
283+
skippedDtsWrite := data.SkippedDtsWrite
290284
if err != nil {
291285
e.emitterDiagnostics.Add(ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, jsFilePath, err.Error()))
292286
} else if !skippedDtsWrite {
@@ -297,6 +291,13 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
297291
e.writer.Clear()
298292
}
299293

294+
func (e *emitter) writeText(fileName string, text string, writeByteOrderMark bool, data *WriteFileData) error {
295+
if e.writeFile != nil {
296+
return e.writeFile(fileName, text, writeByteOrderMark, data)
297+
}
298+
return e.host.WriteFile(fileName, text, writeByteOrderMark)
299+
}
300+
300301
func shouldEmitSourceMaps(mapOptions *core.CompilerOptions, sourceFile *ast.SourceFile) bool {
301302
return (mapOptions.SourceMap.IsTrue() || mapOptions.InlineSourceMap.IsTrue()) &&
302303
!tspath.FileExtensionIs(sourceFile.FileName(), tspath.ExtensionJson)

internal/compiler/program.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,6 @@ func (p *Program) verifyCompilerOptions() {
542542
}
543543
}
544544

545-
getStrictOptionValue := func(value core.Tristate) bool {
546-
if value != core.TSUnknown {
547-
return value == core.TSTrue
548-
}
549-
return options.Strict == core.TSTrue
550-
}
551-
552545
// Removed in TS7
553546

554547
if options.BaseUrl != "" {
@@ -586,10 +579,10 @@ func (p *Program) verifyCompilerOptions() {
586579
createRemovedOptionDiagnostic("module", "UMD", "")
587580
}
588581

589-
if options.StrictPropertyInitialization.IsTrue() && !getStrictOptionValue(options.StrictNullChecks) {
582+
if options.StrictPropertyInitialization.IsTrue() && !options.GetStrictOptionValue(options.StrictNullChecks) {
590583
createDiagnosticForOptionName(diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "strictPropertyInitialization", "strictNullChecks")
591584
}
592-
if options.ExactOptionalPropertyTypes.IsTrue() && !getStrictOptionValue(options.StrictNullChecks) {
585+
if options.ExactOptionalPropertyTypes.IsTrue() && !options.GetStrictOptionValue(options.StrictNullChecks) {
593586
createDiagnosticForOptionName(diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "exactOptionalPropertyTypes", "strictNullChecks")
594587
}
595588

@@ -1341,10 +1334,12 @@ type WriteFileData struct {
13411334
SkippedDtsWrite bool
13421335
}
13431336

1337+
type WriteFile func(fileName string, text string, writeByteOrderMark bool, data *WriteFileData) error
1338+
13441339
type EmitOptions struct {
13451340
TargetSourceFile *ast.SourceFile // Single file to emit. If `nil`, emits all files
13461341
EmitOnly EmitOnly
1347-
WriteFile func(fileName string, text string, writeByteOrderMark bool, data *WriteFileData) error
1342+
WriteFile WriteFile
13481343
}
13491344

13501345
type EmitResult struct {

internal/core/compileroptions.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ func (options *CompilerOptions) GetJSXTransformEnabled() bool {
292292
return jsx == JsxEmitReact || jsx == JsxEmitReactJSX || jsx == JsxEmitReactJSXDev
293293
}
294294

295+
func (options *CompilerOptions) GetStrictOptionValue(value Tristate) bool {
296+
if value != TSUnknown {
297+
return value == TSTrue
298+
}
299+
return options.Strict == TSTrue
300+
}
301+
295302
func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (result []string, fromConfig bool) {
296303
if options.TypeRoots != nil {
297304
return options.TypeRoots, true

internal/core/watchoptions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package core
22

3+
import "time"
4+
35
type WatchOptions struct {
46
Interval *int `json:"watchInterval"`
57
FileKind WatchFileKind `json:"watchFile"`
@@ -41,3 +43,11 @@ const (
4143
PollingKindDynamicPriority PollingKind = 3
4244
PollingKindFixedChunkSize PollingKind = 4
4345
)
46+
47+
func (w *WatchOptions) WatchInterval() time.Duration {
48+
watchInterval := 1000 * time.Millisecond
49+
if w != nil && w.Interval != nil {
50+
watchInterval = time.Duration(*w.Interval) * time.Millisecond
51+
}
52+
return watchInterval
53+
}

internal/diagnostics/diagnostics_generated.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/diagnostics/extraDiagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@
3838
"Failed to update timestamp of file '{0}'.": {
3939
"category": "Message",
4040
"code": 5074
41+
},
42+
"Project '{0}' is out of date because it has errors.": {
43+
"category": "Message",
44+
"code": 6423
4145
}
4246
}

internal/diagnosticwriter/diagnosticwriter.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"unicode"
1111

1212
"github.com/microsoft/typescript-go/internal/ast"
13+
"github.com/microsoft/typescript-go/internal/core"
1314
"github.com/microsoft/typescript-go/internal/diagnostics"
1415
"github.com/microsoft/typescript-go/internal/scanner"
1516
"github.com/microsoft/typescript-go/internal/tspath"
@@ -139,7 +140,8 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
139140
fmt.Fprint(writer, resetEscapeSequence)
140141
fmt.Fprint(writer, gutterSeparator)
141142
fmt.Fprint(writer, squiggleColor)
142-
if i == firstLine {
143+
switch i {
144+
case firstLine:
143145
// If we're on the last line, then limit it to the last character of the last line.
144146
// Otherwise, we'll just squiggle the rest of the line, giving 'slice' no end position.
145147
var lastCharForLine int
@@ -153,10 +155,10 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
153155
// then squiggle the remainder of the line.
154156
fmt.Fprint(writer, strings.Repeat(" ", firstLineChar))
155157
fmt.Fprint(writer, strings.Repeat("~", lastCharForLine-firstLineChar))
156-
} else if i == lastLine {
158+
case lastLine:
157159
// Squiggle until the final character.
158160
fmt.Fprint(writer, strings.Repeat("~", lastLineChar))
159-
} else {
161+
default:
160162
// Squiggle the entire line.
161163
fmt.Fprint(writer, strings.Repeat("~", len(lineContent)))
162164
}
@@ -263,13 +265,14 @@ func WriteErrorSummaryText(output io.Writer, allDiagnostics []*ast.Diagnostic, f
263265
message = diagnostics.Found_1_error_in_0.Format(firstFileName)
264266
}
265267
} else {
266-
if numErroringFiles == 0 {
268+
switch numErroringFiles {
269+
case 0:
267270
// No file-specific errors.
268271
message = diagnostics.Found_0_errors.Format(totalErrorCount)
269-
} else if numErroringFiles == 1 {
272+
case 1:
270273
// One file with errors.
271274
message = diagnostics.Found_0_errors_in_the_same_file_starting_at_Colon_1.Format(totalErrorCount, firstFileName)
272-
} else {
275+
default:
273276
// Multiple files with errors.
274277
message = diagnostics.Found_0_errors_in_1_files.Format(totalErrorCount, numErroringFiles)
275278
}
@@ -397,3 +400,19 @@ func FormatDiagnosticsStatusAndTime(output io.Writer, time string, diag *ast.Dia
397400
fmt.Fprint(output, time, " - ")
398401
WriteFlattenedDiagnosticMessage(output, diag, formatOpts.NewLine)
399402
}
403+
404+
var ScreenStartingCodes = []int32{
405+
diagnostics.Starting_compilation_in_watch_mode.Code(),
406+
diagnostics.File_change_detected_Starting_incremental_compilation.Code(),
407+
}
408+
409+
func TryClearScreen(output io.Writer, diag *ast.Diagnostic, options *core.CompilerOptions) bool {
410+
if !options.PreserveWatchOutput.IsTrue() &&
411+
!options.ExtendedDiagnostics.IsTrue() &&
412+
!options.Diagnostics.IsTrue() &&
413+
slices.Contains(ScreenStartingCodes, diag.Code()) {
414+
fmt.Fprint(output, "\x1B[2J\x1B[3J\x1B[H") // Clear screen and move cursor to home position
415+
return true
416+
}
417+
return false
418+
}

0 commit comments

Comments
 (0)