Jared
Jared

Reputation: 6080

How can es-lint be configured to warn/error if useState is used

I've not been able to find a rule, but is it possible to throw a warning or an error if someone attempts to reach for useState instead of the state provider?

I'm hoping I don't need to reach for a custom rule, but can work through that if there isn't an existing one out there.

Upvotes: 1

Views: 433

Answers (1)

hendrixchord
hendrixchord

Reputation: 5434

You can use no-restricted-syntax

All you need to do is play around with AST Explorer. I have done it for you and you need to add this to your .eslintrc file in rules section.

In the example below depending upon your need you can either throw it as an error or warn.

"no-restricted-syntax": [
     "error",
     {
         "selector": "CallExpression[callee.name='useState']",
         "message": "Please use state provider instead."
     }
],

Upvotes: 1

Related Questions