eslint-config-tm-cli

0.3.0 • Public • Published

eslint-config-tm-cli

tm-cli 统一 ESLint 配置

参考

用法

  1. 安装 eslint-config-tm-cli:
npm install eslint-config-tm-cli --save-dev
  1. 将以下内容添加到 eslint 配置文件:

.eslintrc.js

module.exports = {
  extends: ['tm-cli'],
};

.eslintrc

{
  "extends": ["tm-cli"]
}

全部配置

module.exports = {
  // 停止在父级目录中寻找 .eslintrc.*
  root: true,
  // 预定义的全局变量
  env: {
    // 浏览器全局变量
    browser: true,
    // commonjs 语法支持
    commonjs: true,
    // 启用 ES6 语法支持 及 新的 ES6 全局变量
    es6: true,
  },
  // eslint扩展集成
  extends: ['plugin:vue/essential', 'airbnb-base'],
  // JavaScript 语言选项
  parserOptions: {
    parser: 'babel-eslint',
    ecmaFeatures: {
      jsx: true,
    },
  },
  // eslint插件
  plugins: ['html', 'vue'],
  // 添加可能会用到的全局变量
  globals: {
    wx: true,
    qq: true,
    'qq.maps': true,
    $: true,
    Vue: true,
    getApp: true,
    ELEMENT: true,
  },
  // 'off' 或 0 - 关闭规则
  // 'warn' 或 1 - 开启规则,使用警告级别的错误:warn (不会导致程序退出)
  // 'error' 或 2 - 开启规则,使用错误级别的错误:error (当被触发的时候,程序会退出)
  rules: {
    /**
     * ESlint 统一配置(语雀文档)
     * https://xsuxt.yuque.com/xsued/book/vusqln
     */
    'arrow-body-style': [0], // 在箭头函数体中需要大括号
    'class-methods-use-this': [0], // 强制使用类方法this
    'consistent-return': [0], // 要求return语句总是或从不指定值
    'generator-star-spacing': [0], // 强制 generator 函数中 * 号周围使用一致的空格
    'global-require': [0], // 在顶级模块范围内强制执行 require()
    'import/extensions': [0], // 导入文件必须要扩展名
    'import/first': [0], // 导入必须在最顶部
    'no-param-reassign': [0], // 禁止重新分配函数参数
    'no-plusplus': [0], // 禁止一元运算符++和--
    'import/no-extraneous-dependencies': [0], // 禁止使用非项目所有的依赖
    'import/prefer-default-export': [0], // 优先使用export default
    'import/no-unresolved': [0], // 模块路径解析
    'import/no-absolute-path': [0], // 禁止绝对路径
    'linebreak-style': [0], // 强制一致的换行样式
    'no-bitwise': [0], // 禁止按位运算符
    'no-cond-assign': [0], // 在条件语句中禁止赋值运算符
    'no-console': [0], // 禁用 console
    'no-debugger': [0], // 禁用 debugger
    'no-else-return': [0], // 不允许在 else 之前返回
    'no-nested-ternary': [0], // 禁止嵌套三元表达式
    'no-restricted-syntax': [0], // 禁止指定语法
    'no-trailing-spaces': [0], // 禁用行尾空格
    'no-use-before-define': [0], // 禁止变量在未定义前调用
    'no-useless-escape': [0], // 禁用不必要的转义字符
    'prefer-template': [0], // 建议使用模板文字而不是字符串连接
    'require-yield': [1], // 禁止没有的生成器函数yield

    /**
     * tm-element-admin 模板 .eslintrc.js 额外配置
     */
    // 禁止在外部作用域中声明的隐藏变量声明变量
    'no-shadow': [
      0,
      {
        builtinGlobals: false,
        hoist: 'functions',
        allow: ['e', 'state'],
      },
    ],
    // 禁止未使用的表达式
    'no-unused-expressions': [
      0,
      {
        allowShortCircuit: true,
        allowTernary: true,
      },
    ],
    // 限制最大行长度
    'max-len': [
      2,
      {
        code: 120,
        tabWidth: 2,
        ignoreComments: true,
        ignoreTrailingComments: true,
        ignoreUrls: true,
        ignoreStrings: true,
        ignoreTemplateLiterals: true,
        ignoreRegExpLiterals: true,
        ignorePattern: 'd="([\\s\\S]*?)"',
      },
    ],
    // vue文件html强制换行
    'vue/singleline-html-element-content-newline': [0],
    'vue/multiline-html-element-content-newline': [0],
    // v-for指令与scope属性里定义的值需要被使用
    'vue/no-unused-vars': [0],
    // 强制使用骆驼拼写法命名约定
    camelcase: [1, { ignoreDestructuring: true }],
    // 建议从数组和对象中解构
    'prefer-destructuring': [
      0,
      {
        object: true,
        array: false,
      },
    ],

    /**
     * vue相关规则
     */
    // 组件的属性必须为一定的顺序
    'vue/order-in-components': [
      2,
      {
        order: [
          'el',
          'name',
          'parent',
          'functional',
          ['delimiters', 'comments'],
          ['components', 'directives', 'filters'],
          'extends',
          'mixins',
          'inheritAttrs',
          'model',
          ['props', 'propsData'],
          'data',
          'computed',
          'watch',
          'LIFECYCLE_HOOKS',
          'methods',
          ['template', 'render'],
          'renderError',
        ],
      },
    ],

    /**
     * 额外配置,参考如下:
     * 1. JavaScript 开发手册(编码规范)https://xsuxt.yuque.com/xsued/book/hl03wu
     * 2. 没有 plugin:prettier/recommended 集成的冲突规则
     */
    // 在 if / else / for / do / while语句中,即使只有一行,也不得省略块 {...}
    curly: [2, 'all'],
    // 禁用 alert
    'no-alert': process.env.NODE_ENV === 'production' ? 2 : 0,
    // 强制在大括号之间换行
    'object-curly-newline': [0],
    // 使用隐式返回强制箭头函数体的位置
    'implicit-arrow-linebreak': [0],
    // 在函数括号内强制执行一致的换行
    'function-paren-newline': [0],
    // 要求立即调用函数用圆括号括起来
    'wrap-iife': [0],
    // 强制操作符使用一致的换行符
    'operator-linebreak': [0],
  },
};

推荐 prettier 配置(.prettierrc)

{
  "semi": true,
  "trailingComma": "all",
  "singleQuote": true,
  "bracketSpacing": true,
  "useTabs": false,
  "arrowParens": "always",
  "htmlWhitespaceSensitivity": "ignore",
  "printWidth": 120
}

Readme

Keywords

Package Sidebar

Install

npm i eslint-config-tm-cli

Weekly Downloads

3

Version

0.3.0

License

ISC

Unpacked Size

14.3 kB

Total Files

8

Last publish

Collaborators

  • swzj