Skip to content

Fix trailing line comment classified as enclosed in validators#227

Merged
DylanPiercey merged 1 commit into
mainfrom
claude/htmljs-parser-regression-pgpr2x
Jul 3, 2026
Merged

Fix trailing line comment classified as enclosed in validators#227
DylanPiercey merged 1 commit into
mainfrom
claude/htmljs-parser-regression-pgpr2x

Conversation

@DylanPiercey

Copy link
Copy Markdown
Contributor

Description

Fixes isValidAttrValue (and isValidStatement / isValidScriptlet) reporting enclosed for a value that ends in a trailing // line comment, e.g. "hello" // note.

isValid only downgraded enclosedvalid when it scanned a literal newline byte. A line comment that runs to EOF contains no newline, so hadUnguardedNewline never tripped and the value stayed enclosed. But a trailing line comment consumes everything that would follow it on the line, so the value is not safe to emit verbatim.

The fix flags the enclosing expression as having an unguarded newline when a line comment reaches EOF while no group is open — mirroring the existing unguarded-newline handling. It lives in EXPRESSION.return, so it only affects line comments whose parent is a JS expression (not the same state entered from tag/HTML contexts).

input before after
"hello" // c enclosed valid
foo // c enclosed valid
1 + 2 // c enclosed valid
"hello" /* c */ // d enclosed valid
"hello" /* c */ enclosed enclosed (block comment self-closes)
("hello" // c\n) enclosed enclosed (guarded by parens)
"hello"\n// c valid valid

Motivation and Context

Regression introduced in 5.11.0 (last good: 5.10.2). enclosed promises consumers "safe to emit verbatim, no wrapping needed" — so prettier-plugin-marko emitted such values bare, ejecting the comment and commenting out the tag close:

<!-- input -->
<custom-tag id=("hello" // this comment should not be removed")/>

<!-- broken output: comment ejected, would comment out the tag close -->
<custom-tag id="hello"/> // this comment should not be removed

Checklist:

  • I have updated/added documentation affected by my changes.
  • I have added tests to cover my changes.

Generated by Claude Code

A `//` line comment that runs to the end of a validated expression (no
terminating newline) consumes everything that would follow it on the
line, so the value is not safe to emit verbatim as an attribute value.

`isValid` only downgraded `enclosed` -> `valid` when it saw a literal
newline byte, so a value ending in a trailing line comment (e.g.
`"hello" // note`) was wrongly reported as `enclosed`, regressing
prettier-plugin-marko which then emitted the value bare and commented
out the tag close.

Flag the enclosing expression as having an unguarded newline when a line
comment reaches EOF while no group is open, mirroring the existing
unguarded-newline handling. This is done in EXPRESSION.return so it only
affects line comments whose parent is a JS expression. A comment guarded
by a group (`("hello" // c\n)`) or a self-closing block comment stays
`enclosed`.
@changeset-bot

changeset-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4b97e68

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
htmljs-parser Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.95%. Comparing base (16f453a) to head (4b97e68).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #227   +/-   ##
=======================================
  Coverage   99.95%   99.95%           
=======================================
  Files          34       34           
  Lines        4261     4270    +9     
  Branches      792      793    +1     
=======================================
+ Hits         4259     4268    +9     
  Misses          2        2           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9ef8cb99-fa5d-47ae-8779-d7fa0a83b7a7

📥 Commits

Reviewing files that changed from the base of the PR and between 16f453a and 4b97e68.

📒 Files selected for processing (3)
  • .changeset/tidy-carrots-repair.md
  • src/__tests__/validate.test.ts
  • src/states/EXPRESSION.ts

Walkthrough

This change fixes validation logic for inline JavaScript-in-HTML values ending with a trailing // line comment. EXPRESSION.ts now marks expressions as having an unguarded newline when a line comment reaches end-of-input without an active group, preventing incorrect "enclosed" classification. Tests were added covering isValidAttrValue, isValidStatement, and isValidScriptlet behavior with trailing, grouped, and block comments. A changeset documents the patch and clarifies classification rules for line comments.

Changes

Area Change
src/states/EXPRESSION.ts Sets hadUnguardedNewline = true for // comments reaching end-of-input with no active group stack
src/__tests__/validate.test.ts Adds tests for trailing/grouped/block comment classification across isValidAttrValue, isValidStatement, isValidScriptlet
.changeset/tidy-carrots-repair.md Documents patch fix for htmljs-parser validation reporting

Sequence Diagram(s)

sequenceDiagram
  participant Parser
  participant EXPRESSION
  participant Expression
  Parser->>EXPRESSION: return(child, expression) for JS_COMMENT_LINE
  EXPRESSION->>EXPRESSION: check pos === maxPos and no groupStack
  EXPRESSION->>Expression: set hadUnguardedNewline = true
Loading

Estimated code review effort: Low

Related PRs: None identified

Suggested labels: patch, bug fix

Suggested reviewers: None identified

🐰 A comment that trails off at the end,
No longer confused as "enclosed," my friend,
A newline unguarded, EOF in sight,
Now classified properly, validation's delight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: trailing line comments being misclassified as enclosed.
Description check ✅ Passed The description is directly related to the changeset and accurately explains the validation fix and tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/htmljs-parser-regression-pgpr2x

Comment @coderabbitai help to get the list of available commands.

@DylanPiercey
DylanPiercey merged commit a576b5c into main Jul 3, 2026
11 checks passed
@DylanPiercey
DylanPiercey deleted the claude/htmljs-parser-regression-pgpr2x branch July 3, 2026 02:32
@github-actions github-actions Bot mentioned this pull request Jul 3, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Roadmap Jul 14, 2026
@github-project-automation github-project-automation Bot moved this from Todo to Done in Roadmap Jul 14, 2026
@DylanPiercey DylanPiercey self-assigned this Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant