我正在使用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"
};

当前回答

2018年10月,

只做:

// tslint:disable-next-line:no-console

其他人用

// eslint-disable-next-line no-console

不管用!

其他回答

更好的选择是根据节点环境有条件地显示console.log和调试器语句。

  rules: {
    // allow console and debugger in development
    'no-console': process.env.NODE_ENV === 'production' ? 2 : 0,
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
  },

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

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

您可以添加其他环境。

2018年10月,

只做:

// tslint:disable-next-line:no-console

其他人用

// eslint-disable-next-line no-console

不管用!

如果你在你的本地项目下安装eslint,你应该有一个目录/node_modules/eslint/conf/,在这个目录下有一个文件eslint.json。你可以编辑文件,并修改“no-console”条目的值为“off”(尽管也支持0值):

"rules": {
    "no-alert": "off",
    "no-array-constructor": "off",
    "no-bitwise": "off",
    "no-caller": "off",
    "no-case-declarations": "error",
    "no-catch-shadow": "off",
    "no-class-assign": "error",
    "no-cond-assign": "error",
    "no-confusing-arrow": "off",
    "no-console": "off",
    ....

我希望这个“配置”可以帮助你。

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

禁用下一行:

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

禁用当前行。

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