Skip to content

[Bug] data/components.ts: extractHtmlFromRegistry regex /return\s*\(\s*([\s\S]*?)\s*\);/ is greedy-lazy and fails on components with multiple return statements or nested parentheses #596

Description

@anshul23102

Bug Summary

extractHtmlFromRegistry in src/data/components.ts uses a single regex to extract the JSX return value from registry component source code:

const returnMatch = reactCode.match(/return\s*\(\s*([\s\S]*?)\s*\);/);

This regex has two failure modes that affect real-world registry components:

1. Wrong match on early return statements:
Any component that uses an early return (for example, a guard clause) before the main JSX return will match the first return (...) found in the source, capturing the wrong content:

const MyComponent = () => {
  if (!data) return (<div>Loading...</div>);  // ← regex matches this
  return (
    <div className="main-component">...</div>  // ← this is never reached
  );
};

The lazy [\s\S]*? quantifier stops at the first );\n it encounters, which may be the early return rather than the main component JSX.

2. Nested parentheses break the match:
JSX components frequently contain nested function calls with parentheses (for example, Array.map(), Object.entries(), String.split(), ternary ? (a) : (b)). The regex stops at the first ); token it finds, truncating the returned JSX mid-expression:

return (
  <div>
    {items.map((item) => (   // ← the `);` here ends the match prematurely
      <span>{item}</span>
    ))}
  </div>
);

Both failure modes silently produce malformed HTML strings. These are then rendered into the code display panels and used to generate HTML, Vue, and Angular template strings for all components in the registry.

Expected Behavior

The JSX extraction should use a parenthesis-counting approach (tracking depth) or an AST-based parser rather than a regex, so nested parentheses are handled correctly and only the final top-level return (...) statement is extracted.

Actual Behavior

The regex fails on any component with early returns or nested parentheses, producing truncated or incorrect HTML output in the code tab.

Affected File

src/data/components.ts, extractHtmlFromRegistry function.


@singhtrivendra I would like to work on this issue. Could you please assign/ it to me? Contributing under NSoC '26.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions