sort-keys.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 natural.
  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. messages: {
  102. sortKeys: "Expected object keys to be in {{natural}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'."
  103. }
  104. },
  105. create(context) {
  106. // Parse options.
  107. const order = context.options[0] || "asc";
  108. const options = context.options[1];
  109. const insensitive = options && options.caseSensitive === false;
  110. const natural = options && options.natural;
  111. const minKeys = options && options.minKeys;
  112. const isValidOrder = isValidOrders[
  113. order + (insensitive ? "I" : "") + (natural ? "N" : "")
  114. ];
  115. // The stack to save the previous property's name for each object literals.
  116. let stack = null;
  117. return {
  118. ObjectExpression(node) {
  119. stack = {
  120. upper: stack,
  121. prevName: null,
  122. numKeys: node.properties.length
  123. };
  124. },
  125. "ObjectExpression:exit"() {
  126. stack = stack.upper;
  127. },
  128. SpreadElement(node) {
  129. if (node.parent.type === "ObjectExpression") {
  130. stack.prevName = null;
  131. }
  132. },
  133. Property(node) {
  134. if (node.parent.type === "ObjectPattern") {
  135. return;
  136. }
  137. const prevName = stack.prevName;
  138. const numKeys = stack.numKeys;
  139. const thisName = getPropertyName(node);
  140. if (thisName !== null) {
  141. stack.prevName = thisName;
  142. }
  143. if (prevName === null || thisName === null || numKeys < minKeys) {
  144. return;
  145. }
  146. if (!isValidOrder(prevName, thisName)) {
  147. context.report({
  148. node,
  149. loc: node.key.loc,
  150. messageId: "sortKeys",
  151. data: {
  152. thisName,
  153. prevName,
  154. order,
  155. insensitive: insensitive ? "insensitive " : "",
  156. natural: natural ? "natural " : ""
  157. }
  158. });
  159. }
  160. }
  161. };
  162. }
  163. };