Skip to content

Commit a9f5fab

Browse files
committed
Move getComponentProperties to a util function
1 parent 1b83214 commit a9f5fab

File tree

5 files changed

+24
-70
lines changed

5 files changed

+24
-70
lines changed

lib/rules/no-redundant-should-component-update.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,13 @@ module.exports = {
2525
},
2626

2727
create: Components.detect((context, components, utils) => {
28-
/**
29-
* Get properties for a given AST node
30-
* @param {ASTNode} node The AST node being checked.
31-
* @returns {Array} Properties array.
32-
*/
33-
function getComponentProperties(node) {
34-
switch (node.type) {
35-
case 'ClassExpression':
36-
case 'ClassDeclaration':
37-
return node.body.body;
38-
default:
39-
return [];
40-
}
41-
}
42-
4328
/**
4429
* Checks for shouldComponentUpdate property
4530
* @param {ASTNode} node The AST node being checked.
4631
* @returns {Boolean} Whether or not the property exists.
4732
*/
4833
function hasShouldComponentUpdate(node) {
49-
const properties = getComponentProperties(node);
34+
const properties = astUtil.getComponentProperties(node);
5035
return properties.some(property => {
5136
const name = astUtil.getPropertyName(property);
5237
return name === 'shouldComponentUpdate';

lib/rules/prefer-stateless-function.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,6 @@ module.exports = {
4444
// Public
4545
// --------------------------------------------------------------------------
4646

47-
/**
48-
* Get properties for a given AST node
49-
* @param {ASTNode} node The AST node being checked.
50-
* @returns {Array} Properties array.
51-
*/
52-
function getComponentProperties(node) {
53-
switch (node.type) {
54-
case 'ClassExpression':
55-
case 'ClassDeclaration':
56-
return node.body.body;
57-
case 'ObjectExpression':
58-
return node.properties;
59-
default:
60-
return [];
61-
}
62-
}
63-
6447
/**
6548
* Checks whether a given array of statements is a single call of `super`.
6649
* @see ESLint no-useless-constructor rule
@@ -195,7 +178,7 @@ module.exports = {
195178
* @returns {Boolean} True if the node has at least one other property, false if not.
196179
*/
197180
function hasOtherProperties(node) {
198-
const properties = getComponentProperties(node);
181+
const properties = astUtil.getComponentProperties(node);
199182
return properties.some(property => {
200183
const name = astUtil.getPropertyName(property);
201184
const isDisplayName = name === 'displayName';

lib/rules/require-render-return.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,13 @@ module.exports = {
3333
});
3434
}
3535

36-
/**
37-
* Get properties for a given AST node
38-
* @param {ASTNode} node The AST node being checked.
39-
* @returns {Array} Properties array.
40-
*/
41-
function getComponentProperties(node) {
42-
switch (node.type) {
43-
case 'ClassDeclaration':
44-
return node.body.body;
45-
case 'ObjectExpression':
46-
return node.properties;
47-
default:
48-
return [];
49-
}
50-
}
51-
5236
/**
5337
* Check if a given AST node has a render method
5438
* @param {ASTNode} node The AST node being checked.
5539
* @returns {Boolean} True if there is a render method, false if not
5640
*/
5741
function hasRenderMethod(node) {
58-
const properties = getComponentProperties(node);
42+
const properties = astUtil.getComponentProperties(node);
5943
for (let i = 0, j = properties.length; i < j; i++) {
6044
if (astUtil.getPropertyName(properties[i]) !== 'render' || !properties[i].value) {
6145
continue;

lib/rules/sort-comp.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,23 +295,6 @@ module.exports = {
295295
}
296296
}
297297

298-
/**
299-
* Get properties for a given AST node
300-
* @param {ASTNode} node The AST node being checked.
301-
* @returns {Array} Properties array.
302-
*/
303-
function getComponentProperties(node) {
304-
switch (node.type) {
305-
case 'ClassExpression':
306-
case 'ClassDeclaration':
307-
return node.body.body;
308-
case 'ObjectExpression':
309-
return node.properties.filter(property => property.type === 'Property');
310-
default:
311-
return [];
312-
}
313-
}
314-
315298
/**
316299
* Compare two properties and find out if they are in the right order
317300
* @param {Array} propertiesInfos Array containing all the properties metadata.
@@ -424,7 +407,7 @@ module.exports = {
424407
if (!has(list, component)) {
425408
continue;
426409
}
427-
const properties = getComponentProperties(list[component].node);
410+
const properties = astUtil.getComponentProperties(list[component].node);
428411
checkPropsOrder(properties);
429412
}
430413

lib/util/ast.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ function getPropertyName(node) {
1717
return '';
1818
}
1919

20+
/**
21+
* Get properties for a given AST node
22+
* @param {ASTNode} node The AST node being checked.
23+
* @returns {Array} Properties array.
24+
*/
25+
function getComponentProperties(node) {
26+
switch (node.type) {
27+
case 'ClassDeclaration':
28+
case 'ClassExpression':
29+
return node.body.body;
30+
case 'ObjectExpression':
31+
// return node.properties;
32+
return node.properties;
33+
default:
34+
return [];
35+
}
36+
}
37+
2038
module.exports = {
21-
getPropertyName: getPropertyName
39+
getPropertyName: getPropertyName,
40+
getComponentProperties: getComponentProperties
2241
};

0 commit comments

Comments
 (0)