Skip to content

Instantly share code, notes, and snippets.

@ataulm
Created July 17, 2020 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ataulm/3ebeb00919245a27de2bd6a931eee6af to your computer and use it in GitHub Desktop.
Save ataulm/3ebeb00919245a27de2bd6a931eee6af to your computer and use it in GitHub Desktop.
internal class StyleAttrUsesThemeDetector : ResourceXmlDetector() {
companion object {
val ISSUE = createErrorThemesAndStylesIssue(StyleAttrUsesThemeDetector::class, Scope.RESOURCE_FILE_SCOPE)
}
override fun appliesTo(folderType: ResourceFolderType) = folderType == ResourceFolderType.LAYOUT
override fun getApplicableAttributes(): Collection<String> = XmlScannerConstants.ALL
override fun visitAttribute(context: XmlContext, attribute: Attr) {
if (attribute.name != "style") {
return
}
ResourceUrl.parse(attribute.nodeValue)?.let { resourceUrl ->
if (resourceUrl.name.contains("Theme")) {
reportIssue(context, attribute)
}
}
}
private fun reportIssue(context: XmlContext, attribute: Attr) = context.report(
issue = ISSUE,
scope = attribute,
location = context.getValueLocation(attribute),
message = "`style` should not reference theme resources."
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment