Got rid of "it" in multiline lambda's
diff --git a/ktlint-core/src/main/kotlin/com/github/shyiko/ktlint/core/KtLint.kt b/ktlint-core/src/main/kotlin/com/github/shyiko/ktlint/core/KtLint.kt index 02f30cb..1b1ea2d 100644 --- a/ktlint-core/src/main/kotlin/com/github/shyiko/ktlint/core/KtLint.kt +++ b/ktlint-core/src/main/kotlin/com/github/shyiko/ktlint/core/KtLint.kt
@@ -133,8 +133,7 @@ val isSuppressed = calculateSuppressedRegions(rootNode) val r = flatten(ruleSets) rootNode.visit { node -> - r.forEach { - val (id, rule) = it + r.forEach { (id, rule) -> if (!isSuppressed(node.startOffset, id)) { try { rule.visit(node, false) { offset, errorMessage, _ -> @@ -179,10 +178,10 @@ } private fun calculateSuppressedRegions(rootNode: ASTNode) = - SuppressionHint.collect(rootNode).let { - if (it.isEmpty()) nullSuppression else { offset, ruleId -> - it.any { (it.disabledRules.isEmpty() || it.disabledRules.contains(ruleId)) && - it.range.contains(offset) } + SuppressionHint.collect(rootNode).let { listOfHints -> + if (listOfHints.isEmpty()) nullSuppression else { offset, ruleId -> + listOfHints.any { (range, disabledRules) -> + (disabledRules.isEmpty() || disabledRules.contains(ruleId)) && range.contains(offset) } } } @@ -243,8 +242,7 @@ val r = flatten(ruleSets) var autoCorrect = false rootNode.visit { node -> - r.forEach { - val (id, rule) = it + r.forEach { (id, rule) -> if (!isSuppressed(node.startOffset, id)) { try { rule.visit(node, false) { offset, errorMessage, canBeAutoCorrected -> @@ -263,8 +261,7 @@ } if (autoCorrect) { rootNode.visit { node -> - r.forEach { - val (id, rule) = it + r.forEach { (id, rule) -> if (!isSuppressed(node.startOffset, id)) { try { rule.visit(node, true) { _, _, canBeAutoCorrected -> @@ -401,8 +398,8 @@ if (this is PsiErrorElement) { return this } - this.children.forEach { - val errorElement = it.findErrorElement() + this.children.forEach { child -> + val errorElement = child.findErrorElement() if (errorElement != null) { return errorElement }
diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/IndentationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/IndentationRule.kt index db435e9..87b833a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/IndentationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/IndentationRule.kt
@@ -35,9 +35,9 @@ return } if (node is PsiWhiteSpace && !node.isPartOf(PsiComment::class)) { - val split = node.getText().split("\n") - if (split.size > 1) { - var offset = node.startOffset + split.first().length + 1 + val lines = node.getText().split("\n") + if (lines.size > 1) { + var offset = node.startOffset + lines.first().length + 1 val firstParameterColumn = lazy { val firstParameter = PsiTreeUtil.findChildOfType( node.getNonStrictParentOfType(KtParameterList::class.java), @@ -48,19 +48,19 @@ TextRange(startOffset, startOffset)).column } ?: 0 } - split.tail().forEach { - if (it.length % indent != 0) { + lines.tail().forEach { line -> + if (line.length % indent != 0) { if (node.isPartOf(KtParameterList::class) && firstParameterColumn.value != 0) { - if (firstParameterColumn.value - 1 != it.length) { - emit(offset, "Unexpected indentation (${it.length}) (" + + if (firstParameterColumn.value - 1 != line.length) { + emit(offset, "Unexpected indentation (${line.length}) (" + "parameters should be either vertically aligned or indented by the multiple of $indent" + ")", false) } } else { - emit(offset, "Unexpected indentation (${it.length}) (it should be multiple of $indent)", false) + emit(offset, "Unexpected indentation (${line.length}) (it should be multiple of $indent)", false) } } - offset += it.length + 1 + offset += line.length + 1 } } }
diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoTrailingSpacesRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoTrailingSpacesRule.kt index d04582a..8cdd988 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoTrailingSpacesRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/NoTrailingSpacesRule.kt
@@ -11,30 +11,30 @@ override fun visit(node: ASTNode, autoCorrect: Boolean, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { if (node is PsiWhiteSpace) { - val split = node.getText().split("\n") - if (split.size > 1) { - checkForTrailingSpaces(split.head(), node.startOffset, emit) + val lines = node.getText().split("\n") + if (lines.size > 1) { + checkForTrailingSpaces(lines.head(), node.startOffset, emit) if (autoCorrect) { - (node as LeafPsiElement).replaceWithText("\n".repeat(split.size - 1) + split.last()) + (node as LeafPsiElement).replaceWithText("\n".repeat(lines.size - 1) + lines.last()) } } else if (PsiTreeUtil.nextLeaf(node) == null /* eof */) { - checkForTrailingSpaces(split, node.startOffset, emit) + checkForTrailingSpaces(lines, node.startOffset, emit) if (autoCorrect) { - (node as LeafPsiElement).replaceWithText("\n".repeat(split.size - 1)) + (node as LeafPsiElement).replaceWithText("\n".repeat(lines.size - 1)) } } } } - private fun checkForTrailingSpaces(split: List<String>, offset: Int, + private fun checkForTrailingSpaces(lines: List<String>, offset: Int, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) { var violationOffset = offset - return split.forEach { - if (!it.isEmpty()) { + return lines.forEach { line -> + if (!line.isEmpty()) { emit(violationOffset, "Trailing space(s)", true) } - violationOffset += it.length + 1 + violationOffset += line.length + 1 } }
diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/package-test.kt b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/package-test.kt index 3140fa5..016cbd4 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/package-test.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/package-test.kt
@@ -19,14 +19,14 @@ throw RuntimeException("$resource must contain '// expect' line") } val input = resourceText.substring(0, dividerIndex) - val errors = resourceText.substring(dividerIndex + 1).split('\n').mapNotNull { - if (it.isBlank() || it == "// expect") null else - it.trimMargin("// ").split(':', limit = 3).let { - if (it.size != 3) { + val errors = resourceText.substring(dividerIndex + 1).split('\n').mapNotNull { line -> + if (line.isBlank() || line == "// expect") null else + line.trimMargin("// ").split(':', limit = 3).let { expectation -> + if (expectation.size != 3) { throw RuntimeException("$resource expectation must be a triple <line>:<column>:<message>") // " (<message> is not allowed to contain \":\")") } - LintError(it[0].toInt(), it[1].toInt(), rule.id, it[2]) + LintError(expectation[0].toInt(), expectation[1].toInt(), rule.id, expectation[2]) } } assertThat(rule.lint(input, userData)).isEqualTo(errors)
diff --git a/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt b/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt index 4e50c51..2911790 100644 --- a/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt +++ b/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt
@@ -10,11 +10,11 @@ val res = ArrayList<LintError>() val debug = debugAST() KtLint.lint(text, (if (debug) listOf(RuleSet("debug", DumpAST())) else emptyList()) + - listOf(RuleSet("standard", this@lint)), userData) { + listOf(RuleSet("standard", this@lint)), userData) { e -> if (debug) { System.err.println("^^ lint error") } - res.add(it) + res.add(e) } return res }
diff --git a/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt b/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt index 63f862b..286e601 100644 --- a/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt +++ b/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt
@@ -145,9 +145,9 @@ @JvmStatic fun main(args: Array<String>) { - args.forEach { - if (it.startsWith("--") && it.contains("=")) { - val flag = it.substringBefore("=") + args.forEach { arg -> + if (arg.startsWith("--") && arg.contains("=")) { + val flag = arg.substringBefore("=") val alt = DEPRECATED_FLAGS[flag] if (alt != null) { System.err.println("$flag flag is deprecated and will be removed in 1.0.0 (use $alt instead)") @@ -188,8 +188,8 @@ rp.forEach { System.err.println("[DEBUG] Discovered ruleset \"${it.first}\"") } } data class R(val id: String, val config: Map<String, String>, var output: String?) - val rr = this.reporters.map { - val split = it.split(",") + val rr = this.reporters.map { reporter -> + val split = reporter.split(",") val (reporterId, rawReporterConfig) = split[0].split("?", limit = 2) + listOf("") R(reporterId, mapOf("verbose" to verbose.toString()) + parseQuery(rawReporterConfig), split.getOrNull(1)) @@ -231,11 +231,11 @@ } }.toTypedArray()) // load .editorconfig - val userData = locateEditorConfig(File(workDir))?.let { + val userData = locateEditorConfig(File(workDir))?.let { editoConfigFile -> if (debug) { - System.err.println("[DEBUG] Discovered .editorconfig (${it.parent})") + System.err.println("[DEBUG] Discovered .editorconfig (${editoConfigFile.parent})") } - loadEditorConfig(it) + loadEditorConfig(editoConfigFile) } ?: emptyMap() if (debug) { System.err.println("[DEBUG] ${userData.mapKeys { it.key }} loaded from .editorconfig") @@ -363,13 +363,13 @@ CHECKSUM_POLICY_IGNORE)).build(), RemoteRepository.Builder( "jitpack", "default", "http://jitpack.io").build() - ) + repositories.map { - val colon = it.indexOf("=").apply { - if (this == -1) { throw RuntimeException("$it is not a valid repository entry " + + ) + repositories.map { repository -> + val colon = repository.indexOf("=").apply { + if (this == -1) { throw RuntimeException("$repository is not a valid repository entry " + "(make sure it's provided as <id>=<url>") } } - val id = it.substring(0, colon) - val url = it.substring(colon + 1) + val id = repository.substring(0, colon) + val url = repository.substring(colon + 1) RemoteRepository.Builder(id, "default", url).build() }, forceUpdate @@ -385,16 +385,16 @@ fun loadJARs(dependencyResolver: MavenDependencyResolver, artifacts: List<String>) { (ClassLoader.getSystemClassLoader() as java.net.URLClassLoader) - .addURLs(artifacts.flatMap { + .addURLs(artifacts.flatMap { artifact -> if (debug) { - System.err.println("[DEBUG] Resolving $it") + System.err.println("[DEBUG] Resolving $artifact") } val result = try { - dependencyResolver.resolve(DefaultArtifact(it)).map { it.toURI().toURL() } + dependencyResolver.resolve(DefaultArtifact(artifact)).map { it.toURI().toURL() } } catch (e: IllegalArgumentException) { - val file = File(expandTilde(it)) + val file = File(expandTilde(artifact)) if (!file.exists()) { - System.err.println("Error: $it does not exist") + System.err.println("Error: $artifact does not exist") exitProcess(1) } listOf(file.toURI().toURL()) @@ -402,7 +402,7 @@ if (debug) { e.printStackTrace() } - System.err.println("Error: $it wasn't found") + System.err.println("Error: $artifact wasn't found") exitProcess(1) } if (debug) { @@ -415,8 +415,8 @@ fun parseQuery(query: String) = query.split("&") .fold(HashMap<String, String>()) { map, s -> if (!s.isEmpty()) { - s.split("=", limit = 2).let { map.put(it[0], - URLDecoder.decode(it.getOrElse(1) { "true" }, "UTF-8")) } + s.split("=", limit = 2).let { e -> map.put(e[0], + URLDecoder.decode(e.getOrElse(1) { "true" }, "UTF-8")) } } map }