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

Format Selection can throw syntax errors #272

Open
1 task
dallonf opened this issue Dec 1, 2023 · 0 comments
Open
1 task

Format Selection can throw syntax errors #272

dallonf opened this issue Dec 1, 2023 · 0 comments

Comments

@dallonf
Copy link

dallonf commented Dec 1, 2023

Your Environment

  • Prettier version: 2.8.8
  • node version: v18.18.2
  • package manager: pnpm@8.9.0
  • IDE: VS Code

Describe the bug

When using "Format Selection" tool to format only part of a document in VS Code with this plugin enabled, it will often fail with a syntax error of "SyntaxError: 'return' outside of function." or similar.

To Reproduce

In a project that has this Prettier plugin configured, add a file with these contents (misformatting and all):

export function something() {
  if (true)
    return   /* space intentional */   5
}

In VS Code with the Prettier plugin, select the interior of the function between curly brackets, and press Cmd/Ctrl-Shift-P to open the command palette and run the "Format Selection" command. Then check the Prettier output in the Output pane for the error.

Expected behavior

The code should be formatted, same as if this plugin was not installed at all.

Screenshots, code sample, etc

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

// prettier.config.js
module.exports = {
  singleQuote: true,
  semi: true,
  plugins: [
    // I commented out the other plugin for this test
    // 'prettier-plugin-tailwindcss',
    '@trivago/prettier-plugin-sort-imports',
  ],
  // `tailwindFunctions` has to do with the commented-out plugin
  tailwindFunctions: ['clsx', 'cn', 'cva'],
  importOrder: [
    // framework-level packages
    '^vitest(/.*)?$',
    '^@testing-library(/.*)?$',
    '^(react|next)(/.*)?$',

    '<THIRD_PARTY_MODULES>',
    '^@/.*', // Absolute imports from the current project
    '^[./]', // Relative imports
  ],
};

Error log

["ERROR" - 9:34:30 AM] Error formatting document.
SyntaxError: 'return' outside of function. (10:2)
    at constructor (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:356:19)
    at TypeScriptParserMixin.raise (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:3223:19)
    at TypeScriptParserMixin.parseReturnStatement (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:13000:12)
    at TypeScriptParserMixin.parseStatementContent (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:12626:21)
    at TypeScriptParserMixin.parseStatementContent (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:9345:18)
    at TypeScriptParserMixin.parseStatementLike (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:12593:17)
    at TypeScriptParserMixin.parseStatementOrSloppyAnnexBFunctionDeclaration (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:12583:17)
    at TypeScriptParserMixin.parseIfStatement (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:12994:28)
    at TypeScriptParserMixin.parseStatementContent (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:12624:21)
    at TypeScriptParserMixin.parseStatementContent (/Users/dallonf/devroot/biggie/node_modules/.pnpm/@babel+parser@7.23.0/node_modules/@babel/parser/lib/index.js:9345:18)

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

  • I'm willing to fix this bug 🥇
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant