From 03a6eeb6436780c78026c1bbf453feec1d6ba422 Mon Sep 17 00:00:00 2001 From: Paul van Brenk Date: Fri, 21 Oct 2016 14:56:38 -0700 Subject: [PATCH] Major refactoring after PR feedback --- .../codefixes/unusedIdentifierFixes.ts | 225 ++++++++---------- 1 file changed, 103 insertions(+), 122 deletions(-) diff --git a/src/services/codefixes/unusedIdentifierFixes.ts b/src/services/codefixes/unusedIdentifierFixes.ts index 9a69af04bfc..4c5f0873d5f 100644 --- a/src/services/codefixes/unusedIdentifierFixes.ts +++ b/src/services/codefixes/unusedIdentifierFixes.ts @@ -10,134 +10,106 @@ namespace ts.codefix { const start = context.span.start; const token = getTokenAtPosition(sourceFile, start); - if (token.kind === ts.SyntaxKind.Identifier) { - if (token.parent.kind === ts.SyntaxKind.VariableDeclaration) { - if (token.parent.parent.parent.kind === SyntaxKind.ForStatement) { - const forStatement = token.parent.parent.parent; - const initializer = forStatement.initializer; - if (initializer.declarations.length === 1) { - return createCodeFix("", initializer.pos, initializer.end - initializer.pos); - } - else { - if (initializer.declarations[0] === token.parent) { - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1); - } - else { - return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1); - } - } - } - else if (token.parent.parent.parent.kind === SyntaxKind.ForInStatement) { - const forInStatement = token.parent.parent.parent; - const initializer = forInStatement.initializer; - return createCodeFix("{}", initializer.declarations[0].pos, initializer.declarations[0].end - initializer.declarations[0].pos); - } - else if (token.parent.parent.parent.kind === SyntaxKind.ForOfStatement) { - const forOfStatement = token.parent.parent.parent; - const initializer = forOfStatement.initializer; - return createCodeFix("{}", initializer.declarations[0].pos, initializer.declarations[0].end - initializer.declarations[0].pos); - } - else if (token.parent.parent.kind === SyntaxKind.CatchClause) { - const catchClause = token.parent.parent; - const parameter = catchClause.variableDeclaration.getChildren()[0]; - return createCodeFix("", parameter.pos, parameter.end - parameter.pos); - } - else { - const variableStatement = token.parent.parent.parent; - if (variableStatement.declarationList.declarations.length === 1) { - return createCodeFix("", variableStatement.pos, variableStatement.end - variableStatement.pos); - } - else { - const declarations = variableStatement.declarationList.declarations; - if (declarations[0].name === token) { - return createCodeFix("", token.parent.pos + 1, token.parent.end - token.parent.pos); - } - else { - return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1); - } - } - } - } + switch (token.kind) { + case ts.SyntaxKind.Identifier: + switch (token.parent.kind) { + case ts.SyntaxKind.VariableDeclaration: + switch (token.parent.parent.parent.kind) { + case SyntaxKind.ForStatement: + const forStatement = token.parent.parent.parent; + const forInitializer = forStatement.initializer; + if (forInitializer.declarations.length === 1) { + return createCodeFix("", forInitializer.pos, forInitializer.end - forInitializer.pos); + } + else { + return removeSingleItem(forInitializer.declarations, token); + } - if (token.parent.kind === SyntaxKind.FunctionDeclaration || - token.parent.kind === SyntaxKind.ClassDeclaration || - token.parent.kind === SyntaxKind.InterfaceDeclaration || - token.parent.kind === SyntaxKind.MethodDeclaration || - token.parent.kind === SyntaxKind.ModuleDeclaration || - token.parent.kind === SyntaxKind.PropertyDeclaration || - token.parent.kind === SyntaxKind.ArrowFunction) { + case SyntaxKind.ForOfStatement: + case SyntaxKind.ForInStatement: + const forOfStatement = token.parent.parent.parent; + if (forOfStatement.initializer.kind === SyntaxKind.VariableDeclarationList) { + const forOfInitializer = forOfStatement.initializer; + return createCodeFix("{}", forOfInitializer.declarations[0].pos, forOfInitializer.declarations[0].end - forOfInitializer.declarations[0].pos); + } + break; + + case SyntaxKind.CatchClause: + const catchClause = token.parent.parent; + const parameter = catchClause.variableDeclaration.getChildren()[0]; + return createCodeFix("", parameter.pos, parameter.end - parameter.pos); + + default: + const variableStatement = token.parent.parent.parent; + if (variableStatement.declarationList.declarations.length === 1) { + return createCodeFix("", variableStatement.pos, variableStatement.end - variableStatement.pos); + } + else { + const declarations = variableStatement.declarationList.declarations; + return removeSingleItem(declarations, token); + } + } + + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.ClassDeclaration: + case SyntaxKind.InterfaceDeclaration: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.PropertyDeclaration: + case SyntaxKind.ArrowFunction: + return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); + + case SyntaxKind.TypeParameter: + const typeParameters = (token.parent.parent).typeParameters; + if (typeParameters.length === 1) { + return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 2); + } + else { + return removeSingleItem(typeParameters, token); + } + + case ts.SyntaxKind.Parameter: + const functionDeclaration = token.parent.parent; + if (functionDeclaration.parameters.length === 1) { + return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); + } + else { + return removeSingleItem(functionDeclaration.parameters, token); + } + + case SyntaxKind.ImportSpecifier: + const namedImports = token.parent.parent; + const elements = namedImports.elements; + if (elements.length === 1) { + // Only 1 import and it is unused. So the entire line could be removed. + return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); + } + else { + return removeSingleItem(elements, token); + } + + case SyntaxKind.ImportEqualsDeclaration: + return createCodeFix("{}", token.pos, token.end - token.pos); + + case SyntaxKind.EnumDeclaration: + return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); + } + + if (token.parent.parent.kind === SyntaxKind.ImportClause || token.parent.parent.kind === SyntaxKind.ImportDeclaration) { + return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos); + } + break; + + case SyntaxKind.PrivateKeyword: + case SyntaxKind.PropertyDeclaration: return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); - } - if (token.parent.kind === SyntaxKind.TypeParameter) { - const typeParameters = (token.parent.parent).typeParameters; - if (typeParameters.length === 1) { - return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 2); - } - else { - if (typeParameters[0] === token.parent) { - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1); - } - else { - return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1); - } - } - } - - if (token.parent.kind === ts.SyntaxKind.Parameter) { - const functionDeclaration = token.parent.parent; - if (functionDeclaration.parameters.length === 1) { - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); - } - else { - if (functionDeclaration.parameters[0] === token.parent) { - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1); - } - else { - return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1); - } - } - } - - if (token.parent.kind === SyntaxKind.ImportSpecifier) { - const namedImports = token.parent.parent; - const elements = namedImports.elements; - if (elements.length === 1) { - // Only 1 import and it is unused. So the entire line could be removed. - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); - } - else { - if (elements[0] === token.parent) { - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1); - } - else { - return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1); - } - } - } - - if (token.parent.parent.kind === SyntaxKind.ImportClause || token.parent.parent.kind === SyntaxKind.ImportDeclaration) { + case SyntaxKind.AsteriskToken: + case SyntaxKind.NamespaceImport: return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos); - } - - if (token.parent.kind === SyntaxKind.ImportEqualsDeclaration) { - return createCodeFix("{}", token.pos, token.end - token.pos); - } - - if (token.parent.kind === SyntaxKind.EnumDeclaration) { - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); - } } - if (token.kind === SyntaxKind.PrivateKeyword && token.parent.kind === SyntaxKind.PropertyDeclaration) { - return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos); - } - - if (token.kind === SyntaxKind.AsteriskToken && token.parent.kind === SyntaxKind.NamespaceImport) { - return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos); - } - - return undefined; + return []; function createCodeFix(newText: string, start: number, length: number): CodeAction[] { return [{ @@ -148,6 +120,15 @@ namespace ts.codefix { }] }]; } + + function removeSingleItem(elements: NodeArray, token: T): CodeAction[] { + if (elements[0] === token.parent) { + return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos + 1); + } + else { + return createCodeFix("", token.parent.pos - 1, token.parent.end - token.parent.pos + 1); + } + } } }); }