项目原始demo,不改动
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
Bu depo arşivlendi. Dosyaları görüntüleyebilir ve klonlayabilirsiniz ama işlem gönderemez ve konu/değişiklik isteği açamazsınız.

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