From 4d2fdf648f5345ea09f5a70d4e55f80b3852bbb8 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Tue, 5 Sep 2023 18:28:36 +0200 Subject: [PATCH 1/4] Fix ktlint cli parameter --code-style Pass parameter correctly to KtlintRuleEngine. Also, deprecate parameters '--code-style', '--disabled-rules' and '--experimental' as those should be specified in the '.editorconfig' properties --- .../ktlint/cli/internal/KtlintCommandLine.kt | 40 ++++++++++++++----- .../com/pinterest/ktlint/cli/SimpleCLITest.kt | 8 ++-- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt index 0b8b0efab5..bc35430add 100644 --- a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt +++ b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt @@ -105,11 +105,13 @@ internal class KtlintCommandLine { scope = CommandLine.ScopeType.INHERIT, names = ["--code-style"], description = [ - "Defines the code style (ktlint_official, intellij_idea or android_studio) to be used for formatting the code. It is " + - "advised to define '.editorconfig' property 'ktlint_code_style'.", + "Defines the code style (ktlint_official, intellij_idea or android_studio) to be used for formatting the code. This option " + + "is deprecated, and will be removed in Ktlint 1.1. The code style has to be defined as '.editorconfig' property " + + "'ktlint_code_style'.", ], converter = [CodeStyleValueConverter::class], ) + @Deprecated("Marked for removal in Ktlint 1.1") var codeStyle: CodeStyleValue? = null @Option( @@ -127,10 +129,12 @@ internal class KtlintCommandLine { @Option( names = ["--disabled_rules"], description = [ - "Comma-separated list of rules to globally disable." + - " To disable standard ktlint rule-set use --disabled_rules=standard", + "Comma-separated list of rules to globally disable. This option is deprecated, and will be removed in Ktlint 1.1. The " + + "disabled rules have to be defined as '.editorconfig' properties. See " + + "https://pinterest.github.io/ktlint/1.0.0/faq/#how-do-i-enable-or-disable-a-rule", ], ) + @Deprecated("Marked for removal in Ktlint 1.1") var disabledRules: String = "" @Option( @@ -202,8 +206,13 @@ internal class KtlintCommandLine { @Option( names = ["--experimental"], - description = ["Enable experimental rules"], + description = [ + "Enable experimental rules. This option is deprecated, and will be removed in Ktlint 1.1. The experimental flag has to be " + + "set as '.editorconfig' property 'ktlint_experimental'. See " + + "https://pinterest.github.io/ktlint/1.0.0/faq/#how-do-i-enable-or-disable-a-rule-set", + ], ) + @Deprecated("Marked for removal in Ktlint 1.1") var experimental: Boolean = false @Option( @@ -246,14 +255,25 @@ internal class KtlintCommandLine { EditorConfigOverride .EMPTY_EDITOR_CONFIG_OVERRIDE .applyIf(experimental) { - logger.debug { "Add editor config override to allow the experimental rule set" } + logger.warn { + "Parameter `--experimental is deprecated, and will be removed in Ktlint 1.1. The experimental flag has to be " + + "set as '.editorconfig' property 'ktlint_experimental = enabled'. See " + + "https://pinterest.github.io/ktlint/1.0.0/faq/#how-do-i-enable-or-disable-a-rule-set" + } plus(EXPERIMENTAL_RULES_EXECUTION_PROPERTY to RuleExecution.enabled) }.applyIf(disabledRules.isNotBlank()) { - logger.debug { "Add editor config override to disable rules: '$disabledRules'" } + logger.warn { + "Parameter `--disabled-rules is deprecated, and will be removed in Ktlint 1.1. The disabled rules have to be " + + "defined as '.editorconfig' properties. See " + + "https://pinterest.github.io/ktlint/1.0.0/faq/#how-do-i-enable-or-disable-a-rule" + } plus(*disabledRulesEditorConfigOverrides()) - }.applyIf(codeStyle == CodeStyleValue.android_studio) { - logger.debug { "Add editor config override to set code style to 'android_studio'" } - plus(CODE_STYLE_PROPERTY to CodeStyleValue.android_studio) + }.applyIf(codeStyle != null) { + logger.warn { + "Parameter `--code-style=${codeStyle?.name} is deprecated. The code style should be defined as '.editorconfig' " + + "property 'ktlint_code_style'." + } + plus(CODE_STYLE_PROPERTY to codeStyle) }.applyIf(stdin) { logger.debug { "Add editor config override to disable 'filename' rule which can not be used in combination with reading from " + diff --git a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt index 9aeafce4af..b81b4fddea 100644 --- a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt +++ b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt @@ -387,12 +387,12 @@ class SimpleCLITest { CommandLineTestRunner(tempDir) .run( "too-many-empty-lines", - listOf("--code-style=ktlint_official", "generateEditorConfig"), + listOf("--code-style=intellij_idea", "generateEditorConfig"), ) { SoftAssertions() .apply { assertNormalExitCode() - assertThat(normalOutput).containsLineMatching("ktlint_code_style = ktlint_official") + assertThat(normalOutput).containsLineMatching("ktlint_code_style = intellij_idea") }.assertAll() } } @@ -526,7 +526,9 @@ class SimpleCLITest { arguments = listOf("--code-style=android_studio"), ) { assertThat(normalOutput).containsLineMatching( - Regex(".*Add editor config override to set code style to 'android_studio'.*"), + Regex( + ".*WARN.*Parameter `--code-style=android_studio is deprecated. The code style should be defined as " + + "'.editorconfig' property 'ktlint_code_style'.*"), ) } } From fd28edefa490025ea9bec685b5ce64e32ceea806 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Tue, 5 Sep 2023 18:37:43 +0200 Subject: [PATCH 2/4] Update changelog Closes #2238 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a31172e28..fc0bf21e37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +* Fix ktlint cli parameter `--code-style` [#2238](https://github.com/pinterest/ktlint/pull/2238) + ### Changed ## 1.0.0 - 2023-09-05 From d2b785fd563698e9dbf229d4dc08f4feca7e8bc1 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Tue, 5 Sep 2023 19:06:48 +0200 Subject: [PATCH 3/4] Update documentation --- documentation/snapshot/docs/faq.md | 5 ----- documentation/snapshot/docs/install/cli.md | 18 +++++------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/documentation/snapshot/docs/faq.md b/documentation/snapshot/docs/faq.md index e0f84053c0..c2b9be08f8 100644 --- a/documentation/snapshot/docs/faq.md +++ b/documentation/snapshot/docs/faq.md @@ -86,11 +86,6 @@ As of Ktlint 0.50, an error can only be suppressed using @Suppress or @SuppressW import foo.* ``` -## How do I globally disable a rule without `.editorconfig`? - -When using Ktlint CLI, you may pass a list of disabled rules via the `--disabled_rules` command line flag. The value is a comma separated list of rule id's that have to be disabled. The rule id must be fully qualified (e.g. must be prefixed with the rule set id). - - ## Why is `.editorconfig` property `disabled_rules` deprecated and how do I resolve this? The `.editorconfig` properties `disabled_rules` and `ktlint_disabled_rules` are deprecated as of KtLint version `0.48` and are removed in version `0.49`. Those properties contain a comma separated list of rules which are disabled. Using a comma separated list of values has some disadvantages. diff --git a/documentation/snapshot/docs/install/cli.md b/documentation/snapshot/docs/install/cli.md index e072121411..a945061fd0 100644 --- a/documentation/snapshot/docs/install/cli.md +++ b/documentation/snapshot/docs/install/cli.md @@ -59,20 +59,14 @@ On Arch Linux install package [ktlint AUR](https://aur.archlinux.org/ ### Rule set(s) -When no arguments are specified, the style of all Kotlin files (ending with '.kt' or '.kts') inside the current dir (recursively) are validated with the (non-experimental) rules from the [standard ruleset](../../rules/standard/). Hidden folders will be skipped. +When no arguments are specified, the style of all Kotlin files (ending with '.kt' or '.kts') inside the current dir (recursively) are validated with the rules from the [standard ruleset](../../rules/standard/). Hidden folders will be skipped. ```shell title="Default validation with standard ruleset" ktlint ``` -To validate with the [standard ruleset](../../rules/standard/) including the experimental rules run command below: - -```shell title="Validation with standard ruleset including the experimental rules" -ktlint --experimental -``` - !!! note - Instead of using this command line flag, it is advised to set `.editorconfig` property `ktlint_experimental = enabled` if you want the project always to be checked with the experimental rules. + The experimental rules in the standard rule set will only be run when `.editorconfig` property `ktlint_experimental = enabled` is set. To validate with a [custom ruleset](../../api/custom-rule-set/) run command below: @@ -83,7 +77,7 @@ ktlint -R /path/to/custom-ruleset.jar ``` !!! note - If the custom rule set contains rules that are marked as experimental, those rule will only be run when `.editorconfig` property `ktlint_experimental = enabled` is set (or command line parameter `--experimental` is specified). + If the custom rule set contains rules that are marked as experimental, those rule will only be run when `.editorconfig` property `ktlint_experimental = enabled` is set. ### Format (autocorrect) @@ -149,9 +143,9 @@ A scaffold of the `.editorconfig file` can be generated with command below. Note ```shell title="Generate .editorconfig" ktlint generateEditorConfig # or -ktlint --experimental generateEditorConfig +ktlint generateEditorConfig # or -ktlint --experimental --ruleset=/path/to/custom-ruleset.jar generateEditorConfig +ktlint --ruleset=/path/to/custom-ruleset.jar generateEditorConfig ``` Normally this file is located in the root of your project directory. In case the file is located in a sub folder of the project, the settings of that file only applies to that subdirectory and its folders (recursively). Ktlint automatically detects and reads all `.editorconfig` files in your project. @@ -202,8 +196,6 @@ ktlint installGitPrePushHook `--color` and `--color-name=`: Make output colorful and optionally set the color name to use. -`--disabled_rules=`: A comma-separated list of rules to globally disable. To disable the standard ktlint rule-set use `--disabled_rules=standard`. This flag is most likely to be removed in a future version. Use [`.editorconfig disabled_rules`](../../rules/configuration-ktlint/#disabled-rules). - `-h` or `--help`: Prints help information. `--limit=`: Maximum number of errors to show (default: show all) From 11194d4de7ddc34ce3c9abe96df84c28372228ca Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Tue, 5 Sep 2023 20:14:09 +0200 Subject: [PATCH 4/4] Fix rule violation --- .../src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt index b81b4fddea..9474f49e5b 100644 --- a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt +++ b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt @@ -528,7 +528,8 @@ class SimpleCLITest { assertThat(normalOutput).containsLineMatching( Regex( ".*WARN.*Parameter `--code-style=android_studio is deprecated. The code style should be defined as " + - "'.editorconfig' property 'ktlint_code_style'.*"), + "'.editorconfig' property 'ktlint_code_style'.*", + ), ) } }