Merge pull request #6469 from ShyykoSerhiy/fix-528

Fixes issue https://github.com/Microsoft/TypeScript/issues/528 (Show better error message for unresolved references due to missing prefix)
This commit is contained in:
Daniel Rosenwasser
2016-01-14 13:13:07 -08:00
11 changed files with 79 additions and 35 deletions

View File

@@ -741,7 +741,9 @@ namespace ts {
if (!result) {
if (nameNotFoundMessage) {
error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
if (!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg)) {
error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
}
}
return undefined;
}
@@ -778,6 +780,40 @@ namespace ts {
return result;
}
function checkAndReportErrorForMissingPrefix(errorLocation: Node, name: string, nameArg: string | Identifier): boolean {
if (!errorLocation || (errorLocation.kind === SyntaxKind.Identifier && (isTypeReferenceIdentifier(<Identifier>errorLocation)) || isInTypeQuery(errorLocation))) {
return false;
}
const container = getThisContainer(errorLocation, /* includeArrowFunctions */ true);
let location = container;
while (location) {
if (isClassLike(location.parent)) {
const symbol = getSymbolOfNode(location.parent);
let classType: Type;
if (location.flags & NodeFlags.Static) {
classType = getTypeOfSymbol(symbol);
if (getPropertyOfType(classType, name)) {
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg), symbolToString(symbol));
return true;
}
}
else {
if (location === container) {
classType = (<InterfaceType>getDeclaredTypeOfSymbol(symbol)).thisType;
if (getPropertyOfType(classType, name)) {
error(errorLocation, Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
return true;
}
}
}
}
location = location.parent;
}
return false;
}
function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void {
Debug.assert((result.flags & SymbolFlags.BlockScopedVariable) !== 0);
// Block-scoped variables cannot be used before their definition

View File

@@ -1767,6 +1767,14 @@
"category": "Error",
"code": 2661
},
"Cannot find name '{0}'. Did you mean the static member '{1}.{0}'?": {
"category": "Error",
"code": 2662
},
"Cannot find name '{0}'. Did you mean the instance member 'this.{0}'?": {
"category": "Error",
"code": 2663
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000