From dd6e05cce071093c12b74e741dec81f55570134c Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 16 Aug 2023 22:27:01 -0700 Subject: [PATCH] [Refactor] `sort-prop-types`: hoist some functions to module level --- lib/rules/sort-prop-types.js | 68 ++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/rules/sort-prop-types.js b/lib/rules/sort-prop-types.js index a5b96c35bc..4a79aa5407 100644 --- a/lib/rules/sort-prop-types.js +++ b/lib/rules/sort-prop-types.js @@ -21,6 +21,38 @@ const messages = { propsNotSorted: 'Prop types declarations should be sorted alphabetically', }; +function getKey(context, node) { + if (node.type === 'ObjectTypeProperty') { + return context.getSourceCode().getFirstToken(node).value; + } + if (node.key && node.key.value) { + return node.key.value; + } + return context.getSourceCode().getText(node.key || node.argument); +} + +function getValueName(node) { + return node.type === 'Property' && node.value.property && node.value.property.name; +} + +function isCallbackPropName(propName) { + return /^on[A-Z]/.test(propName); +} + +function isRequiredProp(node) { + return getValueName(node) === 'isRequired'; +} + +function isShapeProp(node) { + return Boolean( + node && node.callee && node.callee.property && node.callee.property.name === 'shape' + ); +} + +function toLowerCase(item) { + return String(item).toLowerCase(); +} + module.exports = { meta: { docs: { @@ -71,38 +103,6 @@ module.exports = { const typeAnnotations = new Map(); - function getKey(node) { - if (node.type === 'ObjectTypeProperty') { - return context.getSourceCode().getFirstToken(node).value; - } - if (node.key && node.key.value) { - return node.key.value; - } - return context.getSourceCode().getText(node.key || node.argument); - } - - function getValueName(node) { - return node.type === 'Property' && node.value.property && node.value.property.name; - } - - function isCallbackPropName(propName) { - return /^on[A-Z]/.test(propName); - } - - function isRequiredProp(node) { - return getValueName(node) === 'isRequired'; - } - - function isShapeProp(node) { - return Boolean( - node && node.callee && node.callee.property && node.callee.property.name === 'shape' - ); - } - - function toLowerCase(item) { - return String(item).toLowerCase(); - } - /** * Checks if propTypes declarations are sorted * @param {Array} declarations The array of AST nodes being checked. @@ -137,8 +137,8 @@ module.exports = { return decls[idx + 1]; } - let prevPropName = getKey(prev); - let currentPropName = getKey(curr); + let prevPropName = getKey(context, prev); + let currentPropName = getKey(context, curr); const previousIsRequired = isRequiredProp(prev); const currentIsRequired = isRequiredProp(curr); const previousIsCallback = isCallbackPropName(prevPropName);