Skip to content

Commit 13eacf1

Browse files
Merge pull request #31 from Omega-R/develop
Develop
2 parents d5c7d94 + 62c44e5 commit 13eacf1

File tree

15 files changed

+436
-379
lines changed

15 files changed

+436
-379
lines changed

checks/src/main/java/com/omegar/lint/checks/LintIssueRegistry.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.order.file_c
1515
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.order.function_params.PositionArgumentDetector
1616
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.class_length.MaxClassLengthDetector
1717
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.class_methods_count.MaxMethodCountDetector
18+
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.classes_in_package_count.MaxClassInPackageDetector
1819
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.emptiness.EmptyBodyFunctionDetector
1920
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.function_length.MaxFunctionLengthDetector
2021
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.line_length.MaxLineLengthDetector
22+
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.packages_count.MaxPackageCountDetector
2123
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.restrictions.params_count.MaxFunctionsArgumentsDetector
2224
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.simplifications.control_instructions.SimplificationsControlInstructionsDetector
2325
import com.omegar.lint.checks.detector.code_guidelines.kotlin_style.simplifications.function.SimplificationsFunctionDetector
@@ -52,8 +54,8 @@ class LintIssueRegistry : IssueRegistry() {
5254
SpaceMethodDetector.ISSUE,
5355
NameFileSufixDetector.ISSUE,
5456
AttributesPositionXmlDetector.ISSUE,
55-
// MaxClassInPackageDetector.ISSUE, TODO this working only for classes which user has visited after rebuild, need to fix, and count all classes
56-
// MaxPackageCountDetector.ISSUE, TODO this working only for classes which user has visited after rebuild, need to fix, and count all classes
57+
MaxClassInPackageDetector.ISSUE,// TODO need testing
58+
MaxPackageCountDetector.ISSUE, // TODO need testing
5759
SimplificationsControlInstructionsDetector.ISSUE, // TODO need testing
5860
IntentExtraParametersDetector.ISSUE,
5961
ArgumentsBundleKeyPrefixDetector.ISSUE,

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/general_recommendations/parameter_passing/intent_creation/IntentExtraParametersDetector.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ class IntentExtraParametersDetector : Detector(), Detector.UastScanner {
1212
val ISSUE: Issue = Issue.create(
1313
// ID: used in @SuppressLint warnings etc
1414
id = "OMEGA_USE_EXTRA_PREFIX_FOR_INTENT_PARAMS",
15-
// Title -- shown in the IDE's preference dialog, as category headers in the
16-
// Analysis results window, etc
1715
briefDescription = "Use EXTRA prefix for intent arguments.",
18-
// Full explanation of the issue; you can use some markdown markup such as
19-
// `monospace`, *italic*, and **bold**.
2016
explanation = """
2117
Use EXTRA prefix for intent arguments
2218
http://wiki.omega-r.club/dev-android-code#rec228392168

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/kotlin_style/elements_formating/annotation/AnnotationDetector.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ class AnnotationDetector : Detector(), Detector.UastScanner {
1010
/** Issue describing the problem and pointing to the detector implementation */
1111
@JvmField
1212
val ISSUE: Issue = Issue.create(
13-
id = "OMEGA_SINGLE_LINE_ANNOTATION",
14-
briefDescription = "If there are multiple annotations for a class / field / method, place each annotation on a new line",
15-
explanation = """
13+
"OMEGA_SINGLE_LINE_ANNOTATION",
14+
"If there are multiple annotations for a class / field / method, place each annotation on a new line",
15+
"""
1616
If there are multiple annotations for a class / field / method, place each annotation on a new line.
1717
http://wiki.omega-r.club/dev-android-code#rec228389852
1818
""",
19-
category = Category.CORRECTNESS,
20-
priority = 7,
21-
severity = Severity.INFORMATIONAL,
22-
implementation = Implementation(
23-
AnnotationDetector::class.java,
24-
Scope.JAVA_FILE_SCOPE
25-
)
26-
)
19+
Category.CORRECTNESS,
20+
7,
21+
Severity.INFORMATIONAL,
22+
Implementation(
23+
AnnotationDetector::class.java,
24+
Scope.JAVA_FILE_SCOPE
25+
)
26+
)
2727

2828
private val ONE_EXPRESSION_REGEX = Regex("""\s*@\s*([a-z]|[A-Z]|["]|[']|[(]|[)]|[=]|[\s])*@""")
2929
}
@@ -44,11 +44,11 @@ class AnnotationDetector : Detector(), Detector.UastScanner {
4444

4545
if (line.contains(ONE_EXPRESSION_REGEX)) {
4646
context.report(
47-
ISSUE,
48-
node,
49-
context.getRangeLocation(node.parent, beginPosition, length),
50-
ISSUE.getExplanation(TextFormat.TEXT)
51-
)
47+
ISSUE,
48+
node,
49+
context.getRangeLocation(node.parent, beginPosition, length),
50+
ISSUE.getExplanation(TextFormat.TEXT)
51+
)
5252
}
5353

5454
beginPosition += line.length

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/kotlin_style/elements_formating/lambda/LambdaDetector.kt

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ class LambdaDetector : Detector(), Detector.UastScanner {
1313
@JvmField
1414
val ISSUE: Issue = Issue.create(
1515
id = "OMEGA_USE_MULTI_LINE_LAMBDA_IN_ONE_LINE",
16-
briefDescription = "When declaring parameter names in a multi-line lambda, put the names on the first line, followed by an arrow and a new line:",
16+
"When declaring parameter names in a multi-line lambda," +
17+
" put the names on the first line, followed by an arrow and a new line:",
1718
explanation = """
18-
When declaring parameter names in a multi-line lambda, put the names on the first line, followed by an arrow and a new line:
19+
When declaring parameter names in a multi-line lambda,
20+
put the names on the first line, followed by an arrow and a new line:
1921
http://wiki.omega-r.club/dev-android-code#rec228389710
2022
""",
2123
category = Category.CORRECTNESS,
@@ -30,6 +32,7 @@ class LambdaDetector : Detector(), Detector.UastScanner {
3032
private const val LAMBDA_VAL = "->"
3133
private const val CLOSE_SCOPE_VAL = "}"
3234
private const val OPEN_SCOPE_VAL = "{"
35+
private const val QUOTE_VAL = "\""
3336
private val EMPTY_ARROW_REGEX = Regex("""^\s*->""")
3437
private val SWITCH_VAL = Regex("""(switch|when)""")
3538
}
@@ -49,32 +52,9 @@ class LambdaDetector : Detector(), Detector.UastScanner {
4952
val line = lines[i]
5053
val length = line.length
5154

52-
if (line.contains(LAMBDA_VAL)) {
53-
if ((line.matches(EMPTY_ARROW_REGEX))) {
54-
context.report(
55-
ISSUE,
56-
node,
57-
context.getRangeLocation(node.parent, beginPosition, length),
58-
line + "\n" + ISSUE.getExplanation(TextFormat.TEXT)
59-
)
60-
} else if (i - 1 >= 0) {
61-
val previousLine = lines[i - 1]
62-
if (previousLine.contains(OPEN_SCOPE_VAL)
63-
&& !line.contains(CLOSE_SCOPE_VAL)
64-
&& !previousLine.contains(CLOSE_SCOPE_VAL)
65-
&& !previousLine.contains(SWITCH_VAL)
66-
&& previousLine.length + line.trim().length < MAX_LENGTH
67-
) {
68-
context.report(
69-
ISSUE,
70-
node,
71-
context.getRangeLocation(node.parent, beginPosition, length),
72-
previousLine + "\n" + previousLine.contains(CLOSE_SCOPE_VAL) + "\n" + ISSUE.getExplanation(
73-
TextFormat.TEXT
74-
)
75-
)
76-
}
77-
}
55+
if (line.contains(LAMBDA_VAL) && !line.contains(QUOTE_VAL)) {
56+
val params = Params(context, node, lines, line, beginPosition, i)
57+
checkLambda(params)
7858
}
7959

8060
beginPosition += length
@@ -83,5 +63,43 @@ class LambdaDetector : Detector(), Detector.UastScanner {
8363
}
8464
}
8565
}
66+
67+
private fun checkLambda(params: Params) {
68+
val length = params.line.length
69+
if ((params.line.matches(EMPTY_ARROW_REGEX))) {
70+
params.context.report(
71+
ISSUE,
72+
params.node,
73+
params.context.getRangeLocation(params.node.parent, params.beginPosition, length),
74+
params.line + "\n" + ISSUE.getExplanation(TextFormat.TEXT)
75+
)
76+
} else if (params.index - 1 >= 0) {
77+
val previousLine = params.lines[params.index - 1]
78+
if (previousLine.contains(OPEN_SCOPE_VAL)
79+
&& !previousLine.contains(CLOSE_SCOPE_VAL)
80+
&& !previousLine.contains(SWITCH_VAL)
81+
&& previousLine.length + params.line.trim().length < MAX_LENGTH
82+
&& !params.line.contains(OPEN_SCOPE_VAL)
83+
) {
84+
params.context.report(
85+
ISSUE,
86+
params.node,
87+
params.context.getRangeLocation(params.node.parent, params.beginPosition, length),
88+
previousLine + "\n" + previousLine.contains(CLOSE_SCOPE_VAL) + "\n" + ISSUE.getExplanation(
89+
TextFormat.TEXT
90+
)
91+
)
92+
}
93+
}
94+
}
95+
96+
private class Params(
97+
val context: JavaContext,
98+
val node: UClass,
99+
val lines: List<String>,
100+
val line: String,
101+
val beginPosition: Int,
102+
val index: Int
103+
)
86104
}
87105

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/kotlin_style/name/abbreviation/AbbreviationDetector.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ class AbbreviationDetector : Detector(), Detector.UastScanner {
3232

3333
//exclusion
3434
private const val MILLISECONDS_LABEL = "MSec"
35+
private const val UELEMENT_LABEL = "UElement"
3536
private const val TODO_LABEL = "TODO"
37+
private const val CLASS_LABEL = "class"
38+
private const val ENUM_LABEL = "enum class"
3639

3740
val exclusionsList = listOf(
3841
MILLISECONDS_LABEL,
39-
TODO_LABEL
42+
TODO_LABEL,
43+
UELEMENT_LABEL
4044
)
4145

4246
}
@@ -50,6 +54,12 @@ class AbbreviationDetector : Detector(), Detector.UastScanner {
5054
override fun createUastHandler(context: JavaContext): UElementHandler {
5155
return object : UElementHandler() {
5256
override fun visitDeclaration(node: UDeclaration) {
57+
val parent = node.parent ?: return
58+
val some = parent.text.lines()
59+
val nameLine = some.firstOrNull { it.contains(CLASS_LABEL) }
60+
if (nameLine != null && nameLine.contains(ENUM_LABEL)) {
61+
return
62+
}
5363
val lines = node.text?.lines() ?: return
5464

5565
var checkText = getNameString(lines) ?: return
@@ -64,7 +74,7 @@ class AbbreviationDetector : Detector(), Detector.UastScanner {
6474
ISSUE,
6575
node as UElement,
6676
context.getNameLocation(node),
67-
ISSUE.getExplanation(TextFormat.TEXT)
77+
checkText + "\n" + ISSUE.getExplanation(TextFormat.TEXT)
6878
)
6979
}
7080
}
@@ -96,6 +106,4 @@ class AbbreviationDetector : Detector(), Detector.UastScanner {
96106
}
97107
return resultText
98108
}
99-
100-
101109
}

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/kotlin_style/name/class/NameFileSufixDetector.kt

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ class NameFileSufixDetector : Detector(), Detector.UastScanner {
1111
/** Issue describing the problem and pointing to the detector implementation */
1212
@JvmField
1313
val ISSUE: Issue = Issue.create(
14-
id = "OMEGA_USE_PARENT_NAME_AS_SUFFIX_FOR_CHILD",
15-
briefDescription = "The file name does not match the coding convention",
16-
explanation = """
14+
id = "OMEGA_USE_PARENT_NAME_AS_SUFFIX_FOR_CHILD",
15+
briefDescription = "The file name does not match the coding convention",
16+
explanation = """
1717
Class name should has parent name in sufix. Rename this file.
1818
http://wiki.omega-r.club/dev-android-code#rec226456384
1919
""",
20-
category = Category.CORRECTNESS,
21-
priority = 6,
22-
severity = Severity.WARNING,
23-
implementation = Implementation(
24-
NameFileSufixDetector::class.java,
25-
Scope.JAVA_FILE_SCOPE
26-
)
27-
)
20+
category = Category.CORRECTNESS,
21+
priority = 6,
22+
severity = Severity.WARNING,
23+
implementation = Implementation(
24+
NameFileSufixDetector::class.java,
25+
Scope.JAVA_FILE_SCOPE
26+
)
27+
)
2828

2929
private const val ACTIVITY_VALUE = "Activity"
3030
private val ACTIVITY_VALUE_REGEX = Regex(".*${ACTIVITY_VALUE}$")
@@ -68,47 +68,46 @@ class NameFileSufixDetector : Detector(), Detector.UastScanner {
6868
when {
6969
part.matches(ACTIVITY_VALUE_REGEX) ->
7070
if (!name.matches(ACTIVITY_VALUE_REGEX)) {
71-
makeContextReport(value = ACTIVITY_VALUE, node)
71+
makeContextReport(context, ACTIVITY_VALUE, node)
7272
}
7373

7474
part.matches(FRAGMENT_VALUE_REGEX) ->
7575
if (!name.matches(FRAGMENT_VALUE_REGEX)) {
76-
makeContextReport(value = FRAGMENT_VALUE, node)
76+
makeContextReport(context, FRAGMENT_VALUE, node)
7777
}
7878

7979
part.matches(VIEW_VALUE_REGEX) ->
8080
if (!name.matches(VIEW_VALUE_REGEX)) {
81-
makeContextReport(value = VIEW_VALUE, node)
81+
makeContextReport(context, VIEW_VALUE, node)
8282
}
8383

8484
part.matches(SERVICE_VALUE_REGEX) ->
8585
if (!name.matches(SERVICE_VALUE_REGEX)) {
86-
makeContextReport(value = SERVICE_VALUE, node)
86+
makeContextReport(context, SERVICE_VALUE, node)
8787
}
8888

8989
part.matches(PRESENTER_VALUE_REGEX) ->
9090
if (!name.matches(PRESENTER_VALUE_REGEX)) {
91-
makeContextReport(value = PRESENTER_VALUE, node)
91+
makeContextReport(context, PRESENTER_VALUE, node)
9292
}
9393

9494
part.matches(PROVIDER_VALUE_REGEX) ->
9595
if (!name.matches(PROVIDER_VALUE_REGEX)) {
96-
makeContextReport(value = PROVIDER_VALUE, node)
96+
makeContextReport(context, PROVIDER_VALUE, node)
9797
}
98-
9998
}
10099
}
101100

102101
}
103-
104-
private fun makeContextReport(value: String, node: UClass) {
105-
return context.report(
106-
ISSUE,
107-
node,
108-
context.getNameLocation(node),
109-
"$REPORT_MESSAGE_BEGIN $value.\n${ISSUE.getExplanation(TextFormat.TEXT)}"
110-
)
111-
}
112102
}
113103
}
104+
105+
private fun makeContextReport(context: JavaContext, value: String, node: UClass) {
106+
return context.report(
107+
ISSUE,
108+
node,
109+
context.getNameLocation(node),
110+
"$REPORT_MESSAGE_BEGIN $value.\n${ISSUE.getExplanation(TextFormat.TEXT)}"
111+
)
112+
}
114113
}

0 commit comments

Comments
 (0)