forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-property-placement.js
165 lines (143 loc) · 5.78 KB
/
static-property-placement.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
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
/**
* @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 propsUtil = require('../util/props');
// ------------------------------------------------------------------------------
// 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]: '\'{{name}}\' should be declared as a static class property.',
[STATIC_GETTER]: '\'{{name}}\' should be declared as a static getter class function.',
[PROPERTY_ASSIGNMENT]: '\'{{name}}\' should be declared outside the class body.'
};
// ------------------------------------------------------------------------------
// 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
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Defines where React component static properties should be positioned.',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('static-property-placement')
},
fixable: null, // or 'code' or 'whitespace'
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) {
// Report the error
context.report({
node,
message: ERROR_MESSAGES[config[name]],
data: {name}
});
}
}
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return {
ClassProperty: node => 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 || !utils.isES6Component(relatedComponent.node)) {
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 (isContextInClass() && node.static && node.kind === 'get') {
// Report error if needed
reportNodeIncorrectlyPositioned(node, STATIC_GETTER);
}
}
};
})
};