-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from JelloRanger/jelloranger/add-single-class…
…-matches-filename-rule Add rule to check that a single top level class name matches the file name
- Loading branch information
Showing
8 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/ClassNameMatchesFileNameRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.shyiko.ktlint.ruleset.standard | ||
|
||
import com.github.shyiko.ktlint.core.KtLint | ||
import com.github.shyiko.ktlint.core.Rule | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.lexer.KtTokens | ||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes | ||
import java.nio.file.Paths | ||
|
||
/** | ||
* If there is only one top level class in a given file, then its name should match the file's name | ||
*/ | ||
class ClassNameMatchesFileNameRule : Rule("class-name-matches-file-name"), Rule.Modifier.RestrictToRoot { | ||
|
||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | ||
) { | ||
val filePath = node.getUserData(KtLint.FILE_PATH_USER_DATA_KEY) | ||
|
||
// Ignore all non ".kt" files (including ".kts") | ||
if (filePath?.endsWith(".kt") != true) { | ||
return | ||
} | ||
|
||
val topLevelClassNames = node.getChildren(null) | ||
.filter { it.elementType == KtStubElementTypes.CLASS } | ||
.mapNotNull { it.findChildByType(KtTokens.IDENTIFIER)?.text } | ||
|
||
val name = Paths.get(filePath).fileName.toString().substringBefore(".") | ||
if (topLevelClassNames.size == 1 && name != topLevelClassNames.first()) { | ||
val className = topLevelClassNames.first() | ||
emit(0, | ||
"Class $className should be declared in a file named $className.kt", | ||
false) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...test/kotlin/com/github/shyiko/ktlint/ruleset/standard/ClassNameMatchesFileNameRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.github.shyiko.ktlint.ruleset.standard | ||
|
||
import com.github.shyiko.ktlint.core.LintError | ||
import com.github.shyiko.ktlint.test.lint | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.testng.annotations.Test | ||
|
||
class ClassNameMatchesFileNameRuleTest { | ||
|
||
@Test | ||
fun testMatchingSingleClassName() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
class A | ||
""".trimIndent(), | ||
fileName("/some/path/A.kt") | ||
)).isEmpty() | ||
} | ||
|
||
@Test | ||
fun testNonMatchingSingleClassName() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
class B | ||
""".trimIndent(), | ||
fileName("A.kt") | ||
)).isEqualTo(listOf( | ||
LintError(1, 1, "class-name-matches-file-name", "Class B should be declared in a file named B.kt") | ||
)) | ||
} | ||
|
||
@Test | ||
fun testMultipleTopLevelClasses() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
class B | ||
class C | ||
""".trimIndent(), | ||
fileName("A.kt") | ||
)).isEmpty() | ||
} | ||
|
||
@Test | ||
fun testMultipleNonTopLevelClasses() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
class B { | ||
class C | ||
class D | ||
} | ||
""".trimIndent(), | ||
fileName("A.kt") | ||
)).isEqualTo(listOf( | ||
LintError(1, 1, "class-name-matches-file-name", "Class B should be declared in a file named B.kt") | ||
)) | ||
} | ||
|
||
@Test | ||
fun testCaseSensitiveMatching() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
interface Woohoo | ||
""".trimIndent(), | ||
fileName("woohoo.kt") | ||
)).isEqualTo(listOf( | ||
LintError(1, 1, "class-name-matches-file-name", "Class Woohoo should be declared in a file named Woohoo.kt") | ||
)) | ||
} | ||
|
||
@Test | ||
fun testIgnoreKotlinScriptFiles() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
class B | ||
""".trimIndent(), | ||
fileName("A.kts") | ||
)).isEmpty() | ||
} | ||
|
||
private fun fileName(fileName: String) = mapOf("file_path" to fileName) | ||
} |
File renamed without changes.