Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sort-keys.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. },
  85. natural: {
  86. type: "boolean"
  87. }
  88. },
  89. additionalProperties: false
  90. }
  91. ]
  92. },
  93. create(context) {
  94. // Parse options.
  95. const order = context.options[0] || "asc";
  96. const options = context.options[1];
  97. const insensitive = (options && options.caseSensitive) === false;
  98. const natual = Boolean(options && options.natural);
  99. const isValidOrder = isValidOrders[
  100. order + (insensitive ? "I" : "") + (natual ? "N" : "")
  101. ];
  102. // The stack to save the previous property's name for each object literals.
  103. let stack = null;
  104. return {
  105. ObjectExpression() {
  106. stack = {
  107. upper: stack,
  108. prevName: null
  109. };
  110. },
  111. "ObjectExpression:exit"() {
  112. stack = stack.upper;
  113. },
  114. Property(node) {
  115. if (node.parent.type === "ObjectPattern" || node.parent.properties.some(n => n.type === "SpreadElement")) {
  116. return;
  117. }
  118. const prevName = stack.prevName;
  119. const thisName = getPropertyName(node);
  120. stack.prevName = thisName || prevName;
  121. if (!prevName || !thisName) {
  122. return;
  123. }
  124. if (!isValidOrder(prevName, thisName)) {
  125. context.report({
  126. node,
  127. loc: node.key.loc,
  128. message: "Expected object keys to be in {{natual}}{{insensitive}}{{order}}ending order. '{{thisName}}' should be before '{{prevName}}'.",
  129. data: {
  130. thisName,
  131. prevName,
  132. order,
  133. insensitive: insensitive ? "insensitive " : "",
  134. natual: natual ? "natural " : ""
  135. }
  136. });
  137. }
  138. }
  139. };
  140. }
  141. };