Skip to content

Commit

Permalink
Add jsx-sort-props ignoreTags option
Browse files Browse the repository at this point in the history
  • Loading branch information
fracz committed Apr 30, 2021
1 parent 1185b37 commit fa43041
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/rules/jsx-sort-props.md
Expand Up @@ -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" />
```

## 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.
21 changes: 21 additions & 0 deletions lib/rules/jsx-sort-props.js
Expand Up @@ -249,6 +249,9 @@ module.exports = {
},
reservedFirst: {
type: ['array', 'boolean']
},
ignoreTags: {
type: 'array'
}
},
additionalProperties: false
Expand All @@ -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');
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/jsx-sort-props.js
Expand Up @@ -99,6 +99,9 @@ const reservedFirstAsEmptyArrayArgs = [{
const reservedFirstAsInvalidArrayArgs = [{
reservedFirst: ['notReserved']
}];
const ignoreTagsApp = [{
ignoreTags: ['App']
}];

ruleTester.run('jsx-sort-props', rule, {
valid: [
Expand Down Expand Up @@ -165,6 +168,10 @@ ruleTester.run('jsx-sort-props', rule, {
{
code: '<App key="key" c="c" b />',
options: reservedFirstWithShorthandLast
},
{
code: '<App c b a />;',
options: ignoreTagsApp
}
],
invalid: [
Expand Down Expand Up @@ -484,6 +491,10 @@ ruleTester.run('jsx-sort-props', rule, {
code: '<App key={5} />',
options: reservedFirstAsInvalidArrayArgs,
errors: [expectedInvalidReservedFirstError]
},
{
code: '<NoApp c b a />;',
options: ignoreTagsApp
}
]
});

0 comments on commit fa43041

Please sign in to comment.