Skip to content

Commit 7599a10

Browse files
CopilotjakebaileyCopilot
authored
Add missing TS6504 diagnostic for JavaScript files without allowJs (#2104)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jakebailey <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent c86b860 commit 7599a10

File tree

28 files changed

+270
-360
lines changed

28 files changed

+270
-360
lines changed

internal/compiler/fileloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (p *fileLoader) toPath(file string) tspath.Path {
144144

145145
func (p *fileLoader) addRootTask(fileName string, libFile *LibFile, includeReason *FileIncludeReason) {
146146
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
147-
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
147+
if p.opts.Config.CompilerOptions().AllowNonTsExtensions.IsTrue() || tspath.HasExtension(absPath) {
148148
p.rootTasks = append(p.rootTasks, &parseTask{
149149
normalizedFilePath: absPath,
150150
libFile: libFile,

internal/compiler/filesparser.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/microsoft/typescript-go/internal/ast"
99
"github.com/microsoft/typescript-go/internal/collections"
1010
"github.com/microsoft/typescript-go/internal/core"
11+
"github.com/microsoft/typescript-go/internal/diagnostics"
1112
"github.com/microsoft/typescript-go/internal/module"
1213
"github.com/microsoft/typescript-go/internal/tsoptions"
1314
"github.com/microsoft/typescript-go/internal/tspath"
@@ -31,6 +32,7 @@ type parseTask struct {
3132
typeResolutionsInFile module.ModeAwareCache[*module.ResolvedTypeReferenceDirective]
3233
typeResolutionsTrace []module.DiagAndArgs
3334
resolutionDiagnostics []*ast.Diagnostic
35+
processingDiagnostics []*processingDiagnostic
3436
importHelpersImportSpecifier *ast.Node
3537
jsxRuntimeImportSpecifier *jsxRuntimeImportSpecifier
3638

@@ -61,6 +63,34 @@ func (t *parseTask) load(loader *fileLoader) {
6163
return
6264
}
6365

66+
if tspath.HasExtension(t.normalizedFilePath) {
67+
compilerOptions := loader.opts.Config.CompilerOptions()
68+
allowNonTsExtensions := compilerOptions.AllowNonTsExtensions.IsTrue()
69+
if !allowNonTsExtensions {
70+
canonicalFileName := tspath.GetCanonicalFileName(t.normalizedFilePath, loader.opts.Host.FS().UseCaseSensitiveFileNames())
71+
supported := false
72+
for _, ext := range loader.supportedExtensions {
73+
if tspath.FileExtensionIs(canonicalFileName, ext) {
74+
supported = true
75+
break
76+
}
77+
}
78+
if !supported {
79+
if tspath.HasJSFileExtension(canonicalFileName) {
80+
t.processingDiagnostics = append(t.processingDiagnostics, &processingDiagnostic{
81+
kind: processingDiagnosticKindExplainingFileInclude,
82+
data: &includeExplainingDiagnostic{
83+
diagnosticReason: t.includeReason,
84+
message: diagnostics.File_0_is_a_JavaScript_file_Did_you_mean_to_enable_the_allowJs_option,
85+
args: []any{t.normalizedFilePath},
86+
},
87+
})
88+
}
89+
return
90+
}
91+
}
92+
}
93+
6494
loader.totalFileCount.Add(1)
6595
if t.libFile != nil {
6696
loader.libFileCount.Add(1)
@@ -319,6 +349,11 @@ func (w *filesParser) getProcessedFiles(loader *fileLoader) processedFiles {
319349
}
320350
file := task.file
321351
path := task.path
352+
353+
if len(task.processingDiagnostics) > 0 {
354+
loader.includeProcessor.processingDiagnostics = append(loader.includeProcessor.processingDiagnostics, task.processingDiagnostics...)
355+
}
356+
322357
if file == nil {
323358
// !!! sheetal file preprocessing diagnostic explaining getSourceFileFromReferenceWorker
324359
missingFiles = append(missingFiles, task.normalizedFilePath)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
error TS5052: Option 'checkJs' cannot be specified without specifying option 'allowJs'.
2+
error TS6504: File 'a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
3+
The file is in the program because:
4+
Root file specified for compilation
25

36

47
!!! error TS5052: Option 'checkJs' cannot be specified without specifying option 'allowJs'.
8+
!!! error TS6504: File 'a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
9+
!!! error TS6504: The file is in the program because:
10+
!!! error TS6504: Root file specified for compilation
511
==== a.js (0 errors) ====
612
var x;

testdata/baselines/reference/submodule/compiler/checkJsFiles6.errors.txt.diff

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error TS6504: File 'a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
2+
The file is in the program because:
3+
Root file specified for compilation
4+
5+
6+
!!! error TS6504: File 'a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
7+
!!! error TS6504: The file is in the program because:
8+
!!! error TS6504: Root file specified for compilation
9+
==== a.js (0 errors) ====
10+
declare var v;

testdata/baselines/reference/submodule/compiler/jsFileCompilationWithoutJsExtensions.errors.txt.diff

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
2+
The file is in the program because:
3+
Root file specified for compilation
4+
error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
5+
The file is in the program because:
6+
Root file specified for compilation
7+
8+
9+
!!! error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
10+
!!! error TS6504: The file is in the program because:
11+
!!! error TS6504: Root file specified for compilation
12+
!!! error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
13+
!!! error TS6504: The file is in the program because:
14+
!!! error TS6504: Root file specified for compilation
15+
==== /node_modules/conditions/package.json (0 errors) ====
16+
{
17+
"name": "conditions",
18+
"version": "1.0.0",
19+
"type": "module",
20+
"main": "index.cjs",
21+
"types": "index.d.cts",
22+
"exports": {
23+
".": {
24+
"node": "./index.node.js",
25+
"default": "./index.web.js"
26+
}
27+
}
28+
}
29+
30+
==== /node_modules/conditions/index.node.js (0 errors) ====
31+
export const node = 0;
32+
33+
==== /node_modules/conditions/index.node.d.ts (0 errors) ====
34+
export const node: number;
35+
36+
==== /node_modules/conditions/index.web.js (0 errors) ====
37+
export const web = 0;
38+
39+
==== /node_modules/conditions/index.web.d.ts (0 errors) ====
40+
export const web: number;
41+
42+
==== /main.ts (0 errors) ====
43+
import { web } from "conditions";
44+

testdata/baselines/reference/submodule/conformance/bundlerConditionsExcludesNode(module=esnext).errors.txt.diff

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
2+
The file is in the program because:
3+
Root file specified for compilation
4+
error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
5+
The file is in the program because:
6+
Root file specified for compilation
7+
8+
9+
!!! error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
10+
!!! error TS6504: The file is in the program because:
11+
!!! error TS6504: Root file specified for compilation
12+
!!! error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
13+
!!! error TS6504: The file is in the program because:
14+
!!! error TS6504: Root file specified for compilation
15+
==== /node_modules/conditions/package.json (0 errors) ====
16+
{
17+
"name": "conditions",
18+
"version": "1.0.0",
19+
"type": "module",
20+
"main": "index.cjs",
21+
"types": "index.d.cts",
22+
"exports": {
23+
".": {
24+
"node": "./index.node.js",
25+
"default": "./index.web.js"
26+
}
27+
}
28+
}
29+
30+
==== /node_modules/conditions/index.node.js (0 errors) ====
31+
export const node = 0;
32+
33+
==== /node_modules/conditions/index.node.d.ts (0 errors) ====
34+
export const node: number;
35+
36+
==== /node_modules/conditions/index.web.js (0 errors) ====
37+
export const web = 0;
38+
39+
==== /node_modules/conditions/index.web.d.ts (0 errors) ====
40+
export const web: number;
41+
42+
==== /main.ts (0 errors) ====
43+
import { web } from "conditions";
44+

testdata/baselines/reference/submodule/conformance/bundlerConditionsExcludesNode(module=preserve).errors.txt.diff

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)