admin管理员组文章数量:1135091
I'm using Prettier in VS Code. I noticed that when using format on save, Prettier adds trailing commas on the last line of an object every time.
For example, let's say I had a JS object like this:
obj = {
hello: 'hello',
world: 'world'
}
Prettier turns it into this:
obj = {
hello: 'hello',
world: 'world',
}
Notice the extra comma after
'world'
Didn't find in the settings an option to fix this.
I'm using Prettier in VS Code. I noticed that when using format on save, Prettier adds trailing commas on the last line of an object every time.
For example, let's say I had a JS object like this:
obj = {
hello: 'hello',
world: 'world'
}
Prettier turns it into this:
obj = {
hello: 'hello',
world: 'world',
}
Notice the extra comma after
'world'
Didn't find in the settings an option to fix this.
Share Improve this question edited Dec 30, 2020 at 14:41 Penny Liu 17.3k5 gold badges86 silver badges108 bronze badges asked Apr 22, 2020 at 16:49 SiiilverSurferSiiilverSurfer 9391 gold badge7 silver badges13 bronze badges 4 |8 Answers
Reset to default 154You can update .prettierrc.json
and set option trailingComma
to none
like:
{
"trailingComma" : "none",
...
}
Trailing commas are a code-style convention that was introduced to avoid spurious differences in version controls (ie Git).
Imagine you have version controlled code and you have to change it. When you add a new line to your object without the trailing comma, you will have to change the original last line by adding a comma. In version control this then shows up as two changed lines. The code reviewer or a future dev have to check if you effectively changed the last line, or only added the comma.
Zuckerberg's answer shows you how to change it. However, it is better to change your style, than change prettier's style.
Trailing commas are a standard already because they result in a cleaner commit history. If you have to add a property down the road, git will show a single line changed instead of a new line AND the new comma on the previous line.
To modify the setting in VSCode:
- Go to FILE -> PREFERENCES -> SETTINGS. (VS Code Menus)
- Settings window should open. Above (Top) there is a search. Type "Prettier"
- You should see the available Prettier settings. You can modify them
Now change trailingComma
to none
Trailing commas are modern JS, but if you really don't like them they can be disabled.
In my case, the configuration above was not enough:
{
"trailingComma" : "none",
}
For this to work for me, in addition to this setting, I had to remove from setting.json:
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
And with the extension already installed Prettier
next, enter the command
ctrl + shift + P
in vscode
Click on Configure...
Choose Prettier - Code formatter
Now this:
const a = {
a: 10,
b: 15,
};
turns into this:
const a = {
a: 10,
b: 15
}
If anyone's wondering how to have VSCode's prettier
extension behave like pre-commit
's prettier
hook, this worked for me:
"prettier.trailingComma": "all"
The above should be used in combination with prettier
as thedefaultFormatter
for the relevant files, as suggested by others.
If that alone is not enough and you're also using EditorConfig
for other customizations, you may need to also add:
"prettier.useEditorConfig": false
To disable trailing comma for one specific file:
- Update prettier.config.cjs file:
config: {
overrides: [
{
files: ['.hintrc'],
options: {
trailingComma: 'none',
},
},
],
}
- Restart your editor/ESLint server after making this change.
本文标签: javascriptTrailing comma after last line in objectStack Overflow
版权声明:本文标题:javascript - Trailing comma after last line in object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736813798a1954004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"trailingComma": "all"
then or check if you are eslint also. That might be overriding some setting. – Zuckerberg Commented Apr 22, 2020 at 16:56Default value changed from es5 to all in v3.0.0
– Bernhard Döbler Commented Jul 17, 2024 at 8:33