@@ -14498,6 +14498,34 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
14498
14498
)
14499
14499
}
14500
14500
}
14501
+ } else if c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
14502
+ location.Flags&ast.NodeFlagsAmbient == 0 &&
14503
+ !tspath.IsDeclarationFileName(moduleReference) &&
14504
+ !ast.IsLiteralImportTypeNode(location) &&
14505
+ !ast.IsPartOfTypeOnlyImportOrExportDeclaration(location) {
14506
+ shouldRewrite := c.shouldRewriteModuleSpecifier(moduleReference)
14507
+ if !resolvedModule.ResolvedUsingTsExtension && shouldRewrite {
14508
+ relativeToSourceFile := tspath.GetRelativePathFromDirectory(
14509
+ tspath.GetDirectoryPath(tspath.GetNormalizedAbsolutePath(importingSourceFile.FileName(), c.program.GetCurrentDirectory())),
14510
+ resolvedModule.ResolvedFileName,
14511
+ tspath.ComparePathsOptions{
14512
+ UseCaseSensitiveFileNames: c.program.UseCaseSensitiveFileNames(),
14513
+ CurrentDirectory: c.program.GetCurrentDirectory(),
14514
+ },
14515
+ )
14516
+ c.error(
14517
+ errorNode,
14518
+ diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
14519
+ relativeToSourceFile,
14520
+ )
14521
+ } else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.sourceFileMayBeEmitted(sourceFile) {
14522
+ c.error(
14523
+ errorNode,
14524
+ 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,
14525
+ tspath.GetAnyExtensionFromPath(moduleReference, nil, false),
14526
+ )
14527
+ }
14528
+ // TODO: Add project reference check when GetResolvedProjectReferenceToRedirect is implemented
14501
14529
}
14502
14530
14503
14531
if sourceFile.Symbol != nil {
@@ -30379,3 +30407,26 @@ func (c *Checker) GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) *e
30379
30407
func (c *Checker) GetAliasedSymbol(symbol *ast.Symbol) *ast.Symbol {
30380
30408
return c.resolveAlias(symbol)
30381
30409
}
30410
+
30411
+ func (c *Checker) sourceFileMayBeEmitted(sourceFile *ast.SourceFile) bool {
30412
+ options := c.compilerOptions
30413
+ if options.NoEmit.IsTrue() || options.EmitDeclarationOnly.IsTrue() {
30414
+ return false
30415
+ }
30416
+ // Check if this source file is a declaration file
30417
+ if tspath.IsDeclarationFileName(sourceFile.FileName()) {
30418
+ return false
30419
+ }
30420
+ // Check if this is a JS file and allowJs is disabled
30421
+ if tspath.HasJSFileExtension(sourceFile.FileName()) && !options.AllowJs.IsTrue() {
30422
+ return false
30423
+ }
30424
+ return true
30425
+ }
30426
+
30427
+ func (c *Checker) shouldRewriteModuleSpecifier(specifier string) bool {
30428
+ return c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
30429
+ tspath.PathIsRelative(specifier) &&
30430
+ !tspath.IsDeclarationFileName(specifier) &&
30431
+ tspath.HasTSFileExtension(specifier)
30432
+ }
0 commit comments