Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions internal/execute/incremental/affectedfileshandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/checker"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tspath"
)
Expand Down Expand Up @@ -67,19 +66,7 @@ func (h *affectedFilesHandler) removeDiagnosticsOfLibraryFiles() {
}

func (h *affectedFilesHandler) computeDtsSignature(file *ast.SourceFile) string {
var signature string
h.program.program.Emit(h.ctx, compiler.EmitOptions{
TargetSourceFiles: core.SingleElementSlice(file),
EmitOnly: compiler.EmitOnlyForcedDts,
WriteFile: func(fileName string, text string, data *compiler.WriteFileData) error {
if !tspath.IsDeclarationFileName(fileName) {
panic("File extension for signature expected to be dts, got : " + fileName)
}
signature = h.program.snapshot.computeSignatureWithDiagnostics(file, text, data)
return nil
},
})
return signature
return h.program.snapshot.computeDtsSignatureOfFile(h.ctx, h.program.program, file)
}

func (h *affectedFilesHandler) updateShapeSignature(file *ast.SourceFile, useFileVersionAsSignature bool) bool {
Expand All @@ -101,8 +88,17 @@ func (h *affectedFilesHandler) updateShapeSignature(file *ast.SourceFile, useFil
}
// Default is to use file version as signature
if update.signature == "" {
update.signature = info.version
update.kind = SignatureUpdateKindUsedVersion
if useFileVersionAsSignature && !file.IsDeclarationFile &&
!h.program.snapshot.options.GetEmitDeclarations() &&
prevSignature != "" && prevSignature != info.version {
update.signature = h.computeDtsSignature(file)
}
if update.signature == "" {
update.signature = info.version
update.kind = SignatureUpdateKindUsedVersion
} else {
update.kind = SignatureUpdateKindComputedDts
}
}
return update.signature != prevSignature
}
Expand Down
5 changes: 5 additions & 0 deletions internal/execute/incremental/programtosnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ func (t *toProgramSnapshot) computeProgramFileChanges() {
} else {
t.snapshot.addFileToAffectedFilesPendingEmit(file.Path(), GetFileEmitKind(t.snapshot.options))
signature = version
if !file.IsDeclarationFile && !t.snapshot.options.GetEmitDeclarations() {
if dtsSignature := t.snapshot.computeDtsSignatureOfFile(context.Background(), t.program, file); dtsSignature != "" {
signature = dtsSignature
}
}
}
t.snapshot.fileInfos.Store(file.Path(), &FileInfo{
version: version,
Expand Down
17 changes: 17 additions & 0 deletions internal/execute/incremental/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package incremental

import (
"context"
"encoding/hex"
"fmt"
"strings"
Expand Down Expand Up @@ -396,6 +397,22 @@ func (s *snapshot) computeSignatureWithDiagnostics(file *ast.SourceFile, text st
return s.computeHash(builder.String())
}

func (s *snapshot) computeDtsSignatureOfFile(ctx context.Context, program *compiler.Program, file *ast.SourceFile) string {
var signature string
program.Emit(ctx, compiler.EmitOptions{
TargetSourceFiles: core.SingleElementSlice(file),
EmitOnly: compiler.EmitOnlyForcedDts,
WriteFile: func(fileName string, text string, data *compiler.WriteFileData) error {
if !tspath.IsDeclarationFileName(fileName) {
panic("File extension for signature expected to be dts, got : " + fileName)
}
signature = s.computeSignatureWithDiagnostics(file, text, data)
return nil
},
})
return signature
}

func diagnosticToStringBuilder(diagnostic *ast.Diagnostic, file *ast.SourceFile, builder *strings.Builder) {
if diagnostic == nil {
return
Expand Down
65 changes: 65 additions & 0 deletions internal/execute/tsctests/tsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,12 @@ func TestTscIncremental(t *testing.T) {
sys.writeFileNoError("/home/src/workspaces/project/constants.ts", "export default 2;")
},
},
{
caption: "whitespace only edit of file whose inferred declaration changed",
edit: func(sys *TestSys) {
sys.appendFile("/home/src/workspaces/project/class1.ts", "\n")
},
},
},
},
{
Expand All @@ -1748,6 +1754,12 @@ func TestTscIncremental(t *testing.T) {
sys.writeFileNoError("/home/src/workspaces/project/constants.ts", "export default 2;")
},
},
{
caption: "whitespace only edit of file whose inferred declaration changed",
edit: func(sys *TestSys) {
sys.appendFile("/home/src/workspaces/project/class1.ts", "\n")
},
},
},
},
{
Expand Down Expand Up @@ -2234,6 +2246,59 @@ func TestTscIncremental(t *testing.T) {
},
commandLineArgs: []string{"--noEmit"},
},
{
subScenario: "whitespace edit through const enum barrel",
files: FileMap{
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
{
"compilerOptions": {
"incremental": true,
"module": "esnext",
"moduleResolution": "bundler",
"strict": true,
},
"include": ["src/**/*.ts"],
}`),
"/home/src/workspaces/project/src/core.ts": stringtestutil.Dedent(`
export interface CoreThing { a: number }
export const CORE = 1;
`),
"/home/src/workspaces/project/src/core-barrel.ts": "export * from './core';\n",
"/home/src/workspaces/project/src/mid.ts": stringtestutil.Dedent(`
import { CORE, CoreThing } from './core-barrel';
export const MID: number = CORE + 1;
export function useCore(x: CoreThing): number { return x.a + MID; }
export const enum MidMode { A = 1, B = 2 }
`),
"/home/src/workspaces/project/src/index.ts": "export * from './mid';\n",
"/home/src/workspaces/project/src/leaf0.ts": stringtestutil.Dedent(`
import { MID } from './index';
export const L0: number = MID + 0;
`),
"/home/src/workspaces/project/src/leaf1.ts": stringtestutil.Dedent(`
import { MID } from './index';
export const L1: number = MID + 1;
`),
"/home/src/workspaces/project/src/leaf2.ts": stringtestutil.Dedent(`
import { MID } from './index';
export const L2: number = MID + 2;
`),
},
edits: []*tscEdit{
{
caption: "whitespace only edit of mid.ts",
edit: func(sys *TestSys) {
sys.appendFile("/home/src/workspaces/project/src/mid.ts", "\n")
},
},
{
caption: "same whitespace only edit of mid.ts again",
edit: func(sys *TestSys) {
sys.appendFile("/home/src/workspaces/project/src/mid.ts", "\n")
},
},
},
},
}

for _, test := range testCases {
Expand Down
Loading