简体   繁体   English

如何让 eslint 遵守 babel 规则?

[英]How to get eslint to comply with babel rules?

I have a simple babel config transpiling latest ES code into target which is 12.x but when I try use the latest ES features such as optional chaining, eslint doesn't like it.我有一个简单的 babel 配置,将最新的 ES 代码转换为12.x的目标,但是当我尝试使用最新的 ES 功能(例如可选链接)时,eslint 不喜欢它。

My babel config is like so:我的 babel 配置是这样的:

{
  "sourceMaps": "inline",
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-transform-runtime",
    [
      "module-resolver",
      {
        "alias": {
          "^@/(.+)": "./src/\\1"
        }
      }
    ]
  ]
}

and an eslint config like so:和这样的 eslint 配置:

module.exports = {
  extends: [
    'airbnb',
    'airbnb/hooks',
    'plugin:jest/recommended',
    'plugin:jest/style',
    'plugin:cypress/recommended',
    'eslint-config-prettier',
  ],
  env: {
    node: true,
    es6: true,
    jest: true,
    browser: true,
  },
  plugins: ['no-autofix', 'jest', 'cypress'],
  rules: {
    ...
  },
  parserOptions: {
    sourceType: 'module',
    ecmaVersion: 2019,
    ecmaFeatures: {
      jsx: true,
    },
  }
};

How can I tell eslint that ES version is actually not node v12.x but the 'latest'?我如何告诉 eslint ES 版本实际上不是 node v12.x 而是“最新”?

Turns out I needed to update parserOptions.ecmaVersion to be 2021原来我需要将parserOptions.ecmaVersion更新为2021

If you are using ESLint 8 (and you should), in your ESLint config, replace es6: true with es2021: true and remove ecmaVersion: 2019 .如果你正在使用 ESLint 8(你应该),在你的 ESLint 配置中,将es6: true替换为es2021: true并删除ecmaVersion: 2019 According to the documentation this根据文档,

adds all ECMAScript 2021 globals and automatically sets the ecmaVersion parser option to 12.添加所有 ECMAScript 2021 全局变量并自动将ecmaVersion解析器选项设置为 12。

If you really want to parse the latest language version rather than ES2021, then in the parserOptions you could set ecmaVersion: "latest" .如果你真的想解析最新的语言版本而不是 ES2021,那么你可以在parserOptions中设置ecmaVersion: "latest"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM