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

Ktlint official code style #1767

Merged

Conversation

paul-dingemans
Copy link
Collaborator

@paul-dingemans paul-dingemans commented Jan 5, 2023

Description

Mark experimental rules using interface "Rule.Experimental"

Add interface "Rule.Experimental" to mark a rule as an experimental rule. Rules no longer have to be part of the "experimental" rule set to be treated as an experimental rule. This allows experimental rules to be defined in any rule set. Whenever a rule is no longer experimental it does not need to be moved to a non-experimental rule set. Benefit is that users of that rule, do not need to change ktlint disable directives.

Simplify code by moving logic from QualifiedRuleId to Rule. All logic regarding qualified rule (set) id is now kept together in once class.

The 'experimentalrule set of Ktlint has been merged with thestandard` rule set.

Add code style ktlint_official

Add code style ktlint_official (functionally equivalent with old official code style), intellij_idea (replacement for old official code style) and android_studio (replacement for old android code style).

Closes #1699

Closes #1689

Add rule no-empty-first-line-in-class-body

Add new rule no-empty-first-line-in-class-body. This rule disallows a class to start with a blank line. The rule is marked as experimental and will only be run when the 'ktlint_official' code style is enabled.

Fix package name containing all standard rules. Ensure that RuleSetProviderTest fails whenever the given package does not contain any Rule classes.

Parameter wrapping in function literal

Wrap the parameters of a function literal containing a multiline parameter list (only in ktlint_official code style).

Add rule if-else-bracing

Add new experimental rule if-else-bracing for ktlint_official code style. This rule enforces consistent usage of braces in all branches of a singe if or if-else-if statement.

Closes #1573

Fix continuation indent

Fix continuation indent for a dot qualified array access expression in ktlint_official code style only.

Closes #1540

Add new experimental rule no-consecutive-comments

This rule disallows consecutive comments except EOL comments for ktlint_official code style.

Closes #1454

Checklist

  • PR description added
  • tests are added
  • KtLint has been applied on source code itself and violations are fixed
  • documentation is updated
  • CHANGELOG.md is updated

In case of adding a new rule:

* Add interface "Rule.Experimental" to mark a rule as an experimental rule. This allows experimental rules to be defined in any rule set. Whenever a rule is no longer experimental it does not need to be moved to a non-experimental rule set. Benefit is that users of that rule, do not need to change ktlint disable directives.

* Merge `experimental` ruleset with `standard` ruleset.

* Rearrange standard rules to separate package

* Simplify code by moving logic from QualifiedRuleId to Rule. All logic regarding qualified rule (set) id is now kept together in once class.
* Add code style `ktlint_official` (functionally equivalent with old `official` code style), `intellij_idea` (replacement for old `official` code style) and `android_studio` (replacement for old `android` code style).

Closes pinterest#1699

* Format project itself to "official ktlint" codestyle.

* Add Gradle "ktlint format" task.

* Ensure that the subcommand generateEditorConfig, gitPreCommitHook and gitPrePushHook can receive the code-style before or after the subcommand and print error in case the code style option is not specified explicitly.

* Add option to KtLintRuleEngine to ignore the '.editorconfig' on the file system. Unit test classed have to obey to '.editorconfig' settings of the project. Code snippets inside those unit tests should not be influenced by the '.editorconfig' settings of the project but only by properties which have been explicitly defined in the unit test.

* Files used for CLI tests contain lint violations on purpose in order to validate the tests. The tests may not be affected by the ".editorconfig" files which are used to lint/format the code of the Ktlint project itself. Neither may the ".editorconfig" files of the project lead to changing the input files for the tests. Files used as input for the cli test use the ".test" extension so that those files can be separated from the actual project files.
 The tests that used the "filename" rule have been replaced with the "no-wildcard-imports" rule. Reason for this is that the "filename" rule contains a hardcoded check that excludes all files not ending with a ".kt" extension.

* Do not add the (first line of the) body expression on the same line as the function signature in case the max line length would be exceeded. `function-signature`.

* Do not add the first line of a multiline body expression on the same line as the function signature in case function body expression wrapping property is set to `multiline`. `function-signature`.
… a class to start with a blank line. The rule is marked as experimental and will only be run when the 'ktlint_official' code style is enabled.

Fix package name containing all standard rules. Ensure that RuleSetProviderTest fails whenever the given package does not contain any Rule classes.
…meter list (only in `ktlint_official` code style).

Closes pinterest#1681

The parameters of a function literal containing a multiline parameter list are aligned with first parameter whenever the first parameter is on the same line as the start of that function literal (not allowed in `ktlint_official` code style)

Closes pinterest#1756
…e style. This rule enforces consistent usage of braces in all branches of a singe if or if-else-if statement.

Closes pinterest#1573
@paul-dingemans paul-dingemans marked this pull request as draft January 5, 2023 18:58
@paul-dingemans paul-dingemans added this to the 0.49.0 milestone Jan 5, 2023
This rule disallows consecutive comments except EOL comments for `ktlint_official` code style.
* Add interface "Rule.Experimental" to mark a rule as an experimental rule. This allows experimental rules to be defined in any rule set. Whenever a rule is no longer experimental it does not need to be moved to a non-experimental rule set. Benefit is that users of that rule, do not need to change ktlint disable directives.

* Merge `experimental` ruleset with `standard` ruleset.

* Rearrange standard rules to separate package

* Simplify code by moving logic from QualifiedRuleId to Rule. All logic regarding qualified rule (set) id is now kept together in once class.
* Add code style `ktlint_official` (functionally equivalent with old `official` code style), `intellij_idea` (replacement for old `official` code style) and `android_studio` (replacement for old `android` code style).

Closes pinterest#1699

* Format project itself to "official ktlint" codestyle.

* Add Gradle "ktlint format" task.

* Ensure that the subcommand generateEditorConfig, gitPreCommitHook and gitPrePushHook can receive the code-style before or after the subcommand and print error in case the code style option is not specified explicitly.

* Add option to KtLintRuleEngine to ignore the '.editorconfig' on the file system. Unit test classed have to obey to '.editorconfig' settings of the project. Code snippets inside those unit tests should not be influenced by the '.editorconfig' settings of the project but only by properties which have been explicitly defined in the unit test.

* Files used for CLI tests contain lint violations on purpose in order to validate the tests. The tests may not be affected by the ".editorconfig" files which are used to lint/format the code of the Ktlint project itself. Neither may the ".editorconfig" files of the project lead to changing the input files for the tests. Files used as input for the cli test use the ".test" extension so that those files can be separated from the actual project files.
 The tests that used the "filename" rule have been replaced with the "no-wildcard-imports" rule. Reason for this is that the "filename" rule contains a hardcoded check that excludes all files not ending with a ".kt" extension.

* Do not add the (first line of the) body expression on the same line as the function signature in case the max line length would be exceeded. `function-signature`.

* Do not add the first line of a multiline body expression on the same line as the function signature in case function body expression wrapping property is set to `multiline`. `function-signature`.
… a class to start with a blank line. The rule is marked as experimental and will only be run when the 'ktlint_official' code style is enabled.

Fix package name containing all standard rules. Ensure that RuleSetProviderTest fails whenever the given package does not contain any Rule classes.
…meter list (only in `ktlint_official` code style).

Closes pinterest#1681

The parameters of a function literal containing a multiline parameter list are aligned with first parameter whenever the first parameter is on the same line as the start of that function literal (not allowed in `ktlint_official` code style)

Closes pinterest#1756
…e style. This rule enforces consistent usage of braces in all branches of a singe if or if-else-if statement.

Closes pinterest#1573
This rule disallows consecutive comments except EOL comments for `ktlint_official` code style.
… ktlint-official-code-style

# Conflicts:
#	CHANGELOG.md
#	ktlint-api-consumer/src/test/kotlin/com/pinterest/ktlint/api/consumer/KtLintRuleEngineTest.kt
#	ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/VisitorProvider.kt
#	ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/VisitorProviderTest.kt
…t-core" with the "ktlint-ruleset-xxx" modules
* Rename module `ktlint` to `ktlint-cli`
* Extract module `ktlint-cli-reporter-core` from `ktlint-core`
* Rename all reporter modules `ktlint-reporter-xxx` to `ktlint-cli-reporter-xxx`
Reverse dependency between the editor config properties IndentSize and IndentStyle versus the default IndentConfig so that the IndentSizeEditorConfigProperty in 'ktlint-rule-engine' does not depend on the `ktlint-ruleset-core` module.
* Remove klob dependency as this was replaced with custom implementation in ktlint 0.41.0 via pinterest#999.
* Remove unused libs.kotlin.compiler dependency
* Deprecate RuleSetProviderV2 and RuleProvider. Add new RuleSetProviderV3 and RuleProviderV3 in module ktlint-cli-core.
* Deprecate Reporter and ReporterProvider. Add new ReporterV2 and ReporterProviderV2 in new module ktlint-cli-reporter-core. ReporterV2 no longer supports to create an aggregated reporter. This has been moved into ktlint-cli module.
* Add KtlintCliError in ktlint-cli-reporter-core. Use KtlintCliError instead of LintError in communication between the invocation of the KtlintRuleEngine and the reporters. In this way the reporters do not have to depend on ktlint-rule-engine module. LintError in ktlint-core is replaced with new version in ktlint-rule-engine.
* Rename module ktlint-ruleset-test to ktlint-ruleset-test-tooling.
* Move IndentConfig from ktlint-core to ktlint-ruleset-core
…ternal and internal processing

For backwards compatibility, unqualified ruleId's can still be used in:
- The `--disabled-rules` parameter in KtLint CLI and `.editorconfig`.
- The `source` element in the KtLint CLI `baseline.xml` file.
- The KtLint disable directives `ktlint-enable` / `ktlint-disable` and the `@Suppress('ktlint:...')` annotations.
* Move 'com.pinterest.ktlint.core.KtLintKLoggerInitializer.kt' (module 'ktlint-core') to 'com.pinterest.ktlint.logger.api.KtLintKLoggerInitializer.kt' (new module 'ktlint-logger').
* Move 'com.pinterest.ktlint.core.KtlintVersion' (module 'ktlint-core') to 'com.pinterest.ktlint.ruleset.core.api.KtlintVersion' (module 'ktlint-cli-reporter-core')
* Deprecate 'com.pinterest.ktlint.core.api.EditorConfigProperties' (module 'ktlint-core'). Add new 'com.pinterest.ktlint.ruleset.core.api.EditorConfigProperties' (module 'ktlint-ruleset-core').
* Remove reference to module 'ktlint-core' except for modules 'ktlint-cli' and 'ktlint-test-ruleset-provider-v2-deprecated' which still need the reference for backwards compatibility.
* Fixing lint violations (also from previous restructuring commits)
… exception handling into the RuleExecutionContext

The KtLintRuleEngine is not aware of the RuleSetProvider, but it does catch and handles exception thrown during execution of the rule. About information is required for logging purposes in the exception handling.
@paul-dingemans paul-dingemans marked this pull request as ready for review February 15, 2023 17:05
# Conflicts:
#	docs/faq.md
#	docs/rules/configuration-ktlint.md
#	gradle/verification-metadata.xml
#	ktlint/src/test/kotlin/com/pinterest/ktlint/SimpleCLITest.kt
The Github CICD build is typically slower than the local machine. As of that it might need more time to complete a single CLI tests. For a developer it is usually not important whether the CICD build takes 5 versus 10 minutes. On the local machine it is annoying when the unit tests take long due because the developer is waiting for the tests to complete. So on the local machine it is better to fail the test after a shorter timeout.
@paul-dingemans paul-dingemans merged commit 2d1ab84 into pinterest:master Feb 15, 2023
@paul-dingemans paul-dingemans deleted the ktlint-official-code-style branch February 15, 2023 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant