美文网首页
eslint 写法 默认不继承自带的 自定义eslint检查

eslint 写法 默认不继承自带的 自定义eslint检查

作者: 一碗米粉的故事 | 来源:发表于2019-10-25 18:08 被阅读0次
module.exports = {
    "env": {
        "browser": true,
        "node": true,
        "es6": true
    },
    "extends": [
        // "eslint:recommended",
        "plugin:vue/essential"
    ],
    "globals": {
        "Atomics": "readonly",
      "document": true,
      "localStorage": true,
      "window": true,
      "$": true,
      "jQuery": true,

      "define": true,

        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "plugins": [
        "vue"
    ],
    "rules": {
      "vue/no-side-effects-in-computed-properties":0,
      "vue/require-valid-default-prop":0,
      "vue/require-v-for-key":0,
      "vue/no-unused-components":0,
      "vue/no-async-in-computed-properties":0,
      "vue/return-in-computed-property":0,
      "vue/valid-v-for":0,
      "vue/no-parsing-error":0,
      "vue/no-use-v-if-with-v-for":0,
      "vue/no-dupe-keys":0,
      "no-unreachable": 2,  // 防止代码合并冲突不知道 这个可以防止手滑
      // "no-console": 0, //不允许出现console语句
      // " vue/valid-template-root": 0, //在template里不允许出现手滑
      // "no-unused-vars": 0, //不允许出现console语句
      // "no-unused-labels": "error",
      // "nonblock-statement-body-position": ["error", "beside", { "overrides": { "while": "below" } }]
    //  "no-ternary": 0,//禁止使用三目运算符,
      // "no-use-before-define": "error",
      // 'no-empty-label': 2, // 禁止使用空label
      // 'no-labels': 2, // 禁止标签声明
     // 'no-undef': 2, // 不能有未定义的变量   这个可以防止手滑 GC appVue 都抛错
   //   'no-use-before-define': 2, // 未定义前不能使用  书写代码有关系 从上到下
   //    'init-declarations': 0, // 声明时必须赋初值
     // 'no-unused-vars': [2, { vars: 'all', args: 'after-used' }], // 不能有声明后未被使用的变量或参数  这个严格意义上来说应该需要加上
      // "no-unused-expressions": "error",//禁止无用的表达式
      // "no-nested-ternary": 0,//禁止使用嵌套的三目运算
    }
};

自定义检查
建立 .eslintrc.js文件

module.exports = {
    "rules": {
        "no-dupe-val": 2,
    }
};

在同级目录下建立 eslint-rules 新建 no-dupe-val.js

/**
 * @fileoverview Rule to flag use of duplicate keys in an object.
 * @author Ian Christian Myers
 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
let globalMark=[];
function checkMark(nextChild) {
    let bl=true;
    // if(!nextChild.mark || nextChild.mark===""){bl=false}
            if(nextChild.mark===null){return true}
            if(globalMark.indexOf(nextChild.mark)>-1){
                bl=false
            }else{
                globalMark.push(nextChild.mark)
            }
            return bl;
}
function getStaticPropertyName(node) {
    let prop;

    switch (node && node.type) {
        case "Property":
        case "MethodDefinition":
            if(node.key.name!=='mark'){
                return null
            }
            prop = node.value;
            break;

        case "MemberExpression":
            prop = node.property;
            break;

        // no default
    }

    switch (prop && prop.type) {
        case "Literal":
            return String(prop.value);

        case "TemplateLiteral":
            if (prop.expressions.length === 0 && prop.quasis.length === 1) {
                return prop.quasis[0].value.cooked;
            }
            break;

        case "Identifier":
            if (!node.computed) {
                return prop.value;
            }
            break;

        // no default
    }

    return null;
}

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const GET_KIND = /^(?:init|get)$/u;
const SET_KIND = /^(?:init|set)$/u;

/**
 * The class which stores properties' information of an object.
 */
class ObjectInfo {

    /**
     * @param {ObjectInfo|null} upper - The information of the outer object.
     * @param {ASTNode} node - The ObjectExpression node of this information.
     */
    constructor(upper, node) {
        this.upper = upper;
        this.node = node;
        this.properties = new Map();
    }

    /**
     * Gets the information of the given Property node.
     * @param {ASTNode} node - The Property node to get.
     * @returns {{get: boolean, set: boolean}} The information of the property.
     */
    getPropertyInfo(node) {
        const name = getStaticPropertyName(node);

        if (!this.properties.has(name)) {
            this.properties.set(name, { get: false, set: false });
        }
        return this.properties.get(name);
    }

    /**
     * Checks whether the given property has been defined already or not.
     * @param {ASTNode} node - The Property node to check.
     * @returns {boolean} `true` if the property has been defined.
     */
    isPropertyDefined(node) {
        const entry = this.getPropertyInfo(node);

        return (
            (GET_KIND.test(node.kind) && entry.get) ||
            (SET_KIND.test(node.kind) && entry.set)
        );
    }

    /**
     * Defines the given property.
     * @param {ASTNode} node - The Property node to define.
     * @returns {void}
     */
    defineProperty(node) {
        const entry = this.getPropertyInfo(node);

        if (GET_KIND.test(node.kind)) {
            entry.get = true;
        }
        if (SET_KIND.test(node.kind)) {
            entry.set = true;
        }
    }
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
    meta: {
        type: "problem",

        docs: {
            description: "disallow duplicate values in object literals",
            category: "Possible Errors",
            recommended: true,
            url: ""
        },

        schema: [],

        messages: {
            unexpected: "Duplicate values '{{name}}'."
        }
    },

    create(context) {
        let info = null;

        return {
            ObjectExpression(node) {
                info = new ObjectInfo(info, node);
            },
            "ObjectExpression:exit"() {
                info = info.upper;
            },

            Property(node) {
                const name = getStaticPropertyName(node);
                if (!checkMark({mark:name})) {
                    context.report({
                        node: info.node,
                        loc: node.key.loc,
                        messageId: "unexpected",
                        data: { name }
                    });
                }
                // console.log(name);
                // console.log("======");

                // Skip destructuring.
                if (node.parent.type !== "ObjectExpression") {
                    return;
                }

                // Skip if the name is not static.
                if (!name) {
                    return;
                }

                // Reports if the name is defined already.
                // if (info.isPropertyDefined(node)) {
                //     context.report({
                //         node: info.node,
                //         loc: node.key.loc,
                //         messageId: "unexpected",
                //         data: { name }
                //     });
                // }


                // Update info.
                info.defineProperty(node);
            }
        };
    }
};

相关文章

网友评论

      本文标题:eslint 写法 默认不继承自带的 自定义eslint检查

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