Share
Embed
Enforcing code conventions with Lint
Daniel Vila
LINE App Dev Team7 Software Engineer
https://linedevday.linecorp.com/2020/ja/sessions/1687
https://linedevday.linecorp.com/2020/en/sessions/1687
LINE DevDay 2020
November 26, 2020
Transcript
63 slides
-
Big teams Big teams bring challenges: › Onboarding of new
members. › Code consistency. -
Big teams Big teams bring challenges: › Onboarding of new
members. › Code consistency. › Code conventions. -
Big teams Big teams bring challenges: › Onboarding of new
members. › Code consistency. › Code conventions. › Internal APIs. -
Code conventions fun getInt()k= (0..10).map { it.also { "Some side
effect" } }.also { "Some other side effect" }.apply { size }.let { it.last() }.takeIf { it > 10 } -
Code conventions fun getInt(): Int?k= (0..10).map { it.also { "Some
side effect" } }.also { "Some other side effect" }.apply { size }.let { it.last() }.takeIf { it > 10 } -
Why Lint? › Compatible with Java/Kotlin (UAST). › Lint client
is implemented by Android Studio. -
Why Lint? › Compatible with Java/Kotlin (UAST). › Lint client
is implemented by Android Studio. › Quick fixes in Android Studio. -
Custom Lint checks In order to create a custom Lint
check we have to roughly follow these steps: -
Custom Lint checks In order to create a custom Lint
check we have to roughly follow these steps: › Create a Lint Issue. -
Custom Lint checks In order to create a custom Lint
check we have to roughly follow these steps: › Create a Lint Issue. › Register the Issue so lint can track it. -
Custom Lint checks In order to create a custom Lint
check we have to roughly follow these steps: › Create a Lint Issue. › Register the Issue so lint can track it. › Create a detector for the Issue. -
Custom Lint checks In order to create a custom Lint
check we have to roughly follow these steps: › Create a Lint Issue. › Register the Issue so lint can track it. › Create a detector for the Issue. › Report the Issue. -
Custom Lint checks In order to create a custom Lint
check we have to roughly follow these steps: › Create a Lint Issue. › Register the Issue so lint can track it. › Create a detector for the Issue. › Report the Issue. › [Optional] Provide a fix. -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType", “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType”, “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType”, “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType”, “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType”, “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType”, “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType”, “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Create an issue class MyDetector : Detector(), SourceCodeScanner { companion
object { @JvmField val ISSUE: Issue = Issue.create( “InferredType”, “MyDetector short description", "My detector **very** *long* `description`.", Category.CORRECTNESS, 6, Severity.WARNING, Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE) )k }k …k }k -
Register the issue Now we have to add the issue
to the lint issue registry: class MyIssueRegistry : IssueRegistry() {k override val issues: List<Issue> = listOf(MyDetector.ISSUE) override val api: Int = CURRENT_API override val minApi: Int = MIN_API companion object {k const val MIN_API = 2 // Corresponds to android gradle plugin 3.2+ }k }k -
Register the issue Now we have to add the issue
to the lint issue registry: @AutoService(IssueRegistry::class) class MyIssueRegistry : IssueRegistry() {k override val issues: List<Issue> = listOf(MyDetector.ISSUE) override val api: Int = CURRENT_API override val minApi: Int = MIN_API companion object {k const val MIN_API = 2 // Corresponds to android gradle plugin 3.2+ }k }k -
Create a detector A Detector is responsible for detecting occurrences
of an Issue in the source code. -
Custom Lint checks Lint has a number of Detector specializations
which makes a lot easier to write checks: -
Custom Lint checks Lint has a number of Detector specializations
which makes a lot easier to write checks: › XmlScanner: lets you visit the XML file with DOM. -
Custom Lint checks Lint has a number of Detector specializations
which makes a lot easier to write checks: › XmlScanner: lets you visit the XML file with DOM. › SourceCodeScanner: useful for Kotlin/Java analysis. -
Custom Lint checks Lint has a number of Detector specializations
which makes a lot easier to write checks: › XmlScanner: lets you visit the XML file with DOM. › SourceCodeScanner: useful for Kotlin/Java analysis. › ClassScanner, BinaryResourceScanner, ResourceFolderScanner… -
SourceCodeScanner fun getInt()k= (0..10).map { it.also { "test" } }.also
{ “Test2" }.apply { size }.let { it.last() }.takeIf { it > 10 } -
SourceCodeScanner fun getInt(): Int?k= (0..10).map { it.also { "test" }
}.also { “Test2" }.apply { size }.let { it.last() }.takeIf { it > 10 } -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k ... } k -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k ... } k -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k … } k -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k … } k -
SourceCodeScanner private fun isInferredMethod(node: UMethod): Boolean { val function =
node.sourcePsi as? KtNamedFunction ?: return false val isFunction = function.funKeyword != null val hasColon = function.colon != null val isVoid = returnType == PsiType.VOID || returnType.presentableText == “Unit" return isFunction && !hasColon && !isVoid } -
SourceCodeScanner private fun isInferredMethod(node: UMethod): Boolean { val function =
node.sourcePsi as? KtNamedFunction ?: return false val isFunction = function.funKeyword != null val hasColon = function.colon != null val isVoid = returnType == PsiType.VOID || returnType.presentableText == “Unit" return isFunction && !hasColon && !isVoid } -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k ... } k -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k ... } k -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k ... } k -
SourceCodeScanner class MyDetector : Detector(), SourceCodeScanner { ... override fun
getApplicableUastTypes(): MutableList<Class<out UElement>> = mutableListOf(UMethod::class.java) override fun createUastHandler(context: JavaContext): UElementHandler = InferredTypeHandler(context) class InferredTypeHandler(private val context: JavaContext) : UElementHandler() { ... override fun visitMethod(node: UMethod) { if (!isKotlin(node)) return if (isInferredMethod(node)){ report(context) } k } k ... } k -
Examples of checks Some examples of checks we are currently
using: › BaseFragment within a package. › Internal APIs to recommend to use a custom subscriber. › Nesting in lambdas. › Nesting in custom LiveData. › Top level class comments. › Internal deprecations. -
References › https://engineering.linecorp.com/en/blog/synchronizing-geographically- distributed-teams-with-lint/ › https://groups.google.com/forum/#!forum/lint-dev › https://docs.google.com/presentation/d/ 1Sr-6E3Tk1lBguUob0GigqSEsfN-04qS56Whj0UEr0AE/ edit#slide=id.g29100eff00_3_126
› https://en.wikipedia.org/wiki/Static_program_analysis