forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoTrailingCommaRule.kt
260 lines (239 loc) · 10.5 KB
/
NoTrailingCommaRule.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.pinterest.ktlint.ruleset.experimental
import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.assertElementType
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.isRoot
import com.pinterest.ktlint.core.ast.prevLeaf
import kotlin.properties.Delegates
import org.ec4j.core.model.PropertyType
import org.ec4j.core.model.PropertyType.PropertyValueParser
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.psiUtil.endOffset
@OptIn(FeatureInAlphaState::class)
class NoTrailingCommaRule :
Rule("no-trailing-comma"),
UsesEditorConfigProperties {
private var allowTrailingComma by Delegates.notNull<Boolean>()
private var allowTrailingCommaOnCallSite by Delegates.notNull<Boolean>()
override val editorConfigProperties: List<UsesEditorConfigProperties.EditorConfigProperty<*>> = listOf(
ijKotlinAllowTrailingCommaEditorConfigProperty,
ijKotlinAllowTrailingCommaOnCallSiteEditorConfigProperty,
)
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node.isRoot()) {
getEditorConfigValues(node)
return
}
// Keep processing of element types in sync with Intellij Kotlin formatting settings.
// https://github.com/JetBrains/intellij-kotlin/blob/master/formatter/src/org/jetbrains/kotlin/idea/formatter/trailingComma/util.kt
if (!allowTrailingComma) {
when (node.elementType) {
ElementType.DESTRUCTURING_DECLARATION -> visitDestructuringDeclaration(node, emit, autoCorrect)
ElementType.FUNCTION_LITERAL -> visitFunctionLiteral(node, emit, autoCorrect)
ElementType.FUNCTION_TYPE -> visitFunctionType(node, emit, autoCorrect)
ElementType.TYPE_PARAMETER_LIST -> visitTypeList(node, emit, autoCorrect)
ElementType.VALUE_PARAMETER_LIST -> visitValueList(node, emit, autoCorrect)
ElementType.WHEN_ENTRY -> visitWhenEntry(node, emit, autoCorrect)
else -> Unit
}
}
if (!allowTrailingCommaOnCallSite) {
when (node.elementType) {
ElementType.COLLECTION_LITERAL_EXPRESSION -> visitCollectionLiteralExpression(node, emit, autoCorrect)
ElementType.INDICES -> visitIndices(node, emit, autoCorrect)
ElementType.TYPE_ARGUMENT_LIST -> visitTypeList(node, emit, autoCorrect)
ElementType.VALUE_ARGUMENT_LIST -> visitValueList(node, emit, autoCorrect)
else -> Unit
}
}
}
private fun getEditorConfigValues(node: ASTNode) {
val android = node.getUserData(KtLint.ANDROID_USER_DATA_KEY) ?: false
val editorConfig = node.getUserData(KtLint.EDITOR_CONFIG_PROPERTIES_USER_DATA_KEY)!!
allowTrailingComma =
editorConfig.getEditorConfigValue(ijKotlinAllowTrailingCommaEditorConfigProperty, android)
allowTrailingCommaOnCallSite =
editorConfig.getEditorConfigValue(ijKotlinAllowTrailingCommaOnCallSiteEditorConfigProperty, android)
}
private fun visitCollectionLiteralExpression(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.COLLECTION_LITERAL_EXPRESSION)
.children()
.last { it.elementType == ElementType.RBRACKET }
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
private fun visitDestructuringDeclaration(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.DESTRUCTURING_DECLARATION)
.children()
.last { it.elementType == ElementType.RPAR }
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
private fun visitFunctionLiteral(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.FUNCTION_LITERAL)
.children()
.lastOrNull() { it.elementType == ElementType.ARROW }
?.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
private fun visitFunctionType(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.FUNCTION_TYPE)
.firstChildNode
.assertElementType(ElementType.VALUE_PARAMETER_LIST)
.children()
.last()
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
private fun visitIndices(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.INDICES)
.children()
.last { it.elementType == ElementType.RBRACKET }
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
private fun visitValueList(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
if (node.treeParent.elementType != ElementType.FUNCTION_LITERAL &&
node.treeParent.elementType != ElementType.FUNCTION_TYPE
) {
val inspectNode = node
.assertElementType(ElementType.VALUE_ARGUMENT_LIST, ElementType.VALUE_PARAMETER_LIST)
.children()
.last { it.elementType == ElementType.RPAR }
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
}
private fun visitTypeList(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.TYPE_ARGUMENT_LIST, ElementType.TYPE_PARAMETER_LIST)
.children()
.first { it.elementType == ElementType.GT }
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
private fun visitWhenEntry(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val inspectNode = node
.assertElementType(ElementType.WHEN_ENTRY)
.children()
.first { it.elementType == ElementType.ARROW }
.prevLeaf()
node.reportAndOrCorrectTrailingCommaNodeBefore(inspectNode, emit, autoCorrect)
}
private fun ASTNode.reportAndOrCorrectTrailingCommaNodeBefore(
inspectNode: ASTNode?,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
inspectNode
.findPreviousTrailingCommaNodeOrNull()
?.let { trailingCommaNode ->
emit(trailingCommaNode.psi.endOffset - 1, "Trailing comma is redundant", true)
if (autoCorrect) {
this.removeChild(trailingCommaNode)
}
}
}
private fun ASTNode?.findPreviousTrailingCommaNodeOrNull(): ASTNode? {
var node = this
while (node != null && node.isIgnorable()) {
node = node.prevLeaf()
}
return if (node != null && node.elementType == ElementType.COMMA) {
node
} else {
null
}
}
private fun ASTNode.isIgnorable(): Boolean =
elementType == ElementType.WHITE_SPACE ||
elementType == ElementType.EOL_COMMENT ||
elementType == ElementType.BLOCK_COMMENT
internal companion object {
/** A parser for kotlin boolean values `true` and `false` */
private var KOTLIN_BOOLEAN_VALUE_PARSER: PropertyValueParser<Boolean> =
PropertyValueParser { name, value ->
when {
value == null -> PropertyType.PropertyValue.invalid(
null,
"Property '$name' expects a boolean; found: null"
)
value.equals("true", ignoreCase = true) -> PropertyType.PropertyValue.valid(value, true)
value.equals("false", ignoreCase = true) -> PropertyType.PropertyValue.valid(value, false)
else -> PropertyType.PropertyValue.invalid(
value,
"Property '$name' expects a boolean. The parsed '$value' is not a boolean."
)
}
}
internal val ijKotlinAllowTrailingCommaEditorConfigProperty =
UsesEditorConfigProperties.EditorConfigProperty<Boolean>(
type = PropertyType(
"ij_kotlin_allow_trailing_comma",
"ij_kotlin_allow_trailing_comma description",
KOTLIN_BOOLEAN_VALUE_PARSER,
setOf("true", "false")
),
defaultValue = false,
defaultAndroidValue = false,
propertyWriter = { it.toString() }
)
internal val ijKotlinAllowTrailingCommaOnCallSiteEditorConfigProperty =
UsesEditorConfigProperties.EditorConfigProperty<Boolean>(
type = PropertyType(
"ij_kotlin_allow_trailing_comma_on_call_site",
"ij_kotlin_allow_trailing_comma_on_call_site description",
KOTLIN_BOOLEAN_VALUE_PARSER,
setOf("true", "false")
),
defaultValue = false,
defaultAndroidValue = false,
propertyWriter = { it.toString() }
)
}
}