|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of referential-type variables as default param in functional component |
| 3 | + * @author Chang Yan |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const docsUrl = require('../util/docsUrl'); |
| 9 | +const report = require('../util/report'); |
| 10 | + |
| 11 | +const FORBIDDEN_TYPES_MAP = { |
| 12 | + ArrowFunctionExpression: 'arrow function', |
| 13 | + FunctionExpression: 'function expression', |
| 14 | + ObjectExpression: 'object literal', |
| 15 | + ArrayExpression: 'array literal', |
| 16 | + ClassExpression: 'class expression', |
| 17 | + NewExpression: 'construction expression', |
| 18 | + JSXElement: 'JSX element', |
| 19 | +}; |
| 20 | + |
| 21 | +const FORBIDDEN_TYPES = new Set(Object.keys(FORBIDDEN_TYPES_MAP)); |
| 22 | +const MESSAGE_ID = 'forbiddenTypeDefaultParam'; |
| 23 | + |
| 24 | +const messages = { |
| 25 | + [MESSAGE_ID]: '{{propName}} has a/an {{forbiddenType}} as default prop. This could lead to potential infinite render loop in React. Use a variable reference instead of {{forbiddenType}}.', |
| 26 | +}; |
| 27 | + |
| 28 | +function isReactComponentName(node) { |
| 29 | + if (node.id && node.id.type === 'Identifier' && node.id.name) { |
| 30 | + const firstLetter = node.id.name[0]; |
| 31 | + return firstLetter.toUpperCase() === firstLetter; |
| 32 | + } |
| 33 | + |
| 34 | + return false; |
| 35 | +} |
| 36 | + |
| 37 | +function isReactComponentVariableDeclarator(variableDeclarator) { |
| 38 | + if (!isReactComponentName(variableDeclarator)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + return ( |
| 42 | + variableDeclarator.init != null |
| 43 | + && variableDeclarator.init.type === 'ArrowFunctionExpression' |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +function hasUsedObjectDestructuringSyntax(params) { |
| 48 | + return ( |
| 49 | + params != null |
| 50 | + && params.length === 1 |
| 51 | + && params[0].type === 'ObjectPattern' |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function verifyDefaultPropsDestructuring(context, properties) { |
| 56 | + // Loop through each of the default params |
| 57 | + properties.filter((prop) => prop.type === 'Property').forEach((prop) => { |
| 58 | + const propName = prop.key.name; |
| 59 | + const propDefaultValue = prop.value; |
| 60 | + |
| 61 | + if (propDefaultValue.type !== 'AssignmentPattern') { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const propDefaultValueType = propDefaultValue.right.type; |
| 66 | + |
| 67 | + if ( |
| 68 | + propDefaultValueType === 'Literal' |
| 69 | + && propDefaultValue.right.regex != null |
| 70 | + ) { |
| 71 | + report(context, messages[MESSAGE_ID], MESSAGE_ID, { |
| 72 | + node: propDefaultValue, |
| 73 | + data: { |
| 74 | + propName, |
| 75 | + forbiddenType: 'regex literal', |
| 76 | + }, |
| 77 | + }); |
| 78 | + } else if ( |
| 79 | + propDefaultValueType === 'CallExpression' |
| 80 | + && propDefaultValue.right.callee.type === 'Identifier' |
| 81 | + && propDefaultValue.right.callee.name === 'Symbol' |
| 82 | + ) { |
| 83 | + report(context, messages[MESSAGE_ID], MESSAGE_ID, { |
| 84 | + node: propDefaultValue, |
| 85 | + data: { |
| 86 | + propName, |
| 87 | + forbiddenType: 'Symbol literal', |
| 88 | + }, |
| 89 | + }); |
| 90 | + } else if (FORBIDDEN_TYPES.has(propDefaultValueType)) { |
| 91 | + report(context, messages[MESSAGE_ID], MESSAGE_ID, { |
| 92 | + node: propDefaultValue, |
| 93 | + data: { |
| 94 | + propName, |
| 95 | + forbiddenType: FORBIDDEN_TYPES_MAP[propDefaultValueType], |
| 96 | + }, |
| 97 | + }); |
| 98 | + } |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = { |
| 103 | + meta: { |
| 104 | + docs: { |
| 105 | + description: 'Disallow usage of referential-type variables as default param in functional component', |
| 106 | + category: 'Best Practices', |
| 107 | + recommended: false, |
| 108 | + url: docsUrl('no-object-type-as-default-prop'), |
| 109 | + }, |
| 110 | + messages, |
| 111 | + }, |
| 112 | + create(context) { |
| 113 | + return { |
| 114 | + FunctionDeclaration(node) { |
| 115 | + if ( |
| 116 | + !isReactComponentName(node) |
| 117 | + || !hasUsedObjectDestructuringSyntax(node.params) |
| 118 | + ) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + const properties = node.params[0].properties; |
| 123 | + verifyDefaultPropsDestructuring(context, properties); |
| 124 | + }, |
| 125 | + 'VariableDeclarator > :matches(ArrowFunctionExpression, FunctionExpression).init'(node) { |
| 126 | + if ( |
| 127 | + !isReactComponentVariableDeclarator(node.parent) |
| 128 | + || !hasUsedObjectDestructuringSyntax(node.params) |
| 129 | + ) { |
| 130 | + return; |
| 131 | + } |
| 132 | + const properties = node.params[0].properties; |
| 133 | + verifyDefaultPropsDestructuring(context, properties); |
| 134 | + }, |
| 135 | + }; |
| 136 | + }, |
| 137 | +}; |
0 commit comments