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.

id-length.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @fileoverview Rule that warns when identifier names are shorter or longer
  3. * than the values provided in configuration.
  4. * @author Burak Yigit Kaya aka BYK
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "enforce minimum and maximum identifier lengths",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/id-length"
  18. },
  19. schema: [
  20. {
  21. type: "object",
  22. properties: {
  23. min: {
  24. type: "number"
  25. },
  26. max: {
  27. type: "number"
  28. },
  29. exceptions: {
  30. type: "array",
  31. uniqueItems: true,
  32. items: {
  33. type: "string"
  34. }
  35. },
  36. properties: {
  37. enum: ["always", "never"]
  38. }
  39. },
  40. additionalProperties: false
  41. }
  42. ]
  43. },
  44. create(context) {
  45. const options = context.options[0] || {};
  46. const minLength = typeof options.min !== "undefined" ? options.min : 2;
  47. const maxLength = typeof options.max !== "undefined" ? options.max : Infinity;
  48. const properties = options.properties !== "never";
  49. const exceptions = (options.exceptions ? options.exceptions : [])
  50. .reduce((obj, item) => {
  51. obj[item] = true;
  52. return obj;
  53. }, {});
  54. const SUPPORTED_EXPRESSIONS = {
  55. MemberExpression: properties && function(parent) {
  56. return !parent.computed && (
  57. // regular property assignment
  58. (parent.parent.left === parent && parent.parent.type === "AssignmentExpression" ||
  59. // or the last identifier in an ObjectPattern destructuring
  60. parent.parent.type === "Property" && parent.parent.value === parent &&
  61. parent.parent.parent.type === "ObjectPattern" && parent.parent.parent.parent.left === parent.parent.parent)
  62. );
  63. },
  64. AssignmentPattern(parent, node) {
  65. return parent.left === node;
  66. },
  67. VariableDeclarator(parent, node) {
  68. return parent.id === node;
  69. },
  70. Property: properties && function(parent, node) {
  71. return parent.key === node;
  72. },
  73. ImportDefaultSpecifier: true,
  74. RestElement: true,
  75. FunctionExpression: true,
  76. ArrowFunctionExpression: true,
  77. ClassDeclaration: true,
  78. FunctionDeclaration: true,
  79. MethodDefinition: true,
  80. CatchClause: true
  81. };
  82. return {
  83. Identifier(node) {
  84. const name = node.name;
  85. const parent = node.parent;
  86. const isShort = name.length < minLength;
  87. const isLong = name.length > maxLength;
  88. if (!(isShort || isLong) || exceptions[name]) {
  89. return; // Nothing to report
  90. }
  91. const isValidExpression = SUPPORTED_EXPRESSIONS[parent.type];
  92. if (isValidExpression && (isValidExpression === true || isValidExpression(parent, node))) {
  93. context.report({
  94. node,
  95. message: isShort
  96. ? "Identifier name '{{name}}' is too short (< {{min}})."
  97. : "Identifier name '{{name}}' is too long (> {{max}}).",
  98. data: { name, min: minLength, max: maxLength }
  99. });
  100. }
  101. }
  102. };
  103. }
  104. };