Skip to content

Commit

Permalink
[Refactor] sort-prop-types: hoist some functions to module level
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 17, 2023
1 parent cab612f commit dd6e05c
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dd6e05c

Please sign in to comment.