javascript - ESLint 如何修复解析错误 : 'import' and 'export' may only appear at the top level

标签 javascript ecmascript-6 babeljs eslint

注意:代码正在运行,只有 ESLint 给了我一个我想要修复的错误!!

进行动态导入时:

if (true) import x from 'y'; //Parsing error: 'import' and 'export' may only appear at the top level

我的.eslintrc

{
  "root": true,
  "extends": "eslint:recommended",
  "env": {
    "es6": true,
    "node": true,
    "browser": true
  },
  "parserOptions": {
    "ecmaVersion": 10,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "rules": {
    "strict": 0,
    "no-undef": 2,
    "accessor-pairs": 2,
    "comma-dangle": [2, "always-multiline"],
    "consistent-return": 2,
    "dot-notation": 2,
    "eqeqeq": 2,
    "indent": [2, 2, {"SwitchCase": 1}],
    "no-cond-assign": 2,
    "no-constant-condition": 2,
    "no-eval": 2,
    "no-inner-declarations": [0],
    "no-unneeded-ternary": 2,
    "radix": 2,
    "semi": [2, "always"],
    "camelcase": 2,
    "comma-spacing": 2,
    "comma-style": 2,
    "eol-last": 2,
    "linebreak-style": [2, "unix"],
    "new-parens": 2,
    "no-lonely-if": 2,
    "no-multiple-empty-lines": 2,
    "no-nested-ternary": 2,
    "no-spaced-func": 2,
    "no-trailing-spaces": 2,
    "operator-linebreak": 2,
    "quotes": [2, "single"],
    "semi-spacing": 2,
    "space-unary-ops": 2,
    "arrow-parens": 2,
    "arrow-spacing": 2,
    "no-class-assign": 2,
    "no-dupe-class-members": 2,
    "no-var": 2,
    "object-shorthand": 2,
    "prefer-const": 2,
    "prefer-spread": 2,
    "prefer-template": 2
  },

我已经尝试过:
...将 ecmaVersion 切换到 11, 2018, 2019, 2020 这给了我错误的数字或解析器不再工作
...添加了解析器:babel-eslint,这使得解析器不再工作
...添加了 allowImportsExportsAnywhere: true ,但什么也没做

最佳答案

参见https://eslint.org/blog/2019/08/eslint-v6.2.0-released#highlights

This release adds support for ES2020 syntax, which includes support for Dynamic Imports and BigInt. This can be enabled using ecmaVersion: 2020 in your configuration file.

这似乎建议将其添加到您的 .eslintrc.json 文件中并重新加载 vscode:

"parserOptions": {
  "ecmaVersion": 2020,
  "sourceType": "module",
  "ecmaFeatures": {
    "jsx": true
  }
},

"ecmaVersion": 11,这是同一件事。

但是当我这样做时,我收到了关于无效 ecmaVersion 的不同错误选项。 这似乎消除了该错误:

"env": {
    "browser": true,
    "node": true,
    "es2020": true
},

并且 parserOptions 中没有 ecmaVersion! 显然 vscode 的 eslint 版本不支持 "ecmaVersion": 2020还没有。

关于javascript - ESLint 如何修复解析错误 : 'import' and 'export' may only appear at the top level,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338944/

相关文章:

javascript - 将 TypeScript 变量暴露给 JS 全局范围(使用 webpack)

javascript - 如何将事件添加到 javascript 运行时创建的 html 小部件

javascript - 使用 jQuery 暂时延迟页面滚动

javascript - Babel 6 + React - 传递初始数据(浏览器内转译器)

javascript - 是否可能/可行将非 Microsoft REST 客户端应用程序集成到 Sharepoint 2013 OAuth 中以获得 REST 客户端的访问 token ?

javascript - 纯正则表达式解决方案,用于在无法依赖 document.createElement 的环境中从 HTML 字符串获取文本内容?

javascript - 如何扩散到导出

javascript - Browserify Babel ES2015 Knockout 相互依赖模块

javascript - babel 中的 boolean 交换不起作用

javascript - 如何解决错误: SyntaxError: Unexpected token import