withastro / prettier-plugin-astro

Prettier plugin for Astro
Other
479 stars 36 forks source link

🐛 BUG: Error when destructuring with '?' in map loop #383

Closed silveltman closed 12 months ago

silveltman commented 1 year ago

Describe the Bug

this works:

---
const arr = [1, 2, 3]
const object = {
  data: {
    href: '/',
    title: 'Hello',
  },
}
---

{arr.map(() => <button {...object.data} />)}

but this gives an error:

---
const arr = [1, 2, 3]
const object = {
  data: {
    href: '/',
    title: 'Hello',
  },
}
---

{arr.map(() => <button {...object?.data} />)}

vscode error output:

["ERROR" - 14:43:13] Error formatting document.
["ERROR" - 14:43:13] Unexpected token, expected ":" (1:44)
> 1 | arr.map(() => <button {...object?ωP_data} />)
    |                                            ^
SyntaxError: Unexpected token, expected ":" (1:44)
> 1 | arr.map(() => <button {...object?ωP_data} />)
    |                                            ^
    at qr (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier/plugins/babel.mjs:15:1323)
    at Hr (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier/plugins/babel.mjs:15:1472)
    at Object.parse (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier/plugins/babel.mjs:16:16684)
    at Object.parse (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier-plugin-astro/dist/index.js:800:37)
    at Object.parse (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier-plugin-tailwindcss/dist/index.mjs:2382:43963)
    at async parse (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier/index.mjs:16786:11)
    at async textToDoc (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier/index.mjs:17738:19)
    at async wrapParserTryCatch (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier-plugin-astro/dist/index.js:662:16)
    at async file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier-plugin-astro/dist/index.js:572:23
    at async printEmbeddedLanguages (file:///Users/silveltman/Documents/Github/monorepo/astro/node_modules/prettier/index.mjs:17682:20)
["INFO" - 14:43:13] Formatting completed in 55ms.

Steps to Reproduce

  1. npm init astro using template blank
  2. create the html given above
  3. try to format
  4. Error! read the vscode ouput
Princesseuh commented 1 year ago

Ah yeah, in this function: https://github.com/withastro/prettier-plugin-astro/blob/e5dc2ec416dec83332ef86a792fb558752281cad/src/printer/embed.ts#L263 we need to check to make sure we don't replace those identifiers (keywords? operator?). Should be an easy fix, sorry for the inconvenience!

silveltman commented 12 months ago

Is it correct that that the fix has not been published yet? I don't see a newer version available

Princesseuh commented 12 months ago

Is it correct that that the fix has not been published yet? I don't see a newer version available

Yes, not published yet! Sorry for the delay, will do the release this week.