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