-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Codefix for implementing interfaces #11547
Changes from 1 commit
1438f9a
7a5cb5f
bf3e487
6b4f6b5
151f940
d1cea73
c2feab6
f74872d
2abf906
132b746
ecc029f
a66b0ae
5d9a4e3
3ac9ffa
d24236b
7758e6d
7272833
834245c
c89b97b
16dc834
cbaea99
bc21346
99ae5d9
aa6ecd4
3240000
0380f3f
1b60a97
e5279fd
04968ab
d02eb6c
3b0b696
36c5bef
1b8486d
b7b30aa
71d1744
8c35185
bc1bb0e
0ce53f0
b26ba83
11cea6a
7141a2a
55bf3e3
4441380
1ec234a
6bd35fb
4973852
1d6ef6a
d842a6f
b1e97b3
c650c33
0591e1b
263734f
4b202ab
357ed7e
f6fc320
efd16c7
bba96da
cfe50d1
ad3035d
d8b359f
6400d53
c010a0e
395d736
d7d4bf6
a94d955
43afb80
6ed8d18
389959a
69118cd
4af0e2a
680af0f
16b146f
f37640a
5d6a714
bf48564
ba80ce6
8134d64
0c1772b
f0c7713
c511aea
5cd0ea3
c22e47d
c1a41b9
2f51b36
97b3d7a
819a654
5e48e33
1338b94
b9ae36c
d724517
469745b
1ba6c86
ad01110
3cfac08
4673812
4b02099
8be8819
d75d548
4af3937
97f18c9
3fc94bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = <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); | ||
|
||
let textChanges: TextChange[] = undefined; | ||
|
||
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) { | ||
return [{ | ||
description: getLocaleSpecificMessage(Diagnostics.Implement_interface_on_class), | ||
changes: [{ | ||
fileName: sourceFile.fileName, | ||
textChanges: textChanges | ||
}] | ||
}]; | ||
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[] = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the point of this initialization, you can just override it below. consider just leaving this as undefined, and checking for undefined below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored |
||
|
||
if (token.kind === SyntaxKind.Identifier && token.parent.kind === SyntaxKind.ClassDeclaration) { | ||
if (token.kind === SyntaxKind.Identifier && isClassLike(token.parent)) { | ||
const classDeclaration = <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[] } { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the class does not have curlies? can add tests?