Skip to content

fix: skip parsing of JavaScript regexp literals: /test/#1245

Merged
a-h merged 5 commits intomainfrom
issue_1243
Aug 15, 2025
Merged

fix: skip parsing of JavaScript regexp literals: /test/#1245
a-h merged 5 commits intomainfrom
issue_1243

Conversation

@a-h
Copy link
Copy Markdown
Owner

@a-h a-h commented Aug 15, 2025

Fixes #1243

But introduces an issue in code like this:

const result = call(1000 / 10, {{ data }}, 1000 / 10);

In the current PR, / 100, {{ data }}, 1000 / would be collected as a regexp literal, and therefore, the {{ data }} go value wouldn't be interpolated.

@a-h
Copy link
Copy Markdown
Owner Author

a-h commented Aug 15, 2025

I've updated to add a heuristic for divisions.

@a-h a-h marked this pull request as ready for review August 15, 2025 14:26
@a-h a-h requested review from Copilot and joerdav August 15, 2025 14:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for parsing JavaScript regular expression literals in script elements to fix issue #1243. The change prevents the parser from incorrectly interpreting regex patterns as Go template expressions.

Key changes:

  • Adds regex literal parsing capability to the script parser
  • Includes comprehensive test coverage for regex patterns and edge cases
  • Refactors error handling and control flow in the script parser

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
parser/v2/scriptparser.go Implements regex literal parsing with new regexpLiteral parser and integrates it into the main script parsing loop
parser/v2/scriptparser_test.go Adds test cases for regex parsing, refactors existing tests, and improves error handling in test assertions
parser/v2/scriptparsertestdata/regexp_literal.txt Adds test data file demonstrating regex literal parsing behavior

Copy link
Copy Markdown
Collaborator

@joerdav joerdav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, it reduces the cases that would error! Definitely would be ideal to squash the double curly pair bug but I think can be a separate pr.

@a-h a-h merged commit 71d038e into main Aug 15, 2025
9 checks passed
@a-h a-h deleted the issue_1243 branch August 15, 2025 14:56
dgrundel pushed a commit to dgrundel/templ that referenced this pull request Aug 16, 2025
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.

Error parse template error when script has regex in templ "template closing brace not found"

3 participants