Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReRefersToClassRule fix option #16971

Open
wants to merge 1 commit into
base: Pharo13
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/General-Rules-Tests/ReRefersToClassRuleTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ ReRefersToClassRuleTest >> testIsAbstractNotDetected [
| critiques|
critiques := self myCritiquesOnMethod: self class class >> #isAbstract.

self assert: critiques size equals: 0
self assertEmpty: critiques
]

{ #category : 'tests' }
ReRefersToClassRuleTest >> testRuleDetectHardCodedClass [
| critiques|
critiques := self myCritiquesOnMethod: self class >> #referToClassName.

self assert: critiques size equals: 2.
self assert: (self sourceAtChritique: critiques first) equals: ReRefersToClassRuleTest name asString
self assert: critiques size equals: 2
]
34 changes: 15 additions & 19 deletions src/General-Rules/ReRefersToClassRule.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ However we cannot systematically replace Class reference by self class or self b
"
Class {
#name : 'ReRefersToClassRule',
#superclass : 'ReAbstractRule',
#superclass : 'ReNodeRewriteRule',
#category : 'General-Rules-Design Flaws',
#package : 'General-Rules',
#tag : 'Design Flaws'
}

{ #category : 'testing' }
ReRefersToClassRule class >> checksMethod [
^ true
]

{ #category : 'accessing' }
ReRefersToClassRule class >> group [
^ 'Design Flaws'
Expand All @@ -30,26 +25,27 @@ ReRefersToClassRule class >> ruleName [
^ 'Refers to class name instead of "self class"'
]

{ #category : 'accessing' }
{ #category : 'manifest' }
ReRefersToClassRule class >> uniqueIdentifierName [
"This number should be unique and should change only when the rule completely change semantics"

^'RefersToClassRule'
]

{ #category : 'running' }
ReRefersToClassRule >> check: aMethod forCritiquesDo: aCriticBlock [
| class problemLiterals |
{ #category : 'hooks' }
ReRefersToClassRule >> afterCheck: aNode mappings: mappingDict [

"The class method #isAbstract needs to explicitly hardcode class references.
So we do not criticize this."
(aMethod isClassSide and: [ aMethod selector = #isAbstract ]) ifTrue: [ ^ self ].
| class |

class := aNode methodNode .
(class methodClass isClassSide and: [ class selector = #isAbstract ]) ifTrue: [ ^ false ].
aNode isVariable ifFalse: [ ^ false ].
^ aNode isGlobalVariable and: [ aNode name = class methodClass instanceSide name ]
]

class := aMethod methodClass instanceSide.
problemLiterals := (aMethod ast allChildren
select: [ :node | node isGlobalVariable ])
select: [ :var | var name = class name].
{ #category : 'initialization' }
ReRefersToClassRule >> initialize [

problemLiterals do: [ :literal |
aCriticBlock cull: (self createTrivialCritiqueOn: aMethod intervalOf: literal hint:literal name )]
super initialize.
self replace: '`@Object' with: 'self'
]