sort-vars.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @fileoverview Rule to require sorting of variables within a single Variable Declaration block
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../types').Rule.RuleModule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. defaultOptions: [
  14. {
  15. ignoreCase: false,
  16. },
  17. ],
  18. docs: {
  19. description:
  20. "Require variables within the same declaration block to be sorted",
  21. recommended: false,
  22. frozen: true,
  23. url: "https://eslint.org/docs/latest/rules/sort-vars",
  24. },
  25. schema: [
  26. {
  27. type: "object",
  28. properties: {
  29. ignoreCase: {
  30. type: "boolean",
  31. },
  32. },
  33. additionalProperties: false,
  34. },
  35. ],
  36. fixable: "code",
  37. messages: {
  38. sortVars:
  39. "Variables within the same declaration block should be sorted alphabetically.",
  40. },
  41. },
  42. create(context) {
  43. const [{ ignoreCase }] = context.options;
  44. const sourceCode = context.sourceCode;
  45. return {
  46. VariableDeclaration(node) {
  47. const idDeclarations = node.declarations.filter(
  48. decl => decl.id.type === "Identifier",
  49. );
  50. const getSortableName = ignoreCase
  51. ? decl => decl.id.name.toLowerCase()
  52. : decl => decl.id.name;
  53. const unfixable = idDeclarations.some(
  54. decl => decl.init !== null && decl.init.type !== "Literal",
  55. );
  56. let fixed = false;
  57. idDeclarations.slice(1).reduce((memo, decl) => {
  58. const lastVariableName = getSortableName(memo),
  59. currentVariableName = getSortableName(decl);
  60. if (currentVariableName < lastVariableName) {
  61. context.report({
  62. node: decl,
  63. messageId: "sortVars",
  64. fix(fixer) {
  65. if (unfixable || fixed) {
  66. return null;
  67. }
  68. return fixer.replaceTextRange(
  69. [
  70. idDeclarations[0].range[0],
  71. idDeclarations.at(-1).range[1],
  72. ],
  73. idDeclarations
  74. // Clone the idDeclarations array to avoid mutating it
  75. .slice()
  76. // Sort the array into the desired order
  77. .sort((declA, declB) => {
  78. const aName =
  79. getSortableName(declA);
  80. const bName =
  81. getSortableName(declB);
  82. return aName > bName ? 1 : -1;
  83. })
  84. // Build a string out of the sorted list of identifier declarations and the text between the originals
  85. .reduce(
  86. (sourceText, identifier, index) => {
  87. const textAfterIdentifier =
  88. index ===
  89. idDeclarations.length - 1
  90. ? ""
  91. : sourceCode
  92. .getText()
  93. .slice(
  94. idDeclarations[
  95. index
  96. ].range[1],
  97. idDeclarations[
  98. index +
  99. 1
  100. ].range[0],
  101. );
  102. return (
  103. sourceText +
  104. sourceCode.getText(
  105. identifier,
  106. ) +
  107. textAfterIdentifier
  108. );
  109. },
  110. "",
  111. ),
  112. );
  113. },
  114. });
  115. fixed = true;
  116. return memo;
  117. }
  118. return decl;
  119. }, idDeclarations[0]);
  120. },
  121. };
  122. },
  123. };