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

prettier-plugin-svelte doesn't work with eslint-plugin-prettier #472

Closed
chumager opened this issue Jun 22, 2022 · 1 comment · Fixed by #483, #484 or #553 · May be fixed by WontonSam/doc-code-samples#1203 or WontonSam/doc-code-samples#1341
Closed

Comments

@chumager
Copy link

What version of eslint are you using?
v8.17.0
What version of prettier are you using?
2.7.1
What version of eslint-plugin-prettier are you using?
4.0.0
Please paste any applicable config files that you're using (e.g. .prettierrc or .eslintrc files)

{
  "root": true,
  "env": {
    "node": true,
    "es2021": true,
    "mocha": true,
    "browser": true
  },
  "ignorePatterns": ["dist", "**/*.html"],
  "extends": ["eslint:recommended"],
  "plugins": ["prettier", "svelte3"],
  "rules": {
    "strict": 0,
    "no-console": 0,
    "max-len": [
      "error",
      {
        "code": 120,
        "ignoreComments": true
      }
    ],
    "prettier/prettier": [
      "warn",
      {
        "printWidth": 120,
        "tabWidth": 2,
        "bracketSpacing": false,
        "trailingComma": "none",
        "arrowParens": "avoid",
        "pluginSearchDirs": ["."],
        "plugins": ["prettier-plugin-svelte"]
      }
    ]
  },
  "overrides": [
    {
      "files": ["*.svelte"],
      "processor": "svelte3/svelte3"
    }
  ],
  "parserOptions": {
    "sourceType": "module",
    "ecmaVersion": 2021
  }
}

What source code are you linting?

<script>
  /* TODO:
   * implementar links para editar con diálogos y definir si lo que se va a editar
   * es un string, un text o html
   */
  import {onMount} from "svelte";
  import {writable} from "svelte/store";
  function createRef(val) {
    const ref = writable(val);
    return [ref, ref.set];
  }
  // @ts-ignore
  import {requestBase} from "request-strategy";
  
  const request = requestBase(createRef, cb => onMount(() => cb), true);
  export let debug;
  export let locale;
  export let allowTranslations;
  export let showTranslations;
  export let context;
  export let subcontext;
  export let text;
  export let baseurl;
  let input;
  let mounted = false;
  let firstRequest = true;
  let result, error, mutate;
  // eslint-disable-next-line max-len
  $: url =
    (baseurl ? baseurl : "") +
    `/APP/AAA/Translate.translate?xhr=true&context=${context}&sub-context=${subcontext}&text=${text}&locale=${$locale}`;
  onMount(() => {
    ({result, error, mutate} = request(url, "SWR", undefined, true));
    mounted = true;
  });
  $: {
    if ($locale && mounted && firstRequest) {
      firstRequest = false;
    } else if ($locale && mounted && !firstRequest) {
      mutate(url);
    }
  }
  $: width = "min(calc(" + ($result?.text?.length || 10) + "ch + 5ch), 100%)";
  async function onChange() {
    // @ts-ignore
    const cookies = new Map(document.cookie.split("; ").map(c => c.split("=")));
    const xsrf = cookies.get("XSRF-TOKEN");
    const response = await fetch(url, {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
        "XSRF-TOKEN": xsrf
      },
      body: JSON.stringify({translation: input.value}),
      mode: "cors",
      credentials: "include"
    });
    const res = await response.json();
    if (res.status === "ok") {
      // await Promise.delay(500);
      //TODO forzar el mutate
      await mutate(undefined, true);
    }
  }
</script>
<svelte:options tag={null}/>

{#if $error}
  Error loading translation
{:else if !$result}
  Cargando...
{:else}
  <span class={$showTranslations ? $result.status.toLowerCase() : ""}>
    {#if $allowTranslations}
      <input 
value={$result.text} 
bind:this={input}
on:change={onChange}
on:click|preventDefault|stopPropagation
style:width
            />
    {:else}
      {$result.text}
    {/if}
  </span>
{/if}
{#if typeof debug !== "undefined"}
  <div> locale {$locale}</div>
  <div> allowTranslations {$allowTranslations}</div>
  <div> showTranslations {$showTranslations}</div>
  <div> context {context}</div>
  <div> subContext {subcontext}</div>
  <div> text {text}</div>
  {#if $result}
    <div> result {$result.text}</div>
  {/if}
  <div> error {$error}</div>
{/if}
<style>
.ok {
  background-color: green;
  color: white;
  border: dotted;
}
.failed {
  background-color: red;
  color: white;
  border: dotted;
}
.waiting {
  background-color: yellow;
  border: dotted;
}
.generic {
  background-color: lightblue;
  border: dotted;
}

</style>

What did you expect to happen?
I want to be able to format svelte files just like prettier do.

What actually happened?
I'm trying to add a plugin to prettier to allow svelte files to be linted and formated but, I get this output:

pnpm eslint src/lib/Translations.svelte

/home/jcmunoz/Dropbox/Develop/node.js/itdfw-monorepo/web-components/itd-wc-translate-svelte/src/lib/Translations.svelte
  66:26  warning  Replace `⏎);(` with `;`  prettier/prettier
  68:12  warning  Replace `⏎);(` with `;`  prettier/prettier
  70:19  warning  Replace `⏎);(` with `;`  prettier/prettier
  73:69  warning  Replace `⏎);(` with `;`  prettier/prettier
  74:28  warning  Replace `⏎);(` with `;`  prettier/prettier
  76:20  warning  Replace `⏎);(` with `;`  prettier/prettier
  77:17  warning  Replace `⏎);(` with `;`  prettier/prettier
  78:20  warning  Replace `⏎);(` with `;`  prettier/prettier
  83:20  warning  Replace `⏎);(` with `;`  prettier/prettier
  87:34  warning  Replace `⏎);(` with `;`  prettier/prettier
  88:24  warning  Replace `⏎);(` with `;`  prettier/prettier
  89:46  warning  Replace `⏎);(` with `;`  prettier/prettier
  90:44  warning  Replace `⏎);(` with `;`  prettier/prettier
  91:25  warning  Replace `⏎);(` with `;`  prettier/prettier
  92:31  warning  Replace `⏎);(` with `;`  prettier/prettier
  93:19  warning  Replace `⏎);(` with `;`  prettier/prettier
  94:15  warning  Replace `⏎);(` with `;`  prettier/prettier
  95:31  warning  Replace `⏎);(` with `;`  prettier/prettier
  97:22  warning  Insert `;`               prettier/prettier

✖ 19 problems (0 errors, 19 warnings)
  0 errors and 19 warnings potentially fixable with the `--fix` option.

If I use prettier alone, it formats the file correctly, using the same configuration as in the .eslintrc

{
  "printWidth": 120,
  "tabWidth": 2,
  "bracketSpacing": false,
  "trailingComma": "none",
  "arrowParens": "avoid",
  "plugins": ["prettier-plugin-svelte"],
}
pnpm prettier  src/lib/Translations.svelte
<svelte:options tag={null} />

<script>
  /* TODO:
   * implementar links para editar con diálogos y definir si lo que se va a editar
   * es un string, un text o html
   */
  import {onMount} from "svelte";
  import {writable} from "svelte/store";
  function createRef(val) {
    const ref = writable(val);
    return [ref, ref.set];
  }
  // @ts-ignore
  import {requestBase} from "request-strategy";

  const request = requestBase(createRef, cb => onMount(() => cb), true);
  export let debug;
  export let locale;
  export let allowTranslations;
  export let showTranslations;
  export let context;
  export let subcontext;
  export let text;
  export let baseurl;
  let input;
  let mounted = false;
  let firstRequest = true;
  let result, error, mutate;
  // eslint-disable-next-line max-len
  $: url =
    (baseurl ? baseurl : "") +
    `/APP/AAA/Translate.translate?xhr=true&context=${context}&sub-context=${subcontext}&text=${text}&locale=${$locale}`;
  onMount(() => {
    ({result, error, mutate} = request(url, "SWR", undefined, true));
    mounted = true;
  });
  $: {
    if ($locale && mounted && firstRequest) {
      firstRequest = false;
    } else if ($locale && mounted && !firstRequest) {
      mutate(url);
    }
  }
  $: width = "min(calc(" + ($result?.text?.length || 10) + "ch + 5ch), 100%)";
  async function onChange() {
    // @ts-ignore
    const cookies = new Map(document.cookie.split("; ").map(c => c.split("=")));
    const xsrf = cookies.get("XSRF-TOKEN");
    const response = await fetch(url, {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
        "XSRF-TOKEN": xsrf
      },
      body: JSON.stringify({translation: input.value}),
      mode: "cors",
      credentials: "include"
    });
    const res = await response.json();
    if (res.status === "ok") {
      // await Promise.delay(500);
      //TODO forzar el mutate
      await mutate(undefined, true);
    }
  }
</script>

{#if $error}
  Error loading translation
{:else if !$result}
  Cargando...
{:else}
  <span class={$showTranslations ? $result.status.toLowerCase() : ""}>
    {#if $allowTranslations}
      <input
        value={$result.text}
        bind:this={input}
        on:change={onChange}
        on:click|preventDefault|stopPropagation
        style:width
      />
    {:else}
      {$result.text}
    {/if}
  </span>
{/if}
{#if typeof debug !== "undefined"}
  <div>locale {$locale}</div>
  <div>allowTranslations {$allowTranslations}</div>
  <div>showTranslations {$showTranslations}</div>
  <div>context {context}</div>
  <div>subContext {subcontext}</div>
  <div>text {text}</div>
  {#if $result}
    <div>result {$result.text}</div>
  {/if}
  <div>error {$error}</div>
{/if}

<style>
  .ok {
    background-color: green;
    color: white;
    border: dotted;
  }
  .failed {
    background-color: red;
    color: white;
    border: dotted;
  }
  .waiting {
    background-color: yellow;
    border: dotted;
  }
  .generic {
    background-color: lightblue;
    border: dotted;
  }
</style>

I don't know if my configuration is wrong or if the eslint-plugin-prettier can't load other plugins.

Regards.

@JounQin
Copy link
Member

JounQin commented Jun 27, 2022

It should be easy to fix this by adding svelte into

const parserBlocklist = [
'babel',
'babylon',
'flow',
'typescript',
'vue',
'markdown',
'html',
'mdx',
'angular',
];

Would you like to raise a PR?

JounQin added a commit that referenced this issue Jun 30, 2022
JounQin added a commit that referenced this issue Jun 30, 2022
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this issue Jul 4, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier) | devDependencies | minor | [`4.1.0` -> `4.2.1`](https://renovatebot.com/diffs/npm/eslint-plugin-prettier/4.1.0/4.2.1) |

---

### Release Notes

<details>
<summary>prettier/eslint-plugin-prettier</summary>

### [`v4.2.1`](https://github.com/prettier/eslint-plugin-prettier/blob/HEAD/CHANGELOG.md#&#8203;421)

[Compare Source](prettier/eslint-plugin-prettier@v4.2.0...v4.2.1)

##### Patch Changes

-   [#&#8203;485](prettier/eslint-plugin-prettier#485) [`5736ed5`](prettier/eslint-plugin-prettier@5736ed5) Thanks [@&#8203;JounQin](https://github.com/JounQin)! - chore: reuse prettierRcOptions instead of resolveConfig again

### [`v4.2.0`](https://github.com/prettier/eslint-plugin-prettier/blob/HEAD/CHANGELOG.md#&#8203;420)

[Compare Source](prettier/eslint-plugin-prettier@v4.1.0...v4.2.0)

##### Minor Changes

-   [#&#8203;483](prettier/eslint-plugin-prettier#483) [`7bd70b6`](prettier/eslint-plugin-prettier@7bd70b6) Thanks [@&#8203;JounQin](https://github.com/JounQin)! - feat: support svelte out of box

    close [#&#8203;472](prettier/eslint-plugin-prettier#472), close [#&#8203;482](prettier/eslint-plugin-prettier#482)

    We recommend to use [`eslint-plugin-svelte`](https://github.com/ota-meshi/eslint-plugin-svelte) instead of [`eslint-plugin-svelte3`](https://github.com/sveltejs/eslint-plugin-svelte3).

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [x] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <[email protected]>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1442
Reviewed-by: Epsilon_02 <[email protected]>
Co-authored-by: Calciumdibromid Bot <[email protected]>
Co-committed-by: Calciumdibromid Bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment