Skip to content

Commit b5ba315

Browse files
author
Andy
authored
Merge pull request #11704 from Microsoft/refactor_module_resolution
Return both ts and js results from module resolution
2 parents e6bea90 + 718d57f commit b5ba315

File tree

51 files changed

+957
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+957
-389
lines changed

src/compiler/checker.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,8 @@ namespace ts {
13741374
}
13751375

13761376
const resolvedModule = getResolvedModule(getSourceFileOfNode(location), moduleReference);
1377-
const sourceFile = resolvedModule && host.getSourceFile(resolvedModule.resolvedFileName);
1377+
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
1378+
const sourceFile = resolvedModule && !resolutionDiagnostic && host.getSourceFile(resolvedModule.resolvedFileName);
13781379
if (sourceFile) {
13791380
if (sourceFile.symbol) {
13801381
// merged symbol is module declaration symbol combined with all augmentations
@@ -1396,13 +1397,18 @@ namespace ts {
13961397

13971398
if (moduleNotFoundError) {
13981399
// report errors only if it was requested
1399-
const tsExtension = tryExtractTypeScriptExtension(moduleName);
1400-
if (tsExtension) {
1401-
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
1402-
error(errorNode, diag, tsExtension, removeExtension(moduleName, tsExtension));
1400+
if (resolutionDiagnostic) {
1401+
error(errorNode, resolutionDiagnostic, moduleName, resolvedModule.resolvedFileName);
14031402
}
14041403
else {
1405-
error(errorNode, moduleNotFoundError, moduleName);
1404+
const tsExtension = tryExtractTypeScriptExtension(moduleName);
1405+
if (tsExtension) {
1406+
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
1407+
error(errorNode, diag, tsExtension, removeExtension(moduleName, tsExtension));
1408+
}
1409+
else {
1410+
error(errorNode, moduleNotFoundError, moduleName);
1411+
}
14061412
}
14071413
}
14081414
return undefined;

src/compiler/core.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -1938,10 +1938,6 @@ namespace ts {
19381938
return path.substring(0, path.length - extension.length);
19391939
}
19401940

1941-
export function isJsxOrTsxExtension(ext: string): boolean {
1942-
return ext === ".jsx" || ext === ".tsx";
1943-
}
1944-
19451941
export function changeExtension<T extends string | Path>(path: T, newExtension: string): T {
19461942
return <T>(removeFileExtension(path) + newExtension);
19471943
}
@@ -2134,4 +2130,33 @@ namespace ts {
21342130
// pos === undefined || pos === null || isNaN(pos) || pos < 0;
21352131
return !(pos >= 0);
21362132
}
2133+
2134+
/** True if an extension is one of the supported TypeScript extensions. */
2135+
export function extensionIsTypeScript(ext: Extension): boolean {
2136+
return ext <= Extension.LastTypeScriptExtension;
2137+
}
2138+
2139+
/**
2140+
* Gets the extension from a path.
2141+
* Path must have a valid extension.
2142+
*/
2143+
export function extensionFromPath(path: string): Extension {
2144+
if (fileExtensionIs(path, ".d.ts")) {
2145+
return Extension.Dts;
2146+
}
2147+
if (fileExtensionIs(path, ".ts")) {
2148+
return Extension.Ts;
2149+
}
2150+
if (fileExtensionIs(path, ".tsx")) {
2151+
return Extension.Tsx;
2152+
}
2153+
if (fileExtensionIs(path, ".js")) {
2154+
return Extension.Js;
2155+
}
2156+
if (fileExtensionIs(path, ".jsx")) {
2157+
return Extension.Jsx;
2158+
}
2159+
Debug.fail(`File ${path} has unknown extension.`);
2160+
return Extension.Js;
2161+
}
21372162
}

src/compiler/diagnosticMessages.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -2693,7 +2693,7 @@
26932693
"category": "Message",
26942694
"code": 6099
26952695
},
2696-
"'package.json' does not have 'types' field.": {
2696+
"'package.json' does not have a 'types' or 'main' field.": {
26972697
"category": "Message",
26982698
"code": 6100
26992699
},
@@ -2841,7 +2841,7 @@
28412841
"category": "Message",
28422842
"code": 6136
28432843
},
2844-
"No types specified in 'package.json' but 'allowJs' is set, so returning 'main' value of '{0}'": {
2844+
"No types specified in 'package.json', so returning 'main' value of '{0}'": {
28452845
"category": "Message",
28462846
"code": 6137
28472847
},
@@ -2861,6 +2861,14 @@
28612861
"category": "Message",
28622862
"code": 6141
28632863
},
2864+
"Module '{0}' was resolved to '{1}', but '--jsx' is not set.": {
2865+
"category": "Error",
2866+
"code": 6142
2867+
},
2868+
"Module '{0}' was resolved to '{1}', but '--allowJs' is not set.": {
2869+
"category": "Error",
2870+
"code": 6143
2871+
},
28642872
"Variable '{0}' implicitly has an '{1}' type.": {
28652873
"category": "Error",
28662874
"code": 7005

0 commit comments

Comments
 (0)