Skip to content

fix: handle single-line comments in template code blocks#369

Open
harunosakura030303-maker wants to merge 1 commit intobgub:mainfrom
harunosakura030303-maker:fix/escape-single-quotes
Open

fix: handle single-line comments in template code blocks#369
harunosakura030303-maker wants to merge 1 commit intobgub:mainfrom
harunosakura030303-maker:fix/escape-single-quotes

Conversation

@harunosakura030303-maker
Copy link
Copy Markdown

Summary

Fixes #337 — unpaired apostrophes in single-line comments (// it's, // don't) no longer cause parse errors.

Problem

The parser's parseCloseReg regex matches ', ", `, /*, and closing tags. When it encounters a ' inside a // comment, it tries to match a complete string literal and fails with "unclosed string" since the apostrophe is unpaired.

Fix

Added // to parseCloseReg and a handler that skips to end of line when a single-line comment is encountered, matching the existing /* */ block comment behavior.

Test Case

const eta = new Eta();
const result = eta.renderString(`<%
  const foo = 'bar';
  // This comment with unpaired apostrophe: it's fixed
  console.log(foo);
%><%= foo %>`, {});
// Should output: "bar"

Previously, the parser only handled block comments (/* */), causing
unpaired apostrophes in single-line comments (// it's, don't) to be
incorrectly treated as string delimiters, resulting in 'unclosed string'
parse errors.

This adds // detection to parseCloseReg and skips to end of line when
a single-line comment is encountered, matching the existing block comment
handling behavior.

Fixes bgub#337
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.

Unpaired apostrophe in JavaScript comments causes "Unexpected token '%'" error in template parsing

1 participant