Skip to content

Commit 73a3dda

Browse files
Copilotandrewbranch
andcommitted
Fix TS2878 error location reporting by adding errorNode checks
Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
1 parent 2157b94 commit 73a3dda

File tree

4 files changed

+599
-4
lines changed

4 files changed

+599
-4
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4733,7 +4733,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
47334733
error(errorNode, resolutionDiagnostic, moduleReference, resolvedModule.resolvedFileName);
47344734
}
47354735

4736-
if (resolvedModule.resolvedUsingTsExtension && isDeclarationFileName(moduleReference)) {
4736+
if (errorNode && resolvedModule.resolvedUsingTsExtension && isDeclarationFileName(moduleReference)) {
47374737
const importOrExport = findAncestor(location, isImportDeclaration)?.importClause ||
47384738
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
47394739
if (errorNode && importOrExport && !importOrExport.isTypeOnly || findAncestor(location, isImportCall)) {
@@ -4744,16 +4744,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
47444744
);
47454745
}
47464746
}
4747-
else if (resolvedModule.resolvedUsingTsExtension && !shouldAllowImportingTsExtension(compilerOptions, currentSourceFile.fileName)) {
4747+
else if (errorNode && resolvedModule.resolvedUsingTsExtension && !shouldAllowImportingTsExtension(compilerOptions, currentSourceFile.fileName)) {
47484748
const importOrExport = findAncestor(location, isImportDeclaration)?.importClause ||
47494749
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
47504750
if (errorNode && !(importOrExport?.isTypeOnly || findAncestor(location, isImportTypeNode))) {
47514751
const tsExtension = Debug.checkDefined(tryExtractTSExtension(moduleReference));
47524752
error(errorNode, Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled, tsExtension);
47534753
}
47544754
}
4755-
else if (
4756-
compilerOptions.rewriteRelativeImportExtensions
4755+
else if (
4756+
errorNode
4757+
&& compilerOptions.rewriteRelativeImportExtensions
47574758
&& !(location.flags & NodeFlags.Ambient)
47584759
&& !isDeclarationFileName(moduleReference)
47594760
&& !isLiteralImportTypeNode(location)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// === Syntax and Semantic Diagnostics ===
2+
Syntactic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences4.ts':
3+
4+
5+
==== /tests/cases/fourslash/server/src/libs/utils/helper.ts (0 errors) ====
6+
export function helper() { return 42; }
7+
==== /tests/cases/fourslash/server/src/apps/main/index.ts (0 errors) ====
8+
import { helper } from "../../libs/utils/helper.ts";
9+
10+
Semantic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences4.ts':
11+
/tests/cases/fourslash/server/src/apps/main/index.ts(1,24): error TS2878: This import path is unsafe to rewrite because it resolves to another project, and the relative path between the projects' output files is not the same as the relative path between its input files.
12+
13+
14+
==== /tests/cases/fourslash/server/src/libs/utils/helper.ts (0 errors) ====
15+
export function helper() { return 42; }
16+
==== /tests/cases/fourslash/server/src/apps/main/index.ts (1 errors) ====
17+
import { helper } from "../../libs/utils/helper.ts";
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS2878: This import path is unsafe to rewrite because it resolves to another project, and the relative path between the projects' output files is not the same as the relative path between its input files.

0 commit comments

Comments
 (0)