pharo icon indicating copy to clipboard operation
pharo copied to clipboard

[ Refactoring ] RBMoveTemporaryVariableDefinitionTransformation and RBMoveVariableDefinitionToSmallestValidScopeRefactoring should be merged

Open Ducasse opened this issue 3 months ago • 3 comments

Two points

Add more tests.

In the first place I thought that the following was wrong. But since the temp value is not accessed in the second block. It is possible. Now we should get several tests to check this.

moveDefinition
	| temp |
	^(self collect:
			[:each |
			temp := each printString.
			temp , temp])
		select:
			[:each |
			temp := each size.
			temp odd]

transformed into

moveDefinition

	^ (self collect: [ :each |
			   | temp |
			   temp := each printString.
			   temp , temp ]) select: [ :each |
			  | temp |
			  temp := each size.
			  temp odd ]

We should remove the duplication

Now RBMoveVariableDefinitionToSmallestValidScopeRefactoring is working on interval while RBMoveTemporyVariableDefinition on names. So we should merge them.

Ducasse avatar Nov 08 '25 12:11 Ducasse

We should write a test showing that this is wrong.

Ducasse avatar Nov 08 '25 12:11 Ducasse

The transformation implementation looks nicer to my eyes.

buildTransformations

	"ifnil... tight blocks should not be returned as errors"
	blockNodes ifNil: [ self checkMethodForBlocks ].

	^ (OrderedCollection
		withAll: ((blockNodes
			sorted: [ :a :b | a start > b start ])
			collect: [ :blockNode |
				RBAddTemporaryVariableTransformation
					variable: variableName
					inInterval: blockNode body sourceInterval
					inMethod: selector
					inClass: class ]))
		add: (RBRemoveTemporaryVariableTransformation
					variable: variableName
					inMethod: selector
					inClass: class);
		yourself

Ducasse avatar Nov 08 '25 17:11 Ducasse

vs.

privateTransform
	definingNode removeTemporaryNamed: name.
	blockNodes do: [:each | each body addTemporaryNamed: name].
	class compileTree: parseTree

Ducasse avatar Nov 08 '25 17:11 Ducasse