Skip to content

Commit

Permalink
Issue#238 Check directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
yokotaso committed Aug 21, 2018
1 parent 6f9fa67 commit 3e47444
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import java.io.File

/**
* @see [Kotlin Style Guide](https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rules)
* @see [Android Style Guide](https://android.github.io/kotlin-guides/style.html#package-names)
* @see [Kotlin Style Guide](https://kotlinlang.org/docs/reference/coding-conventions.html#directory-structure)
* @author yokotaso <yokotaso.t@gmail.com>
*/
class PackageNameRule : Rule("package-name") {
class DirectoryStructureRule : Rule("directory-structure") {

override fun visit(
node: ASTNode,
Expand All @@ -29,10 +28,6 @@ class PackageNameRule : Rule("package-name") {
.endsWith(File.separatorChar + qualifiedName.replace('.', File.separatorChar))) {
emit(node.startOffset, "Package directive doesn't match file location", false)
}
if (qualifiedName.contains('_')) {
emit(node.startOffset, "Package name must not contain underscore", false)
// "package name must be in lowercase" is violated by too many to projects in the wild to forbid
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StandardRuleSetProvider : RuleSetProvider {
CommentSpacingRule(),
FilenameRule(),
FinalNewlineRule(),
PackageNameRule(),
DirectoryStructureRule(),
// disabled until auto-correct is working properly
// (e.g. try formatting "if (true)\n return { _ ->\n _\n}")
// MultiLineIfElseRule(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.nio.file.Paths
/**
* @author yokotaso <yokotaso.t@gmail.com>
*/
class PackageNameRuleTest {
class DirectoryStructureRuleTest {

@Test
fun testOK() {
Expand All @@ -27,16 +27,7 @@ class PackageNameRuleTest {
assertNOK(
"package hoge.fuga",
"/hoge/moge/A.kt",
listOf(LintError(1, 1, "package-name", "Package directive doesn't match file location"))
)
}

@Test
fun testNOKUnderScore() {
assertNOK(
"package hoge.moge.hoge_moge",
"/hoge/moge/hoge_moge/A.kt",
listOf(LintError(1, 1, "package-name", "Package name must not contain underscore"))
listOf(LintError(1, 1, "directory-structure", "Package directive doesn't match file location"))
)
}

Expand All @@ -52,10 +43,10 @@ class PackageNameRuleTest {
mapOf("file_path" to Paths.get(URI.create("file:///$fileName")).toString())

private fun assertOK(ktScript: String, fileName: String) {
assertThat(PackageNameRule().lint(ktScript, fileName(fileName))).isEmpty()
assertThat(DirectoryStructureRule().lint(ktScript, fileName(fileName))).isEmpty()
}

private fun assertNOK(ktScript: String, fileName: String, lintErrors: List<LintError>) {
assertThat(PackageNameRule().lint(ktScript, fileName(fileName))).isEqualTo(lintErrors)
assertThat(DirectoryStructureRule().lint(ktScript, fileName(fileName))).isEqualTo(lintErrors)
}
}

0 comments on commit 3e47444

Please sign in to comment.