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
13 changes: 13 additions & 0 deletions internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,25 @@ func (p *fileLoader) toPath(file string) tspath.Path {

func (p *fileLoader) addRootTask(fileName string, libFile *LibFile, includeReason *FileIncludeReason) {
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
// Check if file should be included based on extension
// This mirrors the logic from getSourceFileFromReferenceWorker in TypeScript
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
p.rootTasks = append(p.rootTasks, &parseTask{
normalizedFilePath: absPath,
libFile: libFile,
includeReason: includeReason,
})
} else if tspath.HasExtension(absPath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good but arent we currently checking this only for root files and not for resolved triple slash refs or project reference files etc as we did in strada

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true; Strada had this in getSourceFileFromReferenceWorker which was called by processSourceFile, so we do need to do this somewhere else.

Let's try this for fun:

@copilot Reexamine program.ts's getSourceFileFromReferenceWorker, callers, etc, and place this check in the correct place in the Go implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b07f77b. Moved the check from addRootTask to parseTask.load() so it now catches all file inclusion paths (root files, triple-slash references, type references, etc.). Tested with both tsgo a.js and triple-slash references like /// <reference path="helper.js" /> - both now correctly report TS6504.

// File has an extension but it's not in the supported extensions list
// Check if it's a JavaScript file and report the appropriate diagnostic
// This will be caught later in parseTask.load() for consistency with other file inclusion paths
// But for root files, we need to add them to rootTasks so they can be processed
// The diagnostic will be reported in load() before attempting to parse
p.rootTasks = append(p.rootTasks, &parseTask{
normalizedFilePath: absPath,
libFile: libFile,
includeReason: includeReason,
})
}
}

Expand Down
33 changes: 33 additions & 0 deletions internal/compiler/filesparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/module"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
Expand Down Expand Up @@ -68,6 +69,38 @@ func (t *parseTask) load(loader *fileLoader) {
return
}

// Check if file has unsupported extension (similar to getSourceFileFromReferenceWorker in TypeScript)
// Do this check before incrementing file counts
if tspath.HasExtension(t.normalizedFilePath) {
compilerOptions := loader.opts.Config.CompilerOptions()
allowNonTsExtensions := core.Tristate.IsTrue(compilerOptions.AllowNonTsExtensions)
if !allowNonTsExtensions {
canonicalFileName := tspath.GetCanonicalFileName(t.normalizedFilePath, loader.opts.Host.FS().UseCaseSensitiveFileNames())
supported := false
for _, ext := range loader.supportedExtensions {
if tspath.FileExtensionIs(canonicalFileName, ext) {
supported = true
break
}
}
if !supported {
// Report appropriate diagnostic for unsupported extension
if tspath.HasJSFileExtension(canonicalFileName) {
loader.includeProcessor.addProcessingDiagnostic(&processingDiagnostic{
kind: processingDiagnosticKindExplainingFileInclude,
data: &includeExplainingDiagnostic{
diagnosticReason: t.includeReason,
message: diagnostics.File_0_is_a_JavaScript_file_Did_you_mean_to_enable_the_allowJs_option,
args: []any{t.normalizedFilePath},
},
})
}
// File has unsupported extension, don't try to parse it
return
}
}
}

loader.totalFileCount.Add(1)
if t.libFile != nil {
loader.libFileCount.Add(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
error TS5052: Option 'checkJs' cannot be specified without specifying option 'allowJs'.
error TS6504: File 'a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation


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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error TS6504: File 'a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation


!!! error TS6504: File 'a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
==== a.js (0 errors) ====
declare var v;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation
error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation


!!! error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
!!! error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
==== /node_modules/conditions/package.json (0 errors) ====
{
"name": "conditions",
"version": "1.0.0",
"type": "module",
"main": "index.cjs",
"types": "index.d.cts",
"exports": {
".": {
"node": "./index.node.js",
"default": "./index.web.js"
}
}
}

==== /node_modules/conditions/index.node.js (0 errors) ====
export const node = 0;

==== /node_modules/conditions/index.node.d.ts (0 errors) ====
export const node: number;

==== /node_modules/conditions/index.web.js (0 errors) ====
export const web = 0;

==== /node_modules/conditions/index.web.d.ts (0 errors) ====
export const web: number;

==== /main.ts (0 errors) ====
import { web } from "conditions";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation
error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation


!!! error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
!!! error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
==== /node_modules/conditions/package.json (0 errors) ====
{
"name": "conditions",
"version": "1.0.0",
"type": "module",
"main": "index.cjs",
"types": "index.d.cts",
"exports": {
".": {
"node": "./index.node.js",
"default": "./index.web.js"
}
}
}

==== /node_modules/conditions/index.node.js (0 errors) ====
export const node = 0;

==== /node_modules/conditions/index.node.d.ts (0 errors) ====
export const node: number;

==== /node_modules/conditions/index.web.js (0 errors) ====
export const web = 0;

==== /node_modules/conditions/index.web.d.ts (0 errors) ====
export const web: number;

==== /main.ts (0 errors) ====
import { web } from "conditions";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
error TS6504: File '/node_modules/dual/index.cjs' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation
error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation
/main.cts(1,10): error TS2305: Module '"dual"' has no exported member 'esm'.
/main.mts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
/main.ts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.


!!! error TS6504: File '/node_modules/dual/index.cjs' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
!!! error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
==== /node_modules/dual/package.json (0 errors) ====
{
"name": "dual",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
error TS6504: File '/node_modules/dual/index.cjs' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation
error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
The file is in the program because:
Root file specified for compilation
/main.cts(1,10): error TS2305: Module '"dual"' has no exported member 'esm'.
/main.mts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
/main.ts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.


!!! error TS6504: File '/node_modules/dual/index.cjs' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
!!! error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
!!! error TS6504: The file is in the program because:
!!! error TS6504: Root file specified for compilation
==== /node_modules/dual/package.json (0 errors) ====
{
"name": "dual",
Expand Down
Loading
Loading