diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 06591a7ab3d44..be94a7ce4e39c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -51,6 +51,7 @@ namespace ts { const compilerOptions = host.getCompilerOptions(); const languageVersion = compilerOptions.target || ScriptTarget.ES3; const modulekind = getEmitModuleKind(compilerOptions); + const noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters; const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System; const strictNullChecks = compilerOptions.strictNullChecks; @@ -187,6 +188,7 @@ namespace ts { let jsxElementClassType: Type; let deferredNodes: Node[]; + let deferredUnusedIdentifierNodes: Node[]; let flowLoopStart = 0; let flowLoopCount = 0; @@ -394,7 +396,7 @@ namespace ts { target.flags |= source.flags; if (source.valueDeclaration && (!target.valueDeclaration || - (target.valueDeclaration.kind === SyntaxKind.ModuleDeclaration && source.valueDeclaration.kind !== SyntaxKind.ModuleDeclaration))) { + (target.valueDeclaration.kind === SyntaxKind.ModuleDeclaration && source.valueDeclaration.kind !== SyntaxKind.ModuleDeclaration))) { // other kinds of value declarations take precedence over modules target.valueDeclaration = source.valueDeclaration; } @@ -664,8 +666,8 @@ namespace ts { useResult = result.flags & SymbolFlags.TypeParameter // type parameters are visible in parameter list, return type and type parameter list ? lastLocation === (location).type || - lastLocation.kind === SyntaxKind.Parameter || - lastLocation.kind === SyntaxKind.TypeParameter + lastLocation.kind === SyntaxKind.Parameter || + lastLocation.kind === SyntaxKind.TypeParameter // local types not visible outside the function body : false; } @@ -847,6 +849,10 @@ namespace ts { location = location.parent; } + if (result && nameNotFoundMessage && noUnusedIdentifiers) { + result.isReferenced = true; + } + if (!result) { result = getSymbol(globals, name, meaning); } @@ -854,7 +860,7 @@ namespace ts { if (!result) { if (nameNotFoundMessage) { if (!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && - !checkAndReportErrorForExtendingInterface(errorLocation)) { + !checkAndReportErrorForExtendingInterface(errorLocation)) { error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg)); } } @@ -961,7 +967,7 @@ namespace ts { return true; } return false; - } + } function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void { Debug.assert((result.flags & SymbolFlags.BlockScopedVariable) !== 0); @@ -1022,8 +1028,8 @@ namespace ts { const exportDefaultSymbol = isShorthandAmbientModule(moduleSymbol.valueDeclaration) ? moduleSymbol : moduleSymbol.exports["export="] ? - getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") : - resolveSymbol(moduleSymbol.exports["default"]); + getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") : + resolveSymbol(moduleSymbol.exports["default"]); if (!exportDefaultSymbol && !allowSyntheticDefaultImports) { error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol)); @@ -4536,7 +4542,7 @@ namespace ts { : undefined; const typeParameters = classType ? classType.localTypeParameters : declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : - getTypeParametersFromJSDocTemplate(declaration); + getTypeParametersFromJSDocTemplate(declaration); const returnType = getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType); const typePredicate = declaration.type && declaration.type.kind === SyntaxKind.TypePredicate ? createTypePredicateFromTypePredicateNode(declaration.type as TypePredicateNode) : @@ -5165,7 +5171,7 @@ namespace ts { if (typeSet.length === 0) { return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType : typeSet.containsUndefined ? typeSet.containsNonWideningType ? undefinedType : undefinedWideningType : - neverType; + neverType; } else if (typeSet.length === 1) { return typeSet[0]; @@ -5401,8 +5407,8 @@ namespace ts { const count = sources.length; const mapper: TypeMapper = count == 1 ? createUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) : - count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) : - createArrayTypeMapper(sources, targets); + count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) : + createArrayTypeMapper(sources, targets); mapper.mappedTypes = sources; return mapper; } @@ -5725,8 +5731,8 @@ namespace ts { } function isSignatureAssignableTo(source: Signature, - target: Signature, - ignoreReturnTypes: boolean): boolean { + target: Signature, + ignoreReturnTypes: boolean): boolean { return compareSignaturesRelated(source, target, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False; } @@ -5734,11 +5740,11 @@ namespace ts { * See signatureRelatedTo, compareSignaturesIdentical */ function compareSignaturesRelated(source: Signature, - target: Signature, - ignoreReturnTypes: boolean, - reportErrors: boolean, - errorReporter: (d: DiagnosticMessage, arg0?: string, arg1?: string) => void, - compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary { + target: Signature, + ignoreReturnTypes: boolean, + reportErrors: boolean, + errorReporter: (d: DiagnosticMessage, arg0?: string, arg1?: string) => void, + compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary { // TODO (drosen): De-duplicate code between related functions. if (source === target) { return Ternary.True; @@ -5820,10 +5826,10 @@ namespace ts { } function compareTypePredicateRelatedTo(source: TypePredicate, - target: TypePredicate, - reportErrors: boolean, - errorReporter: (d: DiagnosticMessage, arg0?: string, arg1?: string) => void, - compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary { + target: TypePredicate, + reportErrors: boolean, + errorReporter: (d: DiagnosticMessage, arg0?: string, arg1?: string) => void, + compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary { if (source.kind !== target.kind) { if (reportErrors) { errorReporter(Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard); @@ -6904,7 +6910,7 @@ namespace ts { function isStringLiteralUnionType(type: Type): boolean { return type.flags & TypeFlags.StringLiteral ? true : type.flags & TypeFlags.Union ? forEach((type).types, isStringLiteralUnionType) : - false; + false; } /** @@ -6986,11 +6992,11 @@ namespace ts { const resolved = type; const members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral); const regularNew = createAnonymousType(resolved.symbol, - members, - resolved.callSignatures, - resolved.constructSignatures, - resolved.stringIndexInfo, - resolved.numberIndexInfo); + members, + resolved.callSignatures, + resolved.constructSignatures, + resolved.stringIndexInfo, + resolved.numberIndexInfo); regularNew.flags = resolved.flags & ~TypeFlags.FreshObjectLiteral; (type).regularType = regularNew; return regularNew; @@ -7818,7 +7824,7 @@ namespace ts { // We cache results of flow type resolution for shared nodes that were previously visited in // the same getFlowTypeOfReference invocation. A node is considered shared when it is the // antecedent of more than one node. - for (let i = visitedFlowStart; i < visitedFlowCount; i++) { + for (let i = visitedFlowStart; i < visitedFlowCount; i++) { if (visitedFlowNodes[i] === flow) { return visitedFlowTypes[i]; } @@ -8205,7 +8211,7 @@ namespace ts { const targetType = type.flags & TypeFlags.TypeParameter ? getApparentType(type) : type; return isTypeAssignableTo(candidate, targetType) ? candidate : isTypeAssignableTo(type, candidate) ? type : - getIntersectionType([type, candidate]); + getIntersectionType([type, candidate]); } function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { @@ -8319,21 +8325,8 @@ 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 @@ -8624,9 +8617,9 @@ namespace ts { getSpecialPropertyAssignmentKind(container.parent) === SpecialPropertyAssignmentKind.PrototypeProperty) { // Get the 'x' of 'x.prototype.y = f' (here, 'f' is 'container') const className = (((container.parent as BinaryExpression) // x.prototype.y = f - .left as PropertyAccessExpression) // x.prototype.y - .expression as PropertyAccessExpression) // x.prototype - .expression; // x + .left as PropertyAccessExpression) // x.prototype.y + .expression as PropertyAccessExpression) // x.prototype + .expression; // x const classSymbol = checkExpression(className).symbol; if (classSymbol && classSymbol.members && (classSymbol.flags & SymbolFlags.Function)) { return getInferredClassType(classSymbol); @@ -9871,7 +9864,7 @@ namespace ts { elemType = checkExpression(node.tagName); } if (elemType.flags & TypeFlags.Union) { - const types = ( elemType).types; + const types = (elemType).types; return getUnionType(types.map(type => { return getResolvedJsxType(node, type, elemClassType); })); @@ -10248,8 +10241,16 @@ namespace ts { return unknownType; } - if ((compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { - prop.hasReference = true; + if (noUnusedIdentifiers && + (prop.flags & SymbolFlags.ClassMember) && + prop.valueDeclaration && (prop.valueDeclaration.flags & NodeFlags.Private)) { + if (prop.flags & SymbolFlags.Instantiated) { + getSymbolLinks(prop).target.isReferenced = true; + + } + else { + prop.isReferenced = true; + } } getNodeLinks(node).resolvedSymbol = prop; @@ -11803,7 +11804,7 @@ namespace ts { // if inference didn't come up with anything but {}, fall back to the binding pattern if present. if (links.type === emptyObjectType && (parameter.valueDeclaration.name.kind === SyntaxKind.ObjectBindingPattern || - parameter.valueDeclaration.name.kind === SyntaxKind.ArrayBindingPattern)) { + parameter.valueDeclaration.name.kind === SyntaxKind.ArrayBindingPattern)) { links.type = getTypeFromBindingPattern(parameter.valueDeclaration.name); } assignBindingElementTypes(parameter.valueDeclaration); @@ -12194,8 +12195,7 @@ namespace ts { } } } - checkUnusedIdentifiers(node); - checkUnusedTypeParameters(node); + registerForUnusedIdentifiersCheck(node); } } @@ -13423,8 +13423,7 @@ namespace ts { checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node); checkSourceElement(node.body); - checkUnusedIdentifiers(node); - checkUnusedTypeParameters(node); + registerForUnusedIdentifiersCheck(node); const symbol = getSymbolOfNode(node); const firstDeclaration = getDeclarationOfKind(symbol, node.kind); @@ -13570,6 +13569,7 @@ namespace ts { } if (node.parent.kind !== SyntaxKind.ObjectLiteralExpression) { checkSourceElement(node.body); + registerForUnusedIdentifiersCheck(node); } else { checkNodeDeferred(node); @@ -13586,6 +13586,7 @@ namespace ts { function checkAccessorDeferred(node: AccessorDeclaration) { checkSourceElement(node.body); + registerForUnusedIdentifiersCheck(node); } function checkMissingDeclaration(node: Node) { @@ -13618,9 +13619,6 @@ namespace ts { checkGrammarTypeArguments(node, node.typeArguments); const type = getTypeFromTypeReference(node); 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); @@ -14471,8 +14469,7 @@ 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); @@ -14492,40 +14489,96 @@ namespace ts { getReturnTypeOfSignature(getSignatureFromDeclaration(node)); } } + + registerForUnusedIdentifiersCheck(node); + } + + function registerForUnusedIdentifiersCheck(node: Node) { + if (deferredUnusedIdentifierNodes) { + deferredUnusedIdentifierNodes.push(node); + } + } + + function checkUnusedIdentifiers() { + if (deferredUnusedIdentifierNodes) { + for (const node of deferredUnusedIdentifierNodes) { + switch (node.kind) { + case SyntaxKind.SourceFile: + case SyntaxKind.ModuleDeclaration: + checkUnusedModuleMembers(node); + break; + case SyntaxKind.ClassDeclaration: + case SyntaxKind.ClassExpression: + checkUnusedClassMembers(node); + checkUnusedTypeParameters(node); + break; + case SyntaxKind.InterfaceDeclaration: + checkUnusedTypeParameters(node); + break; + case SyntaxKind.Block: + case SyntaxKind.CaseBlock: + case SyntaxKind.ForStatement: + case SyntaxKind.ForInStatement: + case SyntaxKind.ForOfStatement: + checkUnusedLocalsAndParameters(node); + break; + case SyntaxKind.Constructor: + case SyntaxKind.FunctionExpression: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.ArrowFunction: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + if ((node).body) { + checkUnusedLocalsAndParameters(node); + } + checkUnusedTypeParameters(node); + break; + case SyntaxKind.MethodSignature: + case SyntaxKind.CallSignature: + case SyntaxKind.ConstructSignature: + case SyntaxKind.IndexSignature: + case SyntaxKind.FunctionType: + case SyntaxKind.ConstructorType: + checkUnusedTypeParameters(node); + break; + }; + } + } } - function checkUnusedIdentifiers(node: FunctionDeclaration | MethodDeclaration | ConstructorDeclaration | FunctionExpression | ArrowFunction | ForInStatement | Block | CatchClause): void { - if (node.parent.kind !== SyntaxKind.InterfaceDeclaration && (compilerOptions.noUnusedLocals || compilerOptions.noUnusedParameters) && !isInAmbientContext(node)) { + function checkUnusedLocalsAndParameters(node: Node): void { + if (node.parent.kind !== SyntaxKind.InterfaceDeclaration && noUnusedIdentifiers && !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) { + if (!local.isReferenced) { + if (local.valueDeclaration && local.valueDeclaration.kind === SyntaxKind.Parameter) { + if (compilerOptions.noUnusedParameters && !isParameterPropertyDeclaration(local.valueDeclaration)) { error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name); } } + else if (compilerOptions.noUnusedLocals) { + forEach(local.declarations, d => error(d.name || d, Diagnostics._0_is_declared_but_never_used, local.name)); + } } } } } } - function checkUnusedClassLocals(node: ClassDeclaration): void { + function checkUnusedClassMembers(node: ClassDeclaration | ClassExpression): 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) { + if (!member.symbol.isReferenced && member.flags & NodeFlags.Private) { error(member.name, Diagnostics._0_is_declared_but_never_used, member.symbol.name); } } else if (member.kind === SyntaxKind.Constructor) { for (const parameter of (member).parameters) { - if (isPrivateNode(parameter) && !parameter.symbol.hasReference) { + if (!parameter.symbol.isReferenced && parameter.flags & NodeFlags.Private) { error(parameter.name, Diagnostics._0_is_declared_but_never_used, parameter.symbol.name); } } @@ -14535,11 +14588,11 @@ namespace ts { } } - function checkUnusedTypeParameters(node: ClassDeclaration | FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction | ConstructorDeclaration | SignatureDeclaration | InterfaceDeclaration) { + function checkUnusedTypeParameters(node: ClassDeclaration | ClassExpression | 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) { + if (!typeParameter.symbol.isReferenced) { error(typeParameter.name, Diagnostics._0_is_declared_but_never_used, typeParameter.symbol.name); } } @@ -14547,17 +14600,17 @@ namespace ts { } } - function isPrivateNode(node: Node): boolean { - return (node.flags & NodeFlags.Private) !== 0; - } - - function checkUnusedModuleLocals(node: ModuleDeclaration | SourceFile): void { + function checkUnusedModuleMembers(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)); + if (!local.isReferenced && !local.exportSymbol) { + for (const declaration of local.declarations) { + if (!isAmbientModule(declaration)) { + error(declaration.name, Diagnostics._0_is_declared_but_never_used, local.name); + } + } } } } @@ -14570,7 +14623,9 @@ namespace ts { checkGrammarStatementInAmbientContext(node); } forEach(node.statements, checkSourceElement); - checkUnusedIdentifiers(node); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); + } } function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) { @@ -15035,6 +15090,9 @@ namespace ts { if (node.condition) checkExpression(node.condition); if (node.incrementor) checkExpression(node.incrementor); checkSourceElement(node.statement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); + } } function checkForOfStatement(node: ForOfStatement): void { @@ -15075,7 +15133,9 @@ namespace ts { } checkSourceElement(node.statement); - checkUnusedIdentifiers(node); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); + } } function checkForInStatement(node: ForInStatement) { @@ -15123,7 +15183,9 @@ namespace ts { } checkSourceElement(node.statement); - checkUnusedIdentifiers(node); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); + } } function checkForInOrForOfVariableDeclaration(iterationStatement: ForInStatement | ForOfStatement): void { @@ -15496,6 +15558,9 @@ namespace ts { } forEach(clause.statements, checkSourceElement); }); + if (node.caseBlock.locals) { + registerForUnusedIdentifiersCheck(node.caseBlock); + } } function checkLabeledStatement(node: LabeledStatement) { @@ -15563,7 +15628,6 @@ namespace ts { } checkBlock(catchClause.block); - checkUnusedIdentifiers(catchClause); } if (node.finallyBlock) { @@ -15717,6 +15781,7 @@ namespace ts { function checkClassExpressionDeferred(node: ClassExpression) { forEach(node.members, checkSourceElement); + registerForUnusedIdentifiersCheck(node); } function checkClassDeclaration(node: ClassDeclaration) { @@ -15725,8 +15790,8 @@ namespace ts { } checkClassLikeDeclaration(node); forEach(node.members, checkSourceElement); - checkUnusedClassLocals(node); - checkUnusedTypeParameters(node); + + registerForUnusedIdentifiersCheck(node); } function checkClassLikeDeclaration(node: ClassLikeDeclaration) { @@ -16036,7 +16101,6 @@ namespace ts { if (produceDiagnostics) { checkTypeForDuplicateIndexSignatures(node); - updateReferencesForInterfaceHeritiageClauseTargets(node); checkUnusedTypeParameters(node); } } @@ -16434,7 +16498,9 @@ namespace ts { if (node.body) { checkSourceElement(node.body); - checkUnusedModuleLocals(node); + if (!isGlobalScopeAugmentation(node)) { + registerForUnusedIdentifiersCheck(node); + } } } @@ -16615,9 +16681,6 @@ 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 { @@ -16959,12 +17022,22 @@ namespace ts { potentialThisCollisions.length = 0; deferredNodes = []; + deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined; + forEach(node.statements, checkSourceElement); + + checkDeferredNodes(); + if (isExternalModule(node)) { - checkUnusedModuleLocals(node); + registerForUnusedIdentifiersCheck(node); } - checkDeferredNodes(); + + if (!node.isDeclarationFile) { + checkUnusedIdentifiers(); + } + deferredNodes = undefined; + deferredUnusedIdentifierNodes = undefined; if (isExternalOrCommonJsModule(node)) { checkExternalModuleExports(node); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 56029fd316cdb..7d7e92f27c3cd 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2107,6 +2107,8 @@ namespace ts { PropertyOrAccessor = Property | Accessor, Export = ExportNamespace | ExportType | ExportValue, + ClassMember = Method | Accessor | Property, + /* @internal */ // The set of things we consider semantically classifiable. Used to speed up the LS during // classification. @@ -2128,7 +2130,7 @@ namespace ts { /* @internal */ parent?: Symbol; // Parent symbol /* @internal */ exportSymbol?: Symbol; // Exported symbol associated with this symbol /* @internal */ constEnumOnlyModule?: boolean; // True if module contains only const enums or other modules with only const enums - /* @internal */ hasReference?: boolean; // True if the symbol is referenced elsewhere + /* @internal */ isReferenced?: boolean; // True if the symbol is referenced elsewhere } /* @internal */ diff --git a/tests/baselines/reference/unusedImports11.js b/tests/baselines/reference/unusedImports11.js new file mode 100644 index 0000000000000..86561e1021e58 --- /dev/null +++ b/tests/baselines/reference/unusedImports11.js @@ -0,0 +1,41 @@ +//// [tests/cases/compiler/unusedImports11.ts] //// + +//// [b.ts] + +export class Member {} +export default Member; + + +//// [a.ts] +import { Member } from './b'; +import d, { Member as M } from './b'; +import * as ns from './b'; +import r = require("./b"); + +new Member(); +new d(); +new M(); +new ns.Member(); +new r.Member(); + +//// [b.js] +"use strict"; +var Member = (function () { + function Member() { + } + return Member; +}()); +exports.Member = Member; +exports.__esModule = true; +exports["default"] = Member; +//// [a.js] +"use strict"; +var b_1 = require('./b'); +var b_2 = require('./b'); +var ns = require('./b'); +var r = require("./b"); +new b_1.Member(); +new b_2["default"](); +new b_2.Member(); +new ns.Member(); +new r.Member(); diff --git a/tests/baselines/reference/unusedImports11.symbols b/tests/baselines/reference/unusedImports11.symbols new file mode 100644 index 0000000000000..995b4a40c2a85 --- /dev/null +++ b/tests/baselines/reference/unusedImports11.symbols @@ -0,0 +1,43 @@ +=== tests/cases/compiler/a.ts === +import { Member } from './b'; +>Member : Symbol(Member, Decl(a.ts, 0, 8)) + +import d, { Member as M } from './b'; +>d : Symbol(d, Decl(a.ts, 1, 6)) +>Member : Symbol(M, Decl(a.ts, 1, 11)) +>M : Symbol(M, Decl(a.ts, 1, 11)) + +import * as ns from './b'; +>ns : Symbol(ns, Decl(a.ts, 2, 6)) + +import r = require("./b"); +>r : Symbol(r, Decl(a.ts, 2, 26)) + +new Member(); +>Member : Symbol(Member, Decl(a.ts, 0, 8)) + +new d(); +>d : Symbol(d, Decl(a.ts, 1, 6)) + +new M(); +>M : Symbol(M, Decl(a.ts, 1, 11)) + +new ns.Member(); +>ns.Member : Symbol(Member, Decl(b.ts, 0, 0)) +>ns : Symbol(ns, Decl(a.ts, 2, 6)) +>Member : Symbol(Member, Decl(b.ts, 0, 0)) + +new r.Member(); +>r.Member : Symbol(Member, Decl(b.ts, 0, 0)) +>r : Symbol(r, Decl(a.ts, 2, 26)) +>Member : Symbol(Member, Decl(b.ts, 0, 0)) + +=== tests/cases/compiler/b.ts === + +export class Member {} +>Member : Symbol(Member, Decl(b.ts, 0, 0)) + +export default Member; +>Member : Symbol(Member, Decl(b.ts, 0, 0)) + + diff --git a/tests/baselines/reference/unusedImports11.types b/tests/baselines/reference/unusedImports11.types new file mode 100644 index 0000000000000..1d71a0226b6d5 --- /dev/null +++ b/tests/baselines/reference/unusedImports11.types @@ -0,0 +1,48 @@ +=== tests/cases/compiler/a.ts === +import { Member } from './b'; +>Member : typeof Member + +import d, { Member as M } from './b'; +>d : typeof Member +>Member : typeof Member +>M : typeof Member + +import * as ns from './b'; +>ns : typeof ns + +import r = require("./b"); +>r : typeof ns + +new Member(); +>new Member() : Member +>Member : typeof Member + +new d(); +>new d() : Member +>d : typeof Member + +new M(); +>new M() : Member +>M : typeof Member + +new ns.Member(); +>new ns.Member() : Member +>ns.Member : typeof Member +>ns : typeof ns +>Member : typeof Member + +new r.Member(); +>new r.Member() : Member +>r.Member : typeof Member +>r : typeof ns +>Member : typeof Member + +=== tests/cases/compiler/b.ts === + +export class Member {} +>Member : Member + +export default Member; +>Member : Member + + diff --git a/tests/baselines/reference/unusedImports12.errors.txt b/tests/baselines/reference/unusedImports12.errors.txt new file mode 100644 index 0000000000000..89b0687317b6b --- /dev/null +++ b/tests/baselines/reference/unusedImports12.errors.txt @@ -0,0 +1,29 @@ +tests/cases/compiler/a.ts(1,10): error TS6133: 'Member' is declared but never used. +tests/cases/compiler/a.ts(2,8): error TS6133: 'd' is declared but never used. +tests/cases/compiler/a.ts(2,23): error TS6133: 'M' is declared but never used. +tests/cases/compiler/a.ts(3,13): error TS6133: 'ns' is declared but never used. +tests/cases/compiler/a.ts(4,8): error TS6133: 'r' is declared but never used. + + +==== tests/cases/compiler/a.ts (5 errors) ==== + import { Member } from './b'; + ~~~~~~ +!!! error TS6133: 'Member' is declared but never used. + import d, { Member as M } from './b'; + ~ +!!! error TS6133: 'd' is declared but never used. + ~ +!!! error TS6133: 'M' is declared but never used. + import * as ns from './b'; + ~~ +!!! error TS6133: 'ns' is declared but never used. + import r = require("./b"); + ~ +!!! error TS6133: 'r' is declared but never used. + +==== tests/cases/compiler/b.ts (0 errors) ==== + + export class Member {} + export default Member; + + \ No newline at end of file diff --git a/tests/baselines/reference/unusedImports12.js b/tests/baselines/reference/unusedImports12.js new file mode 100644 index 0000000000000..ece9b105bc90f --- /dev/null +++ b/tests/baselines/reference/unusedImports12.js @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/unusedImports12.ts] //// + +//// [b.ts] + +export class Member {} +export default Member; + + +//// [a.ts] +import { Member } from './b'; +import d, { Member as M } from './b'; +import * as ns from './b'; +import r = require("./b"); + + +//// [b.js] +"use strict"; +var Member = (function () { + function Member() { + } + return Member; +}()); +exports.Member = Member; +exports.__esModule = true; +exports["default"] = Member; +//// [a.js] +"use strict"; diff --git a/tests/baselines/reference/unusedLocalsAndParameters.errors.txt b/tests/baselines/reference/unusedLocalsAndParameters.errors.txt new file mode 100644 index 0000000000000..a7b1d1b37f485 --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParameters.errors.txt @@ -0,0 +1,159 @@ +tests/cases/compiler/unusedLocalsAndParameters.ts(5,12): error TS6133: 'a' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(10,22): error TS6133: 'a' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(16,5): error TS6133: 'farrow' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(16,15): error TS6133: 'a' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(19,7): error TS6133: 'C' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(21,12): error TS6133: 'a' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(24,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. +tests/cases/compiler/unusedLocalsAndParameters.ts(24,11): error TS6133: 'v' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(28,5): error TS6133: 'E' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(30,12): error TS6133: 'a' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(33,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. +tests/cases/compiler/unusedLocalsAndParameters.ts(33,11): error TS6133: 'v' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(39,12): error TS6133: 'a' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(42,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. +tests/cases/compiler/unusedLocalsAndParameters.ts(42,11): error TS6133: 'v' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(49,10): error TS6133: 'i' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(53,10): error TS6133: 'i' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(57,17): error TS6133: 'n' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(64,11): error TS6133: 'c' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(69,11): error TS6133: 'a' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(72,11): error TS6133: 'c' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(75,11): error TS6133: 'c' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(80,11): error TS6133: 'N' is declared but never used. +tests/cases/compiler/unusedLocalsAndParameters.ts(81,9): error TS6133: 'x' is declared but never used. + + +==== tests/cases/compiler/unusedLocalsAndParameters.ts (24 errors) ==== + + export { }; + + // function declaration paramter + function f(a) { + ~ +!!! error TS6133: 'a' is declared but never used. + } + f(0); + + // function expression paramter + var fexp = function (a) { + ~ +!!! error TS6133: 'a' is declared but never used. + }; + + fexp(0); + + // arrow function paramter + var farrow = (a) => { + ~~~~~~ +!!! error TS6133: 'farrow' is declared but never used. + ~ +!!! error TS6133: 'a' is declared but never used. + }; + + class C { + ~ +!!! error TS6133: 'C' is declared but never used. + // Method declaration paramter + method(a) { + ~ +!!! error TS6133: 'a' is declared but never used. + } + // Accessor declaration paramter + set x(v: number) { + ~ +!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. + ~ +!!! error TS6133: 'v' is declared but never used. + } + } + + var E = class { + ~ +!!! error TS6133: 'E' is declared but never used. + // Method declaration paramter + method(a) { + ~ +!!! error TS6133: 'a' is declared but never used. + } + // Accessor declaration paramter + set x(v: number) { + ~ +!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. + ~ +!!! error TS6133: 'v' is declared but never used. + } + } + + var o = { + // Object literal method declaration paramter + method(a) { + ~ +!!! error TS6133: 'a' is declared but never used. + }, + // Accessor declaration paramter + set x(v: number) { + ~ +!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. + ~ +!!! error TS6133: 'v' is declared but never used. + } + }; + + o; + + // in a for..in statment + for (let i in o) { + ~ +!!! error TS6133: 'i' is declared but never used. + } + + // in a for..of statment + for (let i of [1, 2, 3]) { + ~ +!!! error TS6133: 'i' is declared but never used. + } + + // in a for. statment + for (let i = 0, n; i < 10; i++) { + ~ +!!! error TS6133: 'n' is declared but never used. + } + + // in a block + + const condition = false; + if (condition) { + const c = 0; + ~ +!!! error TS6133: 'c' is declared but never used. + } + + // in try/catch/finally + try { + const a = 0; + ~ +!!! error TS6133: 'a' is declared but never used. + } + catch (e) { + const c = 1; + ~ +!!! error TS6133: 'c' is declared but never used. + } + finally { + const c = 0; + ~ +!!! error TS6133: 'c' is declared but never used. + } + + + // in a namespace + namespace N { + ~ +!!! error TS6133: 'N' is declared but never used. + var x; + ~ +!!! error TS6133: 'x' is declared but never used. + } + + \ No newline at end of file diff --git a/tests/baselines/reference/unusedLocalsAndParameters.js b/tests/baselines/reference/unusedLocalsAndParameters.js new file mode 100644 index 0000000000000..64ff373db4a91 --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParameters.js @@ -0,0 +1,168 @@ +//// [unusedLocalsAndParameters.ts] + +export { }; + +// function declaration paramter +function f(a) { +} +f(0); + +// function expression paramter +var fexp = function (a) { +}; + +fexp(0); + +// arrow function paramter +var farrow = (a) => { +}; + +class C { + // Method declaration paramter + method(a) { + } + // Accessor declaration paramter + set x(v: number) { + } +} + +var E = class { + // Method declaration paramter + method(a) { + } + // Accessor declaration paramter + set x(v: number) { + } +} + +var o = { + // Object literal method declaration paramter + method(a) { + }, + // Accessor declaration paramter + set x(v: number) { + } +}; + +o; + +// in a for..in statment +for (let i in o) { +} + +// in a for..of statment +for (let i of [1, 2, 3]) { +} + +// in a for. statment +for (let i = 0, n; i < 10; i++) { +} + +// in a block + +const condition = false; +if (condition) { + const c = 0; +} + +// in try/catch/finally +try { + const a = 0; +} +catch (e) { + const c = 1; +} +finally { + const c = 0; +} + + +// in a namespace +namespace N { + var x; +} + + + +//// [unusedLocalsAndParameters.js] +"use strict"; +// function declaration paramter +function f(a) { +} +f(0); +// function expression paramter +var fexp = function (a) { +}; +fexp(0); +// arrow function paramter +var farrow = function (a) { +}; +var C = (function () { + function C() { + } + // Method declaration paramter + C.prototype.method = function (a) { + }; + Object.defineProperty(C.prototype, "x", { + // Accessor declaration paramter + set: function (v) { + }, + enumerable: true, + configurable: true + }); + return C; +}()); +var E = (function () { + function class_1() { + } + // Method declaration paramter + class_1.prototype.method = function (a) { + }; + Object.defineProperty(class_1.prototype, "x", { + // Accessor declaration paramter + set: function (v) { + }, + enumerable: true, + configurable: true + }); + return class_1; +}()); +var o = { + // Object literal method declaration paramter + method: function (a) { + }, + // Accessor declaration paramter + set x(v) { + } +}; +o; +// in a for..in statment +for (var i in o) { +} +// in a for..of statment +for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) { + var i = _a[_i]; +} +// in a for. statment +for (var i = 0, n = void 0; i < 10; i++) { +} +// in a block +var condition = false; +if (condition) { + var c = 0; +} +// in try/catch/finally +try { + var a = 0; +} +catch (e) { + var c = 1; +} +finally { + var c = 0; +} +// in a namespace +var N; +(function (N) { + var x; +})(N || (N = {})); diff --git a/tests/baselines/reference/unusedLocalsAndParametersDeferred.errors.txt b/tests/baselines/reference/unusedLocalsAndParametersDeferred.errors.txt new file mode 100644 index 0000000000000..63afd213f1cc3 --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersDeferred.errors.txt @@ -0,0 +1,172 @@ +tests/cases/compiler/unusedLocalsAndParametersDeferred.ts(42,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. +tests/cases/compiler/unusedLocalsAndParametersDeferred.ts(65,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. +tests/cases/compiler/unusedLocalsAndParametersDeferred.ts(87,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. + + +==== tests/cases/compiler/unusedLocalsAndParametersDeferred.ts (3 errors) ==== + + export { }; + + function defered(a: () => T): T { + return a(); + } + + // function declaration paramter + function f(a) { + defered(() => { + a; + }); + } + f(0); + + // function expression paramter + var fexp = function (a) { + defered(() => { + a; + }); + }; + fexp(1); + + // arrow function paramter + var farrow = (a) => { + defered(() => { + a; + }); + }; + farrow(2); + + let prop1; + + class C { + // Method declaration paramter + method(a) { + defered(() => { + a; + }); + } + // Accessor declaration paramter + set x(v: number) { + ~ +!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. + defered(() => { + v; + }); + } + // in a property initalizer + p = defered(() => { + prop1; + }); + } + + new C(); + + let prop2; + + var E = class { + // Method declaration paramter + method(a) { + defered(() => { + a; + }); + } + // Accessor declaration paramter + set x(v: number) { + ~ +!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. + defered(() => { + v; + }); + } + // in a property initalizer + p = defered(() => { + prop2; + }); + } + + new E(); + + + var o = { + // Object literal method declaration paramter + method(a) { + defered(() => { + a; + }); + }, + // Accessor declaration paramter + set x(v: number) { + ~ +!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. + defered(() => { + v; + }); + }, + // in a property initalizer + p: defered(() => { + prop1; + }) + }; + + o; + + // in a for..in statment + for (let i in o) { + defered(() => { + i; + }); + } + + // in a for..of statment + for (let i of [1,2,3]) { + defered(() => { + i; + }); + } + + // in a for. statment + for (let i = 0; i < 10; i++) { + defered(() => { + i; + }); + } + + // in a block + + const condition = false; + if (condition) { + const c = 0; + defered(() => { + c; + }); + } + + // in try/catch/finally + try { + const a = 0; + defered(() => { + a; + }); + } + catch (e) { + const c = 1; + defered(() => { + c; + }); + } + finally { + const c = 0; + defered(() => { + c; + }); + } + + + // in a namespace + namespace N { + var x; + defered(() => { + x; + }); + } + N; + \ No newline at end of file diff --git a/tests/baselines/reference/unusedLocalsAndParametersDeferred.js b/tests/baselines/reference/unusedLocalsAndParametersDeferred.js new file mode 100644 index 0000000000000..7fbfd15c5261a --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersDeferred.js @@ -0,0 +1,325 @@ +//// [unusedLocalsAndParametersDeferred.ts] + +export { }; + +function defered(a: () => T): T { + return a(); +} + +// function declaration paramter +function f(a) { + defered(() => { + a; + }); +} +f(0); + +// function expression paramter +var fexp = function (a) { + defered(() => { + a; + }); +}; +fexp(1); + +// arrow function paramter +var farrow = (a) => { + defered(() => { + a; + }); +}; +farrow(2); + +let prop1; + +class C { + // Method declaration paramter + method(a) { + defered(() => { + a; + }); + } + // Accessor declaration paramter + set x(v: number) { + defered(() => { + v; + }); + } + // in a property initalizer + p = defered(() => { + prop1; + }); +} + +new C(); + +let prop2; + +var E = class { + // Method declaration paramter + method(a) { + defered(() => { + a; + }); + } + // Accessor declaration paramter + set x(v: number) { + defered(() => { + v; + }); + } + // in a property initalizer + p = defered(() => { + prop2; + }); +} + +new E(); + + +var o = { + // Object literal method declaration paramter + method(a) { + defered(() => { + a; + }); + }, + // Accessor declaration paramter + set x(v: number) { + defered(() => { + v; + }); + }, + // in a property initalizer + p: defered(() => { + prop1; + }) +}; + +o; + +// in a for..in statment +for (let i in o) { + defered(() => { + i; + }); +} + +// in a for..of statment +for (let i of [1,2,3]) { + defered(() => { + i; + }); +} + +// in a for. statment +for (let i = 0; i < 10; i++) { + defered(() => { + i; + }); +} + +// in a block + +const condition = false; +if (condition) { + const c = 0; + defered(() => { + c; + }); +} + +// in try/catch/finally +try { + const a = 0; + defered(() => { + a; + }); +} +catch (e) { + const c = 1; + defered(() => { + c; + }); +} +finally { + const c = 0; + defered(() => { + c; + }); +} + + +// in a namespace +namespace N { + var x; + defered(() => { + x; + }); +} +N; + + +//// [unusedLocalsAndParametersDeferred.js] +"use strict"; +function defered(a) { + return a(); +} +// function declaration paramter +function f(a) { + defered(function () { + a; + }); +} +f(0); +// function expression paramter +var fexp = function (a) { + defered(function () { + a; + }); +}; +fexp(1); +// arrow function paramter +var farrow = function (a) { + defered(function () { + a; + }); +}; +farrow(2); +var prop1; +var C = (function () { + function C() { + // in a property initalizer + this.p = defered(function () { + prop1; + }); + } + // Method declaration paramter + C.prototype.method = function (a) { + defered(function () { + a; + }); + }; + Object.defineProperty(C.prototype, "x", { + // Accessor declaration paramter + set: function (v) { + defered(function () { + v; + }); + }, + enumerable: true, + configurable: true + }); + return C; +}()); +new C(); +var prop2; +var E = (function () { + function class_1() { + // in a property initalizer + this.p = defered(function () { + prop2; + }); + } + // Method declaration paramter + class_1.prototype.method = function (a) { + defered(function () { + a; + }); + }; + Object.defineProperty(class_1.prototype, "x", { + // Accessor declaration paramter + set: function (v) { + defered(function () { + v; + }); + }, + enumerable: true, + configurable: true + }); + return class_1; +}()); +new E(); +var o = { + // Object literal method declaration paramter + method: function (a) { + defered(function () { + a; + }); + }, + // Accessor declaration paramter + set x(v) { + defered(function () { + v; + }); + }, + // in a property initalizer + p: defered(function () { + prop1; + }) +}; +o; +// in a for..in statment +var _loop_1 = function(i) { + defered(function () { + i; + }); +}; +for (var i in o) { + _loop_1(i); +} +// in a for..of statment +var _loop_2 = function(i) { + defered(function () { + i; + }); +}; +for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) { + var i = _a[_i]; + _loop_2(i); +} +// in a for. statment +var _loop_3 = function(i) { + defered(function () { + i; + }); +}; +for (var i = 0; i < 10; i++) { + _loop_3(i); +} +// in a block +var condition = false; +if (condition) { + var c_1 = 0; + defered(function () { + c_1; + }); +} +// in try/catch/finally +try { + var a_1 = 0; + defered(function () { + a_1; + }); +} +catch (e) { + var c_2 = 1; + defered(function () { + c_2; + }); +} +finally { + var c_3 = 0; + defered(function () { + c_3; + }); +} +// in a namespace +var N; +(function (N) { + var x; + defered(function () { + x; + }); +})(N || (N = {})); +N; diff --git a/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.js b/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.js new file mode 100644 index 0000000000000..221d49a0fdf2d --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.js @@ -0,0 +1,45 @@ +//// [unusedLocalsAndParametersOverloadSignatures.ts] + +export function func(details: number, message: string, ...args: any[]): void; +export function func(details: number, message: string): any { + return details + message; +} + +export class C { + constructor(details: number, message: string, ...args: any[]); + constructor(details: number, message: string) { + details + message; + } + + method(details: number, message: string, ...args: any[]): void; + method(details: number, message: string): any { + return details + message; + } +} + + +export function genericFunc(details: number, message: T, ...args: any[]): void; +export function genericFunc(details: number, message: any): any { + return details + message; +} + +//// [unusedLocalsAndParametersOverloadSignatures.js] +"use strict"; +function func(details, message) { + return details + message; +} +exports.func = func; +var C = (function () { + function C(details, message) { + details + message; + } + C.prototype.method = function (details, message) { + return details + message; + }; + return C; +}()); +exports.C = C; +function genericFunc(details, message) { + return details + message; +} +exports.genericFunc = genericFunc; diff --git a/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.symbols b/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.symbols new file mode 100644 index 0000000000000..da3c47f2e42dd --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.symbols @@ -0,0 +1,70 @@ +=== tests/cases/compiler/unusedLocalsAndParametersOverloadSignatures.ts === + +export function func(details: number, message: string, ...args: any[]): void; +>func : Symbol(func, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 0, 0), Decl(unusedLocalsAndParametersOverloadSignatures.ts, 1, 77)) +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 1, 21)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 1, 37)) +>args : Symbol(args, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 1, 54)) + +export function func(details: number, message: string): any { +>func : Symbol(func, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 0, 0), Decl(unusedLocalsAndParametersOverloadSignatures.ts, 1, 77)) +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 2, 21)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 2, 37)) + + return details + message; +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 2, 21)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 2, 37)) +} + +export class C { +>C : Symbol(C, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 4, 1)) + + constructor(details: number, message: string, ...args: any[]); +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 7, 16)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 7, 32)) +>args : Symbol(args, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 7, 49)) + + constructor(details: number, message: string) { +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 8, 16)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 8, 32)) + + details + message; +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 8, 16)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 8, 32)) + } + + method(details: number, message: string, ...args: any[]): void; +>method : Symbol(C.method, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 10, 5), Decl(unusedLocalsAndParametersOverloadSignatures.ts, 12, 67)) +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 12, 11)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 12, 27)) +>args : Symbol(args, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 12, 44)) + + method(details: number, message: string): any { +>method : Symbol(C.method, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 10, 5), Decl(unusedLocalsAndParametersOverloadSignatures.ts, 12, 67)) +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 13, 11)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 13, 27)) + + return details + message; +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 13, 11)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 13, 27)) + } +} + + +export function genericFunc(details: number, message: T, ...args: any[]): void; +>genericFunc : Symbol(genericFunc, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 16, 1), Decl(unusedLocalsAndParametersOverloadSignatures.ts, 19, 82)) +>T : Symbol(T, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 19, 28)) +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 19, 31)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 19, 47)) +>T : Symbol(T, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 19, 28)) +>args : Symbol(args, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 19, 59)) + +export function genericFunc(details: number, message: any): any { +>genericFunc : Symbol(genericFunc, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 16, 1), Decl(unusedLocalsAndParametersOverloadSignatures.ts, 19, 82)) +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 20, 28)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 20, 44)) + + return details + message; +>details : Symbol(details, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 20, 28)) +>message : Symbol(message, Decl(unusedLocalsAndParametersOverloadSignatures.ts, 20, 44)) +} diff --git a/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.types b/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.types new file mode 100644 index 0000000000000..2a216c8acd212 --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersOverloadSignatures.types @@ -0,0 +1,74 @@ +=== tests/cases/compiler/unusedLocalsAndParametersOverloadSignatures.ts === + +export function func(details: number, message: string, ...args: any[]): void; +>func : (details: number, message: string, ...args: any[]) => void +>details : number +>message : string +>args : any[] + +export function func(details: number, message: string): any { +>func : (details: number, message: string, ...args: any[]) => void +>details : number +>message : string + + return details + message; +>details + message : string +>details : number +>message : string +} + +export class C { +>C : C + + constructor(details: number, message: string, ...args: any[]); +>details : number +>message : string +>args : any[] + + constructor(details: number, message: string) { +>details : number +>message : string + + details + message; +>details + message : string +>details : number +>message : string + } + + method(details: number, message: string, ...args: any[]): void; +>method : (details: number, message: string, ...args: any[]) => void +>details : number +>message : string +>args : any[] + + method(details: number, message: string): any { +>method : (details: number, message: string, ...args: any[]) => void +>details : number +>message : string + + return details + message; +>details + message : string +>details : number +>message : string + } +} + + +export function genericFunc(details: number, message: T, ...args: any[]): void; +>genericFunc : (details: number, message: T, ...args: any[]) => void +>T : T +>details : number +>message : T +>T : T +>args : any[] + +export function genericFunc(details: number, message: any): any { +>genericFunc : (details: number, message: T, ...args: any[]) => void +>details : number +>message : any + + return details + message; +>details + message : any +>details : number +>message : any +} diff --git a/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.js b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.js new file mode 100644 index 0000000000000..b854278a3e30d --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.js @@ -0,0 +1,35 @@ +//// [unusedLocalsAndParametersTypeAliases.ts] + +// used in a declaration +type handler1 = () => void; +export interface I1 { + getHandler: handler1; +} + +// exported +export type handler2 = () => void; + +// used in extends clause +type handler3 = () => void; +export interface I3 { + getHandler: T; +} + +// used in another type alias declaration +type handler4 = () => void; +type handler5 = handler4 | (()=>number); +var x: handler5; +x(); + +// used as type argument +type handler6 = () => void; +var y: Array; +y[0](); + + +//// [unusedLocalsAndParametersTypeAliases.js] +"use strict"; +var x; +x(); +var y; +y[0](); diff --git a/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.symbols b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.symbols new file mode 100644 index 0000000000000..9f9004d7a288e --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.symbols @@ -0,0 +1,59 @@ +=== tests/cases/compiler/unusedLocalsAndParametersTypeAliases.ts === + +// used in a declaration +type handler1 = () => void; +>handler1 : Symbol(handler1, Decl(unusedLocalsAndParametersTypeAliases.ts, 0, 0)) + +export interface I1 { +>I1 : Symbol(I1, Decl(unusedLocalsAndParametersTypeAliases.ts, 2, 27)) + + getHandler: handler1; +>getHandler : Symbol(I1.getHandler, Decl(unusedLocalsAndParametersTypeAliases.ts, 3, 21)) +>handler1 : Symbol(handler1, Decl(unusedLocalsAndParametersTypeAliases.ts, 0, 0)) +} + +// exported +export type handler2 = () => void; +>handler2 : Symbol(handler2, Decl(unusedLocalsAndParametersTypeAliases.ts, 5, 1)) + +// used in extends clause +type handler3 = () => void; +>handler3 : Symbol(handler3, Decl(unusedLocalsAndParametersTypeAliases.ts, 8, 34)) + +export interface I3 { +>I3 : Symbol(I3, Decl(unusedLocalsAndParametersTypeAliases.ts, 11, 27)) +>T : Symbol(T, Decl(unusedLocalsAndParametersTypeAliases.ts, 12, 20)) +>handler3 : Symbol(handler3, Decl(unusedLocalsAndParametersTypeAliases.ts, 8, 34)) + + getHandler: T; +>getHandler : Symbol(I3.getHandler, Decl(unusedLocalsAndParametersTypeAliases.ts, 12, 41)) +>T : Symbol(T, Decl(unusedLocalsAndParametersTypeAliases.ts, 12, 20)) +} + +// used in another type alias declaration +type handler4 = () => void; +>handler4 : Symbol(handler4, Decl(unusedLocalsAndParametersTypeAliases.ts, 14, 1)) + +type handler5 = handler4 | (()=>number); +>handler5 : Symbol(handler5, Decl(unusedLocalsAndParametersTypeAliases.ts, 17, 27)) +>handler4 : Symbol(handler4, Decl(unusedLocalsAndParametersTypeAliases.ts, 14, 1)) + +var x: handler5; +>x : Symbol(x, Decl(unusedLocalsAndParametersTypeAliases.ts, 19, 3)) +>handler5 : Symbol(handler5, Decl(unusedLocalsAndParametersTypeAliases.ts, 17, 27)) + +x(); +>x : Symbol(x, Decl(unusedLocalsAndParametersTypeAliases.ts, 19, 3)) + +// used as type argument +type handler6 = () => void; +>handler6 : Symbol(handler6, Decl(unusedLocalsAndParametersTypeAliases.ts, 20, 4)) + +var y: Array; +>y : Symbol(y, Decl(unusedLocalsAndParametersTypeAliases.ts, 24, 3)) +>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>handler6 : Symbol(handler6, Decl(unusedLocalsAndParametersTypeAliases.ts, 20, 4)) + +y[0](); +>y : Symbol(y, Decl(unusedLocalsAndParametersTypeAliases.ts, 24, 3)) + diff --git a/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.types b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.types new file mode 100644 index 0000000000000..82c87deb7d07c --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.types @@ -0,0 +1,63 @@ +=== tests/cases/compiler/unusedLocalsAndParametersTypeAliases.ts === + +// used in a declaration +type handler1 = () => void; +>handler1 : () => void + +export interface I1 { +>I1 : I1 + + getHandler: handler1; +>getHandler : () => void +>handler1 : () => void +} + +// exported +export type handler2 = () => void; +>handler2 : () => void + +// used in extends clause +type handler3 = () => void; +>handler3 : () => void + +export interface I3 { +>I3 : I3 +>T : T +>handler3 : () => void + + getHandler: T; +>getHandler : T +>T : T +} + +// used in another type alias declaration +type handler4 = () => void; +>handler4 : () => void + +type handler5 = handler4 | (()=>number); +>handler5 : (() => void) | (() => number) +>handler4 : () => void + +var x: handler5; +>x : (() => void) | (() => number) +>handler5 : (() => void) | (() => number) + +x(); +>x() : void | number +>x : (() => void) | (() => number) + +// used as type argument +type handler6 = () => void; +>handler6 : () => void + +var y: Array; +>y : (() => void)[] +>Array : T[] +>handler6 : () => void + +y[0](); +>y[0]() : void +>y[0] : () => void +>y : (() => void)[] +>0 : number + diff --git a/tests/baselines/reference/unusedLocalsAndParametersTypeAliases2.errors.txt b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases2.errors.txt new file mode 100644 index 0000000000000..d6abf6f2f3f73 --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases2.errors.txt @@ -0,0 +1,20 @@ +tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts(3,6): error TS6133: 'handler1' is declared but never used. +tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts(7,10): error TS6133: 'handler2' is declared but never used. + + +==== tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts (2 errors) ==== + + // unused + type handler1 = () => void; + ~~~~~~~~ +!!! error TS6133: 'handler1' is declared but never used. + + + function foo() { + type handler2 = () => void; + ~~~~~~~~ +!!! error TS6133: 'handler2' is declared but never used. + foo(); + } + + export {} \ No newline at end of file diff --git a/tests/baselines/reference/unusedLocalsAndParametersTypeAliases2.js b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases2.js new file mode 100644 index 0000000000000..7da916370ca72 --- /dev/null +++ b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases2.js @@ -0,0 +1,18 @@ +//// [unusedLocalsAndParametersTypeAliases2.ts] + +// unused +type handler1 = () => void; + + +function foo() { + type handler2 = () => void; + foo(); +} + +export {} + +//// [unusedLocalsAndParametersTypeAliases2.js] +"use strict"; +function foo() { + foo(); +} diff --git a/tests/baselines/reference/unusedParameterInCatchClause.errors.txt b/tests/baselines/reference/unusedParameterInCatchClause.errors.txt deleted file mode 100644 index e996763e2c6f4..0000000000000 --- a/tests/baselines/reference/unusedParameterInCatchClause.errors.txt +++ /dev/null @@ -1,10 +0,0 @@ -tests/cases/compiler/unusedParameterInCatchClause.ts(3,18): error TS6133: 'ex' is declared but never used. - - -==== tests/cases/compiler/unusedParameterInCatchClause.ts (1 errors) ==== - - function f1() { - try {} catch(ex){} - ~~ -!!! error TS6133: 'ex' is declared but never used. - } \ No newline at end of file diff --git a/tests/baselines/reference/unusedParameterInCatchClause.symbols b/tests/baselines/reference/unusedParameterInCatchClause.symbols new file mode 100644 index 0000000000000..9406d3ea53eb5 --- /dev/null +++ b/tests/baselines/reference/unusedParameterInCatchClause.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/unusedParameterInCatchClause.ts === + +function f1() { +>f1 : Symbol(f1, Decl(unusedParameterInCatchClause.ts, 0, 0)) + + try {} catch(ex){} +>ex : Symbol(ex, Decl(unusedParameterInCatchClause.ts, 2, 17)) +} diff --git a/tests/baselines/reference/unusedParameterInCatchClause.types b/tests/baselines/reference/unusedParameterInCatchClause.types new file mode 100644 index 0000000000000..ad23b5d753b74 --- /dev/null +++ b/tests/baselines/reference/unusedParameterInCatchClause.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/unusedParameterInCatchClause.ts === + +function f1() { +>f1 : () => void + + try {} catch(ex){} +>ex : any +} diff --git a/tests/baselines/reference/unusedPrivateMembers.js b/tests/baselines/reference/unusedPrivateMembers.js new file mode 100644 index 0000000000000..cdb540349c323 --- /dev/null +++ b/tests/baselines/reference/unusedPrivateMembers.js @@ -0,0 +1,114 @@ +//// [unusedPrivateMembers.ts] + +class Test1 { + private initializeInternal() { + } + + public test() { + var x = new Test1(); + x.initializeInternal(); + } +} + +class Test2 { + private p = 0; + public test() { + var x = new Test2(); + x.p; + } +} + +class Test3 { + private get x () { + return 0; + } + + public test() { + var x = new Test3(); + x.x; + } +} + +class Test4 { + private set x(v) { + v; + } + + public test() { + var x = new Test4(); + x.x; + } +} + +class Test5 { + private p: T; + public test() { + var x = new Test5(); + x.p; + } +} + + +//// [unusedPrivateMembers.js] +var Test1 = (function () { + function Test1() { + } + Test1.prototype.initializeInternal = function () { + }; + Test1.prototype.test = function () { + var x = new Test1(); + x.initializeInternal(); + }; + return Test1; +}()); +var Test2 = (function () { + function Test2() { + this.p = 0; + } + Test2.prototype.test = function () { + var x = new Test2(); + x.p; + }; + return Test2; +}()); +var Test3 = (function () { + function Test3() { + } + Object.defineProperty(Test3.prototype, "x", { + get: function () { + return 0; + }, + enumerable: true, + configurable: true + }); + Test3.prototype.test = function () { + var x = new Test3(); + x.x; + }; + return Test3; +}()); +var Test4 = (function () { + function Test4() { + } + Object.defineProperty(Test4.prototype, "x", { + set: function (v) { + v; + }, + enumerable: true, + configurable: true + }); + Test4.prototype.test = function () { + var x = new Test4(); + x.x; + }; + return Test4; +}()); +var Test5 = (function () { + function Test5() { + } + Test5.prototype.test = function () { + var x = new Test5(); + x.p; + }; + return Test5; +}()); diff --git a/tests/baselines/reference/unusedPrivateMembers.symbols b/tests/baselines/reference/unusedPrivateMembers.symbols new file mode 100644 index 0000000000000..0d4426a594ae7 --- /dev/null +++ b/tests/baselines/reference/unusedPrivateMembers.symbols @@ -0,0 +1,113 @@ +=== tests/cases/compiler/unusedPrivateMembers.ts === + +class Test1 { +>Test1 : Symbol(Test1, Decl(unusedPrivateMembers.ts, 0, 0)) + + private initializeInternal() { +>initializeInternal : Symbol(Test1.initializeInternal, Decl(unusedPrivateMembers.ts, 1, 13)) + } + + public test() { +>test : Symbol(Test1.test, Decl(unusedPrivateMembers.ts, 3, 5)) + + var x = new Test1(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 6, 11)) +>Test1 : Symbol(Test1, Decl(unusedPrivateMembers.ts, 0, 0)) + + x.initializeInternal(); +>x.initializeInternal : Symbol(Test1.initializeInternal, Decl(unusedPrivateMembers.ts, 1, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 6, 11)) +>initializeInternal : Symbol(Test1.initializeInternal, Decl(unusedPrivateMembers.ts, 1, 13)) + } +} + +class Test2 { +>Test2 : Symbol(Test2, Decl(unusedPrivateMembers.ts, 9, 1)) + + private p = 0; +>p : Symbol(Test2.p, Decl(unusedPrivateMembers.ts, 11, 13)) + + public test() { +>test : Symbol(Test2.test, Decl(unusedPrivateMembers.ts, 12, 18)) + + var x = new Test2(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 14, 11)) +>Test2 : Symbol(Test2, Decl(unusedPrivateMembers.ts, 9, 1)) + + x.p; +>x.p : Symbol(Test2.p, Decl(unusedPrivateMembers.ts, 11, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 14, 11)) +>p : Symbol(Test2.p, Decl(unusedPrivateMembers.ts, 11, 13)) + } +} + +class Test3 { +>Test3 : Symbol(Test3, Decl(unusedPrivateMembers.ts, 17, 1)) + + private get x () { +>x : Symbol(Test3.x, Decl(unusedPrivateMembers.ts, 19, 13)) + + return 0; + } + + public test() { +>test : Symbol(Test3.test, Decl(unusedPrivateMembers.ts, 22, 5)) + + var x = new Test3(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 25, 11)) +>Test3 : Symbol(Test3, Decl(unusedPrivateMembers.ts, 17, 1)) + + x.x; +>x.x : Symbol(Test3.x, Decl(unusedPrivateMembers.ts, 19, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 25, 11)) +>x : Symbol(Test3.x, Decl(unusedPrivateMembers.ts, 19, 13)) + } +} + +class Test4 { +>Test4 : Symbol(Test4, Decl(unusedPrivateMembers.ts, 28, 1)) + + private set x(v) { +>x : Symbol(Test4.x, Decl(unusedPrivateMembers.ts, 30, 13)) +>v : Symbol(v, Decl(unusedPrivateMembers.ts, 31, 18)) + + v; +>v : Symbol(v, Decl(unusedPrivateMembers.ts, 31, 18)) + } + + public test() { +>test : Symbol(Test4.test, Decl(unusedPrivateMembers.ts, 33, 5)) + + var x = new Test4(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 36, 11)) +>Test4 : Symbol(Test4, Decl(unusedPrivateMembers.ts, 28, 1)) + + x.x; +>x.x : Symbol(Test4.x, Decl(unusedPrivateMembers.ts, 30, 13)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 36, 11)) +>x : Symbol(Test4.x, Decl(unusedPrivateMembers.ts, 30, 13)) + } +} + +class Test5 { +>Test5 : Symbol(Test5, Decl(unusedPrivateMembers.ts, 39, 1)) +>T : Symbol(T, Decl(unusedPrivateMembers.ts, 41, 12)) + + private p: T; +>p : Symbol(Test5.p, Decl(unusedPrivateMembers.ts, 41, 16)) +>T : Symbol(T, Decl(unusedPrivateMembers.ts, 41, 12)) + + public test() { +>test : Symbol(Test5.test, Decl(unusedPrivateMembers.ts, 42, 17)) + + var x = new Test5(); +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 44, 11)) +>Test5 : Symbol(Test5, Decl(unusedPrivateMembers.ts, 39, 1)) + + x.p; +>x.p : Symbol(Test5.p, Decl(unusedPrivateMembers.ts, 41, 16)) +>x : Symbol(x, Decl(unusedPrivateMembers.ts, 44, 11)) +>p : Symbol(Test5.p, Decl(unusedPrivateMembers.ts, 41, 16)) + } +} + diff --git a/tests/baselines/reference/unusedPrivateMembers.types b/tests/baselines/reference/unusedPrivateMembers.types new file mode 100644 index 0000000000000..1d303068f24ba --- /dev/null +++ b/tests/baselines/reference/unusedPrivateMembers.types @@ -0,0 +1,121 @@ +=== tests/cases/compiler/unusedPrivateMembers.ts === + +class Test1 { +>Test1 : Test1 + + private initializeInternal() { +>initializeInternal : () => void + } + + public test() { +>test : () => void + + var x = new Test1(); +>x : Test1 +>new Test1() : Test1 +>Test1 : typeof Test1 + + x.initializeInternal(); +>x.initializeInternal() : void +>x.initializeInternal : () => void +>x : Test1 +>initializeInternal : () => void + } +} + +class Test2 { +>Test2 : Test2 + + private p = 0; +>p : number +>0 : number + + public test() { +>test : () => void + + var x = new Test2(); +>x : Test2 +>new Test2() : Test2 +>Test2 : typeof Test2 + + x.p; +>x.p : number +>x : Test2 +>p : number + } +} + +class Test3 { +>Test3 : Test3 + + private get x () { +>x : number + + return 0; +>0 : number + } + + public test() { +>test : () => void + + var x = new Test3(); +>x : Test3 +>new Test3() : Test3 +>Test3 : typeof Test3 + + x.x; +>x.x : number +>x : Test3 +>x : number + } +} + +class Test4 { +>Test4 : Test4 + + private set x(v) { +>x : any +>v : any + + v; +>v : any + } + + public test() { +>test : () => void + + var x = new Test4(); +>x : Test4 +>new Test4() : Test4 +>Test4 : typeof Test4 + + x.x; +>x.x : any +>x : Test4 +>x : any + } +} + +class Test5 { +>Test5 : Test5 +>T : T + + private p: T; +>p : T +>T : T + + public test() { +>test : () => void + + var x = new Test5(); +>x : Test5 +>new Test5() : Test5 +>Test5 : typeof Test5 + + x.p; +>x.p : number +>x : Test5 +>p : number + } +} + diff --git a/tests/baselines/reference/unusedSwitchStatment.errors.txt b/tests/baselines/reference/unusedSwitchStatment.errors.txt new file mode 100644 index 0000000000000..58359eed90d27 --- /dev/null +++ b/tests/baselines/reference/unusedSwitchStatment.errors.txt @@ -0,0 +1,31 @@ +tests/cases/compiler/unusedSwitchStatment.ts(4,13): error TS6133: 'x' is declared but never used. +tests/cases/compiler/unusedSwitchStatment.ts(7,15): error TS6133: 'c' is declared but never used. +tests/cases/compiler/unusedSwitchStatment.ts(10,13): error TS6133: 'z' is declared but never used. + + +==== tests/cases/compiler/unusedSwitchStatment.ts (3 errors) ==== + + switch (1) { + case 0: + let x; + ~ +!!! error TS6133: 'x' is declared but never used. + break; + case 1: + const c = 1; + ~ +!!! error TS6133: 'c' is declared but never used. + break; + default: + let z = 2; + ~ +!!! error TS6133: 'z' is declared but never used. + } + + + switch (2) { + case 0: + let x; + case 1: + x++; + } \ No newline at end of file diff --git a/tests/baselines/reference/unusedSwitchStatment.js b/tests/baselines/reference/unusedSwitchStatment.js new file mode 100644 index 0000000000000..2fd0d1fc49ee8 --- /dev/null +++ b/tests/baselines/reference/unusedSwitchStatment.js @@ -0,0 +1,38 @@ +//// [unusedSwitchStatment.ts] + +switch (1) { + case 0: + let x; + break; + case 1: + const c = 1; + break; + default: + let z = 2; +} + + +switch (2) { + case 0: + let x; + case 1: + x++; +} + +//// [unusedSwitchStatment.js] +switch (1) { + case 0: + var x = void 0; + break; + case 1: + var c = 1; + break; + default: + var z = 2; +} +switch (2) { + case 0: + var x = void 0; + case 1: + x++; +} diff --git a/tests/cases/compiler/unusedImports11.ts b/tests/cases/compiler/unusedImports11.ts new file mode 100644 index 0000000000000..4f2fed649d152 --- /dev/null +++ b/tests/cases/compiler/unusedImports11.ts @@ -0,0 +1,19 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +// @filename: b.ts +export class Member {} +export default Member; + + +// @filename: a.ts +import { Member } from './b'; +import d, { Member as M } from './b'; +import * as ns from './b'; +import r = require("./b"); + +new Member(); +new d(); +new M(); +new ns.Member(); +new r.Member(); \ No newline at end of file diff --git a/tests/cases/compiler/unusedImports12.ts b/tests/cases/compiler/unusedImports12.ts new file mode 100644 index 0000000000000..14206284b6439 --- /dev/null +++ b/tests/cases/compiler/unusedImports12.ts @@ -0,0 +1,13 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +// @filename: b.ts +export class Member {} +export default Member; + + +// @filename: a.ts +import { Member } from './b'; +import d, { Member as M } from './b'; +import * as ns from './b'; +import r = require("./b"); diff --git a/tests/cases/compiler/unusedLocalsAndParameters.ts b/tests/cases/compiler/unusedLocalsAndParameters.ts new file mode 100644 index 0000000000000..1f4fc944ff4f1 --- /dev/null +++ b/tests/cases/compiler/unusedLocalsAndParameters.ts @@ -0,0 +1,86 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +export { }; + +// function declaration paramter +function f(a) { +} +f(0); + +// function expression paramter +var fexp = function (a) { +}; + +fexp(0); + +// arrow function paramter +var farrow = (a) => { +}; + +class C { + // Method declaration paramter + method(a) { + } + // Accessor declaration paramter + set x(v: number) { + } +} + +var E = class { + // Method declaration paramter + method(a) { + } + // Accessor declaration paramter + set x(v: number) { + } +} + +var o = { + // Object literal method declaration paramter + method(a) { + }, + // Accessor declaration paramter + set x(v: number) { + } +}; + +o; + +// in a for..in statment +for (let i in o) { +} + +// in a for..of statment +for (let i of [1, 2, 3]) { +} + +// in a for. statment +for (let i = 0, n; i < 10; i++) { +} + +// in a block + +const condition = false; +if (condition) { + const c = 0; +} + +// in try/catch/finally +try { + const a = 0; +} +catch (e) { + const c = 1; +} +finally { + const c = 0; +} + + +// in a namespace +namespace N { + var x; +} + + \ No newline at end of file diff --git a/tests/cases/compiler/unusedLocalsAndParametersDeferred.ts b/tests/cases/compiler/unusedLocalsAndParametersDeferred.ts new file mode 100644 index 0000000000000..75b98308199d5 --- /dev/null +++ b/tests/cases/compiler/unusedLocalsAndParametersDeferred.ts @@ -0,0 +1,162 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +export { }; + +function defered(a: () => T): T { + return a(); +} + +// function declaration paramter +function f(a) { + defered(() => { + a; + }); +} +f(0); + +// function expression paramter +var fexp = function (a) { + defered(() => { + a; + }); +}; +fexp(1); + +// arrow function paramter +var farrow = (a) => { + defered(() => { + a; + }); +}; +farrow(2); + +let prop1; + +class C { + // Method declaration paramter + method(a) { + defered(() => { + a; + }); + } + // Accessor declaration paramter + set x(v: number) { + defered(() => { + v; + }); + } + // in a property initalizer + p = defered(() => { + prop1; + }); +} + +new C(); + +let prop2; + +var E = class { + // Method declaration paramter + method(a) { + defered(() => { + a; + }); + } + // Accessor declaration paramter + set x(v: number) { + defered(() => { + v; + }); + } + // in a property initalizer + p = defered(() => { + prop2; + }); +} + +new E(); + + +var o = { + // Object literal method declaration paramter + method(a) { + defered(() => { + a; + }); + }, + // Accessor declaration paramter + set x(v: number) { + defered(() => { + v; + }); + }, + // in a property initalizer + p: defered(() => { + prop1; + }) +}; + +o; + +// in a for..in statment +for (let i in o) { + defered(() => { + i; + }); +} + +// in a for..of statment +for (let i of [1,2,3]) { + defered(() => { + i; + }); +} + +// in a for. statment +for (let i = 0; i < 10; i++) { + defered(() => { + i; + }); +} + +// in a block + +const condition = false; +if (condition) { + const c = 0; + defered(() => { + c; + }); +} + +// in try/catch/finally +try { + const a = 0; + defered(() => { + a; + }); +} +catch (e) { + const c = 1; + defered(() => { + c; + }); +} +finally { + const c = 0; + defered(() => { + c; + }); +} + + +// in a namespace +namespace N { + var x; + defered(() => { + x; + }); +} +N; + \ No newline at end of file diff --git a/tests/cases/compiler/unusedLocalsAndParametersOverloadSignatures.ts b/tests/cases/compiler/unusedLocalsAndParametersOverloadSignatures.ts new file mode 100644 index 0000000000000..32affb28be3d1 --- /dev/null +++ b/tests/cases/compiler/unusedLocalsAndParametersOverloadSignatures.ts @@ -0,0 +1,25 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +export function func(details: number, message: string, ...args: any[]): void; +export function func(details: number, message: string): any { + return details + message; +} + +export class C { + constructor(details: number, message: string, ...args: any[]); + constructor(details: number, message: string) { + details + message; + } + + method(details: number, message: string, ...args: any[]): void; + method(details: number, message: string): any { + return details + message; + } +} + + +export function genericFunc(details: number, message: T, ...args: any[]): void; +export function genericFunc(details: number, message: any): any { + return details + message; +} \ No newline at end of file diff --git a/tests/cases/compiler/unusedLocalsAndParametersTypeAliases.ts b/tests/cases/compiler/unusedLocalsAndParametersTypeAliases.ts new file mode 100644 index 0000000000000..b94f88845bc2e --- /dev/null +++ b/tests/cases/compiler/unusedLocalsAndParametersTypeAliases.ts @@ -0,0 +1,29 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +// used in a declaration +type handler1 = () => void; +export interface I1 { + getHandler: handler1; +} + +// exported +export type handler2 = () => void; + +// used in extends clause +type handler3 = () => void; +export interface I3 { + getHandler: T; +} + +// used in another type alias declaration +type handler4 = () => void; +type handler5 = handler4 | (()=>number); +var x: handler5; +x(); + +// used as type argument +type handler6 = () => void; +var y: Array; +y[0](); + \ No newline at end of file diff --git a/tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts b/tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts new file mode 100644 index 0000000000000..23d23a5bc1892 --- /dev/null +++ b/tests/cases/compiler/unusedLocalsAndParametersTypeAliases2.ts @@ -0,0 +1,13 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +// unused +type handler1 = () => void; + + +function foo() { + type handler2 = () => void; + foo(); +} + +export {} \ No newline at end of file diff --git a/tests/cases/compiler/unusedPrivateMembers.ts b/tests/cases/compiler/unusedPrivateMembers.ts new file mode 100644 index 0000000000000..ab4e5ba9bca33 --- /dev/null +++ b/tests/cases/compiler/unusedPrivateMembers.ts @@ -0,0 +1,51 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true +//@target:ES5 + +class Test1 { + private initializeInternal() { + } + + public test() { + var x = new Test1(); + x.initializeInternal(); + } +} + +class Test2 { + private p = 0; + public test() { + var x = new Test2(); + x.p; + } +} + +class Test3 { + private get x () { + return 0; + } + + public test() { + var x = new Test3(); + x.x; + } +} + +class Test4 { + private set x(v) { + v; + } + + public test() { + var x = new Test4(); + x.x; + } +} + +class Test5 { + private p: T; + public test() { + var x = new Test5(); + x.p; + } +} diff --git a/tests/cases/compiler/unusedSwitchStatment.ts b/tests/cases/compiler/unusedSwitchStatment.ts new file mode 100644 index 0000000000000..53f93b913aad0 --- /dev/null +++ b/tests/cases/compiler/unusedSwitchStatment.ts @@ -0,0 +1,21 @@ +//@noUnusedLocals:true +//@noUnusedParameters:true + +switch (1) { + case 0: + let x; + break; + case 1: + const c = 1; + break; + default: + let z = 2; +} + + +switch (2) { + case 0: + let x; + case 1: + x++; +} \ No newline at end of file