Repository: yannickcr/eslint-plugin-react
Pull request #47 (2015-03-31 01:10:32)

Reset alpha verification on spread attr


Patch:
diff --git a/docs/rules/jsx-sort-props.md b/docs/rules/jsx-sort-props.md
index 660a0616..3e027906 100755
--- a/docs/rules/jsx-sort-props.md
+++ b/docs/rules/jsx-sort-props.md
@@ -4,7 +4,7 @@ Some developers prefer to sort props names alphabetically to be able to find nec
 
 ## Rule Details
 
-This rule checks all JSX components and verifies that all props are sorted alphabetically. This rule is off by default.
+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.
 The default configuration of the rule is case-sensitive.
 
 The following patterns are considered warnings:
@@ -17,6 +17,7 @@ The following patterns are considered okay and do not cause warnings:
 
 ```js
 <Hello firstName="John" lastName="Smith" />;
+<Hello tel={5555555} {...this.props} firstName="John" lastName="Smith" />;
 ```
 
 ## Rule Options
diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js
index 408321b6..bcf1b68d 100755
--- a/lib/rules/jsx-sort-props.js
+++ b/lib/rules/jsx-sort-props.js
@@ -15,11 +15,11 @@ module.exports = function(context) {
 
   return {
     JSXOpeningElement: function(node) {
-      var attributes = node.attributes.filter(function(decl) {
-        return decl.type === 'JSXAttribute';
-      });
+      node.attributes.reduce(function(memo, decl, idx, attrs) {
+        if (decl.type === 'JSXSpreadAttribute') {
+          return attrs[idx + 1];
+        }
 
-      attributes.reduce(function(memo, decl) {
         var lastPropName = memo.name.name;
         var currenPropName = decl.name.name;
 
@@ -34,7 +34,7 @@ module.exports = function(context) {
         }
 
         return decl;
-      }, attributes[0]);
+      }, node.attributes[0]);
     }
   };
 };


Test patch:
diff --git a/tests/lib/rules/jsx-sort-props.js b/tests/lib/rules/jsx-sort-props.js
index bcc30ed3..abc54d2b 100755
--- a/tests/lib/rules/jsx-sort-props.js
+++ b/tests/lib/rules/jsx-sort-props.js
@@ -35,8 +35,10 @@ eslintTester.addRuleTest('lib/rules/jsx-sort-props', {
     {code: '<App {...this.props} />;', ecmaFeatures: features},
     {code: '<App a b c />;', ecmaFeatures: features},
     {code: '<App {...this.props} a b c />;', ecmaFeatures: features},
+    {code: '<App c {...this.props} a b />;', ecmaFeatures: features},
     {code: '<App a="c" b="b" c="a" />;', ecmaFeatures: features},
     {code: '<App {...this.props} a="c" b="b" c="a" />;', ecmaFeatures: features},
+    {code: '<App c="a" {...this.props} a="c" b="b" />;', ecmaFeatures: features},
     {code: '<App A a />;', ecmaFeatures: features},
     {code: '<App a A />;', args: ignoreCaseArgs, ecmaFeatures: features},
     {code: '<App a B c />;', args: ignoreCaseArgs, ecmaFeatures: features},
@@ -45,10 +47,12 @@ eslintTester.addRuleTest('lib/rules/jsx-sort-props', {
   invalid: [
     {code: '<App b a />;', errors: [expectedError], ecmaFeatures: features},
     {code: '<App {...this.props} b a />;', errors: [expectedError], ecmaFeatures: features},
+    {code: '<App c {...this.props} b a />;', errors: [expectedError], ecmaFeatures: features},
     {code: '<App a A />;', errors: [expectedError], ecmaFeatures: features},
     {code: '<App B a />;', args: ignoreCaseArgs, errors: [expectedError], ecmaFeatures: features},
     {code: '<App B A c />;', args: ignoreCaseArgs, errors: [expectedError], ecmaFeatures: features},
     {code: '<App c="a" a="c" b="b" />;', errors: 2, ecmaFeatures: features},
-    {code: '<App {...this.props} c="a" a="c" b="b" />;', errors: 2, ecmaFeatures: features}
+    {code: '<App {...this.props} c="a" a="c" b="b" />;', errors: 2, ecmaFeatures: features},
+    {code: '<App d="d" b="b" {...this.props} c="a" a="c" />;', errors: 2, ecmaFeatures: features}
   ]
 });