Give related error on declaration location for used-before-declared errors.
This commit is contained in:
parent
7dc66e916a
commit
4459730e5b
2 changed files with 33 additions and 7 deletions
|
@ -1655,18 +1655,34 @@ namespace ts {
|
|||
if (declaration === undefined) return Debug.fail("Declaration to checkResolvedBlockScopedVariable is undefined");
|
||||
|
||||
if (!(declaration.flags & NodeFlags.Ambient) && !isBlockScopedNameDeclaredBeforeUse(declaration, errorLocation)) {
|
||||
let err;
|
||||
const declarationName = declarationNameToString(getNameOfDeclaration(declaration));
|
||||
if (result.flags & SymbolFlags.BlockScopedVariable) {
|
||||
error(errorLocation, Diagnostics.Block_scoped_variable_0_used_before_its_declaration, declarationNameToString(getNameOfDeclaration(declaration)));
|
||||
err = error(errorLocation, Diagnostics.Block_scoped_variable_0_used_before_its_declaration, declarationName);
|
||||
}
|
||||
else if (result.flags & SymbolFlags.Class) {
|
||||
error(errorLocation, Diagnostics.Class_0_used_before_its_declaration, declarationNameToString(getNameOfDeclaration(declaration)));
|
||||
err = error(errorLocation, Diagnostics.Class_0_used_before_its_declaration, declarationName);
|
||||
}
|
||||
else if (result.flags & SymbolFlags.RegularEnum) {
|
||||
error(errorLocation, Diagnostics.Enum_0_used_before_its_declaration, declarationNameToString(getNameOfDeclaration(declaration)));
|
||||
err = error(errorLocation, Diagnostics.Enum_0_used_before_its_declaration, declarationName);
|
||||
}
|
||||
else {
|
||||
Debug.assert(!!(result.flags & SymbolFlags.ConstEnum));
|
||||
}
|
||||
|
||||
if (err) {
|
||||
placeRelatedSpanOnLaterDeclaration(declarationName, declaration, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function placeRelatedSpanOnLaterDeclaration(declarationName: string, declarationLocation: Declaration, diagnostic: Diagnostic) {
|
||||
Debug.assert(!diagnostic.relatedInformation);
|
||||
diagnostic.relatedInformation = [
|
||||
createDiagnosticForNode(declarationLocation, Diagnostics._0_was_declared_here, declarationName)
|
||||
];
|
||||
}
|
||||
|
||||
/* Starting from 'initial' node walk up the parent chain until 'stopAt' node is reached.
|
||||
* If at any point current node is equal to 'parent' node - return true.
|
||||
* Return false if 'stopAt' node is reached or isFunctionLike(current) === true.
|
||||
|
@ -17460,16 +17476,22 @@ namespace ts {
|
|||
return;
|
||||
}
|
||||
|
||||
let err;
|
||||
const declarationName = idText(right);
|
||||
if (isInPropertyInitializer(node) &&
|
||||
!isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
|
||||
&& !isPropertyDeclaredInAncestorClass(prop)) {
|
||||
error(right, Diagnostics.Block_scoped_variable_0_used_before_its_declaration, idText(right));
|
||||
err = error(right, Diagnostics.Block_scoped_variable_0_used_before_its_declaration, declarationName);
|
||||
}
|
||||
else if (valueDeclaration.kind === SyntaxKind.ClassDeclaration &&
|
||||
node.parent.kind !== SyntaxKind.TypeReference &&
|
||||
!(valueDeclaration.flags & NodeFlags.Ambient) &&
|
||||
!isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)) {
|
||||
error(right, Diagnostics.Class_0_used_before_its_declaration, idText(right));
|
||||
err = error(right, Diagnostics.Class_0_used_before_its_declaration, declarationName);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
placeRelatedSpanOnLaterDeclaration(declarationName, valueDeclaration, err);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19083,8 +19105,8 @@ namespace ts {
|
|||
if (importNode && !isImportCall(importNode)) {
|
||||
const sigs = getSignaturesOfType(getTypeOfSymbol(getSymbolLinks(apparentType.symbol).target!), kind);
|
||||
if (!sigs || !sigs.length) return;
|
||||
diagnostic.relatedInformation = diagnostic.relatedInformation || [];
|
||||
diagnostic.relatedInformation.push(createDiagnosticForNode(importNode, Diagnostics.Type_originates_at_this_import_A_namespace_style_import_cannot_be_called_or_constructed_and_will_cause_a_failure_at_runtime_Consider_using_a_default_import_or_import_require_here_instead));
|
||||
Debug.assert(!diagnostic.relatedInformation);
|
||||
diagnostic.relatedInformation = [createDiagnosticForNode(importNode, Diagnostics.Type_originates_at_this_import_A_namespace_style_import_cannot_be_called_or_constructed_and_will_cause_a_failure_at_runtime_Consider_using_a_default_import_or_import_require_here_instead)];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2397,6 +2397,10 @@
|
|||
"category": "Error",
|
||||
"code": 2727
|
||||
},
|
||||
"'{0}' was declared here.": {
|
||||
"category": "Error",
|
||||
"code": 2728
|
||||
},
|
||||
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
|
|
Loading…
Reference in a new issue