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