【无标题】

eslint 数组和对象非空判断整理

1 安装vscode的eslint

2 npx eslint --init 初始化eslint

3 配置文件

eslint.config.mjs

复制代码
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";
import noNullCheck from "./no-null-check.js"; // 引入自定义规则



export default [
  {files: ["**/*.{js,mjs,cjs,ts,vue}"]},
  {languageOptions: { globals: globals.browser }},
 

  ...pluginVue.configs["flat/essential"],
  {files: ["**/*.vue"], languageOptions: {parserOptions: {parser: tseslint.parser}}},
  {
    plugins: {
      "example": {
        rules: { "no-null-check": noNullCheck }
      }
  },
  rules: {
    "example/no-null-check": "warn", // 注册自定义规则
},
   
  },
];

no-null-check.js

复制代码
module.exports = {
    meta: {
        type: "problem",
        docs: {
            description: "require non-empty initialization for arrays and objects, and check usage in lifecycle methods",
            category: "Best Practices",
            recommended: false,
        },
        schema: [],
    },
    create: function (context) {
        const dataProperties = {};

        return {
            VariableDeclaration(node) {
                node.declarations.forEach(declaration => {
                    const { init } = declaration;
                    if (init) {
                        if (
                            (init.type === 'ArrayExpression' && init.elements.length === 0) ||
                            (init.type === 'ObjectExpression' && init.properties.length === 0)
                        ) {
                            context.report({
                                node: declaration,
                                message: 'Initialization should not be empty for arrays or objects.',
                            });
                        }
                    }
                });
            },
            'Property[key.name="data"]'(node) {
                if (node.value.type === 'FunctionExpression') {
                    const returnStatement = node.value.body.body.find(
                        stmt => stmt.type === 'ReturnStatement'
                    );
                    if (returnStatement && returnStatement.argument.type === 'ObjectExpression') {
                        returnStatement.argument.properties.forEach(prop => {
                            dataProperties[prop.key.name] = prop.value;
                            if (
                                (prop.value.type === 'ArrayExpression' && prop.value.elements.length === 0) ||
                                (prop.value.type === 'ObjectExpression' && prop.value.properties.length === 0)
                            ) {
                                context.report({
                                    node: prop,
                                    message: '记得非空判断',
                                });
                            }
                        });
                    }
                }
            },
            MemberExpression(node) {
                if (node.object.name === 'this' && dataProperties[node.property.name]) {
                    const prop = dataProperties[node.property.name];
                    if (
                        (prop.type === 'ArrayExpression' && prop.elements.length === 0) ||
                        (prop.type === 'ObjectExpression' && prop.properties.length === 0)
                    ) {
                        context.report({
                            node,
                            message: 'Data property should be checked for non-emptiness before usage.',
                        });
                    }
                }
            },
        };
    },
};
相关推荐
jjw_zyfx1 小时前
成熟的前端vue vite websocket,Django后端实现方案包含主动断开websocket连接的实现
前端·vue.js·websocket
乌夷4 小时前
使用spring boot vue 上传mp4转码为dash并播放
vue.js·spring boot·dash
苹果酱05675 小时前
2020-06-23 暑期学习日更计划(机器学习入门之路(资源汇总)+概率论)
java·vue.js·spring boot·mysql·课程设计
Deepsleep.5 小时前
react和vue的区别之一
javascript·vue.js·react.js
zqlcoding5 小时前
使用el-table表格动态渲染表头数据之后,导致设置fixed的列渲染出现问题
前端·javascript·vue.js
爱吃的强哥5 小时前
vue3 使用 vite 管理多个项目,实现各子项目独立运行,独立打包
前端·javascript·vue.js
涵信6 小时前
第十节:性能优化高频题-虚拟DOM与Diff算法优化
javascript·vue.js·性能优化
拖孩6 小时前
【Nova UI】十一、组件库中 Icon 组件的测试、使用与全局注册全攻略
前端·javascript·vue.js·ui·sass
凉豆菌7 小时前
在html中如何创建vue自定义组件(以自定义文件上传组件为例,vue2+elementUI)
vue.js·elementui·html
广西千灵通网络科技有限公司7 小时前
基于 springboot+vue+elementui 的办公自动化系统设计(
vue.js·spring boot·elementui