如何修复“仅在 ES6 中可用”的 linter 错误?

Posted

技术标签:

【中文标题】如何修复“仅在 ES6 中可用”的 linter 错误?【英文标题】:How to fix “only available in ES6” linter errors? 【发布时间】:2017-03-31 00:25:42 【问题描述】:

我正在尝试向我的 Express 应用程序添加一个 linter,但遇到了几个关于 ES6 的 linter 错误,即 ‘export’ is only available in ES6 (use esnext option)’const’ is only available in javascript 1.7’arrow function syntax’ is only available in Javascript 1.7。我不知道如何摆脱这些错误——任何帮助将不胜感激。我目前已经尝试安装几个东西,包括 Sublime Text 2 linter 和 babel-eslint,但可能会遇到这个错误。

来自相关的package.json:


"dependencies": 
  "babel-cli": "^6.10.1",
  "babel-preset-es2015": "^6.9.0",
  "babel-preset-stage-2": "^6.5.0",
  "bcrypt-nodejs": "0.0.3",
  "botkit-sms": "^1.1.0",
  "dotenv": "^2.0.0",
  "express": "^4.14.0",
  "jwt-simple": "^0.5.0",
  "mongoose": "^4.6.3",
  "passport": "^0.3.2",
  "passport-jwt": "^2.2.1",
  "passport-local": "^1.0.0",
  "request": "^2.76.0",
  "routific": "0.0.2"
,
"devDependencies": 
  "babel-eslint": "^6.1.2",
  "eslint": "^2.13.1",
  "eslint-config-airbnb": "^9.0.1",
  "eslint-plugin-import": "^1.16.0",
  "eslint-plugin-jsx-a11y": "^1.4.2",
  "eslint-plugin-react": "^5.1.1",
  "nodemon": "^1.9.2"

.babelrc:


   "presets": ["es2015", "stage-2"]

.eslintrc:


extends: ["airbnb", "esnext"],
parser: "babel-eslint",
env: 
  browser: false,
  node: true,
  es6: true
,
rules: 
  strict: 0,
  quotes: [2, "single"],
  no-else-return: 0,
  new-cap: ["error", "capIsNewExceptions": ["Router"]],
  no-console: 0,
  import/no-unresolved: [2,  commonjs: true],
  no-unused-vars: ["error",  "vars": "all", "args": "none" ],
  no-underscore-dangle: 0,
  arrow-body-style: ["error", "always"],
  no-shadow: ["error",  "allow": ["done", "res", "cb", "err", "resolve", "reject"] ],
  no-use-before-define: ["error",  "functions": false ],
  max-len: 0
,
plugins: [
  'import'
],
ecmaFeatures: 
  jsx: true,
  modules: true


【问题讨论】:

【参考方案1】:

在 Preferences -> Package Settings -> Sublimelinter -> Settings-User 中将 esnext 设置为 true


  "jshint_options":
    
        "esnext": true
    

【讨论】:

【参考方案2】:

我认为你必须摆脱 esnext 这是我的节点 .eslintrc:

    
  "extends" : "airbnb",
  "rules": 
   // disable requiring trailing commas because it might be nice to revert to
   // being JSON at some point, and I don t want to make big changes now.
   "comma-dangle": 0,
   // we can use escape and template strings
   "quotes": [1, "single", "avoidEscape": true, "allowTemplateLiterals": true],
   "max-len": [0],
   "no-console": 0,
   "no-param-reassign": 0
 ,
 "globals":  "ENV": true ,
 "env": 
    "browser": true,
    "node": true,
    "jasmine": true
  

这些是我的 devDependencies

  "devDependencies": 
    "babel-cli": "^6.9.0",
    "babel-core": "^6.9.0",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-stage-0": "^6.5.0",
    "eslint": "^3.1.1",
    "eslint-config-airbnb": "^12.0.0",
    "eslint-plugin-import": "1.16.0",
    "eslint-plugin-jsx-a11y": "2.2.3",
    "eslint-plugin-react": "6.4.1",
    "nodemon": "^1.9.2"
  

【讨论】:

【参考方案3】:

ecmaFeatures 配置设置位于parserOptions 之下。

...
plugins: [
  'import'
],
parserOptions: 
    ecmaFeatures: 
        jsx: true
    ,
    ecmaVersion: 6,
    sourceType: 'module'
,
...

您应该使用sourceType: 'module' 而不是modules: true

【讨论】:

以上是关于如何修复“仅在 ES6 中可用”的 linter 错误?的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 React linter 中修复“声明的道具类型中的错字:标题”?

如何设置在各种 IDE 中使用的默认 linter

如何在 Jupyter Notebooks 中使用 Pylint 或其他 linter?

如何告诉验证器 linter 不验证子模块?

当代码被拆分到多个文件时,如何使用 Atom 的 linter-jshint?

如何在 Visual Studio 代码中使用 ruby​​-linter?