bcf1b68d763541db135ceff69e604a80b9eb89aa
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Enforce props alphabetical sorting
3  * @author Ilya Volodin, Yannick Croissant
4  */
5 'use strict';
6
7 // ------------------------------------------------------------------------------
8 // Rule Definition
9 // ------------------------------------------------------------------------------
10
11 module.exports = function(context) {
12
13   var configuration = context.options[0] || {};
14   var ignoreCase = configuration.ignoreCase || false;
15
16   return {
17     JSXOpeningElement: function(node) {
18       node.attributes.reduce(function(memo, decl, idx, attrs) {
19         if (decl.type === 'JSXSpreadAttribute') {
20           return attrs[idx + 1];
21         }
22
23         var lastPropName = memo.name.name;
24         var currenPropName = decl.name.name;
25
26         if (ignoreCase) {
27           lastPropName = lastPropName.toLowerCase();
28           currenPropName = currenPropName.toLowerCase();
29         }
30
31         if (currenPropName < lastPropName) {
32           context.report(decl, 'Props should be sorted alphabetically');
33           return memo;
34         }
35
36         return decl;
37       }, node.attributes[0]);
38     }
39   };
40 };