Skip to content

Commit 4cc4a48

Browse files
dariuszkucbrennantaylor
authored andcommitted
Enable Detekt and Ktlint (#36)
1 parent 14fb58e commit 4cc4a48

File tree

14 files changed

+139
-52
lines changed

14 files changed

+139
-52
lines changed

detekt.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# turn on ALL checks
2+
failFast: true
3+
4+
build:
5+
weights:
6+
comments: 0 # missing comments should not fail the build
7+
8+
test-pattern: # Configure exclusions for test sources
9+
active: true
10+
patterns: # Test file regexes
11+
- '.*/test/.*'
12+
- '.*\\test\\.*'
13+
- '.*Test.kt'
14+
- '.*Spec.kt'
15+
- '.*IT.kt'
16+
- '.*TestConfiguration.kt'
17+
18+
comments:
19+
EndOfSentenceFormat:
20+
active: false
21+
CommentOverPrivateFunction:
22+
active: false
23+
24+
complexity:
25+
LongParameterList:
26+
threshold: 10
27+
active: true
28+
29+
style:
30+
MagicNumber:
31+
ignoreEnums: true
32+
MaxLineLength:
33+
maxLineLength: 200
34+
ReturnCount:
35+
active: false

detekt_baseline.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<?xml version="1.0" ?>
22
<SmellBaseline>
3-
<Whitelist timestamp="1535123065325">
4-
<ID>ComplexMethod:SchemaGenerator.kt$SchemaGenerator$private fun function(fn: KFunction&lt;*&gt;, target: Any? = null): GraphQLFieldDefinition</ID>
3+
<Blacklist timestamp="1538767296980"></Blacklist>
4+
<Whitelist timestamp="1538768092037">
5+
<ID>ComplexMethod:typeMappers.kt$internal fun graphQLScalar(type: KType): GraphQLType?</ID>
6+
<ID>FunctionMaxLength:SchemaGenerator.kt$SchemaGenerator$@Throws(InvalidInputFieldTypeException::class) private fun throwIfInterfaceIsNotAuthorized(parameter: KParameter)</ID>
7+
<ID>FunctionOnlyReturningConstant:DirectiveTests.kt$Geography$@DirectiveOnFunction fun somethingCool(): String</ID>
58
<ID>LargeClass:SchemaGenerator.kt$SchemaGenerator</ID>
9+
<ID>MethodOverloading:SchemaGeneratorTest.kt$SchemaGeneratorTest</ID>
10+
<ID>SpreadOperator:annotationExtensions.kt$(*directiveInfo.locations)</ID>
611
<ID>TooManyFunctions:SchemaGenerator.kt$SchemaGenerator</ID>
712
<ID>UnsafeCallOnNullableType:SchemaGenerator.kt$SchemaGenerator$name!!</ID>
813
<ID>UnsafeCallOnNullableType:SchemaGenerator.kt$SchemaGenerator$type.arguments.first().type!!</ID>
14+
<ID>UnsafeCast:SchemaGeneratorAsyncTests.kt$SchemaGeneratorAsyncTests$schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDo").type as GraphQLNonNull</ID>
15+
<ID>UnsafeCast:SchemaGeneratorAsyncTests.kt$SchemaGeneratorAsyncTests$schema.getObjectType("TopLevelQuery").getFieldDefinition("asynchronouslyDoSingle").type as GraphQLNonNull</ID>
16+
<ID>UnsafeCast:SchemaGeneratorAsyncTests.kt$SchemaGeneratorAsyncTests$schema.getObjectType("TopLevelQuery").getFieldDefinition("maybe").type as GraphQLNonNull</ID>
917
</Whitelist>
10-
</SmellBaseline>
18+
</SmellBaseline>

pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
<properties>
7373
<reflections.version>0.9.11</reflections.version>
7474
<kotlin.version>1.2.70</kotlin.version>
75+
<kotlin-ktlint.version>0.29.0</kotlin-ktlint.version>
76+
<kotlin-detekt.version>1.0.0.RC8</kotlin-detekt.version>
7577
<junit.version>4.12</junit.version>
7678
</properties>
7779

@@ -121,6 +123,61 @@
121123
</execution>
122124
</executions>
123125
</plugin>
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-antrun-plugin</artifactId>
129+
<version>1.8</version>
130+
<executions>
131+
<!-- those can be run separately with mvn clean antrun:run@<target_id> -->
132+
<execution>
133+
<id>detekt</id>
134+
<phase>validate</phase>
135+
<configuration>
136+
<target name="detekt">
137+
<java taskname="detekt" dir="${project.basedir}" fork="true" failonerror="true" classname="io.gitlab.arturbosch.detekt.cli.Main" classpathref="maven.plugin.classpath">
138+
<arg value="-i" />
139+
<arg value="${project.basedir}${file.separator}src" />
140+
<arg value="-b" />
141+
<arg value="${project.basedir}${file.separator}detekt_baseline.xml" />
142+
<arg value="-c" />
143+
<arg value="${project.basedir}${file.separator}detekt.yml" />
144+
<arg value="-o" />
145+
<arg value="${project.basedir}${file.separator}target${file.separator}site${file.separator}detekt" />
146+
</java>
147+
</target>
148+
</configuration>
149+
<goals>
150+
<goal>run</goal>
151+
</goals>
152+
</execution>
153+
<execution>
154+
<id>ktlint</id>
155+
<phase>validate</phase>
156+
<configuration>
157+
<target name="ktlint">
158+
<java taskname="ktlint" dir="${project.basedir}" fork="true" failonerror="true" classname="com.github.shyiko.ktlint.Main" classpathref="maven.plugin.classpath">
159+
<arg value="src/**/*.kt" />
160+
<arg value="--reporter=plain" />
161+
<arg value="--reporter=checkstyle,output=${project.build.directory}/ktlint.xml" />
162+
</java>
163+
</target>
164+
</configuration>
165+
<goals><goal>run</goal></goals>
166+
</execution>
167+
</executions>
168+
<dependencies>
169+
<dependency>
170+
<groupId>io.gitlab.arturbosch.detekt</groupId>
171+
<artifactId>detekt-cli</artifactId>
172+
<version>${kotlin-detekt.version}</version>
173+
</dependency>
174+
<dependency>
175+
<groupId>com.github.shyiko</groupId>
176+
<artifactId>ktlint</artifactId>
177+
<version>${kotlin-ktlint.version}</version>
178+
</dependency>
179+
</dependencies>
180+
</plugin>
124181
</plugins>
125182
</build>
126183

src/main/kotlin/com/expedia/graphql/TopLevelObjectDef.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlin.reflect.KClass
66
* Encapsulates an object to use as a target for schema generation and the
77
* class to be used for reflection.
88
*
9-
* @param obj The target object (or proxy to target object)
9+
* @param obj The target object (or proxy to target object)
1010
* @param klazz Optional class of the target (or the proxied object)
1111
*/
1212
data class TopLevelObjectDef(val obj: Any, val klazz: KClass<*> = obj::class)

src/main/kotlin/com/expedia/graphql/annotations/GraphQLDirective.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import graphql.introspection.Introspection.DirectiveLocation.QUERY
1212
*/
1313
@Target(AnnotationTarget.ANNOTATION_CLASS)
1414
annotation class GraphQLDirective(
15-
val name: String = "",
16-
val description: String = "",
17-
val locations: Array<DirectiveLocation> = [QUERY, MUTATION, FIELD, FIELD_DEFINITION, OBJECT]
15+
val name: String = "",
16+
val description: String = "",
17+
val locations: Array<DirectiveLocation> = [QUERY, MUTATION, FIELD, FIELD_DEFINITION, OBJECT]
1818
)

src/main/kotlin/com/expedia/graphql/schema/KotlinDataFetcher.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ data class Parameter(val klazz: Class<*>, val annotations: List<Annotation>)
2121
* @param args The GraphQL arguments passed to the data fetcher
2222
*/
2323
class KotlinDataFetcher(
24-
private val target: Any?,
25-
private val fn: KFunction<*>,
26-
private val args: Map<String, Parameter>
24+
private val target: Any?,
25+
private val fn: KFunction<*>,
26+
private val args: Map<String, Parameter>
2727
) : DataFetcher<Any> {
2828

2929
override fun get(environment: DataFetchingEnvironment): Any? {

src/main/kotlin/com/expedia/graphql/schema/exceptions/InvalidInputFieldTypeException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ package com.expedia.graphql.schema.exceptions
2828
*
2929
* data class PartOfUnion( val property: Int) : UnionMarkup
3030
*/
31-
class InvalidInputFieldTypeException: RuntimeException("Object field argument cannot be an interface or a union")
31+
class InvalidInputFieldTypeException : RuntimeException("Object field argument cannot be an interface or a union")

src/main/kotlin/com/expedia/graphql/schema/exceptions/InvalidSchemaException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package com.expedia.graphql.schema.exceptions
33
/**
44
* Exception thrown on schema creation if no queries and no mutations are specified.
55
*/
6-
class InvalidSchemaException: RuntimeException("Schema requires at least one query or mutation")
6+
class InvalidSchemaException : RuntimeException("Schema requires at least one query or mutation")

src/main/kotlin/com/expedia/graphql/schema/extensions/annotationExtensions.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ internal fun KAnnotatedElement.graphQLDescription(): String? {
2424
val description = this.findAnnotation<GraphQLDescription>()?.value
2525

2626
return when {
27-
description != null && directiveNames.isNotEmpty() -> {
27+
description != null && directiveNames.isNotEmpty() ->
2828
"""$description
2929
|
3030
|Directives: ${directiveNames.joinToString(", ")}
3131
""".trimMargin()
32-
}
33-
description == null && directiveNames.isNotEmpty() -> {
32+
description == null && directiveNames.isNotEmpty() ->
3433
"Directives: ${directiveNames.joinToString(", ")}"
35-
}
3634
else -> description
3735
}
3836
}
@@ -85,7 +83,7 @@ internal fun KAnnotatedElement.directives() =
8583
.validLocations(*directiveInfo.locations)
8684
.description(directiveInfo.description)
8785

88-
annotation::class.properties().forEach{ prop ->
86+
annotation::class.properties().forEach { prop ->
8987
val propertyName = prop.name
9088
val value = prop.call(annotation)
9189
@Suppress("Detekt.UnsafeCast")

src/main/kotlin/com/expedia/graphql/schema/generator/SchemaGenerator.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal class SchemaGenerator(
7777
.forEach { builder.additionalType(it) }
7878
}
7979

80-
private fun addDirectives(builder: GraphQLSchema.Builder)= builder.additionalDirectives(directives)
80+
private fun addDirectives(builder: GraphQLSchema.Builder) = builder.additionalDirectives(directives)
8181

8282
private fun addQueries(builder: GraphQLSchema.Builder) {
8383
val queryBuilder = GraphQLObjectType.Builder()
@@ -237,7 +237,7 @@ internal class SchemaGenerator(
237237

238238
@Throws(InvalidInputFieldTypeException::class)
239239
private fun throwIfInterfaceIsNotAuthorized(parameter: KParameter) {
240-
if(parameter.type.jvmErasure.java.isInterface) throw InvalidInputFieldTypeException()
240+
if (parameter.type.jvmErasure.java.isInterface) throw InvalidInputFieldTypeException()
241241
}
242242

243243
private fun enumType(kClass: KClass<*>): GraphQLEnumType {
@@ -269,9 +269,9 @@ internal class SchemaGenerator(
269269
} else {
270270
klass.superclasses
271271
.asSequence()
272-
.filter { it.canBeGraphQLInterface() && !it.canBeGraphQLUnion()}
273-
.map{ objectFromReflection(it.createType(), false) as GraphQLInterfaceType}
274-
.forEach { builder.withInterface(it)}
272+
.filter { it.canBeGraphQLInterface() && !it.canBeGraphQLUnion() }
273+
.map { objectFromReflection(it.createType(), false) as GraphQLInterfaceType }
274+
.forEach { builder.withInterface(it) }
275275
}
276276

277277
klass.declaredMemberProperties
@@ -353,7 +353,6 @@ internal class SchemaGenerator(
353353
builder.possibleType(objectType(it.kotlin) as GraphQLObjectType)
354354
}
355355

356-
357356
return builder.build()
358357
}
359358
}

0 commit comments

Comments
 (0)