VSCode 单引号到双引号自动替换

VSCode single to double quote automatic replace

当我在 Vue Component.vue 文件上执行 Format Document 命令时 VSCode 将所有单引号字符串替换为双引号字符串。

在我的具体情况下,此规则与需要单引号的 electron-vue lint 配置冲突。

我没有安装更漂亮的扩展(我的设置中没有 prettier.singleQuote

如何自定义 VSCode 来避免这种情况?

我没有安装 prettier 扩展,但在阅读 答案后,我在我的用户设置中从头开始添加(UserSetting.json、Ctrl+、快捷方式):

"prettier.singleQuote": true

部分绿色警告(Unknown configuration setting)单引号不再替换。

我怀疑更漂亮的扩展是不可见的,而是嵌入在 Vetur 扩展中。

对于像我这样的新手:

从顶部的菜单导航栏:Select 文件 -> 首选项 -> 设置。 在搜索文本框中,输入报价 在下方显示的过滤列表中,查找齿轮图标及其旁边的 - "Prettier"。单击复选框以启用 "Prettier: Single Quote"

好吧,就像提到的那个人 (@user2982122) 但不是 File 转到 Code -> Preferences -> Settings,然后查找 Quote、select Prettier 并选中两个框

如@attdona 所述,Vetur 扩展包含更漂亮的内容。

虽然您可以根据接受的答案更改更漂亮的设置,但您也可以更改 vue 组件特定区域的格式化程序。

例如,在这里,我将 Vetur 设置为使用 vscode-typescript 格式化程序,因为它默认使用单引号:

我正在使用打字稿,对我来说,通过在更漂亮的设置下检查 "Tslint integration" 标志(在 vscode 首选项中)解决了这个问题:

来自 vuejs/vetur 问题页面 https://github.com/vuejs/vetur/issues/986# 这个解决方案对我有用。

在 VSCodes settings.json 文件中添加此条目

"vetur.format.defaultFormatterOptions": {
    "prettier": {
        "singleQuote": true
    }
},

您可以在 settings.json

中使用它
"javascript.preferences.quoteStyle": "single"

使用这个扩展。

https://marketplace.visualstudio.com/items?itemName=BriteSnow.vscode-toggle-quotes

cmd ' (ctrl ' on win/Linux) 将在 ' " `

之间循环

正确解法:

我在主根项目中添加 .prettierrc.js 文件 然后写

module.exports = {
    singleQuote: true
  };

看起来这是一个针对此问题的错误:Prettier Bug

None 以上解决方案对我有用。 唯一有用的是,在 package.json:

中添加这行代码
"prettier": {
    "singleQuote": true
  },

对于默认使用 .editorconfig 文件的项目。格式化程序将忽略设置中的规则并使用 .editorconfig 中的规则,那么您可以:

  • 删除 .editorconfig 文件,并使用您的 VSCode 设置。
  • 根据您的文件类型将 quote_type = single 添加到 .editorconfig 文件。您还可以将 quote_type 值设置为 doubleauto.

我在项目文件夹中添加了名为 .prettierrc 的文件。 文件内容:

{
    "singleQuote": true,
    "vetur.format.defaultFormatterOptions": {
        "prettier": {
            "singleQuote": true
        }
    }
}

我在 vscode 中遇到了同样的问题。只需在您的根目录中创建一个 .prettierrc 文件并添加以下 json。 对于单引号添加:

{
  "singleQuote": true
}

双引号加:

  {
      "singleQuote": false
  }

尝试其中一种解决方案

  1. 在 vscode settings.json 文件中添加此条目 "prettier.singleQuote": true
  2. 在vscode中如果你有.editorconfig文件,在根[*]符号下添加这一行quote_type = single
  3. 如果您有 .prettierrc 文件,请在 vscode 中添加此行
{
    "singleQuote": true,
    "vetur.format.defaultFormatterOptions": {
        "prettier": {
            "singleQuote": true
        }
    }
}

请考虑 .editorconfig 覆盖所有内容,使用:

[*]
quote_type = single

只有对我有用的解决方案: 并且仅适用于 Angular 个项目:

只需进入您的项目“.editorconfig”文件并粘贴 'quote_type = single'。 希望它也对你有用。

安装更漂亮的扩展程序并将以下代码粘贴到您的 VSCode settings.json 文件中

 "prettier.useEditorConfig": false,
 "prettier.singleQuote": true

这将忽略您的 .editorconfig 文件设置。

It works for me to check single quote in Prettier as well tslint.autoFixOnSave as true

对于 JSX 使用:

{"jsxSingleQuote": false}
quote_type = single

在 .editorconfig 中添加这个

# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
quote_type = single

对我来说,这两种选择都解决了问题:

  1. 通过在.prettierrc里面添加 - "singleQuote": true

  2. 或在 package.json 中添加以下内容 -> “更漂亮”:{ “单引号”:真 }

尽管我也尝试添加 .prettierrc.js 并关注

module.exports = { 单引号:真 };

这没有用。

对我有用的是设置 .prettierrc.json 配置文件。使用这样的示例配置将其放入项目的根目录:

{
  "singleQuote": true,
  "trailingComma": "all",
  "tabWidth": 2,
  "semi": true,
  "arrowParens": "always"
}

触发格式化文档命令后,一切正常。

旁注:这个解决方案的好处是,由于当前的配置文件,每个团队成员都获得相同的格式输出。

在 .prettierrc 添加

{
  "arrowParens": "avoid",
  "semi": false,
  "singleQuote": true
}

在我的例子中,问题出在字符串中的转义 \ 字符:

message = 'Error argument is not an object, it\'s ' + typeof error

打开 avoidEscape 选项并为该字符串使用双引号解决了问题:

message = "Error argument is not an object, it's " + typeof error

.eslintrc.js

module.exports = {
  rules : {
    // Other rules...
    'quotes' : ['error', 'single', {'avoidEscape' : true}],
  }
}

这对我有用: 尝试右键单击当前文档 并选择“格式化文档”, 并为文档选择您自己的格式扩展名。 :)

在努力解决这个问题之后,我找到了一个有用的工具。如果您点击右下角的 Prettier 字样,您将打开 Output window。在 window 中,一旦你 运行 格式化(在我的例子中是 Alt + Shift + F),你将看到 prettier 将用于格式化文档的所有配置。因此,我们可以清楚地看到在 prettier.singleQuote 中指定 prettier 是错误的。它应该只是 singleQuote。因此,在我的用户根文件夹中包含以下内容的 .prettierrc 文件产生了预期的结果:

{
    "trailingComma": "none",
    "useEditorConfig": false,
    "singleQuote": true
}

此外,请确保您安装了 Prettier 扩展程序。

我在控制 linting 和更漂亮的格式方面遇到了很多问题。我有关于 eslint 的规则,比如

"prettier/prettier": [
      "error",
      { "singleQuote": true, "trailingComma": "none" }
    ],

和 .prettierrc 文件中的规则

{
  "tabWidth": 2
}

但是我的 .prettierrc 文件没有得到处理。我的修复是将 prettier 安装为开发依赖包。所以对我有用的解决方案是安装所有这些包 eslint-config-prettier eslint-plugin-prettierprettier.