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
Add jsx-sort-props ignoreTags option #2979
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,16 @@ With `reservedFirst: ["key"]`, the following will **not** warn: | |
<Hello key={'uuid'} name="John" ref="ref" /> | ||
``` | ||
|
||
### `ignoreTags` | ||
|
||
An array of element (tag) names to ignore when checking the properties order. | ||
|
||
With `ignoreTags: ["Can"]`, the following will **not** warn: | ||
|
||
```jsx | ||
<Can i="create" a="post" /> | ||
Comment on lines
+105
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think rather than ignoring, it would be better to specify a custom ordering that applies to a specific custom component name. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it makes sense in the case of the CASL's Can component. I can also think about using this in FontAwesome components (I'd like to have the However, I'm reluctant because it will be less generic (uou don't always know all possible props that can be used with an element). Moreover, this will make the configuration much more complicated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very very rare use case; I'm not aware of anything in the entire JS ecosystem besides CASL that has a props API like this. I think it's totally fine to be not generic, since I very much doubt there will ever be a second use case (where the order is sentence-like). In the case of FontAwesome, just ignoring it wouldn't enforce your preference, it'd just remove enforcement - which is strictly less useful, even if the alternative would require more config. |
||
``` | ||
|
||
## When not to use | ||
|
||
This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing props isn't a part of your coding standards, then you can leave this rule off. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,6 +249,9 @@ module.exports = { | |
}, | ||
reservedFirst: { | ||
type: ['array', 'boolean'] | ||
}, | ||
ignoreTags: { | ||
type: 'array' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would need to ensure the items are unique, and are valid custom component names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree as for the uniqueness. Not sure about validity of component names - see my comment above. |
||
} | ||
}, | ||
additionalProperties: false | ||
|
@@ -265,9 +268,27 @@ module.exports = { | |
const reservedFirst = configuration.reservedFirst || false; | ||
const reservedFirstError = validateReservedFirstConfig(context, reservedFirst); | ||
let reservedList = Array.isArray(reservedFirst) ? reservedFirst : RESERVED_PROPS_LIST; | ||
const ignoreTags = configuration.ignoreTags || []; | ||
const getTagNameFromMemberExpression = (node) => `${node.property.parent.object.name}.${node.property.name}`; | ||
|
||
return { | ||
JSXOpeningElement(node) { | ||
if (ignoreTags.length > 0) { | ||
const jsxOpeningElement = node.name; | ||
const type = jsxOpeningElement.type; | ||
let tagName; | ||
if (type === 'JSXIdentifier') { | ||
tagName = jsxOpeningElement.name; | ||
} else if (type === 'JSXMemberExpression') { | ||
tagName = getTagNameFromMemberExpression(jsxOpeningElement); | ||
} else { | ||
tagName = undefined; | ||
} | ||
if (tagName && ignoreTags.includes(tagName)) { | ||
return; | ||
} | ||
} | ||
|
||
// `dangerouslySetInnerHTML` is only "reserved" on DOM components | ||
if (reservedFirst && !jsxUtil.isDOMComponent(node)) { | ||
reservedList = reservedList.filter((prop) => prop !== 'dangerouslySetInnerHTML'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these aren't tag names, they're custom component names, so the terminology needs tweaking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to be a custom component name? The rule is not limited to sort props on the components only. It currently sorts the attributes of "normal" HTML elements, too. If you accept the ignore option, it should be possible to whitelist normal tags, too.