Skip to content

Commit 944d1b7

Browse files
vanniktechsamtstern
authored andcommitted
Better lint integration. (#1068)
1 parent f3169b1 commit 944d1b7

File tree

8 files changed

+115
-113
lines changed

8 files changed

+115
-113
lines changed

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
apply from: 'constants.gradle'
2-
31
buildscript {
2+
apply from: 'constants.gradle'
3+
44
repositories {
55
google()
66
jcenter()
77
mavenLocal()
88
}
99

1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.0.0'
11+
classpath 'com.android.tools.build:gradle:3.0.1'
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1213
classpath 'com.google.gms:google-services:3.1.1'
1314
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
1415
}

constants.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ project.ext {
1111
firebaseVersion = '11.8.0'
1212
supportLibraryVersion = '27.0.2'
1313
architectureVersion = '1.0.0'
14+
kotlinVersion = '1.2.10'
1415
}

lint/build.gradle

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
apply plugin: 'java-library'
2-
3-
configurations {
4-
lintChecks
5-
}
1+
apply plugin: 'kotlin'
62

73
dependencies {
84
compileOnly 'com.android.tools.lint:lint-api:26.0.1'
9-
compileOnly 'com.android.tools.lint:lint-checks:26.0.1'
10-
}
5+
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
116

7+
testImplementation 'com.android.tools.lint:lint-api:26.0.1'
8+
testImplementation 'com.android.tools.lint:lint-tests:26.0.1'
9+
}
1210

1311
jar {
1412
manifest {

lint/src/main/java/com/firebaseui/lint/LintIssueRegistry.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.firebaseui.lint
2+
3+
import com.android.tools.lint.client.api.IssueRegistry
4+
5+
/**
6+
* Registry for custom FirebaseUI lint checks.
7+
*/
8+
class LintIssueRegistry : IssueRegistry() {
9+
override fun getIssues() = listOf(NonGlobalIdDetector.NON_GLOBAL_ID)
10+
}

lint/src/main/java/com/firebaseui/lint/NonGlobalIdDetector.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.firebaseui.lint
2+
3+
import com.android.tools.lint.detector.api.Category
4+
import com.android.tools.lint.detector.api.Detector
5+
import com.android.tools.lint.detector.api.Implementation
6+
import com.android.tools.lint.detector.api.Issue
7+
import com.android.tools.lint.detector.api.LayoutDetector
8+
import com.android.tools.lint.detector.api.Scope
9+
import com.android.tools.lint.detector.api.Severity
10+
import com.android.tools.lint.detector.api.XmlContext
11+
import org.w3c.dom.Element
12+
13+
/**
14+
* Lint detector to find layout attributes that reference "@id" where they should reference
15+
* "@+id" instead.
16+
*/
17+
class NonGlobalIdDetector : LayoutDetector() {
18+
override fun getApplicableElements(): List<String> = Detector.XmlScanner.ALL
19+
20+
override fun visitElement(context: XmlContext, element: Element) {
21+
(0 until element.attributes.length)
22+
.map { element.attributes.item(it) }
23+
.filter { it.nodeValue.contains(NON_GLOBAL_ID_PREFIX) }
24+
.forEach {
25+
val quickFix = fix()
26+
.replace()
27+
.name("Fix id")
28+
.text(it.nodeValue)
29+
.with(it.nodeValue.replace(NON_GLOBAL_ID_PREFIX, GLOBAL_ID_PREFIX))
30+
.build()
31+
32+
context.report(NON_GLOBAL_ID, it, context.getLocation(it), REPORT_MESSAGE, quickFix)
33+
}
34+
}
35+
36+
companion object {
37+
private const val NON_GLOBAL_ID_PREFIX = "@id"
38+
private const val GLOBAL_ID_PREFIX = "@+id"
39+
40+
private const val REPORT_MESSAGE = "Use of non-global @id in layout file, consider using @+id instead for compatibility with aapt1."
41+
42+
val NON_GLOBAL_ID = Issue.create(
43+
"NonGlobalIdInLayout",
44+
"Usage of non-global @id in layout file.",
45+
"To maintain compatibility with aapt1, it is safer to always use @+id in layout files.",
46+
Category.CORRECTNESS,
47+
8,
48+
Severity.ERROR,
49+
Implementation(NonGlobalIdDetector::class.java, Scope.ALL_RESOURCES_SCOPE)
50+
)
51+
}
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.firebaseui.lint
2+
3+
import com.android.tools.lint.checks.infrastructure.TestFiles.xml
4+
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
5+
import com.firebaseui.lint.NonGlobalIdDetector.Companion.NON_GLOBAL_ID
6+
import org.junit.Test
7+
8+
class NonGlobalIdDetectorTest {
9+
@Test
10+
fun validId() {
11+
lint()
12+
.files(xml("res/layout/layout.xml", """
13+
|<RelativeLayout
14+
| xmlns:android="http://schemas.android.com/apk/res/android"
15+
| android:id="@+id/valid"/>""".trimMargin()))
16+
.issues(NON_GLOBAL_ID)
17+
.run()
18+
.expectClean()
19+
}
20+
21+
@Test
22+
fun invalidId() {
23+
lint()
24+
.files(xml("res/layout/layout.xml", """
25+
|<ScrollView
26+
| xmlns:android="http://schemas.android.com/apk/res/android"
27+
| android:id="@id/invalid"/>""".trimMargin()))
28+
.issues(NON_GLOBAL_ID)
29+
.run()
30+
.expect("""
31+
|res/layout/layout.xml:3: Error: Use of non-global @id in layout file, consider using @+id instead for compatibility with aapt1. [NonGlobalIdInLayout]
32+
| android:id="@id/invalid"/>
33+
| ~~~~~~~~~~~~~~~~~~~~~~~~
34+
|1 errors, 0 warnings""".trimMargin())
35+
.expectFixDiffs("""
36+
|Fix for res/layout/layout.xml line 2: Fix id:
37+
|@@ -3 +3
38+
|- android:id="@id/invalid"/>
39+
|@@ -4 +3
40+
|+ android:id="@+id/invalid"/>
41+
|""".trimMargin())
42+
}
43+
}

0 commit comments

Comments
 (0)