Skip to content

fix: ask KotlinPoet whether a @property name needs backticks - #270

Merged
Marius Volkhart (MariusVolkhart) merged 1 commit into
mainfrom
fix/kdoc-property-backtick-predicate
Sep 4, 2026
Merged

fix: ask KotlinPoet whether a @property name needs backticks#270
Marius Volkhart (MariusVolkhart) merged 1 commit into
mainfrom
fix/kdoc-property-backtick-predicate

Conversation

@MariusVolkhart

@MariusVolkhart Marius Volkhart (MariusVolkhart) commented Sep 4, 2026

Copy link
Copy Markdown
Member

Fixes #247.

formatAsKdocPropertyReference decided whether a KDoc @property name needed backticks with Regex("[A-Za-z_][A-Za-z0-9_]*"), which disagrees with the declaration KotlinPoet renders below it in both directions. café is a plain Kotlin identifier — bare in the declaration, backticked in the tag, which is what the issue reports. Less obviously, object and _ went the other way: bare in the tag, backticked in the declaration.

KotlinPoet escapes a declaration name for four independent reasons — not a legal Java identifier, one of its own reserved KEYWORDS, contains $, or all underscores — and both escapeIfNecessary and the keyword set are internal. A copy of that list here would drift silently on the next KotlinPoet upgrade, so the predicate now renders a throwaway PropertySpec for the name and reads back whether KotlinPoet escaped it. The issue's suggested isJavaIdentifierStart/isJavaIdentifierPart widening would have fixed café and left object, _, and a$b still disagreeing.

The check tests for a backtick anywhere in the rendered text rather than for `name` specifically. KotlinPoet reserves U+00B7 and U+2662 as line-wrapping markers and renders them as spaces, so a name containing one is escaped in the output without appearing there verbatim; matching the name text back read that as "needs no backticks" and emitted a bare, multi-token tag. Names carrying their own backtick are short-circuited before the oracle, since KotlinPoet treats one as already escaped and skips all four checks.

Behavior for a name containing /* or */ (declined outright) and for a name containing a literal backtick is unchanged, as is everything on the SQL-quoting side.

Seven tests in TypeRepositoryTest$PropertyNameKdocDeclarationAgreement assert the @property tag and the val declaration agree in the same rendered file, for café, object, _, a$b, My Col, id, and an interpunct-containing name. No golden file changes; every scenario column name is ASCII.

🤖 Generated with Claude Code

The KDoc @Property predicate was a hand-written ASCII regex, so it
disagreed with the declaration KotlinPoet renders directly below it in
both directions: a Unicode-letter name like "café" was backticked in the
tag but bare in the declaration, while a Kotlin keyword or all-underscore
name was bare in the tag but backticked in the declaration.

Reimplementing KotlinPoet's rule is not an option worth taking — it
escapes for four independent reasons, and both the rule and the keyword
set it consults are internal, so a copy drifts on every KotlinPoet
upgrade. Rendering a throwaway PropertySpec asks KotlinPoet directly and
stays correct by construction.

The check looks for a backtick anywhere in the rendered text rather than
for the name appearing backtick-wrapped: KotlinPoet reserves U+00B7 and
U+2662 as line-wrapping markers and renders them as spaces, so a name
containing one is escaped without appearing verbatim. That requires
ruling out a name carrying its own backtick first, which KotlinPoet would
read as already escaped.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@snyk-io

snyk-io Bot commented Sep 4, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@MariusVolkhart
Marius Volkhart (MariusVolkhart) merged commit bd42334 into main Sep 4, 2026
14 checks passed
@MariusVolkhart
Marius Volkhart (MariusVolkhart) deleted the fix/kdoc-property-backtick-predicate branch September 4, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@property backtick predicate is narrower than Kotlin's identifier grammar

1 participant