static-property-placement.js
6.38 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @fileoverview Defines where React component static properties should be positioned.
* @author Daniel Mason
*/
'use strict';
const fromEntries = require('object.fromentries');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const propsUtil = require('../util/props');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Positioning Options
// ------------------------------------------------------------------------------
const STATIC_PUBLIC_FIELD = 'static public field';
const STATIC_GETTER = 'static getter';
const PROPERTY_ASSIGNMENT = 'property assignment';
const POSITION_SETTINGS = [STATIC_PUBLIC_FIELD, STATIC_GETTER, PROPERTY_ASSIGNMENT];
// ------------------------------------------------------------------------------
// Rule messages
// ------------------------------------------------------------------------------
const ERROR_MESSAGES = {
[STATIC_PUBLIC_FIELD]: 'notStaticClassProp',
[STATIC_GETTER]: 'notGetterClassFunc',
[PROPERTY_ASSIGNMENT]: 'declareOutsideClass',
};
// ------------------------------------------------------------------------------
// Properties to check
// ------------------------------------------------------------------------------
const propertiesToCheck = {
propTypes: propsUtil.isPropTypesDeclaration,
defaultProps: propsUtil.isDefaultPropsDeclaration,
childContextTypes: propsUtil.isChildContextTypesDeclaration,
contextTypes: propsUtil.isContextTypesDeclaration,
contextType: propsUtil.isContextTypeDeclaration,
displayName: (node) => propsUtil.isDisplayNameDeclaration(astUtil.getPropertyNameNode(node)),
};
const classProperties = Object.keys(propertiesToCheck);
const schemaProperties = fromEntries(classProperties.map((property) => [property, { enum: POSITION_SETTINGS }]));
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
notStaticClassProp: '\'{{name}}\' should be declared as a static class property.',
notGetterClassFunc: '\'{{name}}\' should be declared as a static getter class function.',
declareOutsideClass: '\'{{name}}\' should be declared outside the class body.',
};
module.exports = {
meta: {
docs: {
description: 'Enforces where React component static properties should be positioned.',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('static-property-placement'),
},
fixable: null, // or 'code' or 'whitespace'
messages,
schema: [
{ enum: POSITION_SETTINGS },
{
type: 'object',
properties: schemaProperties,
additionalProperties: false,
},
],
},
create: Components.detect((context, components, utils) => {
// variables should be defined here
const options = context.options;
const defaultCheckType = options[0] || STATIC_PUBLIC_FIELD;
const hasAdditionalConfig = options.length > 1;
const additionalConfig = hasAdditionalConfig ? options[1] : {};
// Set config
const config = fromEntries(classProperties.map((property) => [
property,
additionalConfig[property] || defaultCheckType,
]));
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
/**
* Checks if we are declaring context in class
* @returns {Boolean} True if we are declaring context in class, false if not.
*/
function isContextInClass() {
let blockNode;
let scope = context.getScope();
while (scope) {
blockNode = scope.block;
if (blockNode && blockNode.type === 'ClassDeclaration') {
return true;
}
scope = scope.upper;
}
return false;
}
/**
* Check if we should report this property node
* @param {ASTNode} node
* @param {string} expectedRule
*/
function reportNodeIncorrectlyPositioned(node, expectedRule) {
// Detect if this node is an expected property declaration adn return the property name
const name = classProperties.find((propertyName) => {
if (propertiesToCheck[propertyName](node)) {
return !!propertyName;
}
return false;
});
// If name is set but the configured rule does not match expected then report error
if (
name
&& (
config[name] !== expectedRule
|| (!node.static && (config[name] === STATIC_PUBLIC_FIELD || config[name] === STATIC_GETTER))
)
) {
const messageId = ERROR_MESSAGES[config[name]];
report(context, messages[messageId], messageId, {
node,
data: { name },
});
}
}
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return {
'ClassProperty, PropertyDefinition'(node) {
if (!componentUtil.getParentES6Component(context)) {
return;
}
reportNodeIncorrectlyPositioned(node, STATIC_PUBLIC_FIELD);
},
MemberExpression(node) {
// If definition type is undefined then it must not be a defining expression or if the definition is inside a
// class body then skip this node.
const right = node.parent.right;
if (!right || right.type === 'undefined' || isContextInClass()) {
return;
}
// Get the related component
const relatedComponent = utils.getRelatedComponent(node);
// If the related component is not an ES6 component then skip this node
if (!relatedComponent || !componentUtil.isES6Component(relatedComponent.node, context)) {
return;
}
// Report if needed
reportNodeIncorrectlyPositioned(node, PROPERTY_ASSIGNMENT);
},
MethodDefinition(node) {
// If the function is inside a class and is static getter then check if correctly positioned
if (componentUtil.getParentES6Component(context) && node.static && node.kind === 'get') {
// Report error if needed
reportNodeIncorrectlyPositioned(node, STATIC_GETTER);
}
},
};
}),
};