Skip to content

Commit

Permalink
Merge pull request #97 from jecisc/96-Reapply-ChanelExtractReturnFrom…
Browse files Browse the repository at this point in the history
…AllBranchesCleanerTest-on-methods-who-were-cleaned

96-Reapply-ChanelExtractReturnFromAllBranchesCleanerTest-on-methods-who-were-cleaned
  • Loading branch information
jecisc committed May 9, 2020
2 parents 24eb895 + 49342cb commit 71f3eab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ ChanelExtractReturnFromAllBranchesCleanerTest >> testDoesNotExtractReturnIfRetur
self assert: oldMethod identicalTo: class >> #method
]

{ #category : #tests }
ChanelExtractReturnFromAllBranchesCleanerTest >> testExtractReturnDoesNotFailForConditonsInConditions [
class compile: 'method
self toto1 ifTrue: [ self toto2 ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ] ] ifFalse: [ 3 ]'.

self runCleaner.

self assert: (class >> #method) sourceCode equals: 'method
self toto1 ifTrue: [ ^self toto2 ifTrue: [ 1 ] ifFalse: [ 2 ] ] ifFalse: [ 3 ]'
]

{ #category : #tests }
ChanelExtractReturnFromAllBranchesCleanerTest >> testExtractReturnDoesNotFailIfThereIsAlreadyAReturn [
class compile: 'method
Expand Down Expand Up @@ -249,6 +260,17 @@ ChanelExtractReturnFromAllBranchesCleanerTest >> testExtractReturnIfTheLastState
self bar'
]

{ #category : #tests }
ChanelExtractReturnFromAllBranchesCleanerTest >> testExtractReturnInNestedConditions [
class compile: 'method
self toto1 ifTrue: [ self toto2 ifTrue: [ ^1 ] ifFalse: [ ^2 ] ] ifFalse: [ ^3 ]'.

self runCleaner.

self assert: (class >> #method) sourceCode equals: 'method
^self toto1 ifTrue: [ self toto2 ifTrue: [ 1 ] ifFalse: [ 2 ] ] ifFalse: [ 3 ]'
]

{ #category : #tests }
ChanelExtractReturnFromAllBranchesCleanerTest >> testExtractReturnInTrait [
| trait |
Expand Down
14 changes: 10 additions & 4 deletions src/Chanel/ChanelExtractReturnFromAllBranchesCleaner.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ ChanelExtractReturnFromAllBranchesCleaner class >> priority [

{ #category : #cleaning }
ChanelExtractReturnFromAllBranchesCleaner >> clean [
(self configuration localMethods iterator
| #ast collectIt
(self cleanASTs: (self configuration localMethods collect: #ast)) do: #install
]

{ #category : #cleaning }
ChanelExtractReturnFromAllBranchesCleaner >> cleanASTs: aCollectionOfMethods [
"In the end we run again the cleaning on ASTs because we can have case of nested conditionals each of the having returns in their branches."

^ (aCollectionOfMethods iterator
| #allChildren flatCollectIt
| #isMessage selectIt
| #isCascaded rejectIt
| [ :node | node parent isLast: node ] selectIt
| #isConditionNecessarilyExecutingABranch selectIt
| [ :node | node arguments allSatisfy: #isBlock ] selectIt
| [ :node | node arguments allSatisfy: #hasBlockReturn ] selectIt
| [ :node | node arguments allSatisfy: #lastStatementIsReturn ] selectIt
| [ :node | node arguments do: #inlineLastReturn ] doIt
| #wrapsInReturn doIt
| #methodNode collectIt
> Set) do: #install
> Set) ifNotEmpty: [ :updatedASTs | self cleanASTs: updatedASTs. updatedASTs ]
]
5 changes: 5 additions & 0 deletions src/Chanel/RBBlockNode.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ Extension { #name : #RBBlockNode }
RBBlockNode >> inlineLastReturn [
self statements last inline
]

{ #category : #'*Chanel' }
RBBlockNode >> lastStatementIsReturn [
^ self statements last isReturn
]

0 comments on commit 71f3eab

Please sign in to comment.