-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Make jsx-sort-default-props fixable #2429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ljharb
merged 1 commit into
jsx-eslint:master
from
emroussel:fixable-jsx-sort-default-props
Oct 4, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/** | ||
* @fileoverview Common propTypes sorting functionality. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const astUtil = require('./ast'); | ||
|
||
/** | ||
* Returns the value name of a node. | ||
* | ||
* @param {ASTNode} node the node to check. | ||
* @returns {String} The name of the node. | ||
*/ | ||
function getValueName(node) { | ||
return node.type === 'Property' && node.value.property && node.value.property.name; | ||
} | ||
|
||
/** | ||
* Checks if the prop is required or not. | ||
* | ||
* @param {ASTNode} node the prop to check. | ||
* @returns {Boolean} true if the prop is required. | ||
*/ | ||
function isRequiredProp(node) { | ||
return getValueName(node) === 'isRequired'; | ||
} | ||
|
||
/** | ||
* Checks if the proptype is a callback by checking if it starts with 'on'. | ||
* | ||
* @param {String} propName the name of the proptype to check. | ||
* @returns {Boolean} true if the proptype is a callback. | ||
*/ | ||
function isCallbackPropName(propName) { | ||
return /^on[A-Z]/.test(propName); | ||
} | ||
|
||
/** | ||
* Checks if the prop is PropTypes.shape. | ||
* | ||
* @param {ASTNode} node the prop to check. | ||
* @returns {Boolean} true if the prop is PropTypes.shape. | ||
*/ | ||
function isShapeProp(node) { | ||
return Boolean( | ||
node && node.callee && node.callee.property && node.callee.property.name === 'shape' | ||
); | ||
} | ||
|
||
/** | ||
* Returns the properties of a PropTypes.shape. | ||
* | ||
* @param {ASTNode} node the prop to check. | ||
* @returns {Array} the properties of the PropTypes.shape node. | ||
*/ | ||
function getShapeProperties(node) { | ||
return node.arguments && node.arguments[0] && node.arguments[0].properties; | ||
} | ||
|
||
/** | ||
* Compares two elements. | ||
* | ||
* @param {ASTNode} a the first element to compare. | ||
* @param {ASTNode} b the second element to compare. | ||
* @param {Context} context The context of the two nodes. | ||
* @param {Boolean=} ignoreCase whether or not to ignore case when comparing the two elements. | ||
* @param {Boolean=} requiredFirst whether or not to sort required elements first. | ||
* @param {Boolean=} callbacksLast whether or not to sort callbacks after everyting else. | ||
* @returns {Number} the sort order of the two elements. | ||
*/ | ||
function sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast) { | ||
const aKey = String(astUtil.getKeyValue(context, a)); | ||
const bKey = String(astUtil.getKeyValue(context, b)); | ||
|
||
if (requiredFirst) { | ||
if (isRequiredProp(a) && !isRequiredProp(b)) { | ||
return -1; | ||
} | ||
if (!isRequiredProp(a) && isRequiredProp(b)) { | ||
return 1; | ||
} | ||
} | ||
|
||
if (callbacksLast) { | ||
if (isCallbackPropName(aKey) && !isCallbackPropName(bKey)) { | ||
return 1; | ||
} | ||
if (!isCallbackPropName(aKey) && isCallbackPropName(bKey)) { | ||
return -1; | ||
} | ||
} | ||
|
||
if (ignoreCase) { | ||
return aKey.localeCompare(bKey); | ||
} | ||
|
||
if (aKey < bKey) { | ||
return -1; | ||
} | ||
if (aKey > bKey) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
/** | ||
* Fixes sort order of prop types. | ||
* | ||
* @param {Fixer} fixer the first element to compare. | ||
* @param {Object} context the second element to compare. | ||
* @param {Array} declarations The context of the two nodes. | ||
* @param {Boolean=} ignoreCase whether or not to ignore case when comparing the two elements. | ||
* @param {Boolean=} requiredFirst whether or not to sort required elements first. | ||
* @param {Boolean=} callbacksLast whether or not to sort callbacks after everyting else. | ||
* @param {Boolean=} sortShapeProp whether or not to sort propTypes defined in PropTypes.shape. | ||
* @returns {Object|*|{range, text}} the sort order of the two elements. | ||
*/ | ||
function fixPropTypesSort(fixer, context, declarations, ignoreCase, requiredFirst, callbacksLast, sortShapeProp) { | ||
function sortInSource(allNodes, source) { | ||
const originalSource = source; | ||
const nodeGroups = allNodes.reduce((acc, curr) => { | ||
if (curr.type === 'ExperimentalSpreadProperty' || curr.type === 'SpreadElement') { | ||
acc.push([]); | ||
} else { | ||
acc[acc.length - 1].push(curr); | ||
} | ||
return acc; | ||
}, [[]]); | ||
|
||
nodeGroups.forEach((nodes) => { | ||
const sortedAttributes = nodes | ||
.slice() | ||
.sort((a, b) => sorter(a, b, context, ignoreCase, requiredFirst, callbacksLast)); | ||
|
||
source = nodes.reduceRight((acc, attr, index) => { | ||
const sortedAttr = sortedAttributes[index]; | ||
let sortedAttrText = context.getSourceCode().getText(sortedAttr); | ||
if (sortShapeProp && isShapeProp(sortedAttr.value)) { | ||
const shape = getShapeProperties(sortedAttr.value); | ||
if (shape) { | ||
const attrSource = sortInSource( | ||
shape, | ||
originalSource | ||
); | ||
sortedAttrText = attrSource.slice(sortedAttr.range[0], sortedAttr.range[1]); | ||
} | ||
} | ||
return `${acc.slice(0, attr.range[0])}${sortedAttrText}${acc.slice(attr.range[1])}`; | ||
}, source); | ||
}); | ||
return source; | ||
} | ||
|
||
const source = sortInSource(declarations, context.getSourceCode().getText()); | ||
|
||
const rangeStart = declarations[0].range[0]; | ||
const rangeEnd = declarations[declarations.length - 1].range[1]; | ||
return fixer.replaceTextRange([rangeStart, rangeEnd], source.slice(rangeStart, rangeEnd)); | ||
} | ||
|
||
module.exports = { | ||
fixPropTypesSort | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.