美文网首页
HBuilderX代码校验及代码格式化

HBuilderX代码校验及代码格式化

作者: 海之深处爱之港湾 | 来源:发表于2021-04-12 10:29 被阅读0次

    一、添加 EditorConfig 代码风格统一工具

    EditorConfig 有助于维护跨多个编辑器和 IDE 从事同一项目的多个开发人员的一致编码风格,团队必备神器。
    在项目根目录创建.editorconfig并加入以下内容

    .editorconfig

    # http://editorconfig.org
    # 告诉EditorConfig插件,这是根文件,不用继续往上查找
    root = true
    # 匹配全部文件
    [*]
    # 结尾换行符,可选"lf"、"cr"、"crlf"
    end_of_line = lf
    # 在文件结尾插入新行
    insert_final_newline = true
    # 删除一行中的前后空格
    trim_trailing_whitespace = true
    # 匹配js和py结尾的文件
    [*.{js,py}]
    # 设置字符集
    charset = utf-8
    
    # 匹配py结尾的文件
    [*.py]
    # 缩进风格,可选"space"、"tab"
    indent_style = space
    # 缩进的空格数
    indent_size = 2
    
    # 以下匹配,类同
    [Makefile]
    indent_style = tab
    # tab的宽度
    tab_width = 4
    # 以下匹配,类同
    [lib/**.js]
    indent_style = space
    indent_size = 2
    
    [{package.json,.travis.yml}]
    indent_style = space
    indent_size = 2
    

    二、安装eslint-plugin-vue插件,vue语法校验

    1. DCLOUD插件市场搜索eslint-plugin-vue,使用HBuilderX导入

      image.png
    2. 配置步骤

      image.png
    3. .eslintrc.js

    //更详细的配置文档请参考:https://github.com/vuejs/eslint-plugin-vue#gear-configs
    module.exports = {
        "extends": "plugin:vue/base",
        parserOptions: {
            ecmaVersion: 2017,
            sourceType: 'module'
        },
        'settings': {
            'html/html-extensions': [
                ".erb",
                ".handlebars",
                ".hbs",
                ".htm",
                ".html",
                ".mustache",
                ".nunjucks",
                ".php",
                ".tag",
                ".twig",
                ".wxml",
                ".we",
            ]
        },
        "rules": {
            //在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,
            
            'camelcase': 2, //强制驼峰法命名,
            'indent': [2, 4], //缩进风格
            'id-match': 0, //命名检测
            'init-declarations': 1, //声明时必须赋初值
            'no-undef': 1, //不能有未定义的变量
            'no-multi-spaces': 'error', // 禁止多个空格 
            'semi': [2, 'never'], // 自动补充分号 "always" (默认值)在语句结尾处需要分号; "never" 不允许分号作为语句的末尾
            'quotes': ['error', 'single'] // 使用单引号
        }
    };
    
    

    三、安装eslint-js插件

    1. DCLOUD插件市场搜索eslint-js,使用HBuilderX导入
      image.png
    2. 配置步骤
      image.png
    3. .eslintrc.js
    //详细配置教程请参考:http://eslint.cn/docs/user-guide/configuring
    module.exports = {
        "plugins": [
            "html"
        ],
        "parserOptions": {
            "ecmaVersion": 2018,
            "sourceType": "module",
            "ecmaFeatures": {
                "jsx": true
            },
            "allowImportExportEverywhere": false
        },
        "rules": {
         'camelcase': 2, //强制驼峰法命名,
         'indent': [2, 4], //缩进风格
         'id-match': 0, //命名检测
         'init-declarations': 1, //声明时必须赋初值
         'no-undef': 1, //不能有未定义的变量
         'no-multi-spaces': 'error', // 禁止多个空格 
         'semi': [2, 'never'], // 自动补充分号 "always" (默认值)在语句结尾处需要分号; "never" 不允许分号作为语句的末尾
         'quotes': ['error', 'single'] // 使用单引号
        }
    };
    

    四、安装 stylelint插件,css、less、sass语法校验

    1. DCLOUD插件市场搜索stylelint,使用HBuilderX导入
      image.png
    2. 配置步骤
      image.png
    3. .stylelintrc.js
    module.exports = {
        "extends": "stylelint-config-recommended",
        "rules": {
            "unit-no-unknown": false,
            "indentation": "tab", //缩进
            "unit-no-unknown": true, //禁止未知单位
            "color-hex-case": [
                "lower", {
                    "message": "Lowercase letters are easier to distinguish from numbers"
                }
            ],
            "max-empty-lines": 2,
            "unit-whitelist": ["em", "rem", "%", "s", "px", "upx", "rpx"],
            "number-leading-zero": null
        },
        // 忽略其他文件,只校验样式相关的文件
        ignoreFiles: [
            "node_modules/**/*",
            "public/**/*",
            "dist/**/*",
            "**/*.js",
            "**/*.jsx",
            "**/*.tsx",
            "**/*.ts",
        ],
    }
    
    

    五、代码格式化快捷键

    为了代码整齐便于阅读,写完代码随时进行格式化

     windows:ctrl+k,
     Mac: command+k
    

    相关文章

      网友评论

          本文标题:HBuilderX代码校验及代码格式化

          本文链接:https://www.haomeiwen.com/subject/vobtlltx.html