dot-notation.js 6.5 KB

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