关于javascript:HBuilderX配置自动格式化统一代码规范

10次阅读

共计 3136 个字符,预计需要花费 8 分钟才能阅读完成。

1、前言


  团队合作是稍具规模公司必不可少的问题,也是我的项目开发进度保障的重要基石。只有团队合作,各自的功力施展到极致,能力保障团队生产力最大化。随着团队减少,对立的代码格调就越来越重要,为此应用 HBuilderX 配置主动格式化,对立代码标准。

2、代码格调


  • 整体格调

    • javascript大体上遵循 ESlint 标准
    • htmlwxml大体上遵循 prettyhtml 标准
  • javascript细节调整

    • 结尾无分号
    • 超过 140 个字符换行
    • 应用单引号
    • 无尾随逗号
    • 箭头函数单个参数不加分号
    • 函数申明时禁止圆括号前有空格

    3、筹备插件


  • eslint-js 用于校验 js 和 html 中的 js 代码
  • eslint-plugin-vuevue 语法校验和语法提醒

    4、个性化配置


    1. 打开方式
    • 工具 -> 插件装置 -> 已装置插件[插件名称] -> 配置
    2. 个性化配置, 具体操作步骤配置在底下参考资料
  • eslint-js 在 .eslintrc.js 增加规定

    // 具体配置教程请参考:http://eslint.cn/docs/user-guide/configuring
    module.exports = {
      "plugins": ["html"],
      "parserOptions": {
          "ecmaVersion": 2018,
          "sourceType": "module",
          "ecmaFeatures": {"jsx": true},
          "allowImportExportEverywhere": false
      },
      "rules": {
          "no-alert": 0,
          "eqeqeq": ["error", "always"], // 用强等于做判断
          "semi": ["error", "never"], // 结尾不分号 
          "no-multi-spaces": "error",
          "quotes": ["error", "single"], // 应用单引号
          "arrow-parens": ["error", "as-needed"], // 简略箭头函数
          "object-curly-newline": ["error", { "multiline": true}], // 在属性外部或属性之间有换行符,就要求有换行符
          "object-curly-spacing": ["error", "always"] // 要求花括号内有空格 (除了 {})
      }
    };
    
  • eslint-vue 在 .eslintrc.js 增加规定

    module.exports = {
      'extends': 'plugin:vue/essential',
      'parserOptions': {
          ecmaVersion: 2018,
          sourceType: 'module'
      },
      'rules': {
          'no-alert': 0,
          'eqeqeq': ['error', 'always'], // 用强等于做判断
          'semi': ['error', 'never'], // 结尾不分号 
          'no-multi-spaces': 'error',
          'quotes': ['error', 'single'], // 应用单引号
          'arrow-parens': ['error', 'as-needed'], // 简略箭头函数
          'object-curly-newline': ['error', {'multiline': true}], // 在属性外部或属性之间有换行符,就要求有换行符
          'object-curly-spacing': ['error', 'always'], // 要求花括号内有空格 (除了 {})
          // 在 computed properties 中禁用异步 actions
          'vue/no-async-in-computed-properties': 'error',
          // 不容许反复的 keys
          'vue/no-dupe-keys': 'error',
          // 不容许反复的 attributes
          'vue/no-duplicate-attributes': 'warn',
          // 在 <template> 标签下不容许解析谬误
          'vue/no-parsing-error': ['error', {'x-invalid-end-tag': false,}],
          // 不容许笼罩保留关键字
          'vue/no-reserved-keys': 'error',
          // 强制 data 必须是一个带返回值的函数
          // 'vue/no-shared-component-data': 'error',
          // 不容许在 computed properties 中呈现副作用。'vue/no-side-effects-in-computed-properties': 'error',
          //<template> 不容许 key 属性
          'vue/no-template-key': 'warn',
          // 在 <textarea> 中不容许 mustaches
          'vue/no-textarea-mustache': 'error',
          // 不容许在 v -for 或者范畴内的属性呈现未应用的变量定义
          'vue/no-unused-vars': 'warn',
          //<component> 标签须要 v -bind:is 属性
          'vue/require-component-is': 'error',
          // render 函数必须有一个返回值
          'vue/require-render-return': 'error',
          // 保障 v-bind:key 和 v-for 指令成对呈现
          'vue/require-v-for-key': 'error',
          // 查看默认的 prop 值是否无效
          'vue/require-valid-default-prop': 'error',
          // 保障 computed 属性中有 return 语句 
          'vue/return-in-computed-property': 'error',
          // 强制校验 template 根节点
          'vue/valid-template-root': 'error',
          // 强制校验 v-bind 指令
          'vue/valid-v-bind': 'error',
          // 强制校验 v-cloak 指令
          'vue/valid-v-cloak': 'error',
          // 强制校验 v-else-if 指令
          'vue/valid-v-else-if': 'error',
          // 强制校验 v-else 指令 
          'vue/valid-v-else': 'error',
          // 强制校验 v-for 指令
          'vue/valid-v-for': 'error',
          // 强制校验 v-html 指令
          'vue/valid-v-html': 'error',
          // 强制校验 v-if 指令
          'vue/valid-v-if': 'error',
          // 强制校验 v-model 指令
          'vue/valid-v-model': 'error',
          // 强制校验 v-on 指令
          'vue/valid-v-on': 'error',
          // 强制校验 v-once 指令
          'vue/valid-v-once': 'error',
          // 强制校验 v-pre 指令
          'vue/valid-v-pre': 'error',
          // 强制校验 v-show 指令
          'vue/valid-v-show': 'error',
          // 强制校验 v-text 指令
          'vue/valid-v-text': 'error',
          'vue/comment-directive': 0
      }
    };
    
  • tab 键补全

    工具 -> 设置 -> ` 编辑器配置 ` 里选中 `tab 键主动插入代码助手选中项 `

  • 解构赋值不将大括号换行

    工具 -> 设置 -> 插件配置 -> 自定义 `jsbeautify` 格式化规定的 `jsbeautifyrc.js` 中将 "brace_style" 选项增加 "preserve-inline"

参考资料

  • eslint-js 简介
  • eslint-vue 简介
  • 记录 hbuilder 中 jsbeautifyrc 设置格式化时保留大括号 {} 不换行格局
正文完
 0