【问题标题】:TSLint: Ban usage of the Function.length propertyTSLint:禁止使用 Function.length 属性
【发布时间】:2019-05-07 10:26:52
【问题描述】:

我在一个使用knockout.js(使用TypeScript)的项目中,由于knockout observables只是函数,人们经常会遇到错误访问可观察函数的length属性,而不是访问@ 987654322@ 自定义对象模型的属性。

是否有一些 tslint 规则可以禁止使用某种类型的特定属性?我见过“禁止”规则,但这似乎只适用于禁止使用函数和方法,而不是属性。

【问题讨论】:

  • 仍然找不到解决方案。有人知道这是否可能吗?

标签: typescript tslint


【解决方案1】:

有一个 tslint 规则禁止使用特定函数或全局方法。

以下格式的禁用函数或方法列表:

  • 禁止功能:
    • 只是函数的名称:“functionName”
    • 具有一个元素的数组中的函数名称:["functionName"]
    • 以下格式的对象:{"name": "functionName", "message": "optional explanation message"}
  • 禁止方法:
    • 包含对象名称、方法名称和可选消息的数组:["functionName", "methodName", "optional message"]
    • 以下格式的对象:{"name": ["objectName", "methodName"], "message": "optional message"}
      • 你也可以禁止深度嵌套的方法:{"name": ["foo", "bar", "baz"]}bans foo.bar.baz()
      • 第一个元素可以包含匹配所有内容的通配符 (*)。 {"name": ["*", "forEach"]} 禁止[].forEach(...), $(...).forEach(...), arr.forEach(...), etc.

配置示例

"ban": [
  true,
  "eval",
  {"name": "$", "message": "please don't"},
  ["describe", "only"],
  {"name": ["it", "only"], "message": "don't focus tests"},
  {
    "name": ["chai", "assert", "equal"],
    "message": "Use 'strictEqual' instead."
  },
  {"name": ["*", "forEach"], "message": "Use a regular for loop instead."}
]

架构

{
  "type": "list",
  "listType": {
    "anyOf": [
      {
        "type": "string"
      },
      {
        "type": "array",
        "items": {
          "type": "string"
        },
        "minLength": 1,
        "maxLength": 3
      },
      {
        "type": "object",
        "properties": {
          "name": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "minLength": 1
              }
            ]
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "name"
        ]
      }
    ]
  }
}

【讨论】:

  • 正如我在原始问题中提到的,我知道这条规则,但这似乎是针对函数和方法,而不是普通属性,对吗?或者这也可以用来禁止Function.length
  • 现在,eslint-plugin-banonly applies to call expressions
猜你喜欢
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
相关资源
最近更新 更多