Skip to content

Commit

Permalink
Fixes #369
Browse files Browse the repository at this point in the history
* When checking for unexpected spaces preceding a `(`, the rule was only checking elementType `IDENTIFIER`, so added a check for `SUPER_KEYWORD`
* Updated lint and formatting tests
  • Loading branch information
shashachu committed Apr 9, 2019
1 parent 2cecdde commit 3ad4294
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import com.github.shyiko.ktlint.core.Rule
import com.github.shyiko.ktlint.core.ast.ElementType.IDENTIFIER
import com.github.shyiko.ktlint.core.ast.ElementType.LPAR
import com.github.shyiko.ktlint.core.ast.ElementType.RPAR
import com.github.shyiko.ktlint.core.ast.ElementType.SUPER_KEYWORD
import com.github.shyiko.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST
import com.github.shyiko.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST
import com.github.shyiko.ktlint.core.ast.nextLeaf
import com.github.shyiko.ktlint.core.ast.prevLeaf
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace

/**
* Ensures there are no extra spaces around parentheses.
*
* See https://kotlinlang.org/docs/reference/coding-conventions.html, "Horizontal Whitespace"
*/
class SpacingAroundParensRule : Rule("paren-spacing") {

override fun visit(
Expand All @@ -23,7 +29,9 @@ class SpacingAroundParensRule : Rule("paren-spacing") {
val nextLeaf = node.nextLeaf()
val spacingBefore = if (node.elementType == LPAR) {
prevLeaf is PsiWhiteSpace && !prevLeaf.textContains('\n') &&
prevLeaf.prevLeaf()?.elementType == IDENTIFIER && (
(prevLeaf.prevLeaf()?.elementType == IDENTIFIER ||
// Super keyword needs special-casing
prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD) && (
node.treeParent?.elementType == VALUE_PARAMETER_LIST ||
node.treeParent?.elementType == VALUE_ARGUMENT_LIST
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fun main() {
super()
val a = ((1 + 2) / 3)
fn((1 + 2) / 3)
fn((1 + 2) / 3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fun main() {
super ()
val a = ( (1 + 2) / 3 )
fn( (1 + 2) / 3)
fn ((1 + 2) / 3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fun main() {
super ()
val a = ( (1 + 2) / 3 )
fn( (1 + 2) / 3)
fn ((1 + 2) / 3)
Expand All @@ -19,10 +20,11 @@ fun main() {
}

// expect
// 2:14:Unexpected spacing after "("
// 2:26:Unexpected spacing before ")"
// 3:8:Unexpected spacing after "("
// 4:7:Unexpected spacing before "("
// 5:8:Unexpected spacing after "("
// 6:16:Unexpected spacing before "("
// 7:8:Unexpected spacing after "("
// 2:10:Unexpected spacing before "("
// 3:14:Unexpected spacing after "("
// 3:26:Unexpected spacing before ")"
// 4:8:Unexpected spacing after "("
// 5:7:Unexpected spacing before "("
// 6:8:Unexpected spacing after "("
// 7:16:Unexpected spacing before "("
// 8:8:Unexpected spacing after "("

0 comments on commit 3ad4294

Please sign in to comment.