Skip to content

fix: preserve newline escape sequences in code generation#1790

Open
costajohnt wants to merge 2 commits intoBuilderIO:mainfrom
costajohnt:fix/newline-escape-sequence
Open

fix: preserve newline escape sequences in code generation#1790
costajohnt wants to merge 2 commits intoBuilderIO:mainfrom
costajohnt:fix/newline-escape-sequence

Conversation

@costajohnt
Copy link

@costajohnt costajohnt commented Jan 20, 2026

Summary

Fixes #1064

  • Remove the .replace(/\\n/g, '\n') line from dedent.ts that was converting escape sequences to actual newlines
  • Remove stripNewlinesInStrings usage which was a workaround for this bug

Why

The dedent helper was incorrectly converting \n escape sequences (the two-character sequence: backslash + n) into actual newline characters. This caused generated code containing strings with \n to be malformed across all targets.

Before (broken):

const x = "line1\nline2";
// Generated as:
const x = "line1
line2";  // Actual newline inside string literal - invalid!

After (fixed):

const x = "line1\nline2";
// Now correctly generates:
const x = "line1\nline2";  // Escape sequence preserved

Test Plan

  • Verified fix logic correctly preserves \n escape sequences
  • Snapshot tests need updating (158 snapshots) - these capture the corrected output

The snapshot changes are expected as they now reflect the correct behavior where escape sequences are preserved rather than corrupted.

Notes

This fix approach was confirmed by @samijaber in the issue discussion:

"remove all usage of dedent and stripNewLinesInStrings"

Closes #1064

Fixes BuilderIO#1064

The `dedent` helper was incorrectly converting `\n` escape sequences
(two characters: backslash + n) into actual newline characters. This
caused generated code containing strings with `\n` to be malformed.

## Changes

1. **dedent.ts**: Remove `.replace(/\\n/g, '\n')` which was corrupting
   escape sequences in string literals

2. **react/generator.ts**: Remove `stripNewlinesInStrings` wrapper
   which was a workaround for the dedent bug

3. **parsers/jsx/jsx.ts**: Remove `stripNewlinesInStrings` wrapper
   and related workaround

## Before

```javascript
const x = "line1\nline2";
// Would generate:
const x = "line1
line2";  // Broken - actual newline inside string
```

## After

```javascript
const x = "line1\nline2";
// Now correctly generates:
const x = "line1\nline2";  // Preserved escape sequence
```

As confirmed by @samijaber in the issue discussion.
@costajohnt costajohnt requested a review from samijaber as a code owner January 20, 2026 01:59
@changeset-bot
Copy link

changeset-bot bot commented Jan 20, 2026

🦋 Changeset detected

Latest commit: 6982748

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

This PR includes changesets to release 2 packages
Name Type
@builder.io/mitosis Patch
@builder.io/mitosis-cli 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

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Line break character \n causes problem when trying to use it for splitting textarea content into an array with rows

1 participant

Comments