Skip to content

Commit b07f77b

Browse files
Copilotjakebailey
andcommitted
Move JS file diagnostic check to common file loading path
Moved the TS6504 diagnostic check from addRootTask to parseTask.load() so it catches all file inclusion paths (root files, triple-slash refs, type refs, etc.), not just root files. This matches TypeScript's behavior where getSourceFileFromReferenceWorker is called for all file types. Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent ae11735 commit b07f77b

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

internal/compiler/fileloader.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/microsoft/typescript-go/internal/ast"
1111
"github.com/microsoft/typescript-go/internal/collections"
1212
"github.com/microsoft/typescript-go/internal/core"
13-
"github.com/microsoft/typescript-go/internal/diagnostics"
1413
"github.com/microsoft/typescript-go/internal/module"
1514
"github.com/microsoft/typescript-go/internal/tsoptions"
1615
"github.com/microsoft/typescript-go/internal/tspath"
@@ -260,6 +259,8 @@ func (p *fileLoader) toPath(file string) tspath.Path {
260259

261260
func (p *fileLoader) addRootTask(fileName string, libFile *LibFile, includeReason *FileIncludeReason) {
262261
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
262+
// Check if file should be included based on extension
263+
// This mirrors the logic from getSourceFileFromReferenceWorker in TypeScript
263264
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
264265
p.rootTasks = append(p.rootTasks, &parseTask{
265266
normalizedFilePath: absPath,
@@ -269,17 +270,14 @@ func (p *fileLoader) addRootTask(fileName string, libFile *LibFile, includeReaso
269270
} else if tspath.HasExtension(absPath) {
270271
// File has an extension but it's not in the supported extensions list
271272
// Check if it's a JavaScript file and report the appropriate diagnostic
272-
canonicalFileName := tspath.GetCanonicalFileName(absPath, p.opts.Host.FS().UseCaseSensitiveFileNames())
273-
if tspath.HasJSFileExtension(canonicalFileName) {
274-
p.includeProcessor.addProcessingDiagnostic(&processingDiagnostic{
275-
kind: processingDiagnosticKindExplainingFileInclude,
276-
data: &includeExplainingDiagnostic{
277-
diagnosticReason: includeReason,
278-
message: diagnostics.File_0_is_a_JavaScript_file_Did_you_mean_to_enable_the_allowJs_option,
279-
args: []any{fileName},
280-
},
281-
})
282-
}
273+
// This will be caught later in parseTask.load() for consistency with other file inclusion paths
274+
// But for root files, we need to add them to rootTasks so they can be processed
275+
// The diagnostic will be reported in load() before attempting to parse
276+
p.rootTasks = append(p.rootTasks, &parseTask{
277+
normalizedFilePath: absPath,
278+
libFile: libFile,
279+
includeReason: includeReason,
280+
})
283281
}
284282
}
285283

internal/compiler/filesparser.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/microsoft/typescript-go/internal/ast"
88
"github.com/microsoft/typescript-go/internal/collections"
99
"github.com/microsoft/typescript-go/internal/core"
10+
"github.com/microsoft/typescript-go/internal/diagnostics"
1011
"github.com/microsoft/typescript-go/internal/module"
1112
"github.com/microsoft/typescript-go/internal/tsoptions"
1213
"github.com/microsoft/typescript-go/internal/tspath"
@@ -68,6 +69,38 @@ func (t *parseTask) load(loader *fileLoader) {
6869
return
6970
}
7071

72+
// Check if file has unsupported extension (similar to getSourceFileFromReferenceWorker in TypeScript)
73+
// Do this check before incrementing file counts
74+
if tspath.HasExtension(t.normalizedFilePath) {
75+
compilerOptions := loader.opts.Config.CompilerOptions()
76+
allowNonTsExtensions := core.Tristate.IsTrue(compilerOptions.AllowNonTsExtensions)
77+
if !allowNonTsExtensions {
78+
canonicalFileName := tspath.GetCanonicalFileName(t.normalizedFilePath, loader.opts.Host.FS().UseCaseSensitiveFileNames())
79+
supported := false
80+
for _, ext := range loader.supportedExtensions {
81+
if tspath.FileExtensionIs(canonicalFileName, ext) {
82+
supported = true
83+
break
84+
}
85+
}
86+
if !supported {
87+
// Report appropriate diagnostic for unsupported extension
88+
if tspath.HasJSFileExtension(canonicalFileName) {
89+
loader.includeProcessor.addProcessingDiagnostic(&processingDiagnostic{
90+
kind: processingDiagnosticKindExplainingFileInclude,
91+
data: &includeExplainingDiagnostic{
92+
diagnosticReason: t.includeReason,
93+
message: diagnostics.File_0_is_a_JavaScript_file_Did_you_mean_to_enable_the_allowJs_option,
94+
args: []any{t.normalizedFilePath},
95+
},
96+
})
97+
}
98+
// File has unsupported extension, don't try to parse it
99+
return
100+
}
101+
}
102+
}
103+
71104
loader.totalFileCount.Add(1)
72105
if t.libFile != nil {
73106
loader.libFileCount.Add(1)

0 commit comments

Comments
 (0)