LearnNewsExamplesServices
Frontmatter
id7149
titleRefactor Build-Time Parser to be AST-Based for Robustness
stateClosed
labels
enhancement
assigneestobiu
createdAtAug 1, 2025, 1:06 AM
updatedAtAug 2, 2025, 2:47 PM
githubUrlhttps://github.com/neomjs/neo/issues/7149
authortobiu
commentsCount1
parentIssue7130
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtAug 2, 2025, 2:47 PM

Refactor Build-Time Parser to be AST-Based for Robustness

Closed v10.3.0 enhancement
tobiu
tobiu commented on Aug 1, 2025, 1:06 AM

Description: The current build-time approach, which uses regular expressions to find and replace templates, has proven to be brittle and incorrect. It cannot handle nested html templates and can be easily fooled by JSDoc comments or strings that happen to contain the `html`` sequence, leading to build failures. To ensure correctness and consistency with the runtime environment, the build process must be refactored to use a proper JavaScript parser.

Implementation Details:

  • Tools: acorn (to parse JS into an Abstract Syntax Tree) and astring (to generate JS code from the AST).
  • Method:
    1. In the build script, for each .mjs file, use acorn to parse the entire file content into an AST.
    2. Traverse the AST, specifically looking for TaggedTemplateExpression nodes where the tag is an Identifier with the name html.
    3. Process these template nodes recursively (post-order traversal) to correctly handle nested templates from the inside out.
    4. The logic from HtmlTemplateProcessorLogic will be used to convert the template into its VDOM object representation.
    5. The original TaggedTemplateExpression node in the AST will be replaced with a new AST node representing the generated VDOM object (using an object-to-AST converter).
    6. Finally, use astring to generate the final, correct JavaScript code from the modified AST.
    7. This new, robust process will replace the fragile regex-based replace loop.
tobiu assigned to @tobiu on Aug 1, 2025, 1:06 AM
tobiu added the enhancement label on Aug 1, 2025, 1:07 AM
tobiu added parent issue #7130 on Aug 1, 2025, 1:07 AM
tobiu
tobiu Aug 2, 2025, 2:47 PM

resolved

tobiu closed this issue on Aug 2, 2025, 2:47 PM