Skip to content
Merged
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
50 changes: 50 additions & 0 deletions src/Microdown-Rules/MicMethodStructureChecker.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"
I check that methods in code blocks have an empty line after the selector when there is no comment.

Example of incorrect method (no comment, no empty line):
goodWordFor: aString
| pair |
pair := pairs detect: [ :each | each key = aString ].
^ pair value

Example of correct method (no comment, empty line after selector):
goodWordFor: aString

| pair |
pair := pairs detect: [ :each | each key = aString ].
^ pair value

Example of correct method (with comment, no empty line needed):
goodWordFor: aString
""there is a comment""
| pair |
pair := pairs detect: [ :each | each key = aString ].
^ pair value
"
Class {
#name : 'MicMethodStructureChecker',
#superclass : 'MicChecker',
#category : 'Microdown-Rules-MethodStructure',
#package : 'Microdown-Rules',
#tag : 'MethodStructure'
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment because I do tno really understand what it is about?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


{ #category : 'visiting' }
MicMethodStructureChecker >> visitCode: aMicCode [
| lines firstLine secondLine |
lines := aMicCode body lines.
lines size < 2 ifTrue: [ ^ self ].
firstLine := lines first.
secondLine := lines second.
"If the second line begins with a comment, that's correct."
(secondLine trimLeft beginsWith: '"') ifTrue: [ ^ self ].
"If the second line is empty, it's correct."
secondLine trimLeft isEmpty ifTrue: [ ^ self ].
"ifelse, theres's a blank line missing after the selector"
results add: (MicMethodStructureResult new
micElement: aMicCode;
inFile: aMicCode;
detail: firstLine;
yourself
)
]
77 changes: 77 additions & 0 deletions src/Microdown-Rules/MicMethodStructureCheckerTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Class {
#name : 'MicMethodStructureCheckerTest',
#superclass : 'TestCase',
#instVars : [
'fileSystem',
'checker'
],
#category : 'Microdown-Rules-MethodStructure',
#package : 'Microdown-Rules',
#tag : 'MethodStructure'
}

{ #category : 'running' }
MicMethodStructureCheckerTest >> generateFilesystemExample [
| file |
file := fileSystem workingDirectory / 'methodWrong.md'.
file writeStreamDo: [ :stream |
stream nextPutAll: '```
goodWordFor: aString
| pair |
pair := pairs detect: [ :each | each key = aString ].
^ pair value

`````'
].
file := fileSystem workingDirectory / 'methodCorrect.md'.
file writeStreamDo: [ :stream |
stream nextPutAll: '```
goodWordFor: aString

| pair |
pair := pairs detect: [ :each | each key = aString ].
^ pair value

`````'
].
file := fileSystem workingDirectory / 'methodWithComment.md'.
file writeStreamDo: [ :stream |
stream nextPutAll: '```
goodWordFor: aString
"there is a comment"
| pair |
pair := pairs detect: [ :each | each key = aString ].
^ pair value

`````'
].
]

{ #category : 'running' }
MicMethodStructureCheckerTest >> setUp [
super setUp.

fileSystem := FileSystem memory.
self generateFilesystemExample.
checker := MicMethodStructureChecker new.
]

{ #category : 'tests' }
MicMethodStructureCheckerTest >> testMethodCorrect [
checker checkProject: fileSystem / 'methodCorrect.md'.
self assert: checker isOkay.
]

{ #category : 'tests' }
MicMethodStructureCheckerTest >> testMethodWithCommentCorrect [
checker checkProject: fileSystem / 'methodWithComment.md'.
self assert: checker isOkay.
]

{ #category : 'tests' }
MicMethodStructureCheckerTest >> testMethodWrongDetected [
checker checkProject: fileSystem / 'methodWrong.md'.
self deny: checker isOkay.
self assert: checker results size equals: 1.
self assert: checker results first detail equals: 'goodWordFor: aString'.
]
31 changes: 31 additions & 0 deletions src/Microdown-Rules/MicMethodStructureResult.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Class {
#name : 'MicMethodStructureResult',
#superclass : 'MicAbstractResult',
#instVars : [
'detail'
],
#category : 'Microdown-Rules-MethodStructure',
#package : 'Microdown-Rules',
#tag : 'MethodStructure'
}

{ #category : 'accessing' }
MicMethodStructureResult >> detail [

^ detail
]

{ #category : 'accessing' }
MicMethodStructureResult >> detail: aString [

detail := aString
]

{ #category : 'accessing' }
MicMethodStructureResult >> explanation [
^ 'Text: "' , micElement body
, '" in file' , fileReference fullName
, ' contains a mistake: method ['
, detail
, '] should have an empty line after the selector when is no comment.'
]
Loading