Refactor classDeclarations
This commit is contained in:
parent
6b4f6b5864
commit
151f940100
1 changed files with 26 additions and 27 deletions
|
@ -8,21 +8,21 @@ namespace ts.codefix {
|
||||||
const token = getTokenAtPosition(sourceFile, start);
|
const token = getTokenAtPosition(sourceFile, start);
|
||||||
const checker = context.program.getTypeChecker();
|
const checker = context.program.getTypeChecker();
|
||||||
|
|
||||||
let textChanges: TextChange[] = [];
|
if (token.kind === SyntaxKind.Identifier && isClassLike(token.parent)) {
|
||||||
|
|
||||||
if (token.kind === SyntaxKind.Identifier && token.parent.kind === SyntaxKind.ClassDeclaration) {
|
|
||||||
const classDeclaration = <ClassDeclaration>token.parent;
|
const classDeclaration = <ClassDeclaration>token.parent;
|
||||||
const startPos: number = classDeclaration.members.pos;
|
const startPos: number = classDeclaration.members.pos;
|
||||||
const classMembers = getClassMembers(classDeclaration);
|
const classMembers = ts.map(getNamedClassMemberDeclarations(classDeclaration), member => member.name.getText());
|
||||||
const trackingAddedMembers: string[] = [];
|
const trackingAddedMembers: string[] = [];
|
||||||
const interfaceClauses = ts.getClassImplementsHeritageClauseElements(classDeclaration);
|
const interfaceClauses = ts.getClassImplementsHeritageClauseElements(classDeclaration);
|
||||||
|
|
||||||
|
let textChanges: TextChange[] = undefined;
|
||||||
|
|
||||||
for (let i = 0; interfaceClauses && i < interfaceClauses.length; i++) {
|
for (let i = 0; interfaceClauses && i < interfaceClauses.length; i++) {
|
||||||
textChanges = textChanges.concat(getChanges(interfaceClauses[i], classMembers, startPos, checker, /*reference*/ false, trackingAddedMembers, context.newLineCharacter));
|
let newChanges = getChanges(interfaceClauses[i], classMembers, startPos, checker, /*reference*/ false, trackingAddedMembers, context.newLineCharacter);
|
||||||
}
|
textChanges = textChanges ? textChanges.concat(newChanges) : newChanges;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textChanges.length > 0) {
|
if (textChanges && textChanges.length > 0) {
|
||||||
return [{
|
return [{
|
||||||
description: getLocaleSpecificMessage(Diagnostics.Implement_interface_on_class),
|
description: getLocaleSpecificMessage(Diagnostics.Implement_interface_on_class),
|
||||||
changes: [{
|
changes: [{
|
||||||
|
@ -31,6 +31,7 @@ namespace ts.codefix {
|
||||||
}]
|
}]
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
@ -46,13 +47,13 @@ namespace ts.codefix {
|
||||||
|
|
||||||
let textChanges: TextChange[] = [];
|
let textChanges: TextChange[] = [];
|
||||||
|
|
||||||
if (token.kind === SyntaxKind.Identifier && token.parent.kind === SyntaxKind.ClassDeclaration) {
|
if (token.kind === SyntaxKind.Identifier && isClassLike(token.parent)) {
|
||||||
const classDeclaration = <ClassDeclaration>token.parent;
|
const classDeclaration = <ClassDeclaration>token.parent;
|
||||||
const startPos = classDeclaration.members.pos;
|
const startPos = classDeclaration.members.pos;
|
||||||
const classMembers = getClassMembers(classDeclaration);
|
const abstractClassMembers = ts.map(getNamedClassAbstractMemberDeclarations(classDeclaration), member => member.name.getText());
|
||||||
const trackingAddedMembers: string[] = [];
|
const trackingAddedMembers: string[] = [];
|
||||||
const extendsClause = ts.getClassExtendsHeritageClauseElement(classDeclaration);
|
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) {
|
if (textChanges.length > 0) {
|
||||||
|
@ -127,14 +128,12 @@ namespace ts.codefix {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getClassMembers(classDeclaration: ClassDeclaration): string[] {
|
function getNamedClassMemberDeclarations(classDeclaration: ClassDeclaration): ClassElement[] {
|
||||||
const classMembers: string[] = [];
|
return classDeclaration.members.filter(member => member.name);
|
||||||
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 getNamedClassAbstractMemberDeclarations(classDeclaration: ClassDeclaration): ClassElement[] {
|
||||||
|
return getNamedClassMemberDeclarations(classDeclaration).filter(member => getModifierFlags(member) & ModifierFlags.Abstract);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMembersAndStartPosFromReference(variableDeclaration: VariableDeclaration): { startPos: number, members: string[] } {
|
function getMembersAndStartPosFromReference(variableDeclaration: VariableDeclaration): { startPos: number, members: string[] } {
|
||||||
|
|
Loading…
Reference in a new issue