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

Script & style contents lost when invoked with range option #233

Open
tomblachut opened this issue Jun 16, 2021 · 7 comments
Open

Script & style contents lost when invoked with range option #233

tomblachut opened this issue Jun 16, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@tomblachut
Copy link

Hello 👋

There are some cases when users would like to reformat only parts of file, unfortunately it's not working right now.

Reproduction:
Use following file

<script>
  console.
  log('hello')
</script>
<article>
              <section>
</section>
</article>
{#if
  true}
        <div></div>
{/if}
<style>
    .css.is.best{ align-content:
            baseline}
</style>

and run

prettier index.svelte --range-start 0 --range-end 10

Output:

<scriptprettier:content✂="CiAgY29uc29sZS4KICBsb2coJ2hlbGxvJykK">{}</script>
<article>
              <section>
</section>
</article>
{#if
  true}
        <div></div>
{/if}
<styleprettier:content✂="CiAgICAuY3NzLmlzLmJlc3R7IGFsaWduLWNvbnRlbnQ6CiAgICAgICAgICAgIGJhc2VsaW5lfQo="></style>

Apart from nothing being really formatted (different issue, I've noticed it also does not work in official html), we get those base64 encoded placeholders.

During my investigation I've noticed that parser.preprocess encodes content, and printer.embed decodes it, but printer.embed is not called with custom range used.

I'd like to ask, what is the technical reasoning behind those placeholders? This package uses svelte parser under the hood, and it returns deep AST with embedded JS already parsed (and TS types stripped, right?). Would it be possible to get rid of parse.preprocess if svelte parser had shallow option that would leave text of Script and Style nodes as text? I believe it could be then passed to textToDoc in embed

@dummdidumm
Copy link
Member

Unfortunately getting rid of some kind of snipping is not possible given the way the Svelte compiler is written. From what I understand, the compiler expects vanilla JS/CSS in the script/style tags, and is greedy, meaning the moment it encounters a script or style tag it switches to a JS/CSS parser and parses until it finds a closing tag. If those JS/CSS parsers find anything that's not valid JS/CSS syntax, they throw.
So we have to find another way to handle embeds, by somehow unsnipping everything even when ranges are given.

@dummdidumm dummdidumm added the bug Something isn't working label Jun 16, 2021
@tomblachut
Copy link
Author

Hmm you're right it would be really complex. Shallow parser would open Pandora box of brace nesting and quoting issues...

Snipping unfortunately also messes up offset inside file, length of base64 encode is different than that of original.

@dummdidumm
Copy link
Member

I'm not that familar with how formatting a specific range works in prettier. Possible solutions without knowing more:

  • Check if we can do snipping differently, preserving the offsets
  • Check if we can recalculate the offsets after snipping to adjust for the new lengths

@dummdidumm
Copy link
Member

I dug into this today and found out that right now it's impossible to support range formatting, because we can't hook into Prettier at the required position: prettier/prettier#11404

@KraXen72
Copy link

KraXen72 commented Sep 2, 2023

any update on this? it still doesen't work.

@sjunepark
Copy link

+1

@eriknyk
Copy link

eriknyk commented Jan 25, 2024

This is not solved these days , and it was reported first time 3 years ago. :( :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants