trivago / prettier-plugin-sort-imports

A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order.
Apache License 2.0
3.4k stars 134 forks source link

SyntaxError: 'return' outside of function. when used with Astro #210

Open kb1995 opened 1 year ago

kb1995 commented 1 year ago

Your Environment

Describe the bug

I want to use this plugin with my Astro project. My prettier config looks like this:

{
  "semi": true,
  "bracketSpacing": true,
  "printWidth": 100,
  "trailingComma": "es5",
  "tabWidth": 2,
  "useTabs": false,
  "importOrder": [
    "^astro",
    "<THIRD_PARTY_MODULES>",
    "^types/(.*)$",
    "^store/(.*)$",
    "^config/(.*)$",
    "^content/(.*)$",
    "^layouts/(.*)$",
    "^lib/(.*)$",
    "^utils/(.*)$",
    "^components/(.*)$"
  ],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "plugins": [
    "prettier-plugin-astro",
    "@trivago/prettier-plugin-sort-imports",
    "prettier-plugin-tailwindcss"
  ],
  "pluginSearchDirs": false
}

The plugin works great for most files, but I encountered the following error when I'm using Astro's SSR's redirect function:

["ERROR" - 20:33:45] Error formatting document.
["ERROR" - 20:33:45] 'return' outside of function. (19:2)

Expected behavior

The file to be compiled successfully even when using SSR's return function

Error log

at instantiate (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:72:32)
    at constructor (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:359:12)
    at Parser.raise (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:3339:19)
    at Parser.parseReturnStatement (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:15048:12)
    at Parser.parseStatementContent (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14697:21)
    at Parser.parseStatementContent (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:10364:18)
    at Parser.parseStatement (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14643:17)
    at Parser.parseBlockOrModuleBlockBody (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:15286:25)
    at Parser.parseBlockBody (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:15277:10)
    at Parser.parseBlock (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:15261:10)
    at Parser.parseStatementContent (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14727:21)
    at Parser.parseStatementContent (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:10364:18)
    at Parser.parseStatement (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14643:17)
    at Parser.parseIfStatement (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:15041:28)
    at Parser.parseStatementContent (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14694:21)
    at Parser.parseStatementContent (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:10364:18)
    at Parser.parseStatement (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14643:17)
    at Parser.parseBlockOrModuleBlockBody (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:15286:25)
    at Parser.parseBlockBody (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:15277:10)
    at Parser.parseProgram (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14561:10)
    at Parser.parseTopLevel (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:14548:25)
    at Parser.parse (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:16556:10)
    at Parser.parse (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:11154:18)
    at Object.parse (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/node_modules/@babel/parser/lib/index.js:16608:38)
    at Object.preprocessor (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/preprocessors/preprocessor.js:15:24)
    at Object.defaultPreprocessor [as preprocess] (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/preprocessors/default-processor.js:9:27)
    at Object.preprocess (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier-plugin-tailwindcss/dist/index.js:134:3972)
    at Object.parse (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:7511:25)
    at textToDoc (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8478:39)
    at /Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8466:99
    at wrapParserTryCatch (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier-plugin-astro/dist/index.js:628:16)
    at Object.embed (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier-plugin-astro/dist/index.js:587:36)
    at Object.printSubtree (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8466:32)
    at callPluginPrintFunction (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8593:32)
    at mainPrintInternal (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8550:22)
    at mainPrint (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8537:18)
    at /Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8392:27
    at AstPath.each (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8384:11)
    at AstPath.map (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8391:14)
    at Object.print (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier-plugin-astro/dist/index.js:328:50)
    at callPluginPrintFunction (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8601:26)
    at mainPrintInternal (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8550:22)
    at mainPrint (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8537:18)
    at printAstToDoc (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8529:18)
    at coreFormat (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:8837:20)
    at formatWithCursor2 (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:9021:18)
    at /Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:38168:12
    at Object.format (/Users/kris/Documents/code/products/astro-personal-sidebar/node_modules/prettier/index.js:38182:12)
    at t.default.format (/Users/kris/.vscode/extensions/esbenp.prettier-vscode-9.10.4/dist/extension.js:1:14731)
    at async t.PrettierEditProvider.provideEdits (/Users/kris/.vscode/extensions/esbenp.prettier-vscode-9.10.4/dist/extension.js:1:11417)
    at async $.provideDocumentFormattingEdits (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:98:39748)
["INFO" - 20:33:45] Formatting completed in 25ms.

Contribute to @trivago/prettier-plugin-sort-imports

minht11 commented 1 year ago

We are also experiencing this error. Astro supports early returns inside their script part

---
const { slug } = Astro.params

if (!slug) {
    return new Response(null, { status: 404 })
}
---

In regular javascript top level return is not supported. I have zero idea how any of the prettier plugins are integrated, but maybe source from astro code could be wrapped in fake function before giving it to the babel used in this plugin? Or some sort integration with astro plugin?

nemanjam commented 10 months ago

I have this too, how to solve it?

<script is:inline define:vars={{ defaultTheme: UI.theme }}>
  if (window.basic_script) {
    return;
  }
aldrichdev commented 7 months ago

I also ran into this error trying to save some files. Strangely, changing my Format On Save Mode setting of VS Code to file ended up solving the issue for me, amongst a few others related to the Trivago plugin. I don't think the modifications value (which is what I had) works well with this plugin.

acnebs commented 7 months ago

Does using this with .astro files work fine for you? Mine don't seem to sort, while my .ts files do, and the rest of my .astro documents are in fact being formatted.