Skip to content

Commit ed2c7d5

Browse files
committed
Reset alpha verification on spread attr
1 parent 8f3941c commit ed2c7d5

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

docs/rules/jsx-sort-props.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Some developers prefer to sort props names alphabetically to be able to find nec
44

55
## Rule Details
66

7-
This rule checks all JSX components and verifies that all props are sorted alphabetically. This rule is off by default.
7+
This rule checks all JSX components and verifies that all props are sorted alphabetically. A spread attribute resets the verification. This rule is off by default.
88
The default configuration of the rule is case-sensitive.
99

1010
The following patterns are considered warnings:
@@ -17,6 +17,7 @@ The following patterns are considered okay and do not cause warnings:
1717

1818
```js
1919
<Hello firstName="John" lastName="Smith" />;
20+
<Hello tel={5555555} {...this.props} firstName="John" lastName="Smith" />;
2021
```
2122

2223
## Rule Options

lib/rules/jsx-sort-props.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ module.exports = function(context) {
1515

1616
return {
1717
JSXOpeningElement: function(node) {
18-
var attributes = node.attributes.filter(function(decl) {
19-
return decl.type === 'JSXAttribute';
20-
});
18+
node.attributes.reduce(function(memo, decl, idx, attrs) {
19+
if (decl.type === 'JSXSpreadAttribute') {
20+
return attrs[idx + 1];
21+
}
2122

22-
attributes.reduce(function(memo, decl) {
2323
var lastPropName = memo.name.name;
2424
var currenPropName = decl.name.name;
2525

@@ -34,7 +34,7 @@ module.exports = function(context) {
3434
}
3535

3636
return decl;
37-
}, attributes[0]);
37+
}, node.attributes[0]);
3838
}
3939
};
4040
};

tests/lib/rules/jsx-sort-props.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ eslintTester.addRuleTest('lib/rules/jsx-sort-props', {
3535
{code: '<App {...this.props} />;', ecmaFeatures: features},
3636
{code: '<App a b c />;', ecmaFeatures: features},
3737
{code: '<App {...this.props} a b c />;', ecmaFeatures: features},
38+
{code: '<App c {...this.props} a b />;', ecmaFeatures: features},
3839
{code: '<App a="c" b="b" c="a" />;', ecmaFeatures: features},
3940
{code: '<App {...this.props} a="c" b="b" c="a" />;', ecmaFeatures: features},
41+
{code: '<App c="a" {...this.props} a="c" b="b" />;', ecmaFeatures: features},
4042
{code: '<App A a />;', ecmaFeatures: features},
4143
{code: '<App a A />;', args: ignoreCaseArgs, ecmaFeatures: features},
4244
{code: '<App a B c />;', args: ignoreCaseArgs, ecmaFeatures: features},
@@ -45,10 +47,12 @@ eslintTester.addRuleTest('lib/rules/jsx-sort-props', {
4547
invalid: [
4648
{code: '<App b a />;', errors: [expectedError], ecmaFeatures: features},
4749
{code: '<App {...this.props} b a />;', errors: [expectedError], ecmaFeatures: features},
50+
{code: '<App c {...this.props} b a />;', errors: [expectedError], ecmaFeatures: features},
4851
{code: '<App a A />;', errors: [expectedError], ecmaFeatures: features},
4952
{code: '<App B a />;', args: ignoreCaseArgs, errors: [expectedError], ecmaFeatures: features},
5053
{code: '<App B A c />;', args: ignoreCaseArgs, errors: [expectedError], ecmaFeatures: features},
5154
{code: '<App c="a" a="c" b="b" />;', errors: 2, ecmaFeatures: features},
52-
{code: '<App {...this.props} c="a" a="c" b="b" />;', errors: 2, ecmaFeatures: features}
55+
{code: '<App {...this.props} c="a" a="c" b="b" />;', errors: 2, ecmaFeatures: features},
56+
{code: '<App d="d" b="b" {...this.props} c="a" a="c" />;', errors: 2, ecmaFeatures: features}
5357
]
5458
});

0 commit comments

Comments
 (0)