UNPKG

tslint-react-hooks

Version:

TSLint rule that enforces the Rules of Hooks

29 lines (28 loc) • 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isReactApiExpression = void 0; const typescript_1 = require("typescript"); /** * Tests whether an `Expression` is an identifier that matches a predicate. Accepts also property * access of that identifier from React's top-level API. * * @example * const isForwardRef = isReactApiExpression((identifier) => identifier.text === 'forwardRef'); * // would match `isForwardRef` or `React.isForwardRef` * const matches = isForwardRef(node); * * @param predicate Predicate that is run on the actual identifier. */ const isReactApiExpression = (predicate) => (expression) => { if ((0, typescript_1.isIdentifier)(expression)) { return predicate(expression); } else if ((0, typescript_1.isPropertyAccessExpression)(expression)) { return ((0, typescript_1.isIdentifier)(expression.expression) && expression.expression.text === 'React' && (0, typescript_1.isIdentifier)(expression.name) && predicate(expression.name)); } return false; }; exports.isReactApiExpression = isReactApiExpression;