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-vars.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "require variables within the same declaration block to be sorted",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/sort-vars"
  17. },
  18. schema: [
  19. {
  20. type: "object",
  21. properties: {
  22. ignoreCase: {
  23. type: "boolean"
  24. }
  25. },
  26. additionalProperties: false
  27. }
  28. ],
  29. fixable: "code"
  30. },
  31. create(context) {
  32. const configuration = context.options[0] || {},
  33. ignoreCase = configuration.ignoreCase || false,
  34. sourceCode = context.getSourceCode();
  35. return {
  36. VariableDeclaration(node) {
  37. const idDeclarations = node.declarations.filter(decl => decl.id.type === "Identifier");
  38. const getSortableName = ignoreCase ? decl => decl.id.name.toLowerCase() : decl => decl.id.name;
  39. const unfixable = idDeclarations.some(decl => decl.init !== null && decl.init.type !== "Literal");
  40. let fixed = false;
  41. idDeclarations.slice(1).reduce((memo, decl) => {
  42. const lastVariableName = getSortableName(memo),
  43. currentVariableName = getSortableName(decl);
  44. if (currentVariableName < lastVariableName) {
  45. context.report({
  46. node: decl,
  47. message: "Variables within the same declaration block should be sorted alphabetically.",
  48. fix(fixer) {
  49. if (unfixable || fixed) {
  50. return null;
  51. }
  52. return fixer.replaceTextRange(
  53. [idDeclarations[0].range[0], idDeclarations[idDeclarations.length - 1].range[1]],
  54. idDeclarations
  55. // Clone the idDeclarations array to avoid mutating it
  56. .slice()
  57. // Sort the array into the desired order
  58. .sort((declA, declB) => {
  59. const aName = getSortableName(declA);
  60. const bName = getSortableName(declB);
  61. return aName > bName ? 1 : -1;
  62. })
  63. // Build a string out of the sorted list of identifier declarations and the text between the originals
  64. .reduce((sourceText, identifier, index) => {
  65. const textAfterIdentifier = index === idDeclarations.length - 1
  66. ? ""
  67. : sourceCode.getText().slice(idDeclarations[index].range[1], idDeclarations[index + 1].range[0]);
  68. return sourceText + sourceCode.getText(identifier) + textAfterIdentifier;
  69. }, "")
  70. );
  71. }
  72. });
  73. fixed = true;
  74. return memo;
  75. }
  76. return decl;
  77. }, idDeclarations[0]);
  78. }
  79. };
  80. }
  81. };