Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
51 changes: 51 additions & 0 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14497,6 +14497,34 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
tsExtension,
)
}
} else if c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
location.Flags&ast.NodeFlagsAmbient == 0 &&
!tspath.IsDeclarationFileName(moduleReference) &&
!ast.IsLiteralImportTypeNode(location) &&
!ast.IsPartOfTypeOnlyImportOrExportDeclaration(location) {
shouldRewrite := c.shouldRewriteModuleSpecifier(moduleReference)
if !resolvedModule.ResolvedUsingTsExtension && shouldRewrite {
relativeToSourceFile := tspath.GetRelativePathFromDirectory(
tspath.GetDirectoryPath(tspath.GetNormalizedAbsolutePath(importingSourceFile.FileName(), c.program.GetCurrentDirectory())),
resolvedModule.ResolvedFileName,
tspath.ComparePathsOptions{
UseCaseSensitiveFileNames: c.program.UseCaseSensitiveFileNames(),
CurrentDirectory: c.program.GetCurrentDirectory(),
},
)
c.error(
errorNode,
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
relativeToSourceFile,
)
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.sourceFileMayBeEmitted(sourceFile) {
c.error(
errorNode,
diagnostics.This_import_uses_a_0_extension_to_resolve_to_an_input_TypeScript_file_but_will_not_be_rewritten_during_emit_because_it_is_not_a_relative_path,
tspath.GetAnyExtensionFromPath(moduleReference, nil, false),
)
}
// TODO: Add project reference check when GetResolvedProjectReferenceToRedirect is implemented
}
}

Expand Down Expand Up @@ -30379,3 +30407,26 @@ func (c *Checker) GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) *e
func (c *Checker) GetAliasedSymbol(symbol *ast.Symbol) *ast.Symbol {
return c.resolveAlias(symbol)
}

func (c *Checker) sourceFileMayBeEmitted(sourceFile *ast.SourceFile) bool {
options := c.compilerOptions
if options.NoEmit.IsTrue() || options.EmitDeclarationOnly.IsTrue() {
return false
}
// Check if this source file is a declaration file
if tspath.IsDeclarationFileName(sourceFile.FileName()) {
return false
}
// Check if this is a JS file and allowJs is disabled
if tspath.HasJSFileExtension(sourceFile.FileName()) && !options.AllowJs.IsTrue() {
return false
}
return true
}

func (c *Checker) shouldRewriteModuleSpecifier(specifier string) bool {
return c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
tspath.PathIsRelative(specifier) &&
!tspath.IsDeclarationFileName(specifier) &&
tspath.HasTSFileExtension(specifier)
}
4 changes: 2 additions & 2 deletions internal/tsoptions/declscompiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,8 @@ var commonOptionsWithBuild = []*CommandLineOption{
AffectsSemanticDiagnostics: true,
AffectsBuildInfo: true,
Category: diagnostics.Modules,
// description: diagnostics.Rewrite_ts_tsx_mts_and_cts_file_extensions_in_relative_import_paths_to_their_JavaScript_equivalent_in_output_files,
DefaultValueDescription: false,
Description: diagnostics.Rewrite_ts_tsx_mts_and_cts_file_extensions_in_relative_import_paths_to_their_JavaScript_equivalent_in_output_files,
DefaultValueDescription: false,
},
{
Name: "resolvePackageJsonExports",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".


==== index.ts (1 errors) ====
import foo = require("./foo.ts"); // Error
~~~~~~~~~~
!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
import type _foo = require("./foo.ts"); // Ok

==== foo.ts/index.ts (0 errors) ====
export = {};

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
+++ new.cjsErrors(module=node18).errors.txt
@@= skipped -0, +0 lines =@@
-index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
-
-
-==== index.ts (1 errors) ====
- import foo = require("./foo.ts"); // Error
- ~~~~~~~~~~
+index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".


==== index.ts (1 errors) ====
import foo = require("./foo.ts"); // Error
~~~~~~~~~~
-!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
- import type _foo = require("./foo.ts"); // Ok
-
-==== foo.ts/index.ts (0 errors) ====
- export = {};
-
+<no content>
+!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
import type _foo = require("./foo.ts"); // Ok

==== foo.ts/index.ts (0 errors) ====
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".


==== index.ts (1 errors) ====
import foo = require("./foo.ts"); // Error
~~~~~~~~~~
!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
import type _foo = require("./foo.ts"); // Ok

==== foo.ts/index.ts (0 errors) ====
export = {};

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
+++ new.cjsErrors(module=nodenext).errors.txt
@@= skipped -0, +0 lines =@@
-index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
-
-
-==== index.ts (1 errors) ====
- import foo = require("./foo.ts"); // Error
- ~~~~~~~~~~
+index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".


==== index.ts (1 errors) ====
import foo = require("./foo.ts"); // Error
~~~~~~~~~~
-!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
- import type _foo = require("./foo.ts"); // Ok
-
-==== foo.ts/index.ts (0 errors) ====
- export = {};
-
+<no content>
+!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
import type _foo = require("./foo.ts"); // Ok

==== foo.ts/index.ts (0 errors) ====