简体   繁体   English

tslint 说不允许调用 console.log - 我该如何允许?

[英]tslint says calls to console.log are not allowed - How do I allow this?

I just started using create-react-app with typescript我刚开始使用带有 typescript 的 create-react-app

create-react-app my-app --scripts-version=react-scripts-ts

and the default tslint.json configuration does not allow console.log().并且默认的 tslint.json 配置不允许 console.log()。

How can I (for now) enable console.log?我如何(现在)启用console.log?

The docs for this are at https://palantir.github.io/tslint/rules/no-console/ .相关文档位于https://palantir.github.io/tslint/rules/no-console/ But they don't say where to put this line:但他们没有说把这条线放在哪里:

    "no-console": [true, "log", "error"]

I searched and found this tslint.json configuration file syntax , so I tried this:我搜索并找到了这个tslint.json 配置文件语法,所以我尝试了这个:

"rules": {
    "no-console": [true, "warning"]
}

In an attempt to get log messages that would just be warnings.试图获取只是警告的日志消息。 But that didn't work.但这没有用。

I've commented out the few console.log() lines I have but will want to be able to do this in the future.我已经注释掉了我拥有的几行 console.log() 行,但我希望将来能够做到这一点。

Add // tslint:disable-next-line:no-console in the line right before your calls to console.log to prevent the error message only once.在调用console.log之前的行中添加// tslint:disable-next-line:no-console以防止仅出现一次错误消息。

If you want to disable the rule entirely add the following to your tslint.json (most likely in your root folder):如果您想完全禁用该规则,请将以下内容添加到您的tslint.json (很可能在您的根文件夹中):

{
    "rules": {
        "no-console": false
    }
}

For those of you coming here with a mixed codebase of javascript and typescript.对于那些带着 javascript 和 typescript 的混合代码库来到这里的人。

You may need to define the 'no-console' option in jsRules, jslints rules object for javascript files, ie there are separate rules objects for javascript and typescript.您可能需要在 jsRules 中定义 'no-console' 选项,用于 javascript 文件的 jslints 规则对象,即 javascript 和 typescript 有单独的规则对象。

//tslint.json //tslint.json

{
  "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"], //Example... 
  "rules": {
    "no-console": false //Disable for typescript
  },
  "jsRules": {
    "no-console": false //Disable for javascript
  }
}

Add the following to your tslint.json将以下内容添加到您的tslint.json

{
   "rules": {
      "no-console": {
         "severity": "warning",
      } 
   }
}

This is the correct syntax to define the no-console rule (or any other rule for that matter) but only with a warning rather than an error (obviously change the options to whatever you want)这是定义无控制台规则(或任何其他规则)的正确语法,但仅带有警告而不是错误(显然将选项更改为您想要的任何内容)

"no-console": {
    "severity": "warning",
    "options": [
        "log",
        "error",
        "debug",
        "info",
        "time",
        "timeEnd",
        "trace"
    ]
},

The way I handle tslint "no-console" rule is per file which I have found is convenient and isolated in the development phase.我处理tslint “no-console” 规则的方式是每个文件,我发现在开发阶段很方便和隔离。

As soon as I need to use the first console.log();只要我需要使用第一个 console.log(); Visual Studio Code shows the option to add: Visual Studio Code 显示添加选项:

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

console.log();控制台.log();

So here I just delete "-next-line" and this command will cover the entire file.所以在这里我只是删除“-next-line”,这个命令将覆盖整个文件。

// tslint:disable: no-console // tslint:disable: 无控制台

console.log();控制台.log();

I hope it helps as an alternative to disable the feature for the entire app.我希望它有助于替代禁用整个应用程序的功能。

RON罗恩

如果// tslint:disable-next-line:no-console不起作用尝试// eslint:disable-next-line:no-console

in typeScript version 3 update tslint.json under key rule like below:在 typeScript 版本 3 中,在关键规则下更新 tslint.json,如下所示:

"no-console": [
    true,
    "debug",
    "time",
    "timeEnd",
    "trace"
],

this way you just specify debug, time, timeEnd, trace to be not used, if in your default tslint "info" is in the list just remove it.这样,您只需指定不使用的调试、时间、时间结束、跟踪,如果在您的默认 tslint 中“信息”在列表中,只需将其删除。

只是绕过 linter 的肮脏技巧:

const { log } = console; log('Hello linter'); // TODO: remove

According to the docs: https://eslint.org/docs/user-guide/getting-started#configuration根据文档: https ://eslint.org/docs/user-guide/getting-started#configuration

  • "off" or 0 - turn the rule off "off" 或 0 - 关闭规则
  • "warn" or 1 - turn the rule on as a warning (doesn't affect exit code) "warn" 或 1 - 将规则作为警告打开(不影响退出代码)
  • "error" or 2 - turn the rule on as an error (exit code will be 1) “错误”或 2 - 将规则作为错误打开(退出代码将为 1)

By the way, your correct setup would be顺便说一句,您的正确设置是

{
  "rules": {
    "no-console": false
  }
}
  {
    "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
    "linterOptions": {
        "exclude": [
            "config/**/*.js",
            "node_modules/**/*.ts",
            "coverage/lcov-report/*.js"
        ]
    },
    "rules": {
        "no-console": false
    },
    "jsRules": {
        "no-console": false
    }
 }

在此处输入图像描述

The Syntax changed!语法变了!

For the next-line exception there needs to be a space before no-console: // eslint-disable-next-line no-console .对于下一行异常, no-console 之前需要一个空格: // eslint-disable-next-line no-console

For the file exception, it also has to be inside of the Multi-line comment syntax: /* eslint-disable no-console */ .对于文件异常,它还必须在多行注释语法中: /* eslint-disable no-console */

It also might depend on certain configurations.它也可能取决于某些配置。

对于那些来这里寻找一种方法来保持console.log被禁止,但允许使用console.timeconsole.timeEnd等有用方法的人,例如,您可以在 .eslintrc 规则中全局显式定义它:

 "no-console": ["error", {"allow": ["time", "timeEnd", "trace"]}],

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

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