Skip to content

Commit

Permalink
DeprecatedColorInXmlDetector: Bail out early if the check is suppress…
Browse files Browse the repository at this point in the history
…ed (#5)
  • Loading branch information
zmdominguez committed Feb 15, 2022
1 parent d7a78ba commit 83f8cfb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class DeprecatedColorInXmlDetector : ResourceXmlDetector() {
private var colourUsagesLintMap: LintMap = LintMap()

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

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

Expand Down Expand Up @@ -163,6 +168,10 @@ class DeprecatedColorInXmlDetector : ResourceXmlDetector() {
* Look for those usages here.
*/
override fun visitAttribute(context: XmlContext, attribute: Attr) {
// The issue is suppressed for this attribute, skip it
val isIssueSuppressed = context.driver.isSuppressed(context, ISSUE, attribute)
if (isIssueSuppressed) return

// Save the value and location of the XML attribute.
saveColourUsage(
attribute.nodeValue,
Expand All @@ -182,12 +191,11 @@ class DeprecatedColorInXmlDetector : ResourceXmlDetector() {
* ```
*/
override fun visitElement(context: XmlContext, element: Element) {
// Colors can also be in styles as an `<item>` value
// Find those cases here
val tagName = element.tagName
if (tagName != SdkConstants.TAG_ITEM) return
// The issue is suppressed for this element, skip it
val isIssueSuppressed = context.driver.isSuppressed(context, ISSUE, element)
if (isIssueSuppressed) return

// Save the value of this element
// Check if the value of this element is a deprecated colour
if (element.firstChild == null) return

val fileContents = context.getContents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,75 @@ class DeprecatedColorInXmlDetectorTest : LintDetectorTest() {
.expectClean()
}

@Test
fun testSuppressedDeprecatedColorInWidget() {
lint().files(
DEPRECATED_COLOUR_FILE,
xml(
"res/layout/layout.xml",
"""
<View xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/red_error"
tools:ignore="DeprecatedColorInXml"/>
</View>
"""
).indented()
)
.testModes(TestMode.PARTIAL)
.run()
.expectClean()
}

@Test
fun testSuppressedDeprecatedColorInLayoutFile() {
lint().files(
DEPRECATED_COLOUR_FILE,
xml(
"res/layout/layout.xml",
"""
<View xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="DeprecatedColorInXml">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/red_error" />
</View>
"""
).indented()
)
.testModes(TestMode.PARTIAL)
.run()
.expectClean()
}

@Test
fun testMultipleSuppressedDeprecatedColorInLayoutFile() {
lint().files(
DEPRECATED_COLOUR_FILE,
xml(
"res/layout/layout.xml",
"""
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/red_error"
tools:ignore="Orientation,DeprecatedColorInXml" />
"""
).indented()
)
.testModes(TestMode.PARTIAL)
.run()
.expectClean()
}

@Test
fun testDeprecatedColorInLayoutFile() {
lint().files(
Expand Down Expand Up @@ -112,6 +181,31 @@ class DeprecatedColorInXmlDetectorTest : LintDetectorTest() {
)
}

@Test
fun testDeprecatedColorInAlias() {
lint().files(
DEPRECATED_COLOUR_FILE,
xml(
"res/values/colors.xml",
"""
<resources>
<color name="red_alias">@color/red_error</color>
</resources>
"""
).indented()
)
.testModes(TestMode.PARTIAL)
.run()
.expect(
"""
res/values/colors.xml:2: Error: Deprecated colours should not be used [DeprecatedColorInXml]
<color name="red_alias">@color/red_error</color>
~~~~~~~~~~~~~~~~
1 errors, 0 warnings
"""
)
}

@Test
fun testDeprecatedSelectorInLayoutFile() {
lint().files(
Expand Down Expand Up @@ -284,9 +378,9 @@ class DeprecatedColorInXmlDetectorTest : LintDetectorTest() {
"res/layout/layout.xml",
"""
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/rewards_color_primary" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/rewards_color_primary" />
"""
).indented()

Expand Down

0 comments on commit 83f8cfb

Please sign in to comment.