Skip to content

Commit

Permalink
Update refactoring, to clean transformed class and their subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin777 committed Feb 22, 2021
1 parent 2990df5 commit 690d71d
Showing 1 changed file with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"
I am a refactoring for changing a temporary variable to an instance variable.
The temporary variable is added to the class definition and removed from the temporary declaration in this method.
My preconditions verify that this variable is not yet used as an instance variable in this class.
My preconditions verify that this variable is not yet used as an instance variable in the whole hierarchy of this class.
The temporary variable is added to the class definition and removed from the temporary declaration in this method .
If this instance variable is already used in a subclass will be removed from that class, because subclasses already inherit this attribute.
The temporary variables with the same name in hierarchy will be removed, and replaced with the new instance variable.
"
Class {
#name : #RBTemporaryToInstanceVariableRefactoring,
Expand Down Expand Up @@ -61,14 +65,30 @@ RBTemporaryToInstanceVariableRefactoring >> class: aClass selector: aSelector va

{ #category : #preconditions }
RBTemporaryToInstanceVariableRefactoring >> preconditions [
^(RBCondition definesSelector: selector in: class)
& (RBCondition hierarchyOf: class
definesVariable: temporaryVariableName asString) not
^(RBCondition definesSelector: selector in: class)
& (RBCondition definesInstanceVariable: temporaryVariableName asString in: class) not
& (RBCondition withBlock:
[self checkForValidTemporaryVariable.
true])
]

{ #category : #removing }
RBTemporaryToInstanceVariableRefactoring >> removeTemporaryOfClass: aClass [
aClass selectors do: [ :aSymbol | self removeTemporaryOfMethod: aSymbol in: aClass ]
]

{ #category : #removing }
RBTemporaryToInstanceVariableRefactoring >> removeTemporaryOfMethod: aSelector in: aClass [

| parseTree matcher method |
method := aClass methodFor: aSelector.
parseTree := method parseTree.
parseTree ifNil: [ self refactoringFailure: 'Could not parse method' ].
( matcher := self parseTreeRewriterClass removeTemporaryNamed: temporaryVariableName )
executeTree: parseTree.
method compileTree: matcher tree.
]

{ #category : #printing }
RBTemporaryToInstanceVariableRefactoring >> storeOn: aStream [
aStream nextPut: $(.
Expand All @@ -87,13 +107,11 @@ RBTemporaryToInstanceVariableRefactoring >> storeOn: aStream [
{ #category : #transforming }
RBTemporaryToInstanceVariableRefactoring >> transform [
| parseTree matcher method |
method := class methodFor: selector.
parseTree := method parseTree.
parseTree ifNil: [ self refactoringFailure: 'Could not parse method' ].
self removeTemporaryOfClass: class.
class allSubclasses do: [ :cls |
(cls definesInstanceVariable: temporaryVariableName)
ifTrue: [ cls removeInstanceVariable: temporaryVariableName ]
ifFalse: [ self removeTemporaryOfClass: cls ] ].
class addInstanceVariable: temporaryVariableName.
( matcher := self parseTreeRewriterClass removeTemporaryNamed: temporaryVariableName )
executeTree: parseTree.
method compileTree: matcher tree
]

0 comments on commit 690d71d

Please sign in to comment.