Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly print double escaped backslashes with no raw value #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

paoloricciuti
Copy link
Member

Closes sveltejs/svelte#15476

A bit of an edge case but a valid usecase nontheless.

@@ -86,6 +86,42 @@ test('does not escape already-escaped single quotes', () => {
expect(code).toMatchInlineSnapshot(`"const str = 'a\\'b';"`);
});

test('correctly handle double escaped backslashes when theres no raw', () => {
// doing this since if there's a raw value it will prefer that
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I'm misunderstanding your comment, but that's exactly why we have the clean function, that you are already calling. Haven't tested it, but I think you should be fine using load()

Copy link
Member Author

Choose a reason for hiding this comment

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

Mmm the test was passing even before the changes when I called load...will check tho

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, you are right. But I don't know why 🤔 ....

I would have expected the following code to produce exactly the same AST as your manual way.

const ast2 = load("var text = $.text('\\\\');");

But it's not. That's why you were unable to reproduce the issue.

Looks like acorn is doing some preprocessing while converting to the AST so that for whatever reason calling load() brings \\ as the value for the literal instead of \\\\.

So changing the input to ...

const ast = load("var text = $.text('\\\\\\\\');");

... produces the error you were trying to reproduce. And your fix still fixes the issue 😆

That being said, I don't know what's less ugly to write in the test, I will leave that up to you

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup just explored the same and updated the comment...the value generated by acorn is just \\ while raw contains \\\\...I've even thought if we should match this in the svelte parse but tbf it feels more weird (maybe even buggish) what acorn is doing

Copy link

changeset-bot bot commented Mar 9, 2025

🦋 Changeset detected

Latest commit: 9a077cc

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

This PR includes changesets to release 1 package
Name Type
esrap 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

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.

Can’t use double backslash as children in components
2 participants