From 85b48891c61c604c952c8739b50bdae2998c44a7 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Fri, 8 Sep 2017 13:29:16 +0200 Subject: Added code analyzing with Detekt Refactoring --- build.gradle | 12 + detekt.yml | 292 +++++++++++++++++++++ .../kotlin/pl/treksoft/kvision/ApplicationBase.kt | 2 +- src/main/kotlin/pl/treksoft/kvision/HMR.kt | 2 +- src/main/kotlin/pl/treksoft/kvision/Main.kt | 3 +- src/main/kotlin/pl/treksoft/kvision/Showcase.kt | 14 +- src/main/kotlin/pl/treksoft/kvision/basic/Label.kt | 2 +- .../kotlin/pl/treksoft/kvision/core/Container.kt | 2 +- .../kotlin/pl/treksoft/kvision/core/KVManager.kt | 18 +- src/main/kotlin/pl/treksoft/kvision/core/Root.kt | 2 +- src/main/kotlin/pl/treksoft/kvision/core/Widget.kt | 25 +- .../pl/treksoft/kvision/dropdown/DropDown.kt | 31 ++- src/main/kotlin/pl/treksoft/kvision/html/Button.kt | 11 +- src/main/kotlin/pl/treksoft/kvision/html/Image.kt | 7 +- src/main/kotlin/pl/treksoft/kvision/html/List.kt | 9 +- src/main/kotlin/pl/treksoft/kvision/html/Tag.kt | 15 +- .../kotlin/pl/treksoft/kvision/snabbdom/Types.kt | 21 +- .../test/pl/treksoft/kvision/core/KVManagerSpec.kt | 12 +- .../test/pl/treksoft/kvision/html/ButtonSpec.kt | 6 +- .../test/pl/treksoft/kvision/html/ImageSpec.kt | 4 +- 20 files changed, 418 insertions(+), 72 deletions(-) create mode 100644 detekt.yml diff --git a/build.gradle b/build.gradle index c9cf1563..1450ba1b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,11 +4,13 @@ buildscript { repositories { jcenter() maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' } + maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.20" + classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0.RC4-2" } } @@ -17,6 +19,7 @@ version = '0.0.1-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'org.jetbrains.kotlin.frontend' +apply plugin: "io.gitlab.arturbosch.detekt" repositories { jcenter() @@ -61,6 +64,15 @@ kotlinFrontend { } +detekt { + version = "1.0.0.RC4-2" + profile("main") { + input = "$projectDir/src/main/kotlin" + config = "$projectDir/detekt.yml" + filters = ".*test.*,.*/resources/.*,.*/tmp/.*" + } +} + compileKotlin2Js { kotlinOptions.metaInfo = true kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js" diff --git a/detekt.yml b/detekt.yml new file mode 100644 index 00000000..a6fdea75 --- /dev/null +++ b/detekt.yml @@ -0,0 +1,292 @@ +autoCorrect: true +failFast: false + +build: + warningThreshold: 5 + failThreshold: 10 + weights: + complexity: 2 + formatting: 1 + LongParameterList: 1 + comments: 1 + +processors: + active: true + exclude: + # - 'FunctionCountProcessor' + # - 'PropertyCountProcessor' + # - 'ClassCountProcessor' + # - 'PackageCountProcessor' + # - 'KtFileCountProcessor' + +console-reports: + active: true + exclude: + # - 'ProjectStatisticsReport' + # - 'ComplexityReport' + # - 'NotificationReport' + # - 'FindingsReport' + # - 'BuildFailureReport' + +output-reports: + active: true + exclude: + # - 'PlainOutputReport' + # - 'XmlOutputReport' + +potential-bugs: + active: true + DuplicateCaseInWhenExpression: + active: true + EqualsAlwaysReturnsTrueOrFalse: + active: false + EqualsWithHashCodeExist: + active: true + WrongEqualsTypeParameter: + active: false + ExplicitGarbageCollectionCall: + active: true + UnreachableCode: + active: true + LateinitUsage: + active: false + UnsafeCallOnNullableType: + active: false + UnsafeCast: + active: false + UselessPostfixExpression: + active: false + +performance: + active: true + ForEachOnRange: + active: true + SpreadOperator: + active: true + UnnecessaryTemporaryInstantiation: + active: true + +exceptions: + active: true + TooGenericExceptionCatched: + active: true + exceptions: + - ArrayIndexOutOfBoundsException + - Error + - Exception + - IllegalMonitorStateException + - IndexOutOfBoundsException + - NullPointerException + - RuntimeException + TooGenericExceptionThrown: + active: true + exceptions: + - Throwable + - ThrowError + - ThrowException + - ThrowNullPointerException + - ThrowRuntimeException + - ThrowThrowable + +empty-blocks: + active: true + EmptyCatchBlock: + active: true + EmptyClassBlock: + active: true + EmptyDefaultConstructor: + active: true + EmptyDoWhileBlock: + active: true + EmptyElseBlock: + active: true + EmptyFinallyBlock: + active: true + EmptyForBlock: + active: true + EmptyFunctionBlock: + active: true + EmptyIfBlock: + active: true + EmptyInitBlock: + active: true + EmptySecondaryConstructor: + active: true + EmptyWhenBlock: + active: true + EmptyWhileBlock: + active: true + +complexity: + active: true + LongMethod: + threshold: 20 + LongParameterList: + threshold: 5 + LargeClass: + threshold: 150 + ComplexMethod: + threshold: 10 + TooManyFunctions: + threshold: 10 + ComplexCondition: + threshold: 3 + LabeledExpression: + active: false + StringLiteralDuplication: + active: false + threshold: 2 + ignoreAnnotation: true + excludeStringsWithLessThan5Characters: true + ignoreStringsRegex: '$^' + +code-smell: + active: true + FeatureEnvy: + threshold: 0.5 + weight: 0.45 + base: 0.5 + +formatting: + active: true + useTabs: true + Indentation: + active: false + indentSize: 4 + ConsecutiveBlankLines: + active: true + autoCorrect: true + MultipleSpaces: + active: true + autoCorrect: true + SpacingAfterComma: + active: true + autoCorrect: true + SpacingAfterKeyword: + active: true + autoCorrect: true + SpacingAroundColon: + active: true + autoCorrect: true + SpacingAroundCurlyBraces: + active: true + autoCorrect: true + SpacingAroundOperator: + active: true + autoCorrect: true + TrailingSpaces: + active: true + autoCorrect: true + UnusedImports: + active: true + autoCorrect: true + OptionalSemicolon: + active: true + autoCorrect: true + OptionalUnit: + active: true + autoCorrect: true + ExpressionBodySyntax: + active: false + autoCorrect: false + ExpressionBodySyntaxLineBreaks: + active: false + autoCorrect: false + OptionalReturnKeyword: + active: true + autoCorrect: false + +style: + active: true + ReturnCount: + active: true + max: 2 + NewLineAtEndOfFile: + active: true + OptionalAbstractKeyword: + active: true + OptionalWhenBraces: + active: false + EqualsNullCall: + active: false + ForbiddenComment: + active: true + values: 'TODO:,FIXME:,STOPSHIP:' + ForbiddenImport: + active: false + imports: '' + ModifierOrder: + active: true + MagicNumber: + active: true + ignoreNumbers: '-1,0,1,2' + ignoreHashCodeFunction: false + ignorePropertyDeclaration: false + ignoreAnnotation: false + WildcardImport: + active: true + SafeCast: + active: true + MaxLineLength: + active: true + maxLineLength: 120 + excludePackageStatements: false + excludeImportStatements: false + PackageNaming: + active: true + packagePattern: '^[a-z]+(\.[a-z][a-z0-9]*)*$' + ClassNaming: + active: true + classPattern: '[A-Z$][a-zA-Z$]*' + EnumNaming: + active: true + enumEntryPattern: '^[A-Z$][a-zA-Z_$]*$' + FunctionNaming : + active: true + functionPattern: '^[a-z$][a-zA-Z$0-9]*$' + FunctionMaxLength: + active: false + maximumFunctionNameLength: 30 + FunctionMinLength: + active: false + minimumFunctionNameLength: 3 + VariableNaming : + active: true + variablePattern: '^(_)?[a-z$][a-zA-Z$0-9]*$' + ConstantNaming : + active: true + constantPattern: '^([A-Z_]*|serialVersionUID)$' + VariableMaxLength: + active: false + maximumVariableNameLength: 30 + VariableMinLength: + active: false + minimumVariableNameLength: 3 + ProtectedMemberInFinalClass: + active: false + UnnecessaryParentheses: + active: false + +comments: + active: true + CommentOverPrivateMethod: + active: true + CommentOverPrivateProperty: + active: true + UndocumentedPublicClass: + active: false + searchInNestedClass: true + searchInInnerClass: true + searchInInnerObject: true + searchInInnerInterface: true + UndocumentedPublicFunction: + active: false + +# *experimental feature* +# Migration rules can be defined in the same config file or a new one +migration: + active: true + imports: + # your.package.Class: new.package.or.Class + # for example: + # io.gitlab.arturbosch.detekt.api.Rule: io.gitlab.arturbosch.detekt.rule.Rule diff --git a/src/main/kotlin/pl/treksoft/kvision/ApplicationBase.kt b/src/main/kotlin/pl/treksoft/kvision/ApplicationBase.kt index a7b3d465..e9e99fc7 100644 --- a/src/main/kotlin/pl/treksoft/kvision/ApplicationBase.kt +++ b/src/main/kotlin/pl/treksoft/kvision/ApplicationBase.kt @@ -3,4 +3,4 @@ package pl.treksoft.kvision abstract class ApplicationBase { abstract fun start(state: Map) abstract fun dispose(): Map -} \ No newline at end of file +} diff --git a/src/main/kotlin/pl/treksoft/kvision/HMR.kt b/src/main/kotlin/pl/treksoft/kvision/HMR.kt index 1251c187..e657567a 100644 --- a/src/main/kotlin/pl/treksoft/kvision/HMR.kt +++ b/src/main/kotlin/pl/treksoft/kvision/HMR.kt @@ -16,4 +16,4 @@ external interface Hot { fun dispose(callback: (data: dynamic) -> Unit) } -external fun require(name: String): dynamic \ No newline at end of file +external fun require(name: String): dynamic diff --git a/src/main/kotlin/pl/treksoft/kvision/Main.kt b/src/main/kotlin/pl/treksoft/kvision/Main.kt index 918cd51f..385d23e8 100644 --- a/src/main/kotlin/pl/treksoft/kvision/Main.kt +++ b/src/main/kotlin/pl/treksoft/kvision/Main.kt @@ -1,7 +1,6 @@ package pl.treksoft.kvision -import kotlin.browser.* -import kotlin.dom.* +import kotlin.browser.document fun main(args: Array) { var application: ApplicationBase? = null diff --git a/src/main/kotlin/pl/treksoft/kvision/Showcase.kt b/src/main/kotlin/pl/treksoft/kvision/Showcase.kt index 67d7bbd1..b2e6d764 100644 --- a/src/main/kotlin/pl/treksoft/kvision/Showcase.kt +++ b/src/main/kotlin/pl/treksoft/kvision/Showcase.kt @@ -33,7 +33,8 @@ class Showcase : ApplicationBase() { val dd = DropDown("Dropdown", listOf("abc" to "#!/x", "def" to "#!/y"), "flag") root.add(dd) - val dd2 = DropDown("Dropdown2", listOf("abc" to "#!/abc", "def" to "#!/def", "xyz" to DISABLED.POS, "Header" to HEADER.POS, "Separtatorek" to SEPARATOR.POS + val dd2 = DropDown("Dropdown2", listOf("abc" to "#!/abc", "def" to "#!/def", "xyz" to DISABLED.POS, + "Header" to HEADER.POS, "Separtatorek" to SEPARATOR.POS ), "flag", dropup = true) root.add(dd2) @@ -43,7 +44,8 @@ class Showcase : ApplicationBase() { val del = Tag(TAG.DEL, "To jest deleted") root.add(del) - val list = ListTag(LIST.DL_HORIZ, listOf("abc", "defdasdasdasddasdtdasdas", "Dasdsada", "dasdasdads"), true) + val list = ListTag(LIST.DL_HORIZ, listOf("abc", "defdasdasdasddasdtdasdas", "Dasdsada", + "dasdasdads"), true) root.add(list) val list2 = ListTag(LIST.OL, null) @@ -52,15 +54,15 @@ class Showcase : ApplicationBase() { list2.add(Image(Img("kotlin.png"))) root.add(list2) - val img = Image(Img("kotlin.png"), "Image", true, IMAGE_SHAPE.ROUNDED) + val img = Image(Img("kotlin.png"), "Image", true, IMAGESHAPE.ROUNDED) root.add(img) - val button = Button("To jest przycisk FA", "fa-flag", BUTTON_STYLE.DANGER) + val button = Button("To jest przycisk FA", "fa-flag", BUTTONSTYLE.DANGER) button.setEventListener", element?.innerHTML, "Should render correct html button") diff --git a/src/test/kotlin/test/pl/treksoft/kvision/html/ImageSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/html/ImageSpec.kt index 7d8e37fe..ebf3aedb 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/html/ImageSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/html/ImageSpec.kt @@ -2,7 +2,7 @@ package test.pl.treksoft.kvision.html import pl.treksoft.kvision.core.Img import pl.treksoft.kvision.core.Root -import pl.treksoft.kvision.html.IMAGE_SHAPE +import pl.treksoft.kvision.html.IMAGESHAPE import pl.treksoft.kvision.html.Image import test.pl.treksoft.kvision.DomSpec import kotlin.browser.document @@ -16,7 +16,7 @@ class ImageSpec : DomSpec { run { val root = Root("test") val res = Img("kotlin.png") - val image = Image(res, "Image", true, IMAGE_SHAPE.ROUNDED, true) + val image = Image(res, "Image", true, IMAGESHAPE.ROUNDED, true) root.add(image) val element = document.getElementById("test") assertEquals("\"Image\"", element?.innerHTML, "Should render correct html image") -- cgit