TSLint: Ban usage of the Function.length property
Asked Answered
W

1

6

I'm in a project that uses knockout.js (using TypeScript), and since knockout observables are just functions, people often run into issues accessing the length property of the observable function by mistake, instead of accessing the length property of their custom object model.

Is there some tslint rule that can ban the usage of a specific property of a certain type? I've seen the "ban" rule, but that seems to only work for banning usage of functions and methods, but not properties.

Werby answered 5/12, 2018 at 19:18 Comment(1)
still couldn't find a solution. Anyone knows if this is possible?Werby
A
-1

There is a tslint rule that bans the use of specific functions or global methods.

A list of banned functions or methods in the following format:

  • banning functions:
    • just the name of the function: "functionName"
    • the name of the function in an array with one element: ["functionName"]
    • an object in the following format: {"name": "functionName", "message": "optional explanation message"}
  • banning methods:
    • an array with the object name, method name and optional message: ["functionName", "methodName", "optional message"]
    • an object in the following format: {"name": ["objectName", "methodName"], "message": "optional message"}
      • you can also ban deeply nested methods: {"name": ["foo", "bar", "baz"]} bans foo.bar.baz()
      • the first element can contain a wildcard (*) that matches everything. {"name": ["*", "forEach"]} bans [].forEach(...), $(...).forEach(...), arr.forEach(...), etc.

Config examples

"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."}
]

Schema

{
  "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"
        ]
      }
    ]
  }
}
Abradant answered 5/12, 2018 at 19:37 Comment(2)
as I mentioned in the original question, I'm aware of this rule, but that seems to target functions and methods, but not normal properties, correct? Or can this be used to ban Function.length too?Werby
Right now, eslint-plugin-ban only applies to call expressions.Lota

© 2022 - 2024 — McMap. All rights reserved.