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.

dot-notation.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @fileoverview Rule to warn about using dot notation instead of square bracket notation when possible.
  3. * @author Josh Perez
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. const validIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
  14. const keywords = require("../util/keywords");
  15. module.exports = {
  16. meta: {
  17. type: "suggestion",
  18. docs: {
  19. description: "enforce dot notation whenever possible",
  20. category: "Best Practices",
  21. recommended: false,
  22. url: "https://eslint.org/docs/rules/dot-notation"
  23. },
  24. schema: [
  25. {
  26. type: "object",
  27. properties: {
  28. allowKeywords: {
  29. type: "boolean"
  30. },
  31. allowPattern: {
  32. type: "string"
  33. }
  34. },
  35. additionalProperties: false
  36. }
  37. ],
  38. fixable: "code",
  39. messages: {
  40. useDot: "[{{key}}] is better written in dot notation.",
  41. useBrackets: ".{{key}} is a syntax error."
  42. }
  43. },
  44. create(context) {
  45. const options = context.options[0] || {};
  46. const allowKeywords = options.allowKeywords === void 0 || !!options.allowKeywords;
  47. const sourceCode = context.getSourceCode();
  48. let allowPattern;
  49. if (options.allowPattern) {
  50. allowPattern = new RegExp(options.allowPattern);
  51. }
  52. /**
  53. * Check if the property is valid dot notation
  54. * @param {ASTNode} node The dot notation node
  55. * @param {string} value Value which is to be checked
  56. * @returns {void}
  57. */
  58. function checkComputedProperty(node, value) {
  59. if (
  60. validIdentifier.test(value) &&
  61. (allowKeywords || keywords.indexOf(String(value)) === -1) &&
  62. !(allowPattern && allowPattern.test(value))
  63. ) {
  64. const formattedValue = node.property.type === "Literal" ? JSON.stringify(value) : `\`${value}\``;
  65. context.report({
  66. node: node.property,
  67. messageId: "useDot",
  68. data: {
  69. key: formattedValue
  70. },
  71. fix(fixer) {
  72. const leftBracket = sourceCode.getTokenAfter(node.object, astUtils.isOpeningBracketToken);
  73. const rightBracket = sourceCode.getLastToken(node);
  74. if (sourceCode.getFirstTokenBetween(leftBracket, rightBracket, { includeComments: true, filter: astUtils.isCommentToken })) {
  75. // Don't perform any fixes if there are comments inside the brackets.
  76. return null;
  77. }
  78. const tokenAfterProperty = sourceCode.getTokenAfter(rightBracket);
  79. const needsSpaceAfterProperty = tokenAfterProperty &&
  80. rightBracket.range[1] === tokenAfterProperty.range[0] &&
  81. !astUtils.canTokensBeAdjacent(String(value), tokenAfterProperty);
  82. const textBeforeDot = astUtils.isDecimalInteger(node.object) ? " " : "";
  83. const textAfterProperty = needsSpaceAfterProperty ? " " : "";
  84. return fixer.replaceTextRange(
  85. [leftBracket.range[0], rightBracket.range[1]],
  86. `${textBeforeDot}.${value}${textAfterProperty}`
  87. );
  88. }
  89. });
  90. }
  91. }
  92. return {
  93. MemberExpression(node) {
  94. if (
  95. node.computed &&
  96. node.property.type === "Literal"
  97. ) {
  98. checkComputedProperty(node, node.property.value);
  99. }
  100. if (
  101. node.computed &&
  102. node.property.type === "TemplateLiteral" &&
  103. node.property.expressions.length === 0
  104. ) {
  105. checkComputedProperty(node, node.property.quasis[0].value.cooked);
  106. }
  107. if (
  108. !allowKeywords &&
  109. !node.computed &&
  110. keywords.indexOf(String(node.property.name)) !== -1
  111. ) {
  112. context.report({
  113. node: node.property,
  114. messageId: "useBrackets",
  115. data: {
  116. key: node.property.name
  117. },
  118. fix(fixer) {
  119. const dot = sourceCode.getTokenBefore(node.property);
  120. const textAfterDot = sourceCode.text.slice(dot.range[1], node.property.range[0]);
  121. if (textAfterDot.trim()) {
  122. // Don't perform any fixes if there are comments between the dot and the property name.
  123. return null;
  124. }
  125. if (node.object.type === "Identifier" && node.object.name === "let") {
  126. /*
  127. * A statement that starts with `let[` is parsed as a destructuring variable declaration, not
  128. * a MemberExpression.
  129. */
  130. return null;
  131. }
  132. return fixer.replaceTextRange(
  133. [dot.range[0], node.property.range[1]],
  134. `[${textAfterDot}"${node.property.name}"]`
  135. );
  136. }
  137. });
  138. }
  139. }
  140. };
  141. }
  142. };