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.
You 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 :
Now change trailingComma
to none
Trailing commas are modern JS, but if you really don't like them they can be disabled .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.