Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper in kts file #735

Merged
merged 12 commits into from
Feb 18, 2021
4 changes: 1 addition & 3 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,4 @@
enabled: true
# Check if kts script contains other functions except run code
- name: RUN_IN_SCRIPT
enabled: true
configuration:
possibleWrapper: ""
enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ import org.cqfn.diktat.ruleset.constants.Warnings.RUN_IN_SCRIPT
import org.cqfn.diktat.ruleset.utils.*

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_ARGUMENT
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.PARENTHESIZED
import com.pinterest.ktlint.core.ast.ElementType.SCRIPT_INITIALIZER
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement
import java.nio.file.Paths

/**
* Rule that checks if kts script contains other functions except run code
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
* In .kts files allow use only property declaration, function, classes, and code inside `run` block
* In gradle.kts files allow to call expression and dot qualified expression in addition to everything used in .kts files
*/
class RunInScript(private val configRules: List<RulesConfig>) : Rule("run-script") {
private var isFixMode: Boolean = false
Expand All @@ -31,7 +37,31 @@ class RunInScript(private val configRules: List<RulesConfig>) : Rule("run-script
emitWarn = emit

if (node.elementType == SCRIPT_INITIALIZER && node.getRootNode().getFilePath().isKotlinScript()) {
checkScript(node)
node.getRootNode().getFilePath().split(".").run {
if (this.size > 2 && this[this.size-2] == "gradle") {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
checkGradleNode(node)
} else {
checkScript(node)
}
}
}
}

private fun checkGradleNode(node: ASTNode) {
val astNode = if (node.firstChildNode.elementType == PARENTHESIZED) {
node.firstChildNode
} else {
node
}
if (!astNode.hasChildOfType(CALL_EXPRESSION) && !astNode.hasChildOfType(DOT_QUALIFIED_EXPRESSION)) {
RUN_IN_SCRIPT.warnAndFix(configRules, emitWarn, isFixMode, astNode.text, astNode.startOffset, astNode) {
val parent = astNode.treeParent
val newNode = KotlinParser().createNode("run {\n ${astNode.text}\n} \n")
val newScript = CompositeElement(SCRIPT_INITIALIZER)
parent.addChild(newScript, astNode)
newScript.addChild(newNode)
parent.removeChild(astNode)
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,4 @@
enabled: true
# Check if kts script contains other functions except run code
- name: RUN_IN_SCRIPT
enabled: true
configuration:
possibleWrapper: ""
enabled: true
4 changes: 1 addition & 3 deletions diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,4 @@
enabled: true
# Check if kts script contains other functions except run code
- name: RUN_IN_SCRIPT
enabled: true
configuration:
possibleWrapper: ""
enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,44 @@ class RunInScriptWarnTest : LintTestBase(::RunInScript) {
fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts"
)
}

@Test
@Tag(WarningNames.RUN_IN_SCRIPT)
fun `check gradle file`() {
lintMethod(
"""
class A {}

fun foo() {
}

if(true) {
goo()
}

diktat {}

diktat({})

foo/*df*/()

foo( //dfdg
10
)
println("hello")

w.map { it -> it }

(tasks.register("a") {
dependsOn("b")
doFirst {
generateCodeStyle(file("rootDir/guide"), file("rootDir/../wp"))
}
})

""".trimMargin(),
LintError(6, 17, ruleId, "${RUN_IN_SCRIPT.warnText()} if(true) {...", true),
fileName = "src/main/kotlin/org/cqfn/diktat/builds.gradle.kts"
)
}
}