Skip to content

fix(tsx): rework how escaping work#1023

Merged
Princesseuh merged 3 commits into
fix/tsx-extract-scriptsfrom
fix/escaping
Jul 15, 2024
Merged

fix(tsx): rework how escaping work#1023
Princesseuh merged 3 commits into
fix/tsx-extract-scriptsfrom
fix/escaping

Conversation

@Princesseuh
Copy link
Copy Markdown
Member

@Princesseuh Princesseuh commented Jul 12, 2024

Changes

Change how escaping works in TSX. Previously, if a TextNode had any of the following characters: <>{}'", we'd escape the entire thing. This is fine, however, it doesn't work for TSX that is being written, as many times you'll end up with nodes that look like this:

<div></div>
<div

Where <div not being a completed tag, means that it get escaped (becomes <div></div>{` <div`}) and thus considered a string in the editor.

This PR changes two things:

  • Leave this style of full escaping to style tags, JSON script tags and is:raw
  • Only escape the individual offending characters for JSX (> and }), ex: <div>></div> becomes <div>{`>`}</div>

The result isn't perfect, as the compiler still fundamentally does not understand unclosed tags, however it at least doesn't escape it in the vast majority of cases that affect editor tooling.

Fixes #862

Testing

Added tests

Docs

N/A

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jul 12, 2024

⚠️ No Changeset found

Latest commit: e3e943c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

`;
const output = `${TSXPrefix}<Fragment>
<div>
<div div {...{"<":true}}>
Copy link
Copy Markdown
Member Author

@Princesseuh Princesseuh Jul 12, 2024

Choose a reason for hiding this comment

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

(note that this is not new, this exact example is same as current output)

This is what I mean by it not being perfect. Ideally, it would just be outputted as written, but the compiler thinks that those are attributes. This is very wonky looking, but as far as I could test, in the editor it's not a problem, TypeScript just think there's some text further on and an expression.

It might be possible to fix this, because the JS output actually does support this strangely enough. But it's perhaps a bit more complicated in TSX due to all the escaping required, not sure.

@Princesseuh Princesseuh linked an issue Jul 12, 2024 that may be closed by this pull request
@Princesseuh Princesseuh merged commit 1bdb488 into fix/tsx-extract-scripts Jul 15, 2024
@Princesseuh Princesseuh deleted the fix/escaping branch July 15, 2024 15:02
Princesseuh added a commit that referenced this pull request Jul 16, 2024
…sumption (#1019)

* feat(tsx): Extract script and styles from TSX for language-server consumption

* feat: add options for styles

* fix: reverse order of operation

* test: fix

* test: add tests

* fix: count printed bytes so that we can skip over them later

* chore: fix format

* chore: uugh

* chore: biome what is GOING on

* fix(tsx): rework how escaping work (#1023)

* fix(tsx): rework how escaping work

* nit: remove duplicate code

* nit: formatting

* chore: changeset

* fix: also do it for escaped text
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.

🐛 BUG: [TSX] Unclosed tag result in unnecessary escaping

2 participants