简体   繁体   English

具有Babel,Flow,React和React Native的ESLint配置

[英]ESLint configuration with Babel, Flow, React and React Native

I'm developing a mobile application in React Native and I've been trying to configure ESLint properly for a while (including Babel, Flow and Prettier). 我正在用React Native开发一个移动应用程序,并且一段时间以来我一直在尝试正确配置ESLint(包括Babel,Flow和Prettier)。

I get the following error in VSCode : 'module' should be listed in the project's dependencies. eslint(import/no-extraneous-dependencies) 我在VSCode中收到以下错误: 'module' should be listed in the project's dependencies. eslint(import/no-extraneous-dependencies) 'module' should be listed in the project's dependencies. eslint(import/no-extraneous-dependencies) . 'module' should be listed in the project's dependencies. eslint(import/no-extraneous-dependencies) This goes for all my modules and plugins such as react, react-native and others. 这适用于我的所有模块和插件,例如react,react-native等。 I installed every package and the VSCode ESLint extension but ESLint still raises this error. 我安装了每个软件包和VSCode ESLint扩展,但是ESLint仍然会出现此错误。 Maybe I've duplicated some functionalities in the extends section too, I'm wondering if my configuration is good and if I can get rid of this error (everything else looks to work as expected). 也许我在extends部分中也重复了一些功能,我想知道我的配置是否正确,是否可以消除此错误(其他所有功能都可以正常工作)。

.eslintrc.json .eslintrc.json

{
  "extends": [
    "airbnb",
    "plugin:react/recommended",
    "plugin:flowtype/recommended",
    "plugin:prettier/recommended",
    "prettier",
    "prettier/babel",
    "prettier/react",
    "prettier/flowtype",
    "prettier/standard"
  ],
  "plugins": [
    "react",
    "react-native",
    "flowtype",
    "standard",
    "prettier"
  ],
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "modules": true
    }
  },
  "env": {
    "browser": true,
    "jest": true
  },
  "rules": {
    "import/no-unresolved": "off",
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [
          ".js",
          ".jsx"
        ]
      }
    ],
    "prettier/prettier": [
      "error",
      {
        "trailingComma": "es5",
        "singleQuote": true,
        "printWidth": 100
      }
    ]
  }
}

package.json package.json

{
  "name": "PICSTART",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "ios": "react-native run-ios --simulator=\"iPhone XR\"",
    "android": "react-native run-android",
    "debugjs": "react-devtools",
    "test": "jest",
    "flow": "node_modules/flow-bin/vendor/flow"
  },
  "dependencies": {
    "@okgrow/react-native-copilot": "^2.4.1",
    "moment": "^2.24.0",
    "react": "16.5.0",
    "react-native": "0.57.1",
    "react-native-camera": "^1.3.0",
    "react-native-datepicker": "^1.7.2",
    "react-native-fs": "^2.13.3",
    "react-native-image-picker": "^0.28.0",
    "react-native-loading-spinner-overlay": "^1.0.1",
    "react-native-modal": "^9.0.0",
    "react-native-svg": "^9.3.3",
    "react-native-vector-icons": "^6.1.0",
    "react-navigation": "^2.0.4",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0",
    "redux-mock-store": "^1.5.1",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/runtime": "^7.1.2",
    "babel-eslint": "^10.0.1",
    "babel-jest": "23.6.0",
    "eslint": "^5.15.3",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-prettier": "^4.1.0",
    "eslint-plugin-flowtype": "^3.4.2",
    "eslint-plugin-import": "^2.16.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-prettier": "^3.0.1",
    "eslint-plugin-react": "^7.12.4",
    "eslint-plugin-react-native": "^3.6.0",
    "eslint-plugin-standard": "^4.0.0",
    "flow-bin": "^0.78.0",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.47.0",
    "prettier": "^1.16.4",
    "react-test-renderer": "16.5.0"
  },
  "jest": {
    "preset": "react-native",
    "testRegex": "./__tests__/[^setup].*.js$",
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|native-base|react-navigation|react-native-fabric)"
    ],
    "setupFiles": [
      "./__tests__/setup.js"
    ]
  }
}

This error eslint(import/no-extraneous-dependencies) happens because you are importing a module that isn't in your package.json. 发生此错误eslint(import/no-extraneous-dependencies)是因为您要导入的文件不在package.json.

If everything is Ok, no errors while compiling, you could look at the docs here and they explain how to disable this rule. 如果一切正常,在编译时没有错误,您可以在这里查看文档 ,他们解释了如何禁用此规则。

If .eslintrc.json and package.json aren't in the same folder, this may cause the error because eslint looks for package.json file but it can find it because it's in another folder. 如果.eslintrc.jsonpackage.json不在同一文件夹中,则可能会导致错误,因为eslint会查找package.json文件,但由于它在另一个文件夹中而可以找到它。

Edit: Maybe you ar missing "es6": true in "env" 编辑:也许您错过了"es6": true"env" "es6": true

Try 尝试

"env": {
    "es6": true,
    "browser": true,
    "jest": true
},

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

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