我正在使用eslint与Sublime Text 3和我正在编写gulpfile.js。

/*eslint-env node*/
var gulp = require('gulp');

gulp.task('default', function(){
    console.log('default task');
});

但是eslint一直显示error: " error: Unexpected console statement. "(no-console)”

我在这里找到了官方文件,但我仍然不知道如何禁用它。

/*eslint-env node*/
var gulp = require('gulp');

/*eslint no-console: 2*/
gulp.task('default', function(){
    console.log('default task');
});

也不管用。

我的崇高文本3插件:升华和升华-contrib-eslint。

这是我的.eslintrc.js文件:

module.exports = {
    "rules": {
        "no-console":0,
        "indent": [
            2,
            "tab"
        ],
        "quotes": [
            2,
            "single"
        ],
        "linebreak-style": [
            2,
            "unix"
        ],
        "semi": [
            2,
            "always"
        ]
    },
    "env": {
        "browser": true,
        "node": true
    },
    "extends": "eslint:recommended"
};

当前回答

在我的vue项目中,我修复了这个问题:

vim package.json
...
"rules": {
    "no-console": "off"
},
...

Ps:包装。Json是vue项目目录下的一个configfile,最后的内容如下所示:

{
  "name": "metadata-front",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "vue": "^2.5.17",
    "vue-router": "^3.0.2"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.4",
    "@vue/cli-plugin-eslint": "^3.0.4",
    "@vue/cli-service": "^3.0.4",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.8.0",
    "eslint-plugin-vue": "^5.0.0-0",
    "vue-template-compiler": "^2.5.17"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {
        "no-console": "off"
    },
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

其他回答

在我的vue项目中,我修复了这个问题:

vim package.json
...
"rules": {
    "no-console": "off"
},
...

Ps:包装。Json是vue项目目录下的一个configfile,最后的内容如下所示:

{
  "name": "metadata-front",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "vue": "^2.5.17",
    "vue-router": "^3.0.2"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.4",
    "@vue/cli-plugin-eslint": "^3.0.4",
    "@vue/cli-service": "^3.0.4",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.8.0",
    "eslint-plugin-vue": "^5.0.0-0",
    "vue-template-compiler": "^2.5.17"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {
        "no-console": "off"
    },
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

如果您只想禁用该规则一次,则需要查看Exception的答案。

你可以通过只对一行禁用规则来改善这一点:

... 在当前行中:

console.log(someThing); /* eslint-disable-line no-console */

... 或者在下一行:

/* eslint-disable-next-line no-console */
console.log(someThing);

你应该添加一条规则并添加你的env:

{
  "rules": {
    "no-console": "off"
  },
  "env": {
    "browser": true
  }
}

您可以添加其他环境。

如果你想只对一行禁用规则,下面的方法可以在VSCode中使用ESLint。

禁用下一行:

// eslint-disable-next-line no-console
console.log('hello world');

禁用当前行。

console.log('hello world'); // eslint-disable-line no-console

你应该更新eslint配置文件来永久修复这个问题。否则,你可以临时启用或禁用eslint检查控制台如下所示

/* eslint-disable no-console */
console.log(someThing);
/* eslint-enable no-console */