sort-keys.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @fileoverview Rule to require object keys to be sorted
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils"),
  10. naturalCompare = require("natural-compare");
  11. //------------------------------------------------------------------------------
  12. // Helpers
  13. //------------------------------------------------------------------------------
  14. /**
  15. * Gets the property name of the given `Property` node.
  16. *
  17. * - If the property's key is an `Identifier` node, this returns the key's name
  18. * whether it's a computed property or not.
  19. * - If the property has a static name, this returns the static name.
  20. * - Otherwise, this returns null.
  21. * @param {ASTNode} node The `Property` node to get.
  22. * @returns {string|null} The property name or null.
  23. * @private
  24. */
  25. function getPropertyName(node) {
  26. const staticName = astUtils.getStaticPropertyName(node);
  27. if (staticName !== null) {
  28. return staticName;
  29. }
  30. return node.key.name || null;
  31. }
  32. /**
  33. * Functions which check that the given 2 names are in specific order.
  34. *
  35. * Postfix `I` is meant insensitive.
  36. * Postfix `N` is meant natual.
  37. * @private
  38. */
  39. const isValidOrders = {
  40. asc(a, b) {
  41. return a <= b;
  42. },
  43. ascI(a, b) {
  44. return a.toLowerCase() <= b.toLowerCase();
  45. },
  46. ascN(a, b) {
  47. return naturalCompare(a, b) <= 0;
  48. },
  49. ascIN(a, b) {
  50. return naturalCompare(a.toLowerCase(), b.toLowerCase()) <= 0;
  51. },
  52. desc(a, b) {
  53. return isValidOrders.asc(b, a);
  54. },
  55. descI(a, b) {
  56. return isValidOrders.ascI(b, a);
  57. },
  58. descN(a, b) {
  59. return isValidOrders.ascN(b, a);
  60. },
  61. descIN(a, b) {
  62. return isValidOrders.ascIN(b, a);
  63. }
  64. };
  65. //------------------------------------------------------------------------------
  66. // Rule Definition
  67. //------------------------------------------------------------------------------
  68. module.exports = {
  69. meta: {
  70. type: "suggestion",
  71. docs: {
  72. description: "require object keys to be sorted",
  73. category: "Stylistic Issues",
  74. recommended: false,
  75. url: "https://eslint.org/docs/rules/sort-keys"
  76. },
  77. schema: [
  78. {
  79. enum: ["asc", "desc"]
  80. },
  81. {
  82. type: "object",
  83. properties: {
  84. caseSensitive: {
  85. type: "boolean",
  86. default: true
  87. },
  88. natural: {
  89. type: "boolean",
  90. default: false
  91. },
  92. minKeys: {
  93. type: "integer",
  94. minimum: 2,
  95. default: 2
  96. }
  97. },
  98. additionalProperties: false
  99. }
  100. ]
  101. },
  102. create(context) {
  103. // Parse options.
  104. const order = context.options[0] || "asc";
  105. const options = context.options[1];
  106. const insensitive = options && options.caseSensitive === false;
  107. const natual = options && options.natural;
  108. const minKeys = options && options.minKeys;
  109. const isValidOrder = isValidOrders[
  110. order + (insensitive ? "I" : "") + (natual ? "N" : "")
  111. ];
  112. // The stack to save the previous property's name for each object literals.
  113. let stack = null;
  114. return {
  115. ObjectExpression(node) {
  116. stack = {
  117. upper: stack,
  118. prevName: null,
  119. numKeys: node.properties.length
  120. };
  121. },
  122. "ObjectExpression:exit"() {
  123. stack = stack.upper;
  124. },
  125. SpreadElement(node) {
  126. if (node.parent.type === "ObjectExpression") {
  127. stack.prevName = null;
  128. }
  129. },
  130. Property(node) {
  131. if (node.parent.type === "ObjectPattern") {
  132. return;
  133. }
  134. const prevName = stack.prevName;
  135. const numKeys = stack.numKeys;
  136. const thisName = getPropertyName(node);
  137. if (thisName !== null) {
  138. stack.prevName = thisName;
  139. }
  140. if (prevName === null || thisName === null || numKeys < minKeys) {
  141. return;
  142. }
  143. if (!isValidOrder(prevName, thisName)) {
  144. context.report({
  145. node,
  146. loc: node.key.loc,
  147. message: "Expected object keys to be in {{natual}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'.",
  148. data: {
  149. thisName,
  150. prevName,
  151. order,
  152. insensitive: insensitive ? "insensitive " : "",
  153. natual: natual ? "natural " : ""
  154. }
  155. });
  156. }
  157. }
  158. };
  159. }
  160. };