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

Does the plugin have Astro compatibility? #96

Closed
RodrigoTomeES opened this issue May 21, 2023 · 13 comments · Fixed by #97
Closed

Does the plugin have Astro compatibility? #96

RodrigoTomeES opened this issue May 21, 2023 · 13 comments · Fixed by #97
Assignees

Comments

@RodrigoTomeES
Copy link

RodrigoTomeES commented May 21, 2023

Hi,

Does the plugin have Astro compatibility?

Thanks!

@RodrigoTomeES RodrigoTomeES changed the title Does the plugin has Astro Compatibility? Does the plugin have Astro compatibility? May 21, 2023
@IanVS
Copy link
Owner

IanVS commented May 21, 2023

Yes, I use it in my own Astro project.

@IanVS IanVS closed this as completed May 21, 2023
@RodrigoTomeES
Copy link
Author

@IanVS I mentioned it because I recently returned to a project using the latest version of Astro, and it stopped working. The imports are no longer being sorted, and since it's not listed in the compatibility table, I thought that might be the issue.

@IanVS
Copy link
Owner

IanVS commented May 21, 2023

@RodrigoTomeES can you share your .prettierrc or even better a reproduction?

@RodrigoTomeES
Copy link
Author

@IanVS sure, here you have. thanks!

{
  "semi": true,
  "singleQuote": true,
  "endOfLine": "lf",
  "tabWidth": 2,
  "useTabs": false,
  "printWidth": 80,
  "overrides": [
    {
      "files": ["**/*.css"],
      "options": {
        "singleQuote": false
      }
    }
  ],
  "plugins": [
    "prettier-stylelint",
    "prettier-plugin-astro",
    "@ianvs/prettier-plugin-sort-imports",
    "prettier-plugin-tailwindcss"
  ],
  "importOrder": [
    "<THIRD_PARTY_MODULES>",
    "",
    "^@/atoms/(.*)$",
    "^@/molecules/(.*)$",
    "^@/organisms/(.*)$",
    "^@/templates/(.*)$",
    "^@/pages/(.*)$",
    "^@/layouts/(.*)$",
    "",
    "^@/(.*)$",
    "^[./]",
    "",
    "<TYPES>",
    "<TYPES>^[./]"
  ],
  "importOrderMergeDuplicateImports": true,
  "pluginSearchDirs": false
}

@IanVS
Copy link
Owner

IanVS commented May 21, 2023

It could be because you're using prettier-plugin-tailwindcss. Recent versions do include this plugin in its compatibility list, so make sure you're on the latest there.

I upgraded my project to Astro 2.5.1, and was still able to sort correctly. I can look into it a bit more if you're willing to put together a small reproduction and share it.

@RodrigoTomeES
Copy link
Author

@IanVS mmm I have all my dependencies updated, look:

$ yarn outdated
➤ YN0000: ┌ Checking for outdated dependencies
➤ YN0000: └ Completed in 2s 917ms

➤ YN0000: ✨ All your dependencies are up to date!
➤ YN0000: Done in 2s 918ms

This is the project: https://github.com/RodrigoTomeES/awa-db

@IanVS
Copy link
Owner

IanVS commented May 22, 2023

It looks like the latest version of prettier-plugin-astro broke compatibility with this plugin. Using 0.8.0 works fine. It looks like it might be due to them changing from the typescript parser to the babel-ts parser. I should be able to restore compatibility on this end. In the meantime, I'd suggest sticking to 0.8.0 of the astro plugin for now.

@IanVS IanVS reopened this May 22, 2023
@IanVS IanVS closed this as completed in #97 May 22, 2023
IanVS added a commit that referenced this issue May 22, 2023
Restores compatibility with the latest version of
`prettier-plugin-astro`.

Fixes #96.

To confirm, I made this change in the `node_modules` of the project in
the above issue, and confirmed that sorting began working correctly
again.
@IanVS
Copy link
Owner

IanVS commented May 22, 2023

Support has been restored in 4.0.0-beta.0. I've opened a PR on your repo to update it. RodrigoTomeES/awa-db#1.

Thanks for the report!

@RodrigoTomeES
Copy link
Author

@IanVS wow, thanks! I will test it this afternoon and aprove it 🚀

@bobalazek
Copy link

bobalazek commented Jun 29, 2024

Is there an issue currently with Astro and the plugin?

I am experiencing a SyntaxError: 'return' outside of function. (23:2) issue (basically same as here: trivago/prettier-plugin-sort-imports#210):

["ERROR" - 08:30:16] Error formatting document.
SyntaxError: 'return' outside of function. (23:2)
    at constructor (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:351:19)
    at TypeScriptParserMixin.raise (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:3233:19)
    at TypeScriptParserMixin.parseReturnStatement (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:12597:12)
    at TypeScriptParserMixin.parseStatementContent (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:12254:21)
    at TypeScriptParserMixin.parseStatementContent (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:9104:18)
    at TypeScriptParserMixin.parseStatementLike (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:12223:17)
    at TypeScriptParserMixin.parseStatementListItem (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:12203:17)
    at TypeScriptParserMixin.parseBlockOrModuleBlockBody (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:12780:61)
    at TypeScriptParserMixin.parseBlockBody (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:12773:10)
    at TypeScriptParserMixin.parseBlock (/Users/borut/Development/github/bobalazek/project/node_modules/@babel/parser/lib/index.js:12761:10)

package.json

{
  "dependencies": {
      "astro": "^4.10.2",
  },
  "devDependencies": {
      "@ianvs/prettier-plugin-sort-imports": "^4.3.0",
      "@typescript-eslint/parser": "7.14.1",
      "eslint": "^9.6.0",
      "eslint-plugin-astro": "1.2.2",
      "prettier": "^3.3.2",
      "prettier-plugin-astro": "^0.14.0",
      "prettier-plugin-packagejson": "^2.5.0",
      "prettier-plugin-tailwindcss": "^0.6.5"
    },
  }

prettier.config.js

/** @type {import("prettier").Config} */
export default {
  printWidth: 100,
  tabWidth: 2,
  semi: true,
  singleQuote: true,
  useTabs: false,
  trailingComma: 'es5',
  plugins: [
    'prettier-plugin-astro',
    'prettier-plugin-packagejson',
    '@ianvs/prettier-plugin-sort-imports',
    'prettier-plugin-tailwindcss',
  ],
  importOrder: [
    '<TYPES>',
    '',
    '<TYPES>^[.]',
    '',
    '<BUILT_IN_MODULES>',
    '',
    '<THIRD_PARTY_MODULES>',
    '',
    '^@app/(.*)$',
    '',
    '^[./]',
  ],
  overrides: [
    {
      files: ['.*', '*.md', '*.toml', '*.yml', '*.yaml'],
      options: {
        useTabs: false,
      },
    },
    {
      files: ['**/*.astro'],
      options: {
        parser: 'astro',
      },
    },
  ],
};

If I remove @ianvs/prettier-plugin-sort-imports from the plugins, it seems to be working as expected.

@IanVS
Copy link
Owner

IanVS commented Jun 29, 2024

@bobalazek would you be willing to put together a minimal reproduction that I can look into?

Does it fail on the command line, in vscode, or both?

@bobalazek
Copy link

Hey @IanVS - sure, here it is:
https://github.com/bobalazek/prettier-imports-repro

And yes, happens on both CLI and VSCode

@IanVS
Copy link
Owner

IanVS commented Jul 1, 2024

Thanks for the reproduction, @bobalazek, that was very helpful. I've got a PR up to address your issue.

IanVS added a commit that referenced this issue Jul 12, 2024
Addresses
#96 (comment)

In Astro, it's possible to [return a
Response](https://docs.astro.build/en/guides/server-side-rendering/#return-a-response-object)
from inside of a component script, without wrapping it in a function.
This doesn't look like valid JavaScript, so the babel parser choked on
it.

This change checks to see if the file was parsed with the astro plugin,
and if so, enables the `allowReturnOutsideFunction` parser option.

This is admittedly a bit of a band-aid. I think the more correct
approach is to parse the code into AST using the configured parser,
rather than always using our version of babel. This is what some other
prettier plugins do. Some, like tailwindcss, goes so far as to load a
list of compatible other prettier plugins to enable cross-compatibility.

But, that's a much bigger change that will potentially require a
breaking change version bump, so I didn't tackle it here.

The other thing I did do here, is update the README a bit. I added
`Astro` to our supported frameworks as experimental (there may be other
parser options we need to set), and also included support for Svelte,
which we've always supported, as far as I know, but don't really test
for (it would be good to make some tests sometime).
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 a pull request may close this issue.

3 participants