Skip to content

Commit

Permalink
Also report possibly non-normalized duplicate dependency groups
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Oct 17, 2024
1 parent 6058882 commit 190782b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import insyncwithfoo.ryecharm.table
import org.toml.lang.psi.TomlArray
import org.toml.lang.psi.TomlInlineTable
import org.toml.lang.psi.TomlLiteral
import org.toml.lang.psi.TomlTable
import org.toml.lang.psi.TomlVisitor
import org.toml.lang.psi.ext.name

Expand Down Expand Up @@ -46,7 +47,7 @@ private class DependencyGroupNameVisitor(private val holder: ProblemsHolder) : T

private fun reportInvalidGroupName(element: PsiElement, groupName: String) {
val message = message("inspections.dependencyGroupNames.message.invalid", groupName)
val problemHighlightType = ProblemHighlightType.POSSIBLE_PROBLEM
val problemHighlightType = ProblemHighlightType.WARNING

holder.registerProblem(element, message, problemHighlightType)
}
Expand All @@ -58,6 +59,27 @@ private class DependencyGroupNameVisitor(private val holder: ProblemsHolder) : T
holder.registerProblem(element, message, problemHighlightType)
}

override fun visitTable(element: TomlTable) {
val dependencyGroupsTable = element.takeIf { it.isDependencyGroups } ?: return
val entriesByNormalizedName = dependencyGroupsTable.entries.groupBy { it.key.groupName }

entriesByNormalizedName.forEach { (normalizedName, entries) ->
if (normalizedName != null && entries.size > 1) {
entries.forEach { reportDuplicateGroup(it.key, it.key.name!!, normalizedName) }
}
}
}

private fun reportDuplicateGroup(element: PsiElement, originalName: String, normalizedName: String) {
val message = when (originalName == normalizedName) {
true -> message("inspections.dependencyGroupNames.message.duplicate.normalized", normalizedName)
else -> message("inspections.dependencyGroupNames.message.duplicate", originalName, normalizedName)
}
val problemHighlightType = ProblemHighlightType.GENERIC_ERROR

holder.registerProblem(element, message, problemHighlightType)
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private class DependencyGroupReference(element: GroupNameString) : PsiReferenceB
val groupKeys = dependencyGroupsTable.groupKeys
val includedGroupName = element.stringContent!!.normalize()

val key = groupKeys.find { it.name?.normalize() == includedGroupName }
val key = groupKeys.find { it.groupName == includedGroupName }

return key?.segments?.singleOrNull()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package insyncwithfoo.ryecharm.others.dependencygroups

import org.toml.lang.psi.TomlArray
import org.toml.lang.psi.TomlInlineTable
import org.toml.lang.psi.TomlKey
import org.toml.lang.psi.TomlLiteral
Expand All @@ -8,10 +9,15 @@ import org.toml.lang.psi.ext.name


internal typealias DependencyGroupsTable = TomlTable
internal typealias GroupArray = TomlArray
internal typealias IncludeGroupTable = TomlInlineTable
internal typealias GroupNameString = TomlLiteral


internal val TomlKey.groupName: String?
get() = name?.normalize()


internal val TomlTable.isDependencyGroups: Boolean
get() = header.key?.name == "dependency-groups"

Expand All @@ -21,4 +27,4 @@ internal val DependencyGroupsTable.groupKeys: List<TomlKey>


internal val DependencyGroupsTable.groupNames: List<String>
get() = groupKeys.mapNotNull { it.name?.normalize() }
get() = groupKeys.mapNotNull { it.groupName }
2 changes: 2 additions & 0 deletions src/main/resources/messages/ryecharm.properties
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ inspections.ruff.displayName = Linting with Ruff
inspections.dependencyGroupNames.displayName = Validate PEP 735 dependency group names
inspections.dependencyGroupNames.message.invalid = Invalid dependency group name: {0}
inspections.dependencyGroupNames.message.unknown = Unknown dependency group: {0}
inspections.dependencyGroupNames.message.duplicate = Duplicate dependency group: {0} (normalized: {1})
inspections.dependencyGroupNames.message.duplicate.normalized = Duplicate dependency group: {0}

inspections.uvLockEdit.message = uv.lock should not be edited manually.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal class DependencyGroupNameInspectionTest : PlatformTestCase() {
@Test
fun `test invalid key`() = doTest("invalidKey")

@Test
fun `test duplicate groups`() = doTest("duplicateGroups")

private fun doTest(subdrectory: String) = fileBasedTest("$subdrectory/pyproject.toml") {
fixture.enableInspections(DependencyGroupNameInspection())
fixture.checkHighlighting()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependency-groups]
<warning descr="Duplicate dependency group: foo-bar">foo-bar</warning> = ["ruff"]
<warning descr="Duplicate dependency group: foo-bar">"foo.bar"</warning> = ["a-n-plus-b"]
<warning descr="Duplicate dependency group: foo-bar">foo_bar</warning> = [{ include-group = "foo-bar" }]
foo.bar = [{ include-group = "foo.bar" }]

0 comments on commit 190782b

Please sign in to comment.