Eslint常用规范:

1.设置 eslint 忽略某些文件,不进行格式化

在项目根目录下 新建 .eslintignore 文件,以文件相对路径的形式表示项目中那些文件应该被忽略,一般用于忽略某些引入的外部组件。以 # 开头的行会被当作注释,路径是相对于 .eslintignore 的位置或当前工作目录,以 ! 开头的行是否定模式,它将会重新包含一个之前被忽略的模式。
除了 .eslintignore 文件中的设置的被忽略文件,ESLint还会自动忽略 node_modules和 bower_components中的文件。当 ESLint 运行时,在确定哪些文件要检测之前,它会在当前工作目录中查找一个 .eslintignore 文件。如果发现了这个文件,当遍历目录时,将会应用这些偏好设置。一次只有一个 .eslintignore 文件会被使用,所以,不是当前工作目录下的 .eslintignore 文件将不会被用到。
 eslint 忽略的文件夹 或某个单独文件
 components/pagination/*
 assets/css/icon/*
 把被忽略的文件夹中的某个文件拿出来,使其不再被忽略
 !assets/css/icon/icon.css
 忽略所有js文件
 **/*.js

2.eslintrc.js常用规范

    "no-console": "error",                  // 禁止console
    "no-alert": "error",                   // 禁止alert,conirm等
    "no-debugger": "error",                 // 禁止debugger
    "semi": ["error", "never"],               // 禁止分号
    "no-tabs": "error",                   // 禁止使用tab
    "no-unreachable": "error",               // 当有不能执行到的代码时
    "eol-last": "error",                   // 文件末尾强制换行
    "no-new": "error",                    // 禁止在使用new构造一个实例后不赋值
    "quotes": ["error", "backtick"],            // 引号类型 `` "" ''
    "no-unused-vars": ["error", { "vars": "all", "args": "after-used" }],   // 不能有声明后未被使用的变量
    "no-trailing-spaces": "error",             // 一行结束后面不要有空格
    "space-before-function-paren": ["error", "never"], // 函数定义时括号前面要不要有空格
    "no-undef": "error",                   // 不能有未定义的变量,定义之前必须有var或者let
    "curly": ["error", "all"],                // 必须使用 if(){} 中的{}
    'arrow-parens': "error",                 // 箭头函数的参数要有()包裹
    'generator-star-spacing': "error",           // allow async-await
    "space-before-function-paren": ["error", "never"],  // 禁止函数名前有空格,如function Test (aaa,bbb)
    "space-in-parens": ["error", "never"],         // 禁止圆括号有空格,如Test( 2, 3 )
    "space-infix-ops": "error",               //在操作符旁边必须有空格, 如 a + b而不是a+b
    "space-before-blocks": ["error", "always"],      // 语句块之前必须有空格 如 ) {}
    "spaced-comment":["error", "always"],         // 注释前必须有空格
    "arrow-body-style": ["error", "always"],       // 要求箭头函数必须有大括号 如 a => {}
    "arrow-parens": ["error", "always"],         //要求箭头函数的参数必有用括弧包住,如(a) =>{}
    "arrow-spacing": ["error", { "before": true, "after": true }], // 定义箭头函数的箭头前后都必须有空格
    "no-const-assign": "error",                // 禁止修改const变量
    "template-curly-spacing": ["error", "never"],   // 禁止末班字符串中的{}中的变量出现空格,如以下错误`${ a }`
    "no-multi-spaces": "error",             // 禁止多个空格,只有一个空格的地方必须只有一个
    "no-whitespace-before-property": "error",     // 禁止属性前有空格,如obj. a
    "keyword-spacing":["error",{"before": true, "after": true}]   //关键字前后必须有空格 如 } else {
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容