Unused identifiers compiler code by sarangan12 · Pull Request #9200 · microsoft/TypeScript
Expand Up
@@ -8310,8 +8310,21 @@ namespace ts {
return container === declarationContainer;
}
function updateReferencesForInterfaceHeritiageClauseTargets(node: InterfaceDeclaration): void { const extendedTypeNode = getClassExtendsHeritageClauseElement(node); if (extendedTypeNode) { const t = getTypeFromTypeNode(extendedTypeNode); if (t !== unknownType && t.symbol && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { t.symbol.hasReference = true; } } }
function checkIdentifier(node: Identifier): Type { const symbol = getResolvedSymbol(node); if (symbol && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { symbol.hasReference = true; }
// As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects. // Although in down-level emit of arrow function, we emit it using function expression which means that Expand Down Expand Up @@ -10202,6 +10215,10 @@ namespace ts { return unknownType; }
if ((compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { prop.hasReference = true; }
getNodeLinks(node).resolvedSymbol = prop;
if (prop.parent && prop.parent.flags & SymbolFlags.Class) { Expand Down Expand Up @@ -12144,6 +12161,8 @@ namespace ts { } } } checkUnusedIdentifiers(node); checkUnusedTypeParameters(node); } }
Expand Down Expand Up @@ -13214,6 +13233,9 @@ namespace ts { checkAsyncFunctionReturnType(<FunctionLikeDeclaration>node); } } if (!(<FunctionDeclaration>node).body) { checkUnusedTypeParameters(node); } } }
Expand Down Expand Up @@ -13366,6 +13388,8 @@ namespace ts { checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node);
checkSourceElement(node.body); checkUnusedIdentifiers(node); checkUnusedTypeParameters(node);
const symbol = getSymbolOfNode(node); const firstDeclaration = getDeclarationOfKind(symbol, node.kind); Expand Down Expand Up @@ -13558,13 +13582,18 @@ namespace ts { function checkTypeReferenceNode(node: TypeReferenceNode | ExpressionWithTypeArguments) { checkGrammarTypeArguments(node, node.typeArguments); const type = getTypeFromTypeReference(node); if (type !== unknownType && node.typeArguments) { // Do type argument local checks only if referenced type is successfully resolved forEach(node.typeArguments, checkSourceElement); if (produceDiagnostics) { const symbol = getNodeLinks(node).resolvedSymbol; const typeParameters = symbol.flags & SymbolFlags.TypeAlias ? getSymbolLinks(symbol).typeParameters : (<TypeReference>type).target.localTypeParameters; checkTypeArgumentConstraints(typeParameters, node.typeArguments); if (type !== unknownType) { if (type.symbol && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { type.symbol.hasReference = true; } if (node.typeArguments) { // Do type argument local checks only if referenced type is successfully resolved forEach(node.typeArguments, checkSourceElement); if (produceDiagnostics) { const symbol = getNodeLinks(node).resolvedSymbol; const typeParameters = symbol.flags & SymbolFlags.TypeAlias ? getSymbolLinks(symbol).typeParameters : (<TypeReference>type).target.localTypeParameters; checkTypeArgumentConstraints(typeParameters, node.typeArguments); } } } } Expand Down Expand Up @@ -14407,6 +14436,8 @@ namespace ts { }
checkSourceElement(node.body); checkUnusedIdentifiers(node); checkUnusedTypeParameters(node); if (!node.asteriskToken) { const returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); Expand All @@ -14428,12 +14459,83 @@ namespace ts { } }
function checkUnusedIdentifiers(node: FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | FunctionExpression | ArrowFunction | ForInStatement | Block | CatchClause): void { if (node.parent.kind !== SyntaxKind.InterfaceDeclaration && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { for (const key in node.locals) { if (hasProperty(node.locals, key)) { const local = node.locals[key]; if (!local.hasReference && local.valueDeclaration) { if (local.valueDeclaration.kind !== SyntaxKind.Parameter && compilerOptions.noUnusedLocals) { error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name); } else if (local.valueDeclaration.kind === SyntaxKind.Parameter && compilerOptions.noUnusedParameters) { if (local.valueDeclaration.flags === 0) { error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name); } } } } } } }
function checkUnusedClassLocals(node: ClassDeclaration): void { if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { if (node.members) { for (const member of node.members) { if (member.kind === SyntaxKind.MethodDeclaration || member.kind === SyntaxKind.PropertyDeclaration) { if (isPrivateNode(member) && !member.symbol.hasReference) { error(member.name, Diagnostics._0_is_declared_but_never_used, member.symbol.name); } } else if (member.kind === SyntaxKind.Constructor) { for (const parameter of (<ConstructorDeclaration>member).parameters) { if (isPrivateNode(parameter) && !parameter.symbol.hasReference) { error(parameter.name, Diagnostics._0_is_declared_but_never_used, parameter.symbol.name); } } } } } } }
function checkUnusedTypeParameters(node: ClassDeclaration | FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction | ConstructorDeclaration | SignatureDeclaration | InterfaceDeclaration) { if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { if (node.typeParameters) { for (const typeParameter of node.typeParameters) { if (!typeParameter.symbol.hasReference) { error(typeParameter.name, Diagnostics._0_is_declared_but_never_used, typeParameter.symbol.name); } } } } }
function isPrivateNode(node: Node): boolean { return (node.flags & NodeFlags.Private) !== 0; }
function checkUnusedModuleLocals(node: ModuleDeclaration | SourceFile): void { if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { for (const key in node.locals) { if (hasProperty(node.locals, key)) { const local = node.locals[key]; if (!local.hasReference && !local.exportSymbol) { forEach(local.declarations, d => error(d.name, Diagnostics._0_is_declared_but_never_used, local.name)); } } } } }
function checkBlock(node: Block) { // Grammar checking for SyntaxKind.Block if (node.kind === SyntaxKind.Block) { checkGrammarStatementInAmbientContext(node); } forEach(node.statements, checkSourceElement); checkUnusedIdentifiers(node); }
function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) { Expand Down Expand Up @@ -14938,6 +15040,7 @@ namespace ts { }
checkSourceElement(node.statement); checkUnusedIdentifiers(node); }
function checkForInStatement(node: ForInStatement) { Expand Down Expand Up @@ -14985,6 +15088,7 @@ namespace ts { }
checkSourceElement(node.statement); checkUnusedIdentifiers(node); }
function checkForInOrForOfVariableDeclaration(iterationStatement: ForInStatement | ForOfStatement): void { Expand Down Expand Up @@ -15424,6 +15528,7 @@ namespace ts { }
checkBlock(catchClause.block); checkUnusedIdentifiers(catchClause); }
if (node.finallyBlock) { Expand Down Expand Up @@ -15585,6 +15690,8 @@ namespace ts { } checkClassLikeDeclaration(node); forEach(node.members, checkSourceElement); checkUnusedClassLocals(node); checkUnusedTypeParameters(node); }
function checkClassLikeDeclaration(node: ClassLikeDeclaration) { Expand Down Expand Up @@ -15894,6 +16001,8 @@ namespace ts {
if (produceDiagnostics) { checkTypeForDuplicateIndexSignatures(node); updateReferencesForInterfaceHeritiageClauseTargets(node); checkUnusedTypeParameters(node); } }
Expand Down Expand Up @@ -16290,6 +16399,7 @@ namespace ts {
if (node.body) { checkSourceElement(node.body); checkUnusedModuleLocals(node); } }
Expand Down Expand Up @@ -16470,6 +16580,9 @@ namespace ts { if (target.flags & SymbolFlags.Type) { checkTypeNameIsReserved(node.name, Diagnostics.Import_name_cannot_be_0); } if ((compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { target.hasReference = true; } } } else { Expand Down Expand Up @@ -16812,6 +16925,9 @@ namespace ts {
deferredNodes = []; forEach(node.statements, checkSourceElement); if (isExternalModule(node)) { checkUnusedModuleLocals(node); } checkDeferredNodes(); deferredNodes = undefined;
Expand Down
function updateReferencesForInterfaceHeritiageClauseTargets(node: InterfaceDeclaration): void { const extendedTypeNode = getClassExtendsHeritageClauseElement(node); if (extendedTypeNode) { const t = getTypeFromTypeNode(extendedTypeNode); if (t !== unknownType && t.symbol && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { t.symbol.hasReference = true; } } }
function checkIdentifier(node: Identifier): Type { const symbol = getResolvedSymbol(node); if (symbol && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { symbol.hasReference = true; }
// As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects. // Although in down-level emit of arrow function, we emit it using function expression which means that Expand Down Expand Up @@ -10202,6 +10215,10 @@ namespace ts { return unknownType; }
if ((compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { prop.hasReference = true; }
getNodeLinks(node).resolvedSymbol = prop;
if (prop.parent && prop.parent.flags & SymbolFlags.Class) { Expand Down Expand Up @@ -12144,6 +12161,8 @@ namespace ts { } } } checkUnusedIdentifiers(node); checkUnusedTypeParameters(node); } }
Expand Down Expand Up @@ -13214,6 +13233,9 @@ namespace ts { checkAsyncFunctionReturnType(<FunctionLikeDeclaration>node); } } if (!(<FunctionDeclaration>node).body) { checkUnusedTypeParameters(node); } } }
Expand Down Expand Up @@ -13366,6 +13388,8 @@ namespace ts { checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node);
checkSourceElement(node.body); checkUnusedIdentifiers(node); checkUnusedTypeParameters(node);
const symbol = getSymbolOfNode(node); const firstDeclaration = getDeclarationOfKind(symbol, node.kind); Expand Down Expand Up @@ -13558,13 +13582,18 @@ namespace ts { function checkTypeReferenceNode(node: TypeReferenceNode | ExpressionWithTypeArguments) { checkGrammarTypeArguments(node, node.typeArguments); const type = getTypeFromTypeReference(node); if (type !== unknownType && node.typeArguments) { // Do type argument local checks only if referenced type is successfully resolved forEach(node.typeArguments, checkSourceElement); if (produceDiagnostics) { const symbol = getNodeLinks(node).resolvedSymbol; const typeParameters = symbol.flags & SymbolFlags.TypeAlias ? getSymbolLinks(symbol).typeParameters : (<TypeReference>type).target.localTypeParameters; checkTypeArgumentConstraints(typeParameters, node.typeArguments); if (type !== unknownType) { if (type.symbol && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { type.symbol.hasReference = true; } if (node.typeArguments) { // Do type argument local checks only if referenced type is successfully resolved forEach(node.typeArguments, checkSourceElement); if (produceDiagnostics) { const symbol = getNodeLinks(node).resolvedSymbol; const typeParameters = symbol.flags & SymbolFlags.TypeAlias ? getSymbolLinks(symbol).typeParameters : (<TypeReference>type).target.localTypeParameters; checkTypeArgumentConstraints(typeParameters, node.typeArguments); } } } } Expand Down Expand Up @@ -14407,6 +14436,8 @@ namespace ts { }
checkSourceElement(node.body); checkUnusedIdentifiers(node); checkUnusedTypeParameters(node); if (!node.asteriskToken) { const returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); Expand All @@ -14428,12 +14459,83 @@ namespace ts { } }
function checkUnusedIdentifiers(node: FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | FunctionExpression | ArrowFunction | ForInStatement | Block | CatchClause): void { if (node.parent.kind !== SyntaxKind.InterfaceDeclaration && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { for (const key in node.locals) { if (hasProperty(node.locals, key)) { const local = node.locals[key]; if (!local.hasReference && local.valueDeclaration) { if (local.valueDeclaration.kind !== SyntaxKind.Parameter && compilerOptions.noUnusedLocals) { error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name); } else if (local.valueDeclaration.kind === SyntaxKind.Parameter && compilerOptions.noUnusedParameters) { if (local.valueDeclaration.flags === 0) { error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name); } } } } } } }
function checkUnusedClassLocals(node: ClassDeclaration): void { if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { if (node.members) { for (const member of node.members) { if (member.kind === SyntaxKind.MethodDeclaration || member.kind === SyntaxKind.PropertyDeclaration) { if (isPrivateNode(member) && !member.symbol.hasReference) { error(member.name, Diagnostics._0_is_declared_but_never_used, member.symbol.name); } } else if (member.kind === SyntaxKind.Constructor) { for (const parameter of (<ConstructorDeclaration>member).parameters) { if (isPrivateNode(parameter) && !parameter.symbol.hasReference) { error(parameter.name, Diagnostics._0_is_declared_but_never_used, parameter.symbol.name); } } } } } } }
function checkUnusedTypeParameters(node: ClassDeclaration | FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction | ConstructorDeclaration | SignatureDeclaration | InterfaceDeclaration) { if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { if (node.typeParameters) { for (const typeParameter of node.typeParameters) { if (!typeParameter.symbol.hasReference) { error(typeParameter.name, Diagnostics._0_is_declared_but_never_used, typeParameter.symbol.name); } } } } }
function isPrivateNode(node: Node): boolean { return (node.flags & NodeFlags.Private) !== 0; }
function checkUnusedModuleLocals(node: ModuleDeclaration | SourceFile): void { if (compilerOptions.noUnusedLocals && !isInAmbientContext(node)) { for (const key in node.locals) { if (hasProperty(node.locals, key)) { const local = node.locals[key]; if (!local.hasReference && !local.exportSymbol) { forEach(local.declarations, d => error(d.name, Diagnostics._0_is_declared_but_never_used, local.name)); } } } } }
function checkBlock(node: Block) { // Grammar checking for SyntaxKind.Block if (node.kind === SyntaxKind.Block) { checkGrammarStatementInAmbientContext(node); } forEach(node.statements, checkSourceElement); checkUnusedIdentifiers(node); }
function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) { Expand Down Expand Up @@ -14938,6 +15040,7 @@ namespace ts { }
checkSourceElement(node.statement); checkUnusedIdentifiers(node); }
function checkForInStatement(node: ForInStatement) { Expand Down Expand Up @@ -14985,6 +15088,7 @@ namespace ts { }
checkSourceElement(node.statement); checkUnusedIdentifiers(node); }
function checkForInOrForOfVariableDeclaration(iterationStatement: ForInStatement | ForOfStatement): void { Expand Down Expand Up @@ -15424,6 +15528,7 @@ namespace ts { }
checkBlock(catchClause.block); checkUnusedIdentifiers(catchClause); }
if (node.finallyBlock) { Expand Down Expand Up @@ -15585,6 +15690,8 @@ namespace ts { } checkClassLikeDeclaration(node); forEach(node.members, checkSourceElement); checkUnusedClassLocals(node); checkUnusedTypeParameters(node); }
function checkClassLikeDeclaration(node: ClassLikeDeclaration) { Expand Down Expand Up @@ -15894,6 +16001,8 @@ namespace ts {
if (produceDiagnostics) { checkTypeForDuplicateIndexSignatures(node); updateReferencesForInterfaceHeritiageClauseTargets(node); checkUnusedTypeParameters(node); } }
Expand Down Expand Up @@ -16290,6 +16399,7 @@ namespace ts {
if (node.body) { checkSourceElement(node.body); checkUnusedModuleLocals(node); } }
Expand Down Expand Up @@ -16470,6 +16580,9 @@ namespace ts { if (target.flags & SymbolFlags.Type) { checkTypeNameIsReserved(node.name, Diagnostics.Import_name_cannot_be_0); } if ((compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { target.hasReference = true; } } } else { Expand Down Expand Up @@ -16812,6 +16925,9 @@ namespace ts {
deferredNodes = []; forEach(node.statements, checkSourceElement); if (isExternalModule(node)) { checkUnusedModuleLocals(node); } checkDeferredNodes(); deferredNodes = undefined;
Expand Down