项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

118 line
4.0 KiB

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