pharo-wiki icon indicating copy to clipboard operation
pharo-wiki copied to clipboard

Add script to replace usage of `isNil ifTrue:`

Open jecisc opened this issue 4 years ago • 2 comments

Gabriel Cotelli did this script to improve quality of Pharo:

| env rewriter result methods changes |
env := RBBrowserEnvironment default 
    forPackages: (RPackageOrganizer default packages select: [:p | p name beginsWith: 'Spec2' ]).

rewriter := RBParseTreeRewriter new.
rewriter   
    replace:'`@obj isNil ifFalse: `@block' with: '`@obj ifNotNil: `@block';
    replace: '`@obj isNil ifTrue: `@block' with: '`@obj ifNil: `@block';
    replace: '`@obj isNil ifTrue: `@tb ifFalse: `@block' with: '`@obj ifNil: `@tb ifNotNil: `@block'.

changes := Array with: (RBSmalllintChecker runRule: (RBTransformationRule new 
                rewriteRule: rewriter;
                yourself)
                onEnvironment:  env  ) builder.
                
 (ChangesBrowser changes: changes) open.

We should add it to the cool scripts page

jecisc avatar Jul 17 '19 13:07 jecisc

And we can advice people on trying to not using nil in the first place :). There's almost always a better option.

gcotelli avatar Jul 17 '19 13:07 gcotelli

There's now a PR in Pharo including a critics rule with auto-fix funcionality, but could be a good example on how to run the rewrite engine scripted.

gcotelli avatar Jul 17 '19 16:07 gcotelli