diff --git a/src/Microdown-Rules/MicCodeIndentationChecker.class.st b/src/Microdown-Rules/MicCodeIndentationChecker.class.st new file mode 100644 index 00000000..8e8ce4f0 --- /dev/null +++ b/src/Microdown-Rules/MicCodeIndentationChecker.class.st @@ -0,0 +1,30 @@ +" +I check that code blocks use tabs for indentation instead of spaces. +A line that starts with a space character is considered incorrectly indented. +" +Class { + #name : 'MicCodeIndentationChecker', + #superclass : 'MicChecker', + #category : 'Microdown-Rules-CodeIndetation', + #package : 'Microdown-Rules', + #tag : 'CodeIndetation' +} + +{ #category : 'visiting' } +MicCodeIndentationChecker >> visitCode: aMicCode [ + | lines | + lines := aMicCode body lines. + lines do: [ :line | + line isEmpty ifFalse: [ + "verify if the line begin with space" + (line first = Character space) ifTrue: [ + results add: (MicCodeIndentationResult new + micElement: aMicCode; + inFile: aMicCode fromFile; + detail: line; + yourself + ) + ] + ] + ] +] diff --git a/src/Microdown-Rules/MicCodeIndentationCheckerTest.class.st b/src/Microdown-Rules/MicCodeIndentationCheckerTest.class.st new file mode 100644 index 00000000..668f38fc --- /dev/null +++ b/src/Microdown-Rules/MicCodeIndentationCheckerTest.class.st @@ -0,0 +1,53 @@ +Class { + #name : 'MicCodeIndentationCheckerTest', + #superclass : 'TestCase', + #instVars : [ + 'fileSystem', + 'checker' + ], + #category : 'Microdown-Rules-CodeIndetation', + #package : 'Microdown-Rules', + #tag : 'CodeIndetation' +} + +{ #category : 'running' } +MicCodeIndentationCheckerTest >> generateFilesystemExample [ + | file | + "incorrect cases - space instead of tabs" + file := fileSystem workingDirectory / 'indentationWrong.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: '``` +foo + self + `````' + ]. + "correct cases - tabs" + file := fileSystem workingDirectory / 'indentationCorrect.md'. + file writeStreamDo: [ :stream | + stream nextPutAll: '``` +foo + self + ````' ]. +] + +{ #category : 'running' } +MicCodeIndentationCheckerTest >> setUp [ + super setUp. + + fileSystem := FileSystem memory. + self generateFilesystemExample. + checker := MicCodeIndentationChecker new. +] + +{ #category : 'tests' } +MicCodeIndentationCheckerTest >> testIndentationCorrect [ + checker checkProject: fileSystem / 'indentationCorrect.md'. + self assert: checker isOkay. +] + +{ #category : 'tests' } +MicCodeIndentationCheckerTest >> testIndentationWrongDetected [ + checker checkProject: fileSystem / 'indentationWrong.md'. + self deny: checker isOkay. + self assert: checker results size equals: 1. +] diff --git a/src/Microdown-Rules/MicCodeIndentationResult.class.st b/src/Microdown-Rules/MicCodeIndentationResult.class.st new file mode 100644 index 00000000..28d3ee17 --- /dev/null +++ b/src/Microdown-Rules/MicCodeIndentationResult.class.st @@ -0,0 +1,31 @@ +Class { + #name : 'MicCodeIndentationResult', + #superclass : 'MicAbstractResult', + #instVars : [ + 'detail' + ], + #category : 'Microdown-Rules-CodeIndetation', + #package : 'Microdown-Rules', + #tag : 'CodeIndetation' +} + +{ #category : 'accessing' } +MicCodeIndentationResult >> detail [ + + ^ detail +] + +{ #category : 'accessing' } +MicCodeIndentationResult >> detail: anObject [ + + detail := anObject +] + +{ #category : 'accessing' } +MicCodeIndentationResult >> explanation [ + ^ 'Text: "' , micElement body + , '" in file' , fileReference fullName + , ' contains a mistake: line [' + , detail + , '] should use tabs instead of spaces for indentation.' +]