|
| 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 values = require('object.values'); |
| 9 | + |
| 10 | +const Components = require('../util/Components'); |
| 11 | +const docsUrl = require('../util/docsUrl'); |
| 12 | +const report = require('../util/report'); |
| 13 | + |
| 14 | +const FORBIDDEN_TYPES_MAP = { |
| 15 | + ArrowFunctionExpression: 'arrow function', |
| 16 | + FunctionExpression: 'function expression', |
| 17 | + ObjectExpression: 'object literal', |
| 18 | + ArrayExpression: 'array literal', |
| 19 | + ClassExpression: 'class expression', |
| 20 | + NewExpression: 'construction expression', |
| 21 | + JSXElement: 'JSX element', |
| 22 | +}; |
| 23 | + |
| 24 | +const FORBIDDEN_TYPES = new Set(Object.keys(FORBIDDEN_TYPES_MAP)); |
| 25 | +const MESSAGE_ID = 'forbiddenTypeDefaultParam'; |
| 26 | + |
| 27 | +const messages = { |
| 28 | + [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}}.', |
| 29 | +}; |
| 30 | +function hasUsedObjectDestructuringSyntax(params) { |
| 31 | + return ( |
| 32 | + params != null |
| 33 | + && params.length === 1 |
| 34 | + && params[0].type === 'ObjectPattern' |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function verifyDefaultPropsDestructuring(context, properties) { |
| 39 | + // Loop through each of the default params |
| 40 | + properties.filter((prop) => prop.type === 'Property').forEach((prop) => { |
| 41 | + const propName = prop.key.name; |
| 42 | + const propDefaultValue = prop.value; |
| 43 | + |
| 44 | + if (propDefaultValue.type !== 'AssignmentPattern') { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const propDefaultValueType = propDefaultValue.right.type; |
| 49 | + |
| 50 | + if ( |
| 51 | + propDefaultValueType === 'Literal' |
| 52 | + && propDefaultValue.right.regex != null |
| 53 | + ) { |
| 54 | + report(context, messages[MESSAGE_ID], MESSAGE_ID, { |
| 55 | + node: propDefaultValue, |
| 56 | + data: { |
| 57 | + propName, |
| 58 | + forbiddenType: 'regex literal', |
| 59 | + }, |
| 60 | + }); |
| 61 | + } else if ( |
| 62 | + propDefaultValueType === 'CallExpression' |
| 63 | + && propDefaultValue.right.callee.type === 'Identifier' |
| 64 | + && propDefaultValue.right.callee.name === 'Symbol' |
| 65 | + ) { |
| 66 | + report(context, messages[MESSAGE_ID], MESSAGE_ID, { |
| 67 | + node: propDefaultValue, |
| 68 | + data: { |
| 69 | + propName, |
| 70 | + forbiddenType: 'Symbol literal', |
| 71 | + }, |
| 72 | + }); |
| 73 | + } else if (FORBIDDEN_TYPES.has(propDefaultValueType)) { |
| 74 | + report(context, messages[MESSAGE_ID], MESSAGE_ID, { |
| 75 | + node: propDefaultValue, |
| 76 | + data: { |
| 77 | + propName, |
| 78 | + forbiddenType: FORBIDDEN_TYPES_MAP[propDefaultValueType], |
| 79 | + }, |
| 80 | + }); |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +module.exports = { |
| 86 | + meta: { |
| 87 | + docs: { |
| 88 | + description: 'Disallow usage of referential-type variables as default param in functional component', |
| 89 | + category: 'Best Practices', |
| 90 | + recommended: false, |
| 91 | + url: docsUrl('no-object-type-as-default-prop'), |
| 92 | + }, |
| 93 | + messages, |
| 94 | + }, |
| 95 | + create: Components.detect((context, components) => ({ |
| 96 | + 'Program:exit'() { |
| 97 | + const list = components.list(); |
| 98 | + values(list).forEach((component) => { |
| 99 | + const node = component.node; |
| 100 | + if (!hasUsedObjectDestructuringSyntax(node.params)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + const properties = node.params[0].properties; |
| 104 | + verifyDefaultPropsDestructuring(context, properties); |
| 105 | + }); |
| 106 | + }, |
| 107 | + })), |
| 108 | +}; |
0 commit comments