@@ -336,8 +336,8 @@ namespace ts {
336
336
}
337
337
338
338
function addUnusedDiagnostics() {
339
- checkUnusedIdentifiers(getPotentiallyUnusedIdentifiers(file), (kind, diag) => {
340
- if (!unusedIsError(kind)) {
339
+ checkUnusedIdentifiers(getPotentiallyUnusedIdentifiers(file), (containingNode, kind, diag) => {
340
+ if (!containsParseError(containingNode) && ! unusedIsError(kind)) {
341
341
(diagnostics || (diagnostics = [])).push({ ...diag, category: DiagnosticCategory.Suggestion });
342
342
}
343
343
});
@@ -646,7 +646,8 @@ namespace ts {
646
646
Local,
647
647
Parameter,
648
648
}
649
- type AddUnusedDiagnostic = (type: UnusedKind, diagnostic: DiagnosticWithLocation) => void;
649
+ /** @param containingNode Node to check for parse error */
650
+ type AddUnusedDiagnostic = (containingNode: Node, type: UnusedKind, diagnostic: DiagnosticWithLocation) => void;
650
651
651
652
const builtinGlobals = createSymbolTable();
652
653
builtinGlobals.set(undefinedSymbol.escapedName, undefinedSymbol);
@@ -4682,13 +4683,7 @@ namespace ts {
4682
4683
}
4683
4684
}
4684
4685
// Use contextual parameter type if one is available
4685
- let type: Type | undefined;
4686
- if (declaration.symbol.escapedName === "this") {
4687
- type = getContextualThisParameterType(func);
4688
- }
4689
- else {
4690
- type = getContextuallyTypedParameterType(declaration);
4691
- }
4686
+ const type = declaration.symbol.escapedName === "this" ? getContextualThisParameterType(func) : getContextuallyTypedParameterType(declaration);
4692
4687
if (type) {
4693
4688
return addOptionality(type, isOptional);
4694
4689
}
@@ -16209,7 +16204,7 @@ namespace ts {
16209
16204
16210
16205
// If the given type is an object or union type with a single signature, and if that signature has at
16211
16206
// least as many parameters as the given function, return the signature. Otherwise return undefined.
16212
- function getContextualCallSignature(type: Type, node: FunctionExpression | ArrowFunction | MethodDeclaration ): Signature | undefined {
16207
+ function getContextualCallSignature(type: Type, node: SignatureDeclaration ): Signature | undefined {
16213
16208
const signatures = getSignaturesOfType(type, SignatureKind.Call);
16214
16209
if (signatures.length === 1) {
16215
16210
const signature = signatures[0];
@@ -16220,7 +16215,7 @@ namespace ts {
16220
16215
}
16221
16216
16222
16217
/** If the contextual signature has fewer parameters than the function expression, do not use it */
16223
- function isAritySmaller(signature: Signature, target: FunctionExpression | ArrowFunction | MethodDeclaration ) {
16218
+ function isAritySmaller(signature: Signature, target: SignatureDeclaration ) {
16224
16219
let targetParameterCount = 0;
16225
16220
for (; targetParameterCount < target.parameters.length; targetParameterCount++) {
16226
16221
const param = target.parameters[targetParameterCount];
@@ -23538,12 +23533,13 @@ namespace ts {
23538
23533
// yielded values. The only way to trigger these errors is to try checking its return type.
23539
23534
getReturnTypeOfSignature(getSignatureFromDeclaration(node));
23540
23535
}
23541
- // A js function declaration can have a @type tag instead of a return type node, but that type must have a call signature
23542
- if (isInJavaScriptFile(node)) {
23543
- const typeTag = getJSDocTypeTag(node);
23544
- if (typeTag && typeTag.typeExpression && !getSignaturesOfType(getTypeFromTypeNode(typeTag.typeExpression), SignatureKind.Call).length) {
23545
- error(typeTag, Diagnostics.The_type_of_a_function_declaration_must_be_callable);
23546
- }
23536
+ }
23537
+
23538
+ // A js function declaration can have a @type tag instead of a return type node, but that type must have a call signature
23539
+ if (isInJavaScriptFile(node)) {
23540
+ const typeTag = getJSDocTypeTag(node);
23541
+ if (typeTag && typeTag.typeExpression && !getContextualCallSignature(getTypeFromTypeNode(typeTag.typeExpression), node)) {
23542
+ error(typeTag, Diagnostics.The_type_of_a_function_declaration_must_match_the_function_s_signature);
23547
23543
}
23548
23544
}
23549
23545
}
@@ -23617,7 +23613,7 @@ namespace ts {
23617
23613
function errorUnusedLocal(declaration: Declaration, name: string, addDiagnostic: AddUnusedDiagnostic) {
23618
23614
const node = getNameOfDeclaration(declaration) || declaration;
23619
23615
const message = isTypeDeclaration(declaration) ? Diagnostics._0_is_declared_but_never_used : Diagnostics._0_is_declared_but_its_value_is_never_read;
23620
- addDiagnostic(UnusedKind.Local, createDiagnosticForNode(node, message, name));
23616
+ addDiagnostic(declaration, UnusedKind.Local, createDiagnosticForNode(node, message, name));
23621
23617
}
23622
23618
23623
23619
function isIdentifierThatStartsWithUnderscore(node: Node) {
@@ -23638,13 +23634,13 @@ namespace ts {
23638
23634
}
23639
23635
const symbol = getSymbolOfNode(member);
23640
23636
if (!symbol.isReferenced && hasModifier(member, ModifierFlags.Private)) {
23641
- addDiagnostic(UnusedKind.Local, createDiagnosticForNode(member.name!, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolToString(symbol)));
23637
+ addDiagnostic(member, UnusedKind.Local, createDiagnosticForNode(member.name!, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolToString(symbol)));
23642
23638
}
23643
23639
break;
23644
23640
case SyntaxKind.Constructor:
23645
23641
for (const parameter of (<ConstructorDeclaration>member).parameters) {
23646
23642
if (!parameter.symbol.isReferenced && hasModifier(parameter, ModifierFlags.Private)) {
23647
- addDiagnostic(UnusedKind.Local, createDiagnosticForNode(parameter.name, Diagnostics.Property_0_is_declared_but_its_value_is_never_read, symbolName(parameter.symbol)));
23643
+ addDiagnostic(parameter, UnusedKind.Local, createDiagnosticForNode(parameter.name, Diagnostics.Property_0_is_declared_but_its_value_is_never_read, symbolName(parameter.symbol)));
23648
23644
}
23649
23645
}
23650
23646
break;
@@ -23669,7 +23665,7 @@ namespace ts {
23669
23665
if (!(node.flags & NodeFlags.Ambient) && last(getSymbolOfNode(node).declarations) === node) {
23670
23666
for (const typeParameter of typeParameters) {
23671
23667
if (!(getMergedSymbol(typeParameter.symbol).isReferenced! & SymbolFlags.TypeParameter) && !isIdentifierThatStartsWithUnderscore(typeParameter.name)) {
23672
- addDiagnostic(UnusedKind.Parameter, createDiagnosticForNode(typeParameter.name, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolName(typeParameter.symbol)));
23668
+ addDiagnostic(typeParameter, UnusedKind.Parameter, createDiagnosticForNode(typeParameter.name, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolName(typeParameter.symbol)));
23673
23669
}
23674
23670
}
23675
23671
}
@@ -23728,7 +23724,7 @@ namespace ts {
23728
23724
const name = local.valueDeclaration && getNameOfDeclaration(local.valueDeclaration);
23729
23725
if (parameter && name) {
23730
23726
if (!isParameterPropertyDeclaration(parameter) && !parameterIsThisKeyword(parameter) && !isIdentifierThatStartsWithUnderscore(name)) {
23731
- addDiagnostic(UnusedKind.Parameter, createDiagnosticForNode(name, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolName(local)));
23727
+ addDiagnostic(parameter, UnusedKind.Parameter, createDiagnosticForNode(name, Diagnostics._0_is_declared_but_its_value_is_never_read, symbolName(local)));
23732
23728
}
23733
23729
}
23734
23730
else {
@@ -23744,7 +23740,7 @@ namespace ts {
23744
23740
(importClause.namedBindings.kind === SyntaxKind.NamespaceImport ? 1 : importClause.namedBindings.elements.length)
23745
23741
: 0);
23746
23742
if (nDeclarations === unuseds.length) {
23747
- addDiagnostic(UnusedKind.Local, unuseds.length === 1
23743
+ addDiagnostic(importDecl, UnusedKind.Local, unuseds.length === 1
23748
23744
? createDiagnosticForNode(importDecl, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(first(unuseds).name!))
23749
23745
: createDiagnosticForNode(importDecl, Diagnostics.All_imports_in_import_declaration_are_unused));
23750
23746
}
@@ -23759,26 +23755,26 @@ namespace ts {
23759
23755
addToGroup(unusedVariables, bindingPattern.parent.parent, bindingPattern.parent, getNodeId);
23760
23756
}
23761
23757
else {
23762
- addDiagnostic(kind, bindingElements.length === 1
23758
+ addDiagnostic(bindingPattern, kind, bindingElements.length === 1
23763
23759
? createDiagnosticForNode(bindingPattern, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(cast(first(bindingElements).name, isIdentifier)))
23764
23760
: createDiagnosticForNode(bindingPattern, Diagnostics.All_destructured_elements_are_unused));
23765
23761
}
23766
23762
}
23767
23763
else {
23768
23764
for (const e of bindingElements) {
23769
- addDiagnostic(kind, createDiagnosticForNode(e, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(cast(e.name, isIdentifier))));
23765
+ addDiagnostic(e, kind, createDiagnosticForNode(e, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(cast(e.name, isIdentifier))));
23770
23766
}
23771
23767
}
23772
23768
});
23773
23769
unusedVariables.forEach(([declarationList, declarations]) => {
23774
23770
if (declarationList.declarations.length === declarations.length) {
23775
- addDiagnostic(UnusedKind.Local, declarations.length === 1
23771
+ addDiagnostic(declarationList, UnusedKind.Local, declarations.length === 1
23776
23772
? createDiagnosticForNode(first(declarations).name, Diagnostics._0_is_declared_but_its_value_is_never_read, bindingNameText(first(declarations).name))
23777
23773
: createDiagnosticForNode(declarationList.parent.kind === SyntaxKind.VariableStatement ? declarationList.parent : declarationList, Diagnostics.All_variables_are_unused));
23778
23774
}
23779
23775
else {
23780
23776
for (const decl of declarations) {
23781
- addDiagnostic(UnusedKind.Local, createDiagnosticForNode(decl, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(cast(decl.name, isIdentifier))));
23777
+ addDiagnostic(decl, UnusedKind.Local, createDiagnosticForNode(decl, Diagnostics._0_is_declared_but_its_value_is_never_read, idText(cast(decl.name, isIdentifier))));
23782
23778
}
23783
23779
}
23784
23780
});
@@ -26631,8 +26627,8 @@ namespace ts {
26631
26627
}
26632
26628
26633
26629
if (!node.isDeclarationFile && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters)) {
26634
- checkUnusedIdentifiers(getPotentiallyUnusedIdentifiers(node), (kind, diag) => {
26635
- if (unusedIsError(kind)) {
26630
+ checkUnusedIdentifiers(getPotentiallyUnusedIdentifiers(node), (containingNode, kind, diag) => {
26631
+ if (!containsParseError(containingNode) && unusedIsError(kind)) {
26636
26632
diagnostics.add(diag);
26637
26633
}
26638
26634
});
0 commit comments