From c8db7d4fe023aa5f02da870f1654082834413e7b Mon Sep 17 00:00:00 2001 From: Rick Busarow Date: Sun, 5 Feb 2023 09:12:07 -0600 Subject: [PATCH] ignore suppress/noinspection names which don't fit the FindingName pattern fixes #827 --- .../kotlin/modulecheck/finding/FindingName.kt | 14 ++++++++++++- .../dsl/internal/AbstractDependenciesBlock.kt | 9 +++++---- .../dsl/internal/AbstractPluginsBlock.kt | 7 +++---- .../antlr/GroovyDependencyBlockParserTest.kt | 16 ++++++++++++++- .../antlr/GroovyPluginsBlockParserTest.kt | 15 +++++++++++++- .../psi/KotlinDependenciesBlockParserTest.kt | 20 ++++++++++++++++++- .../psi/KotlinPluginsBlockParserTest.kt | 16 ++++++++++++++- 7 files changed, 84 insertions(+), 13 deletions(-) diff --git a/modulecheck-finding/name/src/main/kotlin/modulecheck/finding/FindingName.kt b/modulecheck-finding/name/src/main/kotlin/modulecheck/finding/FindingName.kt index e15ceb996b..6060ee9491 100644 --- a/modulecheck-finding/name/src/main/kotlin/modulecheck/finding/FindingName.kt +++ b/modulecheck-finding/name/src/main/kotlin/modulecheck/finding/FindingName.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Rick Busarow + * Copyright (C) 2021-2023 Rick Busarow * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -52,6 +52,18 @@ data class FindingName( companion object { + /** + * @return a [FindingName] if [maybeFindingName] is `kebab-case`, otherwise `null`. + * @since 0.12.4 + */ + fun safe(maybeFindingName: String): FindingName? { + return if (CaseMatcher.KebabCaseMatcher().matches(maybeFindingName)) { + FindingName(maybeFindingName) + } else { + null + } + } + @Deprecated("This will be removed soon.") fun migrateLegacyIdOrNull(legacyID: String, logger: McLogger): String? { diff --git a/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractDependenciesBlock.kt b/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractDependenciesBlock.kt index 57c6cecc32..d84053330f 100644 --- a/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractDependenciesBlock.kt +++ b/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractDependenciesBlock.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Rick Busarow + * Copyright (C) 2021-2023 Rick Busarow * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -33,7 +33,6 @@ import modulecheck.parsing.gradle.model.ProjectPath.StringProjectPath import modulecheck.reporting.logging.McLogger import modulecheck.utils.lazy.ResetManager import modulecheck.utils.lazy.lazyResets -import modulecheck.utils.mapToSet import modulecheck.utils.remove abstract class AbstractDependenciesBlock( @@ -53,7 +52,9 @@ abstract class AbstractDependenciesBlock( allModuleDeclarations.forEach { (configuredModule, declarations) -> val cached = getOrPut(configuredModule) { - blockSuppressed.mapTo(mutableSetOf()) { FindingName(it) } + blockSuppressed + .mapNotNull { FindingName.safe(it) } + .mapTo(mutableSetOf()) { it } } declarations.forEach { moduleDependencyDeclaration -> @@ -170,7 +171,7 @@ abstract class AbstractDependenciesBlock( } private fun Collection.asFindingNames(): Set { - return mapToSet { FindingName(it) } + return mapNotNull { FindingName.safe(it) }.toSet() } override fun getOrEmpty( diff --git a/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractPluginsBlock.kt b/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractPluginsBlock.kt index a2d085d236..cfb21a35fd 100644 --- a/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractPluginsBlock.kt +++ b/modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractPluginsBlock.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Rick Busarow + * Copyright (C) 2021-2023 Rick Busarow * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -21,7 +21,6 @@ import modulecheck.parsing.gradle.dsl.PluginsBlock import modulecheck.reporting.logging.McLogger import modulecheck.utils.lazy.ResetManager import modulecheck.utils.lazy.lazyResets -import modulecheck.utils.mapToSet import java.io.File abstract class AbstractPluginsBlock( @@ -48,7 +47,7 @@ abstract class AbstractPluginsBlock( _allDeclarations.forEach { pluginDeclaration -> val cached = getOrPut(pluginDeclaration) { - blockSuppressed.mapTo(mutableSetOf()) { FindingName(it) } + blockSuppressed.mapNotNullTo(mutableSetOf()) { FindingName.safe(it) } } cached += pluginDeclaration.suppressed.updateOldSuppresses() @@ -101,7 +100,7 @@ abstract class AbstractPluginsBlock( } private fun Collection.asFindingNames(): Set { - return mapToSet { FindingName(it) } + return mapNotNullTo(mutableSetOf()) { FindingName.safe(it) } } } diff --git a/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyDependencyBlockParserTest.kt b/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyDependencyBlockParserTest.kt index 981f731128..0fb8899ae0 100644 --- a/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyDependencyBlockParserTest.kt +++ b/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyDependencyBlockParserTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Rick Busarow + * Copyright (C) 2021-2023 Rick Busarow * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -80,6 +80,20 @@ internal class GroovyDependenciesBlockParserTest : BaseTest() { ) } + @Test + fun `suppression which doesn't match finding name regex should be ignored`() = parse( + """ + //noinspection DSL_SCOPE_VIOLATION + dependencies { + api project(':core:android') + api project(':core:jvm') + } + """ + ) { + + allSuppressions.values.flatten() shouldBe emptyList() + } + @Test fun `declaration with noinspection with old IDs should include suppressed with argument`() = parse( diff --git a/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyPluginsBlockParserTest.kt b/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyPluginsBlockParserTest.kt index 32c62af4b6..d1584010e4 100644 --- a/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyPluginsBlockParserTest.kt +++ b/modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyPluginsBlockParserTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Rick Busarow + * Copyright (C) 2021-2023 Rick Busarow * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -119,6 +119,19 @@ internal class GroovyPluginsBlockParserTest : BaseTest() { ) } + @Test + fun `suppression which doesn't match finding name regex should be ignored`() = parse( + """ + //noinspection DSL_SCOPE_VIOLATION + plugins { + id 'com.squareup.anvil' + } + """ + ) { + + allSuppressions.values.flatten() shouldBe emptyList() + } + @Test fun `suppress with old ID should be updated`() = parse( """ diff --git a/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinDependenciesBlockParserTest.kt b/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinDependenciesBlockParserTest.kt index 63897782af..021977a4c5 100644 --- a/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinDependenciesBlockParserTest.kt +++ b/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinDependenciesBlockParserTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Rick Busarow + * Copyright (C) 2021-2023 Rick Busarow * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -111,6 +111,24 @@ internal class KotlinDependenciesBlockParserTest : ProjectTest() { ) } + @Test + fun `suppression which doesn't match finding name regex should be ignored`() { + val block = parser + .parse( + """ + @Suppress("DSL_SCOPE_VIOLATION") + dependencies { + api(project(":core:android")) + api(project(":core:jvm")) + @Suppress("DSL_SCOPE_VIOLATION") + testImplementation(project(":core:test")) + } + """ + ).single() + + block.allSuppressions.values.flatten() shouldBe emptyList() + } + @Test fun `declaration with annotation should include annotation with argument`() { val block = parser diff --git a/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinPluginsBlockParserTest.kt b/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinPluginsBlockParserTest.kt index 917c89769f..4738ccba15 100644 --- a/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinPluginsBlockParserTest.kt +++ b/modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinPluginsBlockParserTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Rick Busarow + * Copyright (C) 2021-2023 Rick Busarow * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -104,6 +104,20 @@ internal class KotlinPluginsBlockParserTest : BaseTest() { ) } + @Test + fun `suppression which doesn't match finding name regex should be ignored`() { + val block = parse( + """ + @Suppress("DSL_SCOPE_VIOLATION") + plugins { + id("com.squareup.anvil") + } + """ + ) + + block.allSuppressions.values.flatten() shouldBe emptyList() + } + @Test fun `suppressed id function`() { val block = parse(