Improve error message

This commit is contained in:
Andy Hanson
2016-08-02 06:58:26 -07:00
parent 2821d98265
commit 0f134ed69e
7 changed files with 71 additions and 22 deletions

View File

@@ -1366,8 +1366,10 @@ namespace ts {
if (moduleNotFoundError) {
// report errors only if it was requested
if (hasTypeScriptFileExtensionNonDts(moduleName)) {
error(moduleReferenceLiteral, Diagnostics.Module_name_should_not_include_a_ts_extension_Colon_0, moduleName);
const nonDtsExtension = tryExtractTypeScriptExtensionNonDts(moduleName);
if (nonDtsExtension) {
const diag = Diagnostics.An_import_path_should_not_end_with_a_0_extension_Consider_importing_1_instead;
error(moduleReferenceLiteral, diag, nonDtsExtension, removeExtension(moduleName, nonDtsExtension));
}
else {
error(moduleReferenceLiteral, moduleNotFoundError, moduleName);

View File

@@ -93,6 +93,17 @@ namespace ts {
return undefined;
}
/** Works like Array.prototype.find. */
export function find<T>(array: T[], predicate: (element: T, index: number) => boolean): T | undefined {
for (let i = 0, len = array.length; i < len; i++) {
const value = array[i];
if (predicate(value, i)) {
return value;
}
}
return undefined;
}
export function contains<T>(array: T[], value: T): boolean {
if (array) {
for (const v of array) {
@@ -941,7 +952,7 @@ namespace ts {
* [^./] # matches everything up to the first . character (excluding directory seperators)
* (\\.(?!min\\.js$))? # matches . characters but not if they are part of the .min.js file extension
*/
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
const singleAsteriskRegexFragmentOther = "[^/]*";
export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude") {
@@ -1271,8 +1282,12 @@ namespace ts {
return path;
}
export function tryRemoveExtension(path: string, extension: string): string {
return fileExtensionIs(path, extension) ? path.substring(0, path.length - extension.length) : undefined;
export function tryRemoveExtension(path: string, extension: string): string | undefined {
return fileExtensionIs(path, extension) ? removeExtension(path, extension) : undefined;
}
export function removeExtension(path: string, extension: string): string {
return path.substring(0, path.length - extension.length);
}
export function isJsxOrTsxExtension(ext: string): boolean {

View File

@@ -1951,7 +1951,7 @@
"category": "Error",
"code": 2690
},
"Module name should not include a '.ts' extension: '{0}'.": {
"An import path should not end with a '{0}' extension. Consider importing '{1}' instead.": {
"category": "Error",
"code": 2691
},

View File

@@ -2726,8 +2726,9 @@ namespace ts {
return forEach(supportedTypeScriptExtensions, extension => fileExtensionIs(fileName, extension));
}
export function hasTypeScriptFileExtensionNonDts(fileName: string) {
return forEach(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension));
/** Return ".ts" or ".tsx" if that is the extension. */
export function tryExtractTypeScriptExtensionNonDts(fileName: string): string | undefined {
return find(supportedTypeScriptExtensionsNonDts, extension => fileExtensionIs(fileName, extension));
}
/**