|
| 1 | +/* @internal */ |
| 2 | +namespace ts.refactor.GenerateGetterAndSetter { |
| 3 | + const actionName = "Generate 'get' and 'set' accessors"; |
| 4 | + const actionDescription = Diagnostics.Generate_get_and_set_accessors.message; |
| 5 | + |
| 6 | + registerRefactor(actionName, { getEditsForAction, getAvailableActions }); |
| 7 | + |
| 8 | + function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { |
| 9 | + const { file, startPosition } = context; |
| 10 | + |
| 11 | + const fieldInfo = getConvertibleFieldAtPosition(file, startPosition); |
| 12 | + if (!fieldInfo) return undefined; |
| 13 | + |
| 14 | + return [ |
| 15 | + { |
| 16 | + name: actionName, |
| 17 | + description: actionDescription, |
| 18 | + actions: [ |
| 19 | + { |
| 20 | + name: actionName, |
| 21 | + description: actionDescription |
| 22 | + } |
| 23 | + ] |
| 24 | + } |
| 25 | + ]; |
| 26 | + } |
| 27 | + |
| 28 | + function getEditsForAction(context: RefactorContext, _actionName: string): RefactorEditInfo | undefined { |
| 29 | + const { file, startPosition } = context; |
| 30 | + |
| 31 | + const fieldInfo = getConvertibleFieldAtPosition(file, startPosition); |
| 32 | + if (!fieldInfo) return undefined; |
| 33 | + |
| 34 | + const changeTracker = textChanges.ChangeTracker.fromContext(context); |
| 35 | + const newLineCharacter = getNewLineOrDefaultFromHost(context.host, context.formatContext.options); |
| 36 | + |
| 37 | + const { fieldName, accessorName, propertyDeclaration, needUpdateName, hasModifiers, needUpdateModifiers } = fieldInfo; |
| 38 | + const accessorModifiers = hasModifiers ? createNodeArray([createToken(SyntaxKind.PublicKeyword)]) : undefined; |
| 39 | + |
| 40 | + const getAccessor = generateGetAccessor(propertyDeclaration, fieldName, accessorName, accessorModifiers); |
| 41 | + const setAccessor = generateSetAccessor(propertyDeclaration, fieldName, accessorName, accessorModifiers); |
| 42 | + |
| 43 | + const modifiers = needUpdateModifiers ? createNodeArray([createToken(SyntaxKind.PrivateKeyword)]) : propertyDeclaration.modifiers; |
| 44 | + if (needUpdateName || needUpdateModifiers) { |
| 45 | + changeTracker.replaceNode(file, propertyDeclaration, updateOriginPropertyDeclaration(propertyDeclaration, fieldName, modifiers), { |
| 46 | + suffix: newLineCharacter |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + changeTracker.insertNodeAfter(file, propertyDeclaration, getAccessor); |
| 51 | + changeTracker.insertNodeAfter(file, propertyDeclaration, setAccessor); |
| 52 | + |
| 53 | + return { |
| 54 | + edits: changeTracker.getChanges(), |
| 55 | + renameFilename: undefined, |
| 56 | + renameLocation: undefined, |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + interface Info { originName: string; fieldName: string; accessorName: string; propertyDeclaration: PropertyDeclaration; needUpdateName: boolean; hasModifiers: boolean; needUpdateModifiers: boolean; } |
| 61 | + function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined { |
| 62 | + const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); |
| 63 | + const propertyDeclaration = findAncestor(node.parent, isPropertyDeclaration); |
| 64 | + |
| 65 | + if (!(propertyDeclaration && propertyDeclaration.name.kind === SyntaxKind.Identifier && |
| 66 | + (getModifierFlags(propertyDeclaration) | ModifierFlags.AccessibilityModifier) === ModifierFlags.AccessibilityModifier)) return undefined; |
| 67 | + |
| 68 | + const containerClass = getContainingClass(propertyDeclaration); |
| 69 | + if (!containerClass) return undefined; |
| 70 | + |
| 71 | + const members = getMembersOfDeclaration(containerClass); |
| 72 | + if (!members) return undefined; |
| 73 | + |
| 74 | + const needUpdateName = propertyDeclaration.name.text.charCodeAt(0) !== CharacterCodes._; |
| 75 | + |
| 76 | + const accessorName = needUpdateName ? propertyDeclaration.name.text : propertyDeclaration.name.text.substring(1); |
| 77 | + const fieldName = `_${accessorName}`; |
| 78 | + |
| 79 | + if (find(members, member => needUpdateName ? member.name.getText() === fieldName : member.name.getText() === accessorName)) return undefined; |
| 80 | + |
| 81 | + const hasModifiers = !!find(members, member => !!member.modifiers); |
| 82 | + const needUpdateModifiers = hasModifiers && (!propertyDeclaration.modifiers || hasModifier(propertyDeclaration, ModifierFlags.Public)); |
| 83 | + |
| 84 | + return { |
| 85 | + originName: propertyDeclaration.name.text, |
| 86 | + fieldName, |
| 87 | + accessorName, |
| 88 | + propertyDeclaration, |
| 89 | + needUpdateName, |
| 90 | + hasModifiers, |
| 91 | + needUpdateModifiers |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + function generateGetAccessor (propertyDeclaration: PropertyDeclaration, fieldName: string, name: string, modifiers: ModifiersArray) { |
| 96 | + return createGetAccessor( |
| 97 | + /*decorators*/ undefined, |
| 98 | + modifiers, |
| 99 | + name, |
| 100 | + /*parameters*/ undefined, |
| 101 | + propertyDeclaration.type, |
| 102 | + createBlock([ |
| 103 | + createReturn( |
| 104 | + createPropertyAccess( |
| 105 | + createThis(), |
| 106 | + fieldName |
| 107 | + ) |
| 108 | + ) |
| 109 | + ], /*multiLine*/ true) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + function generateSetAccessor (propertyDeclaration: PropertyDeclaration, fieldName: string, name: string, modifiers: ModifiersArray) { |
| 114 | + return createSetAccessor( |
| 115 | + /*decorators*/ undefined, |
| 116 | + modifiers, |
| 117 | + name, |
| 118 | + [createParameter( |
| 119 | + /*decorators*/ undefined, |
| 120 | + /*modifies*/ undefined, |
| 121 | + /*dotDotDotToken*/ undefined, |
| 122 | + createIdentifier("value"), |
| 123 | + /*questionToken*/ undefined, |
| 124 | + propertyDeclaration.type |
| 125 | + )], |
| 126 | + createBlock([ |
| 127 | + createStatement( |
| 128 | + createAssignment( |
| 129 | + createPropertyAccess( |
| 130 | + createThis(), |
| 131 | + fieldName |
| 132 | + ), |
| 133 | + createIdentifier("value") |
| 134 | + ) |
| 135 | + ) |
| 136 | + ], /*multiLine*/ true) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + function updateOriginPropertyDeclaration (propertyDeclaration: PropertyDeclaration, fieldName: string, modifiers: ModifiersArray) { |
| 141 | + return updateProperty( |
| 142 | + propertyDeclaration, |
| 143 | + /*decorators*/ undefined, |
| 144 | + modifiers, |
| 145 | + fieldName, |
| 146 | + /*questionOrExclamationToken*/ undefined, |
| 147 | + propertyDeclaration.type, |
| 148 | + propertyDeclaration.initializer, |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments