Skip to content

Commit 2536c79

Browse files
authored
Fix listFiles/listFilesOnly (microsoft#964)
1 parent 102852b commit 2536c79

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

internal/execute/tsc.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"runtime"
8+
"slices"
89
"strings"
910
"time"
1011

@@ -203,7 +204,7 @@ func performCompilation(sys System, cb cbType, config *tsoptions.ParsedCommandLi
203204
program := compiler.NewProgramFromParsedCommandLine(config, host)
204205
parseTime := time.Since(parseStart)
205206

206-
result := compileAndEmit(sys, program, reportDiagnostic)
207+
result := emitFilesAndReportErrors(sys, program, reportDiagnostic)
207208
if result.status != ExitStatusSuccess {
208209
// compile exited early
209210
return result.status
@@ -245,59 +246,57 @@ type compileAndEmitResult struct {
245246
totalTime time.Duration
246247
}
247248

248-
func compileAndEmit(sys System, program *compiler.Program, reportDiagnostic diagnosticReporter) (result compileAndEmitResult) {
249-
// todo: check if third return needed after execute is fully implemented
250-
249+
func emitFilesAndReportErrors(sys System, program *compiler.Program, reportDiagnostic diagnosticReporter) (result compileAndEmitResult) {
251250
ctx := context.Background()
252251
options := program.Options()
253-
allDiagnostics := program.GetConfigFileParsingDiagnostics()
252+
allDiagnostics := slices.Clip(program.GetConfigFileParsingDiagnostics())
253+
configFileParsingDiagnosticsLength := len(allDiagnostics)
254+
255+
allDiagnostics = append(allDiagnostics, program.GetSyntacticDiagnostics(ctx, nil)...)
254256

255-
// todo: early exit logic and append diagnostics
256-
diagnostics := program.GetSyntacticDiagnostics(ctx, nil)
257-
if len(diagnostics) == 0 {
257+
if len(allDiagnostics) == configFileParsingDiagnosticsLength {
258+
// Options diagnostics include global diagnostics (even though we collect them separately),
259+
// and global diagnostics create checkers, which then bind all of the files. Do this binding
260+
// early so we can track the time.
258261
bindStart := time.Now()
259262
_ = program.GetBindDiagnostics(ctx, nil)
260263
result.bindTime = time.Since(bindStart)
261264

262-
diagnostics = append(diagnostics, program.GetOptionsDiagnostics(ctx)...)
263-
if options.ListFilesOnly.IsFalse() {
264-
// program.GetBindDiagnostics(nil)
265-
diagnostics = append(diagnostics, program.GetGlobalDiagnostics(ctx)...)
265+
allDiagnostics = append(allDiagnostics, program.GetOptionsDiagnostics(ctx)...)
266+
267+
if options.ListFilesOnly.IsFalseOrUnknown() {
268+
allDiagnostics = append(allDiagnostics, program.GetGlobalDiagnostics(ctx)...)
269+
270+
if len(allDiagnostics) == configFileParsingDiagnosticsLength {
271+
checkStart := time.Now()
272+
allDiagnostics = append(allDiagnostics, program.GetSemanticDiagnostics(ctx, nil)...)
273+
result.checkTime = time.Since(checkStart)
274+
}
275+
276+
// !!! GetDeclarationDiagnostics
277+
// if options.NoEmit.IsTrue() && options.GetEmitDeclarations() && len(allDiagnostics) == configFileParsingDiagnosticsLength {
278+
// }
266279
}
267280
}
268-
if len(diagnostics) == 0 {
269-
checkStart := time.Now()
270-
diagnostics = append(diagnostics, program.GetSemanticDiagnostics(ctx, nil)...)
271-
result.checkTime = time.Since(checkStart)
272-
}
273-
// TODO: declaration diagnostics
274-
if len(diagnostics) == 0 && options.NoEmit == core.TSTrue && (options.Declaration.IsTrue() && options.Composite.IsTrue()) {
275-
result.status = ExitStatusNotImplemented
276-
return result
277-
// addRange(allDiagnostics, program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken));
278-
}
279281

280282
emitResult := &compiler.EmitResult{EmitSkipped: true, Diagnostics: []*ast.Diagnostic{}}
281283
if !options.ListFilesOnly.IsTrue() {
282-
// !!! Emit is not yet fully implemented, will not emit unless `outfile` specified
283284
emitStart := time.Now()
284285
emitResult = program.Emit(compiler.EmitOptions{})
285286
result.emitTime = time.Since(emitStart)
286287
}
287-
diagnostics = append(diagnostics, emitResult.Diagnostics...)
288+
allDiagnostics = append(allDiagnostics, emitResult.Diagnostics...)
288289

289-
allDiagnostics = append(allDiagnostics, diagnostics...)
290290
allDiagnostics = compiler.SortAndDeduplicateDiagnostics(allDiagnostics)
291291
for _, diagnostic := range allDiagnostics {
292292
reportDiagnostic(diagnostic)
293293
}
294294

295-
// !!! if (write)
296295
if sys.Writer() != nil {
297296
for _, file := range emitResult.EmittedFiles {
298297
fmt.Fprint(sys.Writer(), "TSFILE: ", tspath.GetNormalizedAbsolutePath(file, sys.GetCurrentDirectory()))
299298
}
300-
// todo: listFiles(program, sys.Writer())
299+
listFiles(sys, program)
301300
}
302301

303302
createReportErrorSummary(sys, program.Options())(allDiagnostics)
@@ -325,3 +324,13 @@ func showConfig(sys System, config *core.CompilerOptions) {
325324
enc.SetIndent("", " ")
326325
enc.Encode(config) //nolint:errcheck,errchkjson
327326
}
327+
328+
func listFiles(sys System, program *compiler.Program) {
329+
options := program.Options()
330+
// !!! explainFiles
331+
if options.ListFiles.IsTrue() || options.ListFilesOnly.IsTrue() {
332+
for _, file := range program.GetSourceFiles() {
333+
fmt.Fprintf(sys.Writer(), "%s%s", file.FileName(), sys.NewLine())
334+
}
335+
}
336+
}

internal/execute/watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func createWatcher(sys System, configParseResult *tsoptions.ParsedCommandLine, r
3838
func (w *watcher) compileAndEmit() {
3939
// !!! output/error reporting is currently the same as non-watch mode
4040
// diagnostics, emitResult, exitStatus :=
41-
compileAndEmit(w.sys, w.program, w.reportDiagnostic)
41+
emitFilesAndReportErrors(w.sys, w.program, w.reportDiagnostic)
4242
}
4343

4444
func (w *watcher) hasErrorsInTsConfig() bool {

0 commit comments

Comments
 (0)