VsCode不格式化带单引号的TS / JS文件

时间:2019-12-09 06:26:01

标签: javascript typescript visual-studio-code prettier

在VS Code的最新更新之一之后,在Windows中按Ctrl + Shift + F时,它是自动的 尽管我将其设置为仅使用单引号,但我的所有代码都使用双引号而不是单引号。

这是我的设置文件:

{
"[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"typescript.updateImportsOnFileMove.enabled": "always",
"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"atlascode.jira.workingSite": {
    "baseUrlSuffix": "atlassian.net"
},
"yaml.schemas": {
    "file:///c%3A/Users/kevin/.vscode/extensions/atlassian.atlascode-2.1.5/resources/schemas/pipelines-schema.json": "bitbucket-pipelines.yml"
},
"window.zoomLevel": -1,
"[html]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"git.autofetch": true,
"javascript.preferences.quoteStyle": "single",
"typescript.preferences.quoteStyle": "single",
"prettier.jsxSingleQuote": true,
"prettier.singleQuote": true

}

还有其他人在处理吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

在您的设置文件中,您似乎在使用更漂亮的代码格式。 在最新更新中,漂亮的将读取配置从通用设置文件更改为用于漂亮的设置的专用文件。您可以通过提供的许多选项来配置漂亮的图片。

https://prettier.io/docs/en/configuration.html

示例(JSON):

创建以JSON或YAML格式编写的.prettierrc文件,并带有可选扩展名:.json / .yaml / .yml(无扩展名优先)。

.prettierrc

{
  "singleQuote": true
}

然后在.prettierrc(vscode设置文件)中提供settings.json文件的绝对路径。

settings.json

...
"prettier.configPath": "./.prettierrc"
...

希望这会有所帮助!

答案 1 :(得分:0)

Jins Thomas Shaji 的回答很有帮助。但是除了他说的,你还需要在.prettierrc中添加一行

"jsxSingleQuote": true

所以,结论:

.prettierrc

{
"singleQuote": true, 
"jsxSingleQuote": true,
}

settings.json

"prettier.configPath": "./.prettierrc"