Prettier:保持装饰器的多个参数在同一行
I’ve run into this exact kind of formatting quirk with Prettier before, so I feel your frustration! The extra spaces inside the @OneToMany() parentheses are likely due to a specific Prettier configuration setting or a change in how the tool handles decorator function calls in newer versions. Here are a few solid solutions to fix this:
1. Quick Fix: Ignore the Specific Line with a Comment
If you only need to fix this one line and don’t want to tweak global settings, add a // prettier-ignore comment right above the decorator. This tells Prettier to skip formatting that line entirely:
// prettier-ignore @OneToMany(() => Address, address => address.contact, { cascade: true })
This is perfect for one-off cases where you want to preserve your original formatting without adjusting your entire config.
2. Global Fix: Adjust Prettier’s functionParenSpacing Setting
The root cause here is almost certainly the functionParenSpacing configuration option, which controls whether spaces are added inside the parentheses of function calls. If this is set to true in your config (or if a newer Prettier version is applying it unexpectedly), it will add those extra spaces you’re seeing.
To fix this for all your code, update your Prettier config file (.prettierrc, .prettierrc.json, or prettier.config.js) to set this option to false:
{ "functionParenSpacing": false, // Keep your existing configuration options here }
This will ensure all function calls—including decorators like @OneToMany—have no extra spaces inside their parentheses, matching your original desired formatting.
3. Resolve ESLint Conflicts (If Using ESLint + Prettier)
If you’re using ESLint alongside Prettier, make sure there aren’t any conflicting ESLint rules overriding Prettier’s formatting. For example, the space-in-parens ESLint rule might be enforcing spaces inside parentheses, which would clash with your Prettier settings.
To fix this, use the eslint-config-prettier package to disable all ESLint rules that conflict with Prettier, and eslint-plugin-prettier to run Prettier as an ESLint rule. This keeps your formatting consistent across both tools and eliminates unexpected overrides.
内容的提问来源于stack exchange,提问作者Sascha

