Repository navigation
Add KDoc comment format rule - #3369
Conversation
paul-dingemans
left a comment
There was a problem hiding this comment.
Thanks for your contribution. I can see that you made a good attempt to comply with other Ktlint rules. I like that there are plenty of tests and explanatory comments.
Despite that I do have a bunch of remarks that need to be addressed. Please respond on every remark to let me know that it is fixed, or has become obsolete due to a refactoring by another review remark.
Introduce standard:kdoc-comment-format, which checks that KDoc comments follow a consistent shape: a single-line KDoc starts with `/** ` and ends with ` */`, while a multi-line KDoc opens and closes on their own line and has every continuation line start with a leading asterisk, indented one space more than the opening `/**`. The rule implements RuleV2.OfficialCodeStyle so it is enabled by default only for the ktlint_official code style and remains opt-in for intellij_idea and android_studio, matching the discussion on the issue. Autocorrect is intentionally conservative: it only touches whitespace and delimiters, never the actual KDoc content. A continuation line that is missing its leading asterisk is only autocorrected when the line is already indented at least as far as required (so the relative indentation of that content is preserved) and no tabs are involved; otherwise only a violation is reported. Content sharing a line with the opening or closing delimiter is always reported without autocorrect, since repositioning it would require rewriting the KDoc content. Closes ktlint#2982 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MAtdJuhtNDUW995EkkqoQM
- Rename rule kdoc-comment-format to kdoc-delimiter and mark it RuleV2.Experimental (SinceKtlint EXPERIMENTAL instead of STABLE), per review discussion on naming and stability. - Generalize the opening-delimiter check so that any character other than a space or newline directly after "/**" is reported, instead of special-casing "/***"; this also catches banner-style openings. - Detect and report empty/blank KDoc comments (`/***/`, `/** */`, `/**\n */`, `/**\n *\n */`) with a dedicated message, without autocorrect. - Split the single-line diagnostic into separate start/end delimiter messages and emissions instead of one combined message. - Rework the multi-line traversal to walk leaves via sibling/leaf helpers instead of an index-based leaf list, and to handle KDoc comments with tags (multiple sibling KDOC_SECTION elements). - Add checks for an empty line directly after the opening delimiter or directly before the closing delimiter, with autocorrect. - Never autocorrect a missing leading asterisk, since inserting one can change the visual indentation of the continuation line. - Stop autocorrecting a single-line KDoc's extra closing asterisk when it would require replacing the KDOC_END leaf, since IndentationRule can be tracking a reference to that leaf in the same pass. - Fix "align with the first asterisk" wording and replace "star" with "asterisk" throughout messages and tests. - Rewrite tests: use TAB test literal, parameterize the non-official code style test, add coverage for the new checks, and drop cases the rule no longer needs. - Move the rule's documentation to experimental.md and remove the now empty class-level KDoc in EnumWrappingRule.kt that the new rule flags. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L4Y6B9GcCC81Ad6oe4JvkM
c1e4569 to
1462e0d
Compare
|
Thanks for the thorough review! I've pushed a commit ( Rebase: pulled current Naming / stability
Rule behavior changes
AST processing
IndentationRule conflict (root-caused) Tests Verification
Open for your input (left unresolved)
Environment note: this sandbox only had JDK 25 available, while |
…lass - documentation/snapshot/docs/rules/experimental.md: "can not" -> "will not" in the autocorrect note, and add the missing-leading- asterisk and misaligned-closing-delimiter examples requested in review. - KdocDelimiterRuleTest.kt: flatten the single-test "code style is not ktlint_official" nested class into a top-level parameterized test, per review feedback that a nested class isn't needed for a single test. These were part of the previous commit's intent but were not staged before it was pushed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L4Y6B9GcCC81Ad6oe4JvkM
…ld be written as single line
…he intent of the developer can not be determined with certainty.
Rewrite rule to operate (more) on element passed in via `beforeVisitChildNodes` method instead of internally iterating on the KDOC element only. This refactor revealed several new edge cases which are handled. Fix offsets to position where change is to be made (when it would be done manually). Reword some violation messages.
* Do not remove blank line in multiline Kdoc when entire Kdoc is empty * Reduce consecutive blank lines * Remove unneeded leading asterisk before closing delimiter on same line * When opening delimiter is followed by text on same line and containing a KDOC_LPAR, KDOC_RPAR, KDOC_MARKDOWN_LINK
|
@hej090224 Thanks for your contribution. Although I rewrote a big deal of the rule itself, your work was the basis for it. Also I have run the new version on my integration test base and discovered even some more edge cases which I all resolved. |
Description
Adds
standard:kdoc-delimiter, an experimental rule that checks whether a KDoc comment is delimited consistently./**and ends with*/(exactly one space on each side)./**, ends with a line containing only*/, and every other line starts with a leading asterisk indented one space more than the opening/**, followed by a single space and the line content (or by nothing for an otherwise empty line). The line directly after the opening delimiter, and the line directly before the closing delimiter, must not be empty./***/,/** */,/**\n */) is not allowed.The rule implements
RuleV2.OfficialCodeStyleandRuleV2.Experimental, so by default it only runs whenktlint_experimentalis enabled andktlint_code_styleisktlint_official(or when enabled explicitly), per review discussion on this PR.Autocorrect is deliberately conservative and only ever touches whitespace/delimiters, never the content:
*/, a missing space after a leading asterisk, trailing whitespace on an otherwise empty comment line, and an empty line directly after the opening or before the closing delimiter are autocorrected, since these are pure whitespace/delimiter fixes./**or closing*/(including extra asterisks glued to the opening, e.g./***) is always reported without autocorrect, since fixing it would mean rewriting/moving actual content./** foo**/) is reported without autocorrect: fixing it requires replacing the KDOC_END leaf, which conflicts with IndentationRule when both rules run in the same pass (see KDoc comment format #2982 discussion).Tests
KdocDelimiterRuleTestcovers well-formed single-line/multi-line KDoc (tags, markdown, links, code blocks, nested declarations, tab indentation), empty/blank KDoc, malformed openings/closings, single-line spacing (split start/end diagnostics), leading-asterisk alignment and spacing, missing leading asterisk (never autocorrected), empty lines directly after opening / before closing, running alongsidekdoc-wrapping/kdoc/IndentationRule, autocorrect idempotency, a regression test for the IndentationRule conflict, and opt-in behaviour outsidektlint_official.Verification
./gradlew :ktlint-ruleset-standard:test --tests "io.github.ktlint.core.ruleset.standard.rules.KdocDelimiterRuleTest"./gradlew :ktlint-ruleset-standard:test(full module)./gradlew ktlintCheck build(full project, including the project's own self-check with the new rule enabled)All pass.
Closes #2982