-
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 |
---|---|---|
|
@@ -16,22 +16,28 @@ namespace ts.codefix { | |
const startPos: number = classDecl.members.pos; | ||
|
||
const implementedTypeNodes = getClassImplementsHeritageClauseElements(classDecl); | ||
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. consider pushing these inside the loop to enhance readability 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. Changed in commit 16b146f |
||
const implementedTypes = implementedTypeNodes.map(checker.getTypeFromTypeReference); | ||
const implementedTypeSymbols = implementedTypes.map(checker.getPropertiesOfType); | ||
|
||
const result: CodeAction[] = []; | ||
|
||
for (const symbols of implementedTypeSymbols) { | ||
const symbolMap = createMap<Symbol>(); | ||
for (const symbol of symbols) { | ||
symbolMap[symbol.getName()] = symbol; | ||
} | ||
const insertion = getMissingMembersInsertion(classDecl, filterNonPrivate(symbolMap), checker, context.newLineCharacter); | ||
for (const implementedTypeNode of implementedTypeNodes) { | ||
const implementedType = checker.getTypeFromTypeReference(implementedTypeNode); | ||
// Note that this is ultimately derived from a map indexed by symbol names, | ||
// so duplicates cannot occur. | ||
const implementedTypeSymbols = checker.getPropertiesOfType(implementedType); | ||
const nonPrivateMembers = implementedTypeSymbols.filter(symbolRefersToNonPrivateMember); | ||
|
||
const insertion = getMissingMembersInsertion(classDecl, nonPrivateMembers, checker, context.newLineCharacter); | ||
pushAction(result, insertion, getLocaleSpecificMessage(Diagnostics.Implement_interface_on_class)); | ||
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. either we move the error span to be the interface name, or we should add a name for the interface i the message. 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. Added the interface name to the message. |
||
} | ||
|
||
return result; | ||
|
||
function symbolRefersToNonPrivateMember(symbol: Symbol): boolean { | ||
const decls = symbol.getDeclarations(); | ||
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. just: return symbol.valueDecaration.flags & ModifierFlags.Private 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. EDIT: moved response to the right place. Made the change here. |
||
Debug.assert(!!(decls && decls.length > 0)); | ||
return !(getModifierFlags(decls[0]) & ModifierFlags.Private); | ||
} | ||
|
||
function pushAction(result: CodeAction[], insertion: string, description: string): void { | ||
if (insertion && insertion.length) { | ||
const newAction: CodeAction = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1362,51 +1362,16 @@ namespace ts { | |
/** | ||
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. i would put these in 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. Changed. |
||
* Finds members of the resolved type that are missing in the class pointed to by class decl | ||
* and generates source code for the missing members. | ||
* @param possiblyMissingSymbols The collection of symbols to filter. | ||
* @param possiblyMissingSymbols The collection of symbols to filter and then get insertions for. | ||
*/ | ||
export function getMissingMembersInsertion(classDeclaration: ClassLikeDeclaration, possiblyMissingSymbols: Map<Symbol>, checker: TypeChecker, newlineChar: string): string { | ||
const missingMembers = filterMissingMembers(possiblyMissingSymbols, classDeclaration.symbol.members); | ||
return getInsertionsForMembers(missingMembers, classDeclaration, checker, newlineChar); | ||
} | ||
|
||
/** | ||
* Finds the symbols in source but not target, generating a new map with the differences. | ||
*/ | ||
function filterMissingMembers(sourceSymbols: Map<Symbol>, targetSymbols: Map<Symbol>): Map<Symbol> { | ||
const result: Map<Symbol> = createMap<Symbol>(); | ||
for (const sourceName in sourceSymbols) { | ||
if (!(sourceName in targetSymbols)) { | ||
result[sourceName] = sourceSymbols[sourceName]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
function filterSymbolMapByDeclaration(symbolMap: Map<Symbol>, pred: (decl: Declaration) => boolean): Map<Symbol> { | ||
const result = createMap<Symbol>(); | ||
for (const key in symbolMap) { | ||
const declaration = symbolMap[key].getDeclarations(); | ||
Debug.assert(!!(declaration && declaration.length)); | ||
if (pred(declaration[0])) { | ||
result[key] = symbolMap[key]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
export function filterAbstractAndNonPrivate(symbolMap: Map<Symbol>) { | ||
return filterSymbolMapByDeclaration(symbolMap, decl => !(getModifierFlags(decl) & ModifierFlags.Private) && !!(getModifierFlags(decl) & ModifierFlags.Abstract)); | ||
} | ||
|
||
export function filterNonPrivate(symbolMap: Map<Symbol>) { | ||
return filterSymbolMapByDeclaration(symbolMap, decl => !(getModifierFlags(decl) & ModifierFlags.Private)); | ||
} | ||
export function getMissingMembersInsertion(classDeclaration: ClassLikeDeclaration, possiblyMissingSymbols: Symbol[], checker: TypeChecker, newlineChar: string): string { | ||
const classMembers = classDeclaration.symbol.members; | ||
const missingMembers = possiblyMissingSymbols.filter(symbol => !(symbol.getName() in classMembers)); | ||
|
||
function getInsertionsForMembers(symbolMap: MapLike<Symbol>, enclosingDeclaration: ClassLikeDeclaration, checker: TypeChecker, newlineChar: string): string { | ||
let insertion = ""; | ||
|
||
for (const symbolName in symbolMap) { | ||
insertion = insertion.concat(getInsertionForMemberSymbol(symbolMap[symbolName], enclosingDeclaration, checker, newlineChar)); | ||
for (const symbol of missingMembers) { | ||
insertion = insertion.concat(getInsertionForMemberSymbol(symbol, classDeclaration, checker, newlineChar)); | ||
} | ||
return insertion; | ||
} | ||
|
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.
i would ignore private here.
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.
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.
Needed to suppress fix when the user creates a class with a private abstract property/member. Is there a better way to detect general errors on a declaration/node? This would be useful to suppress codefixes in many situations.