sort-keys.js 4.8 KB

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