Skip to content

Commit

Permalink
Move duplicated getPropertyName to util function
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti committed Nov 9, 2017
1 parent 4e21d41 commit 1b83214
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 82 deletions.
23 changes: 5 additions & 18 deletions lib/rules/default-props-match-prop-types.js
Expand Up @@ -9,6 +9,7 @@ const has = require('has');
const Components = require('../util/Components');
const variableUtil = require('../util/variable');
const annotations = require('../util/annotations');
const astUtil = require('../util/ast');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -38,27 +39,13 @@ module.exports = {
const allowRequiredDefaults = configuration.allowRequiredDefaults || false;
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);

/**
* Get properties name
* @param {Object} node - Property.
* @returns {String} Property name.
*/
function getPropertyName(node) {
if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) {
return node.key.name;
} else if (node.type === 'MemberExpression') {
return node.property.name;
}
return '';
}

/**
* Checks if the Identifier node passed in looks like a propTypes declaration.
* @param {ASTNode} node The node to check. Must be an Identifier node.
* @returns {Boolean} `true` if the node is a propTypes declaration, `false` if not
*/
function isPropTypesDeclaration(node) {
return getPropertyName(node) === 'propTypes';
return astUtil.getPropertyName(node) === 'propTypes';
}

/**
Expand All @@ -67,7 +54,7 @@ module.exports = {
* @returns {Boolean} `true` if the node is a defaultProps declaration, `false` if not
*/
function isDefaultPropsDeclaration(node) {
const propName = getPropertyName(node);
const propName = astUtil.getPropertyName(node);
return (propName === 'defaultProps' || propName === 'getDefaultProps');
}

Expand Down Expand Up @@ -336,7 +323,7 @@ module.exports = {
}

function isPropTypeAnnotation(node) {
return (getPropertyName(node) === 'props' && !!node.typeAnnotation);
return (astUtil.getPropertyName(node) === 'props' && !!node.typeAnnotation);
}

function propFromName(propTypes, name) {
Expand Down Expand Up @@ -528,7 +515,7 @@ module.exports = {
return;
}

const propName = getPropertyName(node);
const propName = astUtil.getPropertyName(node);
const isPropType = propName === 'propTypes';
const isDefaultProp = propName === 'defaultProps' || propName === 'getDefaultProps';

Expand Down
15 changes: 2 additions & 13 deletions lib/rules/no-redundant-should-component-update.js
Expand Up @@ -4,6 +4,7 @@
'use strict';

const Components = require('../util/Components');
const astUtil = require('../util/ast');

function errorMessage(node) {
return `${node} does not need shouldComponentUpdate when extending React.PureComponent.`;
Expand All @@ -24,18 +25,6 @@ module.exports = {
},

create: Components.detect((context, components, utils) => {
/**
* Get properties name
* @param {Object} node - Property.
* @returns {String} Property name.
*/
function getPropertyName(node) {
if (node.key) {
return node.key.name;
}
return '';
}

/**
* Get properties for a given AST node
* @param {ASTNode} node The AST node being checked.
Expand All @@ -59,7 +48,7 @@ module.exports = {
function hasShouldComponentUpdate(node) {
const properties = getComponentProperties(node);
return properties.some(property => {
const name = getPropertyName(property);
const name = astUtil.getPropertyName(property);
return name === 'shouldComponentUpdate';
});
}
Expand Down
14 changes: 3 additions & 11 deletions lib/rules/prefer-stateless-function.js
Expand Up @@ -9,6 +9,7 @@
const has = require('has');
const Components = require('../util/Components');
const versionUtil = require('../util/version');
const astUtil = require('../util/ast');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -43,15 +44,6 @@ module.exports = {
// Public
// --------------------------------------------------------------------------

/**
* Get properties name
* @param {Object} node - Property.
* @returns {String} Property name.
*/
function getPropertyName(node) {
return node.key.name;
}

/**
* Get properties for a given AST node
* @param {ASTNode} node The AST node being checked.
Expand Down Expand Up @@ -205,7 +197,7 @@ module.exports = {
function hasOtherProperties(node) {
const properties = getComponentProperties(node);
return properties.some(property => {
const name = getPropertyName(property);
const name = astUtil.getPropertyName(property);
const isDisplayName = name === 'displayName';
const isPropTypes = name === 'propTypes' || name === 'props' && property.typeAnnotation;
const contextTypes = name === 'contextTypes';
Expand Down Expand Up @@ -311,7 +303,7 @@ module.exports = {
}
// Ignore `props` and `context`
const useThis = node.id.properties.some(property => {
const name = getPropertyName(property);
const name = astUtil.getPropertyName(property);
return name !== 'props' && name !== 'context';
});
if (!useThis) {
Expand Down
25 changes: 6 additions & 19 deletions lib/rules/require-default-props.js
Expand Up @@ -8,6 +8,7 @@ const has = require('has');
const Components = require('../util/Components');
const variableUtil = require('../util/variable');
const annotations = require('../util/annotations');
const astUtil = require('../util/ast');

const QUOTES_REGEX = /^["']|["']$/g;

Expand All @@ -29,27 +30,13 @@ module.exports = {
const sourceCode = context.getSourceCode();
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);

/**
* Get properties name
* @param {Object} node - Property.
* @returns {String} Property name.
*/
function getPropertyName(node) {
if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) {
return node.key.name;
} else if (node.type === 'MemberExpression') {
return node.property.name;
}
return '';
}

/**
* Checks if the Identifier node passed in looks like a propTypes declaration.
* @param {ASTNode} node The node to check. Must be an Identifier node.
* @returns {Boolean} `true` if the node is a propTypes declaration, `false` if not
*/
function isPropTypesDeclaration(node) {
return getPropertyName(node) === 'propTypes';
return astUtil.getPropertyName(node) === 'propTypes';
}

/**
Expand All @@ -58,7 +45,7 @@ module.exports = {
* @returns {Boolean} `true` if the node is a defaultProps declaration, `false` if not
*/
function isDefaultPropsDeclaration(node) {
return (getPropertyName(node) === 'defaultProps' || getPropertyName(node) === 'getDefaultProps');
return (astUtil.getPropertyName(node) === 'defaultProps' || astUtil.getPropertyName(node) === 'getDefaultProps');
}

/**
Expand Down Expand Up @@ -306,7 +293,7 @@ module.exports = {
}

function isPropTypeAnnotation(node) {
return (getPropertyName(node) === 'props' && !!node.typeAnnotation);
return (astUtil.getPropertyName(node) === 'props' && !!node.typeAnnotation);
}

/**
Expand Down Expand Up @@ -483,8 +470,8 @@ module.exports = {
return;
}

const isPropType = getPropertyName(node) === 'propTypes';
const isDefaultProp = getPropertyName(node) === 'defaultProps' || getPropertyName(node) === 'getDefaultProps';
const isPropType = astUtil.getPropertyName(node) === 'propTypes';
const isDefaultProp = astUtil.getPropertyName(node) === 'defaultProps' || astUtil.getPropertyName(node) === 'getDefaultProps';

if (!isPropType && !isDefaultProp) {
return;
Expand Down
24 changes: 4 additions & 20 deletions lib/rules/require-render-return.js
Expand Up @@ -6,6 +6,7 @@

const has = require('has');
const Components = require('../util/Components');
const astUtil = require('../util/ast');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -48,23 +49,6 @@ module.exports = {
}
}

/**
* Get properties name
* @param {Object} node - Property.
* @returns {String} Property name.
*/
function getPropertyName(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
const tokens = context.getFirstTokens(node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
} else if (['MethodDefinition', 'Property'].indexOf(node.type) !== -1) {
return node.key.name;
}
return '';
}

/**
* Check if a given AST node has a render method
* @param {ASTNode} node The AST node being checked.
Expand All @@ -73,7 +57,7 @@ module.exports = {
function hasRenderMethod(node) {
const properties = getComponentProperties(node);
for (let i = 0, j = properties.length; i < j; i++) {
if (getPropertyName(properties[i]) !== 'render' || !properties[i].value) {
if (astUtil.getPropertyName(properties[i]) !== 'render' || !properties[i].value) {
continue;
}
return /FunctionExpression$/.test(properties[i].value.type);
Expand All @@ -91,7 +75,7 @@ module.exports = {
}
if (
!/(MethodDefinition|(Class)?Property)$/.test(ancestors[i].type) ||
getPropertyName(ancestors[i]) !== 'render' ||
astUtil.getPropertyName(ancestors[i]) !== 'render' ||
depth > 1
) {
continue;
Expand All @@ -101,7 +85,7 @@ module.exports = {
},

ArrowFunctionExpression: function(node) {
if (node.expression === false || getPropertyName(node.parent) !== 'render') {
if (node.expression === false || astUtil.getPropertyName(node.parent) !== 'render') {
return;
}
markReturnStatementPresent(node);
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/sort-comp.js
Expand Up @@ -8,6 +8,7 @@ const has = require('has');
const util = require('util');

const Components = require('../util/Components');
const astUtil = require('../util/ast');

/**
* Get the methods order from the default config and the user config
Expand Down Expand Up @@ -203,7 +204,7 @@ module.exports = {
return 'setter functions';
}

return node.key.name;
return astUtil.getPropertyName(node);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions lib/util/ast.js
@@ -0,0 +1,22 @@
/**
* @fileoverview Utility functions for AST
*/
'use strict';

/**
* Get properties name
* @param {Object} node - Property.
* @returns {String} Property name.
*/
function getPropertyName(node) {
if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) {
return node.key.name;
} else if (node.type === 'MemberExpression') {
return node.property.name;
}
return '';
}

module.exports = {
getPropertyName: getPropertyName
};

0 comments on commit 1b83214

Please sign in to comment.