-
Notifications
You must be signed in to change notification settings - Fork 504
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
Add rule to check that a single top level class name matches the file name #194
Changes from 5 commits
dfbe86c
9dc16de
83282c1
790710e
1025de1
1e3823c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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 | ||
) { | ||
// Ignore all non ".kt" files (including ".kts") | ||
if (node.getUserData(KtLint.FILE_PATH_USER_DATA_KEY)?.endsWith(".kt") != true) { | ||
return | ||
} | ||
|
||
val topLevelClassNames = mutableListOf<String>() | ||
|
||
node.visit { | ||
if (it.elementType == KtStubElementTypes.CLASS && it.treeParent.elementType == KtStubElementTypes.FILE) { | ||
val classIdentifier = it.findChildByType(KtTokens.IDENTIFIER) | ||
classIdentifier?.let { | ||
val className = it.text | ||
topLevelClassNames.add(className) | ||
} | ||
} | ||
} | ||
|
||
val name = Paths.get(node.getUserData(KtLint.FILE_PATH_USER_DATA_KEY)).fileName.toString().substringBefore(".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (topLevelClassNames.size == 1 && name != topLevelClassNames.first()) { | ||
emit(0, | ||
"Single top level class name [${topLevelClassNames.first()}] does not match file name", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
false) | ||
} | ||
} | ||
} |
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", "Single top level class name [B] does not match file name") | ||
)) | ||
} | ||
|
||
@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", "Single top level class name [B] does not match file name") | ||
)) | ||
} | ||
|
||
@Test | ||
fun testCaseSensitiveMatching() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
interface Woohoo | ||
""".trimIndent(), | ||
fileName("woohoo.kt") | ||
)).isEqualTo(listOf( | ||
LintError(1, 1, "class-name-matches-file-name", "Single top level class name [Woohoo] does not match file name") | ||
)) | ||
} | ||
|
||
@Test | ||
fun testIgnoreKotlinScriptFiles() { | ||
assertThat(ClassNameMatchesFileNameRule().lint( | ||
""" | ||
class B | ||
""".trimIndent(), | ||
fileName("A.kts") | ||
)).isEmpty() | ||
} | ||
|
||
private fun fileName(fileName: String) = mapOf("file_path" to fileName) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to avoid whole AST traversal with something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this a lot more (removes the need to check for immediate parent and should also be more efficient :))