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 | module.exports = {
|
---|
45 | meta: {
|
---|
46 | docs: {
|
---|
47 | description: 'Require all forwardRef components include a ref parameter',
|
---|
48 | category: 'Possible Errors',
|
---|
49 | recommended: false,
|
---|
50 | url: docsUrl('forward-ref-uses-ref'),
|
---|
51 | },
|
---|
52 | messages,
|
---|
53 | schema: [],
|
---|
54 | type: 'suggestion',
|
---|
55 | hasSuggestions: true,
|
---|
56 | },
|
---|
57 |
|
---|
58 | create(context) {
|
---|
59 | const sourceCode = context.getSourceCode();
|
---|
60 |
|
---|
61 | return {
|
---|
62 | 'FunctionExpression, ArrowFunctionExpression'(node) {
|
---|
63 | if (!isForwardRefCall(node.parent)) {
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (node.params.length === 1) {
|
---|
68 | report(context, messages.missingRefParameter, 'missingRefParameter', {
|
---|
69 | node,
|
---|
70 | suggest: [
|
---|
71 | Object.assign(
|
---|
72 | getMessageData('addRefParameter', messages.addRefParameter),
|
---|
73 | {
|
---|
74 | fix(fixer) {
|
---|
75 | const param = node.params[0];
|
---|
76 | // If using shorthand arrow function syntax, add parentheses around the new parameter pair
|
---|
77 | const shouldAddParentheses = node.type === 'ArrowFunctionExpression' && !isParenthesized(context, param);
|
---|
78 | return [].concat(
|
---|
79 | shouldAddParentheses ? fixer.insertTextBefore(param, '(') : [],
|
---|
80 | fixer.insertTextAfter(param, `, ref${shouldAddParentheses ? ')' : ''}`)
|
---|
81 | );
|
---|
82 | },
|
---|
83 | }
|
---|
84 | ),
|
---|
85 | Object.assign(
|
---|
86 | getMessageData('removeForwardRef', messages.removeForwardRef),
|
---|
87 | {
|
---|
88 | fix(fixer) {
|
---|
89 | return fixer.replaceText(node.parent, sourceCode.getText(node));
|
---|
90 | },
|
---|
91 | }
|
---|
92 | ),
|
---|
93 | ],
|
---|
94 | });
|
---|
95 | }
|
---|
96 | },
|
---|
97 | };
|
---|
98 | },
|
---|
99 | };
|
---|