Skip to content

Commit 83f8cfb

Browse files
authored
DeprecatedColorInXmlDetector: Bail out early if the check is suppressed (#5)
1 parent d7a78ba commit 83f8cfb

2 files changed

Lines changed: 111 additions & 9 deletions

File tree

lint-checks/src/main/java/dev/zarah/lint/checks/DeprecatedColorInXmlDetector.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class DeprecatedColorInXmlDetector : ResourceXmlDetector() {
3636
private var colourUsagesLintMap: LintMap = LintMap()
3737

3838
override fun appliesTo(folderType: ResourceFolderType): Boolean {
39+
// Return true if we want to analyse resource files in the specified resource
40+
// folder type.
3941
return folderType in listOf(
4042
ResourceFolderType.LAYOUT,
4143
ResourceFolderType.DRAWABLE,
@@ -45,7 +47,10 @@ class DeprecatedColorInXmlDetector : ResourceXmlDetector() {
4547
}
4648

4749
override fun getApplicableAttributes(): Collection<String>? {
48-
// Look at every attribute in a file
50+
// Return the set of attribute names we want to analyze. The `visitAttribute` method
51+
// below will be called each time lint sees one of these attributes in a
52+
// layout XML resource file. In this case, we want to analyze every attribute
53+
// in every layout XML resource file.
4954
return XmlScannerConstants.ALL
5055
}
5156

@@ -163,6 +168,10 @@ class DeprecatedColorInXmlDetector : ResourceXmlDetector() {
163168
* Look for those usages here.
164169
*/
165170
override fun visitAttribute(context: XmlContext, attribute: Attr) {
171+
// The issue is suppressed for this attribute, skip it
172+
val isIssueSuppressed = context.driver.isSuppressed(context, ISSUE, attribute)
173+
if (isIssueSuppressed) return
174+
166175
// Save the value and location of the XML attribute.
167176
saveColourUsage(
168177
attribute.nodeValue,
@@ -182,12 +191,11 @@ class DeprecatedColorInXmlDetector : ResourceXmlDetector() {
182191
* ```
183192
*/
184193
override fun visitElement(context: XmlContext, element: Element) {
185-
// Colors can also be in styles as an `<item>` value
186-
// Find those cases here
187-
val tagName = element.tagName
188-
if (tagName != SdkConstants.TAG_ITEM) return
194+
// The issue is suppressed for this element, skip it
195+
val isIssueSuppressed = context.driver.isSuppressed(context, ISSUE, element)
196+
if (isIssueSuppressed) return
189197

190-
// Save the value of this element
198+
// Check if the value of this element is a deprecated colour
191199
if (element.firstChild == null) return
192200

193201
val fileContents = context.getContents()

lint-checks/src/test/java/dev/zarah/lint/checks/DeprecatedColorInXmlDetectorTest.kt

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,75 @@ class DeprecatedColorInXmlDetectorTest : LintDetectorTest() {
3131
.expectClean()
3232
}
3333

34+
@Test
35+
fun testSuppressedDeprecatedColorInWidget() {
36+
lint().files(
37+
DEPRECATED_COLOUR_FILE,
38+
xml(
39+
"res/layout/layout.xml",
40+
"""
41+
<View xmlns:android="http://schemas.android.com/apk/res/android"
42+
xmlns:tools="http://schemas.android.com/tools"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content">
45+
<TextView android:layout_width="wrap_content"
46+
android:layout_height="wrap_content"
47+
android:textColor="@color/red_error"
48+
tools:ignore="DeprecatedColorInXml"/>
49+
</View>
50+
"""
51+
).indented()
52+
)
53+
.testModes(TestMode.PARTIAL)
54+
.run()
55+
.expectClean()
56+
}
57+
58+
@Test
59+
fun testSuppressedDeprecatedColorInLayoutFile() {
60+
lint().files(
61+
DEPRECATED_COLOUR_FILE,
62+
xml(
63+
"res/layout/layout.xml",
64+
"""
65+
<View xmlns:android="http://schemas.android.com/apk/res/android"
66+
xmlns:tools="http://schemas.android.com/tools"
67+
android:layout_width="wrap_content"
68+
android:layout_height="wrap_content"
69+
tools:ignore="DeprecatedColorInXml">
70+
<TextView android:layout_width="wrap_content"
71+
android:layout_height="wrap_content"
72+
android:textColor="@color/red_error" />
73+
</View>
74+
"""
75+
).indented()
76+
)
77+
.testModes(TestMode.PARTIAL)
78+
.run()
79+
.expectClean()
80+
}
81+
82+
@Test
83+
fun testMultipleSuppressedDeprecatedColorInLayoutFile() {
84+
lint().files(
85+
DEPRECATED_COLOUR_FILE,
86+
xml(
87+
"res/layout/layout.xml",
88+
"""
89+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
90+
xmlns:tools="http://schemas.android.com/tools"
91+
android:layout_width="wrap_content"
92+
android:layout_height="wrap_content"
93+
android:background="@color/red_error"
94+
tools:ignore="Orientation,DeprecatedColorInXml" />
95+
"""
96+
).indented()
97+
)
98+
.testModes(TestMode.PARTIAL)
99+
.run()
100+
.expectClean()
101+
}
102+
34103
@Test
35104
fun testDeprecatedColorInLayoutFile() {
36105
lint().files(
@@ -112,6 +181,31 @@ class DeprecatedColorInXmlDetectorTest : LintDetectorTest() {
112181
)
113182
}
114183

184+
@Test
185+
fun testDeprecatedColorInAlias() {
186+
lint().files(
187+
DEPRECATED_COLOUR_FILE,
188+
xml(
189+
"res/values/colors.xml",
190+
"""
191+
<resources>
192+
<color name="red_alias">@color/red_error</color>
193+
</resources>
194+
"""
195+
).indented()
196+
)
197+
.testModes(TestMode.PARTIAL)
198+
.run()
199+
.expect(
200+
"""
201+
res/values/colors.xml:2: Error: Deprecated colours should not be used [DeprecatedColorInXml]
202+
<color name="red_alias">@color/red_error</color>
203+
~~~~~~~~~~~~~~~~
204+
1 errors, 0 warnings
205+
"""
206+
)
207+
}
208+
115209
@Test
116210
fun testDeprecatedSelectorInLayoutFile() {
117211
lint().files(
@@ -284,9 +378,9 @@ class DeprecatedColorInXmlDetectorTest : LintDetectorTest() {
284378
"res/layout/layout.xml",
285379
"""
286380
<View xmlns:android="http://schemas.android.com/apk/res/android"
287-
android:layout_width="wrap_content"
288-
android:layout_height="wrap_content"
289-
android:background="@color/rewards_color_primary" />
381+
android:layout_width="wrap_content"
382+
android:layout_height="wrap_content"
383+
android:background="@color/rewards_color_primary" />
290384
"""
291385
).indented()
292386

0 commit comments

Comments
 (0)