-
Notifications
You must be signed in to change notification settings - Fork 501
/
TrailingCommaOnDeclarationSiteRule.kt
425 lines (388 loc) · 17.1 KB
/
TrailingCommaOnDeclarationSiteRule.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package com.pinterest.ktlint.ruleset.standard
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.api.EditorConfigProperties
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.containsLineBreakInRange
import com.pinterest.ktlint.core.ast.lineIndent
import com.pinterest.ktlint.core.ast.lineNumber
import com.pinterest.ktlint.core.ast.prevCodeLeaf
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.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.psi.KtEnumEntry
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtValueArgumentList
import org.jetbrains.kotlin.psi.KtWhenEntry
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
import org.jetbrains.kotlin.utils.addToStdlib.cast
/**
* Linting trailing comma for declaration site.
*
* @see [Kotlin Style Guide](https://kotlinlang.org/docs/coding-conventions.html#trailing-commas)
*/
public class TrailingCommaOnDeclarationSiteRule :
Rule(
id = "trailing-comma-on-declaration-site",
visitorModifiers = setOf(
VisitorModifier.RunAfterRule(
ruleId = "standard:indent",
loadOnlyWhenOtherRuleIsLoaded = true,
runOnlyWhenOtherRuleIsEnabled = true
),
VisitorModifier.RunAsLateAsPossible
)
),
UsesEditorConfigProperties {
override val editorConfigProperties: List<UsesEditorConfigProperties.EditorConfigProperty<*>> = listOf(
allowTrailingCommaProperty
)
private var allowTrailingComma by Delegates.notNull<Boolean>()
private fun ASTNode.isTrailingCommaAllowed() =
elementType in TYPES_ON_DECLARATION_SITE && allowTrailingComma
override fun beforeFirstNode(editorConfigProperties: EditorConfigProperties) {
allowTrailingComma = editorConfigProperties.getEditorConfigValue(allowTrailingCommaProperty)
}
override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
// 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
when (node.elementType) {
ElementType.CLASS -> visitClass(node, emit, autoCorrect)
ElementType.DESTRUCTURING_DECLARATION -> visitDestructuringDeclaration(node, autoCorrect, emit)
ElementType.FUNCTION_LITERAL -> visitFunctionLiteral(node, autoCorrect, emit)
ElementType.TYPE_PARAMETER_LIST -> visitTypeList(node, autoCorrect, emit)
ElementType.VALUE_PARAMETER_LIST -> visitValueList(node, autoCorrect, emit)
ElementType.WHEN_ENTRY -> visitWhenEntry(node, autoCorrect, emit)
else -> Unit
}
}
private fun visitDestructuringDeclaration(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val inspectNode = node
.children()
.last { it.elementType == ElementType.RPAR }
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = inspectNode,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit
)
}
private fun visitFunctionLiteral(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val inspectNode = node
.children()
.lastOrNull { it.elementType == ElementType.ARROW }
?: // lambda w/o an arrow -> no arguments -> no commas
return
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = inspectNode,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit
)
}
private fun visitValueList(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node.treeParent.elementType != ElementType.FUNCTION_LITERAL) {
node
.children()
.lastOrNull { it.elementType == ElementType.RPAR }
?.let { inspectNode ->
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = inspectNode,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit
)
}
}
}
private fun visitTypeList(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val inspectNode = node
.children()
.first { it.elementType == ElementType.GT }
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = inspectNode,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit
)
}
private fun visitWhenEntry(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val psi = node.psi
require(psi is KtWhenEntry)
if (psi.isElse || psi.parent.cast<KtWhenExpression>().leftParenthesis == null) {
// no commas for "else" or when there are no opening parenthesis for the when-expression
return
}
val inspectNode = node
.children()
.first { it.elementType == ElementType.ARROW }
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = inspectNode,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit
)
}
private fun visitClass(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
autoCorrect: Boolean
) {
val psi = node.psi
require(psi is KtClass)
node
.takeIf { psi.isEnum() }
?.findChildByType(ElementType.CLASS_BODY)
?.takeUnless {
// Do nothing when last two entries are on same line as no trailing comma should be inserted
it.lastTwoEnumEntriesAreOnSameLine()
}?.let { classBody ->
val nodeAfterTrailingCommaPosition = classBody.findNodeAfterTrailingCommaPosition()
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = nodeAfterTrailingCommaPosition,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit
)
}
}
/**
* Determines the [ASTNode] before which the trailing comma is allowed.
*
* If the list of enumeration entries is terminated by a semicolon, that semicolon will be returned. Otherwise, the
* last element of the class.
*/
private fun ASTNode.findNodeAfterTrailingCommaPosition(): ASTNode {
val lastEnumEntry = children().last { it.psi is KtEnumEntry }
val semicolonAfterLastEnumEntry = lastEnumEntry
.children()
.singleOrNull { it.elementType == ElementType.SEMICOLON }
return semicolonAfterLastEnumEntry ?: lastChildNode
}
private fun ASTNode.lastTwoEnumEntriesAreOnSameLine(): Boolean {
val lastTwoEnumEntries =
children()
.filter { it.psi is KtEnumEntry }
.toList()
.takeLast(2)
return lastTwoEnumEntries.count() == 2 && lastTwoEnumEntries[0].lineNumber() == lastTwoEnumEntries[1].lineNumber()
}
private fun ASTNode.reportAndCorrectTrailingCommaNodeBefore(
inspectNode: ASTNode,
isTrailingCommaAllowed: Boolean,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val prevLeaf = inspectNode.prevLeaf()
val trailingCommaNode = prevLeaf.findPreviousTrailingCommaNodeOrNull()
val trailingCommaState = when {
isMultiline(psi) -> if (trailingCommaNode != null) TrailingCommaState.EXISTS else TrailingCommaState.MISSING
else -> if (trailingCommaNode != null) TrailingCommaState.REDUNDANT else TrailingCommaState.NOT_EXISTS
}
when (trailingCommaState) {
TrailingCommaState.EXISTS -> if (!isTrailingCommaAllowed) {
emit(
trailingCommaNode!!.startOffset,
"Unnecessary trailing comma before \"${inspectNode.text}\"",
true
)
if (autoCorrect) {
this.removeChild(trailingCommaNode)
}
}
TrailingCommaState.MISSING -> if (isTrailingCommaAllowed) {
val addNewLineBeforeArrowInWhenEntry = addNewLineBeforeArrowInWhen()
val prevNode = inspectNode.prevCodeLeaf()!!
if (addNewLineBeforeArrowInWhenEntry) {
emit(
prevNode.startOffset + prevNode.textLength,
"Missing trailing comma and newline before \"${inspectNode.text}\"",
true
)
} else {
emit(
prevNode.startOffset + prevNode.textLength,
"Missing trailing comma before \"${inspectNode.text}\"",
true
)
}
if (autoCorrect) {
if (addNewLineBeforeArrowInWhenEntry) {
val parentIndent = "\n" + prevNode.getWhenEntryIndent()
val leafBeforeArrow = (psi as KtWhenEntry).arrow?.prevLeaf()
if (leafBeforeArrow != null && leafBeforeArrow is PsiWhiteSpace) {
val newLine = KtPsiFactory(prevNode.psi).createWhiteSpace(parentIndent)
leafBeforeArrow.replace(newLine)
} else {
val newLine = KtPsiFactory(prevNode.psi).createWhiteSpace(parentIndent)
prevNode.psi.parent.addAfter(newLine, prevNode.psi)
}
}
if (inspectNode.treeParent.elementType == ElementType.ENUM_ENTRY) {
with(KtPsiFactory(prevNode.psi)) {
val parentIndent = (prevNode.psi.parent.prevLeaf() as? PsiWhiteSpace)?.text ?: "\n"
val newline = createWhiteSpace(parentIndent)
val enumEntry = inspectNode.treeParent.psi
enumEntry.apply {
add(newline)
removeChild(inspectNode)
parent.addAfter(createSemicolon(), this)
}
}
}
val comma = KtPsiFactory(prevNode.psi).createComma()
prevNode.psi.parent.addAfter(comma, prevNode.psi)
}
}
TrailingCommaState.REDUNDANT -> {
emit(
trailingCommaNode!!.startOffset,
"Unnecessary trailing comma before \"${inspectNode.text}\"",
true
)
if (autoCorrect) {
this.removeChild(trailingCommaNode)
}
}
TrailingCommaState.NOT_EXISTS -> Unit
}
}
private fun ASTNode.getWhenEntryIndent() =
// The when entry can be a simple value but might also be a complex expression.
parents()
.last { it.elementType == ElementType.WHEN_ENTRY }
.lineIndent()
private fun isMultiline(element: PsiElement): Boolean = when {
element.parent is KtFunctionLiteral -> isMultiline(element.parent)
element is KtFunctionLiteral -> containsLineBreakInRange(element.valueParameterList!!, element.arrow!!)
element is KtWhenEntry -> containsLineBreakInRange(element.firstChild, element.arrow!!)
element is KtDestructuringDeclaration -> containsLineBreakInRange(element.lPar!!, element.rPar!!)
element is KtValueArgumentList && element.children.size == 1 && element.anyDescendantOfType<KtCollectionLiteralExpression>() -> {
// special handling for collection literal
// @Annotation([
// "something",
// ])
val lastChild = element.collectDescendantsOfType<KtCollectionLiteralExpression>().last()
containsLineBreakInLeavesRange(lastChild.rightBracket!!, element.rightParenthesis!!)
}
else -> element.textContains('\n')
}
private fun ASTNode.addNewLineBeforeArrowInWhen() =
if (psi is KtWhenEntry) {
val leafBeforeArrow = (psi as KtWhenEntry).arrow?.prevLeaf()
!(leafBeforeArrow is PsiWhiteSpace && leafBeforeArrow.textContains('\n'))
} else {
false
}
private fun ASTNode?.findPreviousTrailingCommaNodeOrNull(): ASTNode? {
var node = this
while (node?.isIgnorable() == true) {
node = node.prevLeaf()
}
return if (node?.elementType == ElementType.COMMA) {
node
} else {
null
}
}
private fun containsLineBreakInLeavesRange(from: PsiElement, to: PsiElement): Boolean {
var leaf: PsiElement? = from
while (leaf != null && !leaf.isEquivalentTo(to)) {
if (leaf.textContains('\n')) {
return true
}
leaf = leaf.nextLeaf(skipEmptyElements = false)
}
return leaf?.textContains('\n') ?: false
}
private fun ASTNode.isIgnorable(): Boolean =
elementType == ElementType.WHITE_SPACE ||
elementType == ElementType.EOL_COMMENT ||
elementType == ElementType.BLOCK_COMMENT
private enum class TrailingCommaState {
/**
* The trailing comma is needed and exists
*/
EXISTS,
/**
* The trailing comma is needed and doesn't exist
*/
MISSING,
/**
* The trailing comma isn't needed and doesn't exist
*/
NOT_EXISTS,
/**
* The trailing comma isn't needed, but exists
*/
REDUNDANT
;
}
public companion object {
internal const val ALLOW_TRAILING_COMMA_NAME = "ij_kotlin_allow_trailing_comma"
private const val ALLOW_TRAILING_COMMA_DESCRIPTION = "Defines whether a trailing comma (or no trailing comma)" +
"should be enforced on the defining side," +
"e.g. parameter-list, type-argument-list, lambda-value-parameters, enum-entries, etc."
private val BOOLEAN_VALUES_SET = setOf("true", "false")
// TODO: Rename property to trailingCommaOnDeclarationSite. The word 'allow' is misleading as the comma is
// enforced when the property is enabled and prohibited when disabled.
public val allowTrailingCommaProperty: UsesEditorConfigProperties.EditorConfigProperty<Boolean> =
UsesEditorConfigProperties.EditorConfigProperty(
type = PropertyType.LowerCasingPropertyType(
ALLOW_TRAILING_COMMA_NAME,
ALLOW_TRAILING_COMMA_DESCRIPTION,
PropertyValueParser.BOOLEAN_VALUE_PARSER,
BOOLEAN_VALUES_SET
),
defaultValue = false
)
private val TYPES_ON_DECLARATION_SITE = TokenSet.create(
ElementType.CLASS,
ElementType.DESTRUCTURING_DECLARATION,
ElementType.FUNCTION_LITERAL,
ElementType.FUNCTION_TYPE,
ElementType.TYPE_PARAMETER_LIST,
ElementType.VALUE_PARAMETER_LIST,
ElementType.WHEN_ENTRY
)
}
}