From 151f940100c40de5fa412b7e622e963438b1afe0 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Tue, 25 Oct 2016 13:38:55 -0700 Subject: [PATCH] Refactor classDeclarations --- src/services/codefixes/interfaceFixes.ts | 53 ++++++++++++------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/services/codefixes/interfaceFixes.ts b/src/services/codefixes/interfaceFixes.ts index dc507360db..a9588fa806 100644 --- a/src/services/codefixes/interfaceFixes.ts +++ b/src/services/codefixes/interfaceFixes.ts @@ -8,28 +8,29 @@ namespace ts.codefix { const token = getTokenAtPosition(sourceFile, start); const checker = context.program.getTypeChecker(); - let textChanges: TextChange[] = []; - - if (token.kind === SyntaxKind.Identifier && token.parent.kind === SyntaxKind.ClassDeclaration) { + if (token.kind === SyntaxKind.Identifier && isClassLike(token.parent)) { const classDeclaration = token.parent; const startPos: number = classDeclaration.members.pos; - const classMembers = getClassMembers(classDeclaration); + const classMembers = ts.map(getNamedClassMemberDeclarations(classDeclaration), member => member.name.getText()); const trackingAddedMembers: string[] = []; const interfaceClauses = ts.getClassImplementsHeritageClauseElements(classDeclaration); - for (let i = 0; interfaceClauses && i < interfaceClauses.length; i++) { - textChanges = textChanges.concat(getChanges(interfaceClauses[i], classMembers, startPos, checker, /*reference*/ false, trackingAddedMembers, context.newLineCharacter)); - } - } + let textChanges: TextChange[] = undefined; - if (textChanges.length > 0) { - return [{ - description: getLocaleSpecificMessage(Diagnostics.Implement_interface_on_class), - changes: [{ - fileName: sourceFile.fileName, - textChanges: textChanges - }] - }]; + for (let i = 0; interfaceClauses && i < interfaceClauses.length; i++) { + let newChanges = getChanges(interfaceClauses[i], classMembers, startPos, checker, /*reference*/ false, trackingAddedMembers, context.newLineCharacter); + textChanges = textChanges ? textChanges.concat(newChanges) : newChanges; + } + + if (textChanges && textChanges.length > 0) { + return [{ + description: getLocaleSpecificMessage(Diagnostics.Implement_interface_on_class), + changes: [{ + fileName: sourceFile.fileName, + textChanges: textChanges + }] + }]; + } } return undefined; @@ -46,13 +47,13 @@ namespace ts.codefix { let textChanges: TextChange[] = []; - if (token.kind === SyntaxKind.Identifier && token.parent.kind === SyntaxKind.ClassDeclaration) { + if (token.kind === SyntaxKind.Identifier && isClassLike(token.parent)) { const classDeclaration = token.parent; const startPos = classDeclaration.members.pos; - const classMembers = getClassMembers(classDeclaration); + const abstractClassMembers = ts.map(getNamedClassAbstractMemberDeclarations(classDeclaration), member => member.name.getText()); const trackingAddedMembers: string[] = []; const extendsClause = ts.getClassExtendsHeritageClauseElement(classDeclaration); - textChanges = textChanges.concat(getChanges(extendsClause, classMembers, startPos, checker, /*reference*/ false, trackingAddedMembers, context.newLineCharacter)); + textChanges = textChanges.concat(getChanges(extendsClause, abstractClassMembers, startPos, checker, /*reference*/ false, trackingAddedMembers, context.newLineCharacter)); } if (textChanges.length > 0) { @@ -127,14 +128,12 @@ namespace ts.codefix { return result; } - function getClassMembers(classDeclaration: ClassDeclaration): string[] { - const classMembers: string[] = []; - for (let i = 0; classDeclaration.members && i < classDeclaration.members.length; i++) { - if (classDeclaration.members[i].name) { - classMembers.push(classDeclaration.members[i].name.getText()); - } - } - return classMembers; + function getNamedClassMemberDeclarations(classDeclaration: ClassDeclaration): ClassElement[] { + return classDeclaration.members.filter(member => member.name); + } + + function getNamedClassAbstractMemberDeclarations(classDeclaration: ClassDeclaration): ClassElement[] { + return getNamedClassMemberDeclarations(classDeclaration).filter(member => getModifierFlags(member) & ModifierFlags.Abstract); } function getMembersAndStartPosFromReference(variableDeclaration: VariableDeclaration): { startPos: number, members: string[] } {