prettier-plugin-sort-imports icon indicating copy to clipboard operation
prettier-plugin-sort-imports copied to clipboard

Removes all line breaks in Svelte

Open tmaxmax opened this issue 4 years ago • 5 comments
trafficstars

Summary

The problem is as stated in the title: when formatting with this plugin, all line breaks disappear.

Actual behavior

<script>
  import Component from './Component.js'
  import { onMount } from 'svelte'
  onMount(() => {
    // --snip--
  })
</script>

Expected behavior

<script>
  import Component from './Component.js'
  import { onMount } from 'svelte'

  onMount(() => {
    // --snip--
  })
</script>

If there are more line breaks, either between imports or normal lines of code, they are all deleted.

tmaxmax avatar Mar 11 '21 16:03 tmaxmax

The same happens in .vue files.

frontendphil avatar Apr 08 '21 09:04 frontendphil

Not only line breaks, but also mangles comments before:

<script lang="ts">
  import poop from './poop';
  import noop from './noop';

  // Comment one
  let someProp: string = "";

  // Comment two
  let anotherProp: string = "";
</script>

<main />

after:

<script lang="ts">
  import noop from "./noop";
  import poop from "./poop";
  ';

  // Comme
  let someProp: string = "";
  ";

  // Comme
  let anotherProp: string = "";
</script>

<main />

see minimal repro: https://github.com/Sharsie/prettier-plugin-sort-imports-svelte-repro

Sharsie avatar Oct 22 '21 22:10 Sharsie

Is this issue still valid? I just applied the plugin to a svelte project, and it did do anything bad regarding line breaks on first glance.

Also, you probably mean "remove all empty lines", not literally "removes all line breaks", right? After all, code block in the actual behavior certainly contains line breaks.

bluenote10 avatar Feb 11 '23 11:02 bluenote10

@bluenote10 I updated the minimal repro to v4 and can confirm both issues appear to be still present.

Original:

<script lang="ts">
  import poop from './poop';
  import noop from './noop';


  onMount(() => {
    // --snip--
  })

  // Comment one
  let someProp: string = "";


  // Comment two
  let anotherProp: string = "";
</script>

<main />

With the plugin:

<script lang="ts">
  import noop from "./noop";
  import poop from "./poop";
  onMount(() => {
     {
    // -
  });
   })

  // Comm
  let someProp: string = "";
  ";


  // Comm
  let anotherProp: string = "";
</script>

<main />

Without the plugin:

<script lang="ts">
  import poop from "./poop";
  import noop from "./noop";

  onMount(() => {
    // --snip--
  });

  // Comment one
  let someProp: string = "";

  // Comment two
  let anotherProp: string = "";
</script>

<main />

Sharsie avatar Feb 11 '23 21:02 Sharsie

@Sharsie I cannot reproduce the problem: This is how your code looks for me after formatting:

image

And this is the commit where applied import ordering in one of my projects: https://github.com/bluenote10/guitar-scales-trainer/commit/95bc1659a1d8790c7538b50d93d668b6c39a1a75

Comparing your reproduction repo with my repo I noticed a few differences:

  • I'm using newer versions of prettier and prettier-plugin-svelte.
  • My npm script for formatting is prettier --plugin-search-dir . --write . which is what the default svelte kit template comes with.

Not sure if this could explain the difference?

bluenote10 avatar Feb 12 '23 22:02 bluenote10