导出语句上的ESLint解析错误

时间:2017-07-13 20:37:30

标签: javascript reactjs babeljs eslint

我跟随课程,作者以下列方式导出组件:

export MainContainer from './Main/MainContainer'

默认的正确方式是export { default as MainContainer } from './Main/MainContainer',它不是那么干净。作者可以用babel-eslint包来做到这一点,但是在我安装该包之后我仍然会得到lint错误。

Repo link

文件结构:

enter image description here

预期

在containers / index.js中使用export MainContainer from './Main/MainContainer'时没有ESlint错误。那个export语句应该允许config / routes.js像这样导入MainContainer:

import { MainContainer } from '../containers'

结果

ESlint错误:

enter image description here

容器/主要/ MainContainer.js

import React from 'react'

export default class MainContainer extends React.Component {
  render () {
    return (
      <p>Hello World</p>
    )
  }
}

容器/ index.js

export MainContainer from './Main/MainContainer'
// export { default as MainContainer } from './Main/MainContainer'

配置/ routes.js

import ReactDom from 'react-dom'
import routes from './config/routes'

ReactDom.render(
  routes,
  document.getElementById('app')
)

我安装了以下软件包:

"devDependencies": {
  "babel-core": "^6.25.0",
  "babel-eslint": "^8.0.0-alpha.15",
  "babel-loader": "^7.1.1",
  "babel-plugin-transform-export-extensions": "^6.22.0",
  "babel-preset-es2015": "^6.24.1",
  "babel-preset-react": "^6.24.1",
  "babel-preset-stage-0": "^6.24.1",
  "babel-preset-stage-1": "^6.24.1",
  "css-loader": "^0.28.4",
  "eslint": "^4.2.0",
  "eslint-config-standard": "^10.2.1",
  "eslint-plugin-promise": "^3.5.0",
  "eslint-plugin-react": "^7.1.0",
  "eslint-plugin-standard": "^3.0.1",
  "html-webpack-plugin": "^2.29.0",
  "style-loader": "^0.18.2",
  "webpack": "^3.2.0",
  "webpack-dev-server": "^2.5.1"
}

npm run lint脚本:

"lint": "eslint app/.; exit 0",

.babelrc

{
  presets: [
    'react',
    'es2015',
    'stage-0',
    'stage-1'
  ],
  "plugins": ["transform-export-extensions"]
}

.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": {
    "no-console": [2, {allow: ["warn", "error"]}],
    "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/self-closing-comp": 2,
    "react/sort-comp": 2
  }
}

2 个答案:

答案 0 :(得分:1)

  

默认的正确方法是导出{默认为MainContainer}来自&#39; ./ Main / MainContainer&#39;

你从哪里获得这些信息?根据{{​​3}},您可以:

export { MainContainer as default } from './Main/MainContainer'

export default MainContainer from './Main/MainContainer'

答案 1 :(得分:0)

在我的应用中eslint --init我选择json作为其中一个选项。

这创建了一个.eslint.json文件,因此我有2个eslint文件,我原来的.eslintrc和json版本。

我相信.json版本覆盖了非JSON版本。

这是包含原始文件的文件:

enter image description here

{
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended"
    ],
    "parser": "babel-eslint",
    "parserOptions": {
        "ecmaFeatures": {
            "experimentalObjectRestSpread": true,
            "jsx": true
        },
        "sourceType": "module"
    },
    "plugins": [
        "react"
    ],
    "rules": {
      "no-console": [2, {"allow": ["warn", "error"]}],
      "indent": ["error", 2],
      "linebreak-style": ["error", "unix"],
      "quotes": ["error","single"],
      "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-max-props-per-line": [2, {"maximum": 3}],
      "react/self-closing-comp": 2,
      "react/sort-comp": 2
    }
}

现在使用时我没有收到错误或警告:

export MainContainer from './Main/MainContainer'