I'm grappling with a challenge in JavaScript (or typescript) - ensuring that developers cannot call a method multiple times with the same argument.
For instance:
const foo = (name: string) => {}
foo("ABC") // ok
foo ("123") // ok
foo ("ABC") // ideally, should prompt an error in the IDE indicating that it was already called with the same value previously
To clarify, I am looking to catch this error during the development process, prior to runtime. My intention is for developers to be notified of any mistakes while coding, prompting the IDE to flag the error.
Is there a way to accomplish this? Perhaps through creating a custom tslint rule?
Edit: If achieving the desired outcome above is not natively supported in JavaScript, then alternatively, a new custom tslint rule could suffice. Rules have the capability to store values and context, which can potentially trigger errors during development time, similar to other linting rules in the IDE.
Is this achievable? How would one go about creating a new lint rule with contextual awareness to address this issue?
Furthermore, I am open to scenarios where foo(x)
is utilized and the rule may not recognize the exact value of x or the order in which the function is invoked.
The rule only needs to be intelligent enough to handle basic cases where possible.