.prettierrc.json 文件示例,建议直接使用以下规则:

{
  "$schema": "https://json.schemastore.org/prettierrc",
  "arrowParens": "always",
  "bracketSameLine": false,
  "bracketSpacing": true,
  "endOfLine": "lf",
  "jsxSingleQuote": true,
  "printWidth": 100,
  "semi": false,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "none"
}

所有参数见官网地址:https://json.schemastore.org/prettierrc

已将参数说明翻译为中文,方便查看:

"properties": {
  "arrowParens": {
    "description": "在唯一参数的箭头函数周围加括号",
    "default": "always",
    "oneOf": [
      {
        "enum": ["always"],
        "description": "(x) => x"
      },
      {
        "enum": ["avoid"],
        "description": "x => x"
      }
    ]
  },
  "bracketSameLine": {
    "description": "将>的开始标记放在最后一行,而不是在新行上",
    "default": false,
    "type": "boolean"
  },
  "bracketSpacing": {
    "description": "在括号之间使用空格",
    "default": true,
    "type": "boolean"
  },
  "cursorOffset": {
    "description": "打印(到stderr),格式化后光标将移动到指定位置",
    "default": -1,
    "type": "integer"
  },
  "embeddedLanguageFormatting": {
    "description": "控制pretier如何格式化文件中嵌入的代码",
    "default": "auto",
    "oneOf": [
      {
        "enum": ["auto"],
        "description": "自动格式化嵌入代码"
      },
      {
        "enum": ["off"],
        "description": "不自动格式化嵌入代码"
      }
    ]
  },
  "endOfLine": {
    "description": "应用哪一个行尾字符",
    "default": "lf",
    "oneOf": [
      {
        "enum": ["lf"],
        "description": "仅换行(\\n),在Linux和macOS以及git repos中很常见"
      },
      {
        "enum": ["crlf"],
        "description": "回车+换行字符(\\r\\n),在Windows上很常见"
      },
      {
        "enum": ["cr"],
        "description": "只有回车字符(\\r),很少使用"
      },
      {
        "enum": ["auto"],
        "description": "维护现有的\n(通过查看第一行之后使用的内容,对一个文件中的混合值进行规范化)"
      }
    ]
  },
  "experimentalTernaries": {
    "description": "使用特殊的三元形式,在条件后面加上问号",
    "default": false,
    "type": "boolean"
  },
  "filepath": {
    "description": "指定输入文件路径。这将用于解析器推理",
    "type": "string"
  },
  "htmlWhitespaceSensitivity": {
    "description": "如何处理HTML中的空白",
    "default": "css",
    "oneOf": [
      {
        "enum": ["css"],
        "description": "遵循CSS显示属性的默认值"
      },
      {
        "enum": ["strict"],
        "description": "空格敏感"
      },
      {
        "enum": ["ignore"],
        "description": "空格不敏感"
      }
    ]
  },
  "insertPragma": {
    "description": "在文件的第一个docblock注释中插入@format编译指示",
    "default": false,
    "type": "boolean"
  },
  "jsxSingleQuote": {
    "description": "在JSX中使用单引号",
    "default": false,
    "type": "boolean"
  },
  "parser": {
    "description": "使用哪个解析器",
    "anyOf": [
      {
        "enum": ["flow"],
        "description": "Flow"
      },
      {
        "enum": ["babel"],
        "description": "JavaScript"
      },
      {
        "enum": ["babel-flow"],
        "description": "Flow"
      },
      {
        "enum": ["babel-ts"],
        "description": "TypeScript"
      },
      {
        "enum": ["typescript"],
        "description": "TypeScript"
      },
      {
        "enum": ["acorn"],
        "description": "JavaScript"
      },
      {
        "enum": ["espree"],
        "description": "JavaScript"
      },
      {
        "enum": ["meriyah"],
        "description": "JavaScript"
      },
      {
        "enum": ["css"],
        "description": "CSS"
      },
      {
        "enum": ["less"],
        "description": "Less"
      },
      {
        "enum": ["scss"],
        "description": "SCSS"
      },
      {
        "enum": ["json"],
        "description": "JSON"
      },
      {
        "enum": ["json5"],
        "description": "JSON5"
      },
      {
        "enum": ["jsonc"],
        "description": "JSON with Comments"
      },
      {
        "enum": ["json-stringify"],
        "description": "JSON.stringify"
      },
      {
        "enum": ["graphql"],
        "description": "GraphQL"
      },
      {
        "enum": ["markdown"],
        "description": "Markdown"
      },
      {
        "enum": ["mdx"],
        "description": "MDX"
      },
      {
        "enum": ["vue"],
        "description": "Vue"
      },
      {
        "enum": ["yaml"],
        "description": "YAML"
      },
      {
        "enum": ["glimmer"],
        "description": "Ember / Handlebars"
      },
      {
        "enum": ["html"],
        "description": "HTML"
      },
      {
        "enum": ["angular"],
        "description": "Angular"
      },
      {
        "enum": ["lwc"],
        "description": "Lightning Web Components"
      },
      {
        "type": "string",
        "description": "Custom parser"
      }
    ]
  },
  "plugins": {
    "description": "添加一个插件。多个插件可以作为单独的“——plugin”传递",
    "default": [],
    "type": "array",
    "items": {
      "type": "string"
    }
  },
  "printWidth": {
    "description": "代码超过多少字符才换行",
    "default": 80,
    "type": "integer"
  },
  "proseWrap": {
    "description": "怎样使代码换行",
    "default": "preserve",
    "oneOf": [
      {
        "enum": ["always"],
        "description": "如果代码超过printWidth指定的宽度,就换行"
      },
      {
        "enum": ["never"],
        "description": "不换行"
      },
      {
        "enum": ["preserve"],
        "description": "保持原样"
      }
    ]
  },
  "quoteProps": {
    "description": "当对象中的属性被引用时更改",
    "default": "as-needed",
    "oneOf": [
      {
        "enum": ["as-needed"],
        "description": "只在需要的地方给对象属性加上引号"
      },
      {
        "enum": ["consistent"],
        "description": "如果对象中至少有一个属性需要引号,则引用所有属性"
      },
      {
        "enum": ["preserve"],
        "description": "尊重对象属性中引号的输入使用"
      }
    ]
  },
  "rangeEnd": {
    "description": "格式化以给定字符偏移量(排他)结尾的代码。该范围将向前扩展到所选语句的末尾",
    "default": null,
    "type": "integer"
  },
  "rangeStart": {
    "description": "从给定的字符偏移量开始格式化代码。该范围将向后扩展到包含所选语句的第一行的开头",
    "default": 0,
    "type": "integer"
  },
  "requirePragma": {
    "description": "要求“@prettier”或“@format”出现在文件的第一个docblock注释\n中,以便格式化",
    "default": false,
    "type": "boolean"
  },
  "semi": {
    "description": "打印分号",
    "default": true,
    "type": "boolean"
  },
  "singleAttributePerLine": {
    "description": "在HTML、Vue和JSX中每行执行一个属性",
    "default": false,
    "type": "boolean"
  },
  "singleQuote": {
    "description": "使用单引号而不是双引号",
    "default": false,
    "type": "boolean"
  },
  "tabWidth": {
    "description": "每个缩进级别的空格数",
    "default": 2,
    "type": "integer"
  },
  "trailingComma": {
    "description": "当多行时,尽可能打印尾随逗号",
    "default": "all",
    "oneOf": [
      {
        "enum": ["all"],
        "description": "在任何可能的地方后面加上逗号(包括函数参数)"
      },
      {
        "enum": ["es5"],
        "description": "尾随逗号在ES5中是有效的(对象、数组等)"
      },
      {
        "enum": ["none"],
        "description": "没有尾随逗号"
      }
    ]
  },
  "useTabs": {
    "description": "用制表符而不是空格缩进",
    "default": false,
    "type": "boolean"
  },
  "vueIndentScriptAndStyle": {
    "description": "缩进Vue文件中的脚本和样式标签",
    "default": false,
    "type": "boolean"
  }
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐