Skip to content

Commit 19f5ecb

Browse files
committed
Watch changes
1 parent 70476c6 commit 19f5ecb

File tree

12 files changed

+46
-58
lines changed

12 files changed

+46
-58
lines changed

internal/execute/export_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package execute
22

33
import (
4+
"github.com/microsoft/typescript-go/internal/ast"
45
"github.com/microsoft/typescript-go/internal/compiler"
6+
"github.com/microsoft/typescript-go/internal/incremental"
57
"github.com/microsoft/typescript-go/internal/tsoptions"
68
)
79

@@ -21,11 +23,12 @@ func RunWatchCycle(w *watcher) {
2123
return
2224
}
2325
// todo: updateProgram()
24-
w.program = compiler.NewProgram(compiler.ProgramOptions{
25-
Config: w.options,
26-
Host: w.host,
27-
})
28-
if w.hasBeenModified(w.program) {
26+
w.program = incremental.NewProgram(compiler.NewProgram(compiler.ProgramOptions{
27+
Config: w.options,
28+
Host: w.host,
29+
JSDocParsingMode: ast.JSDocParsingModeParseForTypeErrors,
30+
}), w.program)
31+
if w.hasBeenModified(w.program.GetProgram()) {
2932
w.compileAndEmit()
3033
}
3134
}

internal/execute/verifytscwatch_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,25 @@ func TestTscNoEmitWatch(t *testing.T) {
133133
noEmitWatchTestInput("syntax errors",
134134
[]string{"-w"},
135135
`const a = "hello`,
136-
[]string{`"outFile": "../outFile.js"`},
136+
nil,
137137
),
138138
noEmitWatchTestInput(
139139
"semantic errors",
140140
[]string{"-w"},
141141
`const a: number = "hello"`,
142-
[]string{`"outFile": "../outFile.js"`},
142+
nil,
143143
),
144144
noEmitWatchTestInput(
145145
"dts errors without dts enabled",
146146
[]string{"-w"},
147147
`const a = class { private p = 10; };`,
148-
[]string{`"outFile": "../outFile.js"`},
148+
nil,
149149
),
150150
noEmitWatchTestInput(
151151
"dts errors",
152152
[]string{"-w"},
153153
`const a = class { private p = 10; };`,
154-
[]string{`"outFile": "../outFile.js"`, `"declaration": true`},
154+
[]string{`"declaration": true`},
155155
),
156156
}
157157

internal/execute/watch.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/microsoft/typescript-go/internal/ast"
88
"github.com/microsoft/typescript-go/internal/compiler"
9+
"github.com/microsoft/typescript-go/internal/incremental"
910
)
1011

1112
func start(w *watcher) ExitStatus {
@@ -26,6 +27,7 @@ func (w *watcher) initialize() {
2627
if w.configFileName == "" {
2728
w.host = compiler.NewCompilerHost(w.options.CompilerOptions(), w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), nil)
2829
}
30+
w.program = incremental.ReadBuildInfoProgram(w.options, incremental.NewBuildInfoReader(w.host))
2931
}
3032

3133
func (w *watcher) doCycle() {
@@ -36,12 +38,13 @@ func (w *watcher) doCycle() {
3638
return
3739
}
3840
// updateProgram()
39-
w.program = compiler.NewProgram(compiler.ProgramOptions{
41+
w.program = incremental.NewProgram(compiler.NewProgram(compiler.ProgramOptions{
4042
Config: w.options,
4143
Host: w.host,
4244
JSDocParsingMode: ast.JSDocParsingModeParseForTypeErrors,
43-
})
44-
if w.hasBeenModified(w.program) {
45+
}), w.program)
46+
47+
if w.hasBeenModified(w.program.GetProgram()) {
4548
fmt.Fprint(w.sys.Writer(), "build starting at ", w.sys.Now(), w.sys.NewLine())
4649
timeStart := w.sys.Now()
4750
w.compileAndEmit()

internal/execute/watcher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/microsoft/typescript-go/internal/collections"
88
"github.com/microsoft/typescript-go/internal/compiler"
99
"github.com/microsoft/typescript-go/internal/core"
10+
"github.com/microsoft/typescript-go/internal/incremental"
1011
"github.com/microsoft/typescript-go/internal/tsoptions"
1112
"github.com/microsoft/typescript-go/internal/tspath"
1213
)
@@ -18,7 +19,7 @@ type watcher struct {
1819
reportDiagnostic diagnosticReporter
1920

2021
host compiler.CompilerHost
21-
program *compiler.Program
22+
program *incremental.Program
2223
prevModified map[string]time.Time
2324
configModified bool
2425
}

internal/incremental/affectedfileshandler.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ func (h *affectedFilesHandler) isChangedSignature(path tspath.Path) bool {
4848
}
4949

5050
func (h *affectedFilesHandler) removeSemanticDiagnosticsOf(path tspath.Path) {
51-
if h.program.snapshot.semanticDiagnosticsFromOldState.Has(path) {
52-
h.filesToRemoveDiagnostics.Add(path)
53-
}
51+
h.filesToRemoveDiagnostics.Add(path)
5452
}
5553

5654
func (h *affectedFilesHandler) removeDiagnosticsOfLibraryFiles() {
@@ -309,14 +307,14 @@ func (h *affectedFilesHandler) handleDtsMayChangeOfGlobalScope(dtsMayChange dtsM
309307
// Handle the dts may change, so they need to be added to pending emit if dts emit is enabled,
310308
// Also we need to make sure signature is updated for these files
311309
func (h *affectedFilesHandler) handleDtsMayChangeOf(dtsMayChange dtsMayChange, path tspath.Path, invalidateJsFiles bool) {
312-
h.removeSemanticDiagnosticsOf(path)
313310
if h.program.snapshot.changedFilesSet.Has(path) {
314311
return
315312
}
316313
file := h.program.program.GetSourceFileByPath(path)
317314
if file == nil {
318315
return
319316
}
317+
h.removeSemanticDiagnosticsOf(path)
320318
// Even though the js emit doesnt change and we are already handling dts emit and semantic diagnostics
321319
// we need to update the signature to reflect correctness of the signature(which is output d.ts emit) of this file
322320
// This ensures that we dont later during incremental builds considering wrong signature.
@@ -340,7 +338,6 @@ func (h *affectedFilesHandler) updateSnapshot() {
340338
return true
341339
})
342340
h.filesToRemoveDiagnostics.Range(func(file tspath.Path) bool {
343-
h.program.snapshot.semanticDiagnosticsFromOldState.Delete(file)
344341
delete(h.program.snapshot.semanticDiagnosticsPerFile, file)
345342
return true
346343
})

internal/incremental/emitfileshandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (h *emitFilesHandler) updateSnapshot() []*compiler.EmitResult {
242242
} else {
243243
h.program.snapshot.affectedFilesPendingEmit[file] = update.pendingKind
244244
}
245-
if update.result == nil {
245+
if update.result != nil {
246246
results = append(results, update.result)
247247
if len(update.result.Diagnostics) != 0 {
248248
if h.program.snapshot.emitDiagnosticsPerFile == nil {

internal/incremental/program.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ func (h *Program) GetDeclarationDiagnostics(ctx context.Context, file *ast.Sourc
133133
func (h *Program) Emit(ctx context.Context, options compiler.EmitOptions) *compiler.EmitResult {
134134
h.panicIfNoProgram("Emit")
135135

136+
var result *compiler.EmitResult
136137
if h.snapshot.options.NoEmit.IsTrue() {
137-
if options.TargetSourceFile != nil {
138-
return &compiler.EmitResult{EmitSkipped: true}
138+
result = &compiler.EmitResult{EmitSkipped: true}
139+
} else {
140+
result = compiler.HandleNoEmitOnError(ctx, h, options.TargetSourceFile)
141+
if ctx.Err() != nil {
142+
return nil
139143
}
140-
buildInfoResult := h.emitBuildInfo(ctx, options)
141-
buildInfoResult.EmitSkipped = true
142-
return buildInfoResult
143144
}
144-
145-
if result := compiler.HandleNoEmitOnError(ctx, h, options.TargetSourceFile); result != nil {
145+
if result != nil {
146146
if options.TargetSourceFile != nil {
147147
return result
148148
}
@@ -155,10 +155,6 @@ func (h *Program) Emit(ctx context.Context, options compiler.EmitOptions) *compi
155155
}
156156
return result
157157
}
158-
if ctx.Err() != nil {
159-
return nil
160-
}
161-
162158
return emitFiles(ctx, h, options, false)
163159
}
164160

@@ -199,7 +195,6 @@ func (h *Program) collectSemanticDiagnosticsOfAffectedFiles(ctx context.Context,
199195

200196
// Commit changes to snapshot
201197
for file, diagnostics := range diagnosticsPerFile {
202-
h.snapshot.semanticDiagnosticsFromOldState.Delete(file.Path())
203198
h.snapshot.semanticDiagnosticsPerFile[file.Path()] = &diagnosticsOrBuildInfoDiagnosticsWithFileName{diagnostics: diagnostics}
204199
}
205200
if len(h.snapshot.semanticDiagnosticsPerFile) == len(h.program.GetSourceFiles()) && h.snapshot.checkPending && !h.snapshot.options.NoCheck.IsTrue() {

internal/incremental/snapshot.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ type snapshot struct {
249249
// Additional fields that are not serialized but needed to track state
250250

251251
// true if build info emit is pending
252-
buildInfoEmitPending bool
253-
// True if the semantic diagnostics were copied from the old state
254-
semanticDiagnosticsFromOldState collections.Set[tspath.Path]
252+
buildInfoEmitPending bool
255253
allFilesExcludingDefaultLibraryFileOnce sync.Once
256254
// Cache of all files excluding default library file for the current program
257255
allFilesExcludingDefaultLibraryFile []*ast.SourceFile
@@ -401,7 +399,6 @@ func newSnapshotForProgram(program *compiler.Program, oldProgram *Program) *snap
401399
// Unchanged file copy diagnostics
402400
if diagnostics, ok := oldProgram.snapshot.semanticDiagnosticsPerFile[file.Path()]; ok {
403401
snapshot.semanticDiagnosticsPerFile[file.Path()] = diagnostics
404-
snapshot.semanticDiagnosticsFromOldState.Add(file.Path())
405402
}
406403
}
407404
}

testdata/baselines/reference/tscWatch/noEmit/dts-errors-without-dts-enabled.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const a = class { private p = 10; };
77
//// [/home/src/workspaces/project/tsconfig.json] new file
88
{
99
"compilerOptions": {
10-
"noEmit": true,
11-
"outFile": "../outFile.js"
10+
"noEmit": true
1211
}
1312
}
1413

@@ -44,7 +43,7 @@ const a = "hello";
4443
//// [/home/src/workspaces/project/tsconfig.json] modified. new content:
4544
{
4645
"compilerOptions": {
47-
"outFile": "../outFile.js"
46+
4847
}
4948
}
5049

@@ -59,7 +58,7 @@ Output::
5958
{
6059
"compilerOptions": {
6160
"noEmit": true,
62-
"outFile": "../outFile.js"
61+
6362
}
6463
}
6564

@@ -87,7 +86,7 @@ const a = class {
8786
//// [/home/src/workspaces/project/tsconfig.json] modified. new content:
8887
{
8988
"compilerOptions": {
90-
"outFile": "../outFile.js"
89+
9190
}
9291
}
9392

@@ -102,7 +101,7 @@ Output::
102101
{
103102
"compilerOptions": {
104103
"noEmit": true,
105-
"outFile": "../outFile.js"
104+
106105
}
107106
}
108107

testdata/baselines/reference/tscWatch/noEmit/dts-errors.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const a = class { private p = 10; };
88
{
99
"compilerOptions": {
1010
"noEmit": true,
11-
"outFile": "../outFile.js",
1211
"declaration": true
1312
}
1413
}
@@ -60,7 +59,6 @@ const a = "hello";
6059
//// [/home/src/workspaces/project/tsconfig.json] modified. new content:
6160
{
6261
"compilerOptions": {
63-
"outFile": "../outFile.js",
6462
"declaration": true
6563
}
6664
}
@@ -77,7 +75,6 @@ Output::
7775
{
7876
"compilerOptions": {
7977
"noEmit": true,
80-
"outFile": "../outFile.js",
8178
"declaration": true
8279
}
8380
}
@@ -138,7 +135,6 @@ const a = class {
138135
//// [/home/src/workspaces/project/tsconfig.json] modified. new content:
139136
{
140137
"compilerOptions": {
141-
"outFile": "../outFile.js",
142138
"declaration": true
143139
}
144140
}
@@ -167,7 +163,6 @@ Found 1 error in a.ts:1
167163
{
168164
"compilerOptions": {
169165
"noEmit": true,
170-
"outFile": "../outFile.js",
171166
"declaration": true
172167
}
173168
}

0 commit comments

Comments
 (0)