-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
DuplicateCrowdInStrings.kt
186 lines (174 loc) · 8.09 KB
/
DuplicateCrowdInStrings.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2020 David Allison <davidallisongithub@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* https://android.googlesource.com/platform/tools/base/+/studio-master-dev/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/StringCasingDetector.kt?autodive=0%2F
*/
@file:Suppress("UnstableApiUsage")
package com.ichi2.anki.lint.rules
import com.android.SdkConstants.*
import com.android.resources.ResourceFolderType
import com.android.tools.lint.checks.StringCasingDetector.StringDeclaration
import com.android.tools.lint.detector.api.*
import com.android.tools.lint.detector.api.Scope.Companion.ALL_RESOURCES_SCOPE
import com.android.utils.Pair
import com.ichi2.anki.lint.utils.Constants
import com.ichi2.anki.lint.utils.StringFormatDetector
import org.w3c.dom.Element
import org.w3c.dom.Node
import java.util.*
class DuplicateCrowdInStrings : ResourceXmlDetector() {
/*
* Map of all locale,strings in lower case, to their raw elements to ensure that there are no
* duplicate strings.
*/
private val allStrings: HashMap<Pair<String, String>, MutableList<StringDeclaration>> = HashMap<Pair<String, String>, MutableList<StringDeclaration>>()
override fun appliesTo(folderType: ResourceFolderType): Boolean {
return folderType == ResourceFolderType.VALUES
}
override fun getApplicableElements() = listOf(TAG_STRING)
override fun visitElement(context: XmlContext, element: Element) {
// Only check the golden copy - not the translated sources.
// We currently do not have the ability to do a 'per file'
if ("values" != context.file.parentFile.name) {
return
}
// parentNode == resources, as we only apply this to <string>
element.parentNode.attributes.getNamedItem("tools:ignore")?.let {
if (it.nodeValue.contains(ID)) {
return
}
}
val childNodes = element.childNodes
if (childNodes.length > 0) {
if (childNodes.length == 1) {
val child = childNodes.item(0)
if (child.nodeType == Node.TEXT_NODE || child.nodeType == Node.CDATA_SECTION_NODE) {
checkTextNode(
context,
element,
StringFormatDetector.stripQuotes(child.nodeValue)
)
}
} else {
val sb = StringBuilder()
StringFormatDetector.addText(sb, element)
if (sb.isNotEmpty()) {
checkTextNode(context, element, sb.toString())
}
}
}
}
private fun checkTextNode(context: XmlContext, element: Element, text: String) {
if (VALUE_FALSE == element.getAttribute(ATTR_TRANSLATABLE)) {
return
}
val locale = getLocale(context)
val key = if (locale != null) Pair.of(locale.full, text.lowercase(Locale.forLanguageTag(locale.tag))) else Pair.of("default", text.lowercase(Locale.US))
val handle: Location.Handle = context.createLocationHandle(element)
handle.clientData = element
val handleList: MutableList<StringDeclaration> = allStrings.getOrDefault(key, ArrayList<StringDeclaration>())
handleList.add(StringDeclaration(element.getAttribute(ATTR_NAME), text, handle))
allStrings[key] = handleList
}
override fun afterCheckRootProject(context: Context) {
for (duplicates in allStrings.values) {
if (duplicates.size <= 1) {
continue
}
var firstLocation: Location? = null
var prevLocation: Location? = null
var prevString = ""
var caseVaries = false
val names: MutableList<String> = ArrayList()
if (duplicates.all { x: StringDeclaration ->
val el = x.location.clientData as Element
el.hasAttribute("comment")
}
) {
// skipping as all instances have a comment
continue
}
for (duplicate in duplicates) {
names.add(duplicate.name)
val string: String = duplicate.text
val location: Location = duplicate.location.resolve()
if (prevLocation == null) {
firstLocation = location
} else {
prevLocation.secondary = location
location.message = "Duplicates value in `${names[0]}`. Add a `comment` attribute on both strings to explain this duplication"
location.setSelfExplanatory(false)
if (string != prevString) {
caseVaries = true
location.message = location.message + " (case varies, but you can use " +
"`android:inputType` or `android:capitalize` in the " +
"presentation)"
}
}
prevLocation = location
prevString = string
}
val nameValues: MutableList<String> = ArrayList()
for (name in names) {
nameValues.add("`$name`")
}
val nameList = formatList(nameValues, nameValues.size, sort = true, useConjunction = false)
// we use both quotes and code styling here so it appears in the console quoted
var message = "Duplicate string value \"`$prevString`\", used in $nameList"
if (caseVaries) {
message += ". Use `android:inputType` or `android:capitalize` " +
"to treat these as the same and avoid string duplication."
}
context.report(ISSUE, firstLocation!!, message)
}
}
companion object {
private val IMPLEMENTATION_XML = Implementation(DuplicateCrowdInStrings::class.java, ALL_RESOURCES_SCOPE)
const val ID = "DuplicateCrowdInStrings"
/**
* Whether there are any duplicate strings, including capitalization adjustments.
*/
var ISSUE: Issue = Issue.create(
ID,
"Duplicate Strings (CrowdIn)",
"Duplicate strings are ambiguous for translators." +
"This lint check looks for duplicate strings, including differences for strings" +
"where the only difference is in capitalization. Title casing and all uppercase can" +
"all be adjusted in the layout or in code. Any duplicate strings should have a comment" +
"attribute added if they are intentional and required for translations.",
Constants.ANKI_CROWDIN_CATEGORY,
Constants.ANKI_CROWDIN_PRIORITY,
Constants.ANKI_CROWDIN_SEVERITY,
IMPLEMENTATION_XML
)
}
}