productboardlabs / tslint-pb

📄 (DEPRECATED) Our own custom TSlint rules. What has been seen, cannot be unseen.
MIT License
5 stars 0 forks source link

rule: `selectors-format` #33

Closed Aldredcz closed 5 years ago

Aldredcz commented 5 years ago

To enforce some rules to our selectors.

1) Dependency array must be defined as array literal. It's better practice to have the list of dependencies inlined rather than in some variable outside of selector definition.

2) Dependency array must contain at least one dependency. Otherwise it's probably misused selector and developer should use plain (possibly memoized) function.

3) Function in selector must be defined as arrow literal. First for readability we want the function to be inlined and not defined outside of selector definition. Also, we don't wanna use function definition, to avoid possible this abuse.

4) Default arguments in selector function are forbidden. Unfortunately, JavaScript doesn't play well with default arguments when using memoization on dynamic number of arguments. Therefore we have to disable it to prevent nasty bugs.

5) All arguments in selector function must be typed. Unfortunately if you skip types on arguments, it just uses implicit any (probably because of generics used in select definition). It's potentially error-prone, so it's good idea to enforce it.

Configuration

{
  "rules": {
    "selectors-format": [
      true,
      {
        "importsPaths": {
          "select": ["libs/flux", "libs/flux/select"]
        },
        "reference": "Optional text to explain the error"
      }
    ]
  }
}

Example

select(
  FLUX_DEPENDENCIES,
  ~~~~~~~~~~~~~~~~~  [Dependencies must be defined as array literal.]
  () => {},
);

select(
  [Store],
  func,
  ~~~~  [Function must be defined as arrow function literal.]
);

select(
  [Store],
  (a: number = 10) => false,
   ~~~~~~~~~~~~~~            [Default arguments are forbidden.]
);

select(
  [Store],
  (abc, xyz) => false,
   ~~~             [All arguments must be typed.]
        ~~~        [All arguments must be typed.]
);
Aldredcz commented 5 years ago

@jukben ready for final review! 🤩

jukben commented 5 years ago

Could you please add it to the default? https://github.com/productboardlabs/tslint-pb/blob/master/tslint-pb.json#L5