【问题标题】:eslint throws parsing error unexpected token on named exporteslint 在命名导出上抛出解析错误意外令牌
【发布时间】:2016-07-13 16:47:58
【问题描述】:

我有一个 index.js 文件,其中包含以下命名导出。

export Main from './Main/Main'

但是,eslint 不喜欢这样,会抛出错误

Parsing error: Unexpected token Main

我不知道为什么应用程序可以正常运行,我相信这是有效的语法。

我的 .eslintrc 文件如下所示

{
  env: {
    es6: true,
    browser: true
  },
  parserOptions: {
    ecmaVersion: 6,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true,
      experimentalObjectRestSpread: true
    }
  },
  plugins: [
    "react",
  ],
  extends: ["eslint:recommended", "plugin:react/recommended", "standard"],
  "rules": {
    "comma-dangle" : [2, "always-multiline"],
    "semi": [2, "never"],
    "no-extra-semi": 2,
    "jsx-quotes": [2, "prefer-single"],
    "react/jsx-boolean-value": [2, "always"],
    "react/jsx-closing-bracket-location": [2, {selfClosing: "after-props", nonEmpty: "after-props"}],
    "react/jsx-curly-spacing": [2, "never", {"allowMultiline": false}],
    "react/jsx-max-props-per-line": [2, {maximum: 3}],
    "react/jsx-no-literals": 2,
    "react/sort-prop-types": 2,
    "react/self-closing-comp": 2,
    "react/sort-comp": 2
  },
}

【问题讨论】:

标签: javascript ecmascript-6 eslint


【解决方案1】:

想通了。这是一个实验性功能,所以我需要启用 babel-eslint 作为我的 eslint 解析器。

现在我的 .eslintrc 看起来像这样

{
  parser: "babel-eslint",
  env: {
    es6: true,
    browser: true
  },
  parserOptions: {
    ecmaVersion: 6,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true,
      experimentalObjectRestSpread: true
    }
  },
  plugins: [
    "react",
  ],
  extends: ["eslint:recommended", "plugin:react/recommended", "standard"],
  "rules": {
    "comma-dangle" : [2, "always-multiline"],
    "semi": [2, "never"],
    "no-extra-semi": 2,
    "jsx-quotes": [2, "prefer-single"],
    "react/jsx-boolean-value": [2, "always"],
    "react/jsx-closing-bracket-location": [2, {selfClosing: "after-props", nonEmpty: "after-props"}],
    "react/jsx-curly-spacing": [2, "never", {"allowMultiline": false}],
    "react/jsx-max-props-per-line": [2, {maximum: 3}],
    "react/jsx-no-literals": 2,
    "react/sort-prop-types": 2,
    "react/self-closing-comp": 2,
    "react/sort-comp": 2
  },
}

【讨论】:

  • 如果该实验性功能是您需要 babel-eslint 的唯一原因并且您不想处理开销,您可以使用 export { default as Main } from './Main/Main';,它是有效的 ES6 并且可以在默认解析器。
  • @btmills 这是一个很棒的见解。谢谢!
  • PLZ!标记这是答案!
【解决方案2】:

由于不同的原因,我得到了同样的错误,这就是我出错的地方:

如果你在 atom 包开发中使用 babel,babel 会被每个文件开头的use babel 激活。

use babel,就像use strict 需要在文件的开头!它前面的空格会停用它,然后 js 引擎会绊倒您的导出语句,或者类似的 ``unexpected token export```。

错误:

use babel import { bla } from 'blub'

正确:

use babel import { bla } from 'blub'

【讨论】:

    猜你喜欢
    • 2022-10-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2019-12-25
    • 2018-08-14
    • 2017-11-21
    • 2017-07-16
    • 1970-01-01
    相关资源
    最近更新 更多