| 1 | /**
|
|---|
| 2 | * @fileoverview Require all forwardRef components include a ref parameter
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | 'use strict';
|
|---|
| 6 |
|
|---|
| 7 | const isParenthesized = require('../util/ast').isParenthesized;
|
|---|
| 8 | const docsUrl = require('../util/docsUrl');
|
|---|
| 9 | const report = require('../util/report');
|
|---|
| 10 | const getMessageData = require('../util/message');
|
|---|
| 11 |
|
|---|
| 12 | // ------------------------------------------------------------------------------
|
|---|
| 13 | // Rule Definition
|
|---|
| 14 | // ------------------------------------------------------------------------------
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @param {ASTNode} node
|
|---|
| 18 | * @returns {boolean} If the node represents the identifier `forwardRef`.
|
|---|
| 19 | */
|
|---|
| 20 | function isForwardRefIdentifier(node) {
|
|---|
| 21 | return node.type === 'Identifier' && node.name === 'forwardRef';
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * @param {ASTNode} node
|
|---|
| 26 | * @returns {boolean} If the node represents a function call `forwardRef()` or `React.forwardRef()`.
|
|---|
| 27 | */
|
|---|
| 28 | function isForwardRefCall(node) {
|
|---|
| 29 | return (
|
|---|
| 30 | node.type === 'CallExpression'
|
|---|
| 31 | && (
|
|---|
| 32 | isForwardRefIdentifier(node.callee)
|
|---|
| 33 | || (node.callee.type === 'MemberExpression' && isForwardRefIdentifier(node.callee.property))
|
|---|
| 34 | )
|
|---|
| 35 | );
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | const messages = {
|
|---|
| 39 | missingRefParameter: 'forwardRef is used with this component but no ref parameter is set',
|
|---|
| 40 | addRefParameter: 'Add a ref parameter',
|
|---|
| 41 | removeForwardRef: 'Remove forwardRef wrapper',
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | /** @type {import('eslint').Rule.RuleModule} */
|
|---|
| 45 | module.exports = {
|
|---|
| 46 | meta: {
|
|---|
| 47 | docs: {
|
|---|
| 48 | description: 'Require all forwardRef components include a ref parameter',
|
|---|
| 49 | category: 'Possible Errors',
|
|---|
| 50 | recommended: false,
|
|---|
| 51 | url: docsUrl('forward-ref-uses-ref'),
|
|---|
| 52 | },
|
|---|
| 53 | messages,
|
|---|
| 54 | schema: [],
|
|---|
| 55 | type: 'suggestion',
|
|---|
| 56 | hasSuggestions: true,
|
|---|
| 57 | },
|
|---|
| 58 |
|
|---|
| 59 | create(context) {
|
|---|
| 60 | const sourceCode = context.getSourceCode();
|
|---|
| 61 |
|
|---|
| 62 | return {
|
|---|
| 63 | 'FunctionExpression, ArrowFunctionExpression'(node) {
|
|---|
| 64 | if (!isForwardRefCall(node.parent)) {
|
|---|
| 65 | return;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | if (node.params.length === 1) {
|
|---|
| 69 | report(context, messages.missingRefParameter, 'missingRefParameter', {
|
|---|
| 70 | node,
|
|---|
| 71 | suggest: [
|
|---|
| 72 | Object.assign(
|
|---|
| 73 | getMessageData('addRefParameter', messages.addRefParameter),
|
|---|
| 74 | {
|
|---|
| 75 | fix(fixer) {
|
|---|
| 76 | const param = node.params[0];
|
|---|
| 77 | // If using shorthand arrow function syntax, add parentheses around the new parameter pair
|
|---|
| 78 | const shouldAddParentheses = node.type === 'ArrowFunctionExpression' && !isParenthesized(context, param);
|
|---|
| 79 | return [].concat(
|
|---|
| 80 | shouldAddParentheses ? fixer.insertTextBefore(param, '(') : [],
|
|---|
| 81 | fixer.insertTextAfter(param, `, ref${shouldAddParentheses ? ')' : ''}`)
|
|---|
| 82 | );
|
|---|
| 83 | },
|
|---|
| 84 | }
|
|---|
| 85 | ),
|
|---|
| 86 | Object.assign(
|
|---|
| 87 | getMessageData('removeForwardRef', messages.removeForwardRef),
|
|---|
| 88 | {
|
|---|
| 89 | fix(fixer) {
|
|---|
| 90 | return fixer.replaceText(node.parent, sourceCode.getText(node));
|
|---|
| 91 | },
|
|---|
| 92 | }
|
|---|
| 93 | ),
|
|---|
| 94 | ],
|
|---|
| 95 | });
|
|---|
| 96 | }
|
|---|
| 97 | },
|
|---|
| 98 | };
|
|---|
| 99 | },
|
|---|
| 100 | };
|
|---|