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

Formatting a selection within a return statement can fail #325

Open
1 task done
forivall opened this issue Dec 6, 2024 · 0 comments
Open
1 task done

Formatting a selection within a return statement can fail #325

forivall opened this issue Dec 6, 2024 · 0 comments

Comments

@forivall
Copy link

forivall commented Dec 6, 2024

Your Environment

  • Prettier version: 3.3.3
  • Plugin version: 4.3.0, retested with 5.0.1, still reproducable
  • node version: 22.11.0
  • package manager: [email protected]
  • IDE: VScode

Describe the bug

Formatting a selection from within VScode can fail

To Reproduce

With the following code:

function doStuff() {
  return [
    { foo: 'bar',
    },
  ];
}

select only the return statement

  return [
    { foo: 'bar',
    },
  ];

Format the selection

Expected behavior

The selection should be formatted as

  return [{ foo: 'bar' }];

Screenshots, code sample, etc

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

see the below log. Note the rangeEnd and rangeStart, that can probably be used to detect when a range is being formatted and skip the plugin processing, as a first-pass fix.

I verified that this plugin is the issue by removing the plugin from the config; it works as expected when I do so.

Error log

vscode error log
["INFO" - 6:23:40 PM] Formatting file:///Users/forivall/code/sandbox/prettier-sort-imports-repro/repro.js
["INFO" - 6:23:40 PM] Using config file at /Users/forivall/code/sandbox/prettier-sort-imports-repro/.prettierrc
["INFO" - 6:23:40 PM] PrettierInstance:
{
  "modulePath": "/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/prettier",
  "messageResolvers": {},
  "version": "3.3.3"
}
["INFO" - 6:23:40 PM] Using ignore file (if present) at /Users/forivall/code/sandbox/prettier-sort-imports-repro/.prettierignore
["INFO" - 6:23:40 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 6:23:40 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 6:23:40 PM] Prettier Options:
{
  "filepath": "/Users/forivall/code/sandbox/prettier-sort-imports-repro/repro.js",
  "parser": "babel",
  "rangeEnd": 61,
  "rangeStart": 21,
  "useTabs": false,
  "tabWidth": 2,
  "endOfLine": "lf",
  "bracketSpacing": true,
  "importOrder": [
    "<THIRD_PARTY_MODULES>",
    "^(@ttc|@ttc-dev|ttc-)/(.*)",
    "^[./]"
  ],
  "importOrderCaseInsensitive": true,
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "importOrderParserPlugins": [
    "typescript",
    "decorators-legacy",
    "importAssertions"
  ],
  "plugins": [
    "/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/index.js",
    "/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/prettier-plugin-packagejson/lib/index.cjs"
  ],
  "proseWrap": "never",
  "semi": true,
  "singleQuote": true,
  "trailingComma": "all"
}
["ERROR" - 6:23:40 PM] Error formatting document.
SyntaxError: 'return' outside of function. (1:0)
    at constructor (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:351:19)
    at TypeScriptParserMixin.raise (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:3237:19)
    at TypeScriptParserMixin.parseReturnStatement (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:12610:12)
    at TypeScriptParserMixin.parseStatementContent (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:12267:21)
    at TypeScriptParserMixin.parseStatementContent (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:9110:18)
    at TypeScriptParserMixin.parseStatementLike (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:12236:17)
    at TypeScriptParserMixin.parseModuleItem (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:12213:17)
    at TypeScriptParserMixin.parseBlockOrModuleBlockBody (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:12793:36)
    at TypeScriptParserMixin.parseBlockBody (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:12786:10)
    at TypeScriptParserMixin.parseProgram (/Users/forivall/code/sandbox/prettier-sort-imports-repro/node_modules/@babel/parser/lib/index.js:12113:10)
["INFO" - 6:23:40 PM] Formatting completed in 22ms.

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