sort-keys.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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("../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. docs: {
  69. description: "require object keys to be sorted",
  70. category: "Stylistic Issues",
  71. recommended: false,
  72. url: "https://eslint.org/docs/rules/sort-keys"
  73. },
  74. schema: [
  75. {
  76. enum: ["asc", "desc"]
  77. },
  78. {
  79. type: "object",
  80. properties: {
  81. caseSensitive: {
  82. type: "boolean"
  83. },
  84. natural: {
  85. type: "boolean"
  86. }
  87. },
  88. additionalProperties: false
  89. }
  90. ]
  91. },
  92. create(context) {
  93. // Parse options.
  94. const order = context.options[0] || "asc";
  95. const options = context.options[1];
  96. const insensitive = (options && options.caseSensitive) === false;
  97. const natual = Boolean(options && options.natural);
  98. const isValidOrder = isValidOrders[
  99. order + (insensitive ? "I" : "") + (natual ? "N" : "")
  100. ];
  101. // The stack to save the previous property's name for each object literals.
  102. let stack = null;
  103. return {
  104. ObjectExpression() {
  105. stack = {
  106. upper: stack,
  107. prevName: null
  108. };
  109. },
  110. "ObjectExpression:exit"() {
  111. stack = stack.upper;
  112. },
  113. Property(node) {
  114. if (node.parent.type === "ObjectPattern") {
  115. return;
  116. }
  117. const prevName = stack.prevName;
  118. const thisName = getPropertyName(node);
  119. stack.prevName = thisName || prevName;
  120. if (!prevName || !thisName) {
  121. return;
  122. }
  123. if (!isValidOrder(prevName, thisName)) {
  124. context.report({
  125. node,
  126. loc: node.key.loc,
  127. message: "Expected object keys to be in {{natual}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'.",
  128. data: {
  129. thisName,
  130. prevName,
  131. order,
  132. insensitive: insensitive ? "insensitive " : "",
  133. natual: natual ? "natural " : ""
  134. }
  135. });
  136. }
  137. }
  138. };
  139. }
  140. };