no-useless-computed-key.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @fileoverview Rule to disallow unnecessary computed property keys in object literals
  3. * @author Burak Yigit Kaya
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash");
  10. const astUtils = require("./utils/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "disallow unnecessary computed property keys in objects and classes",
  19. category: "ECMAScript 6",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/no-useless-computed-key"
  22. },
  23. schema: [{
  24. type: "object",
  25. properties: {
  26. enforceForClassMembers: {
  27. type: "boolean",
  28. default: false
  29. }
  30. },
  31. additionalProperties: false
  32. }],
  33. fixable: "code",
  34. messages: {
  35. unnecessarilyComputedProperty: "Unnecessarily computed property [{{property}}] found."
  36. }
  37. },
  38. create(context) {
  39. const sourceCode = context.getSourceCode();
  40. const enforceForClassMembers = context.options[0] && context.options[0].enforceForClassMembers;
  41. /**
  42. * Reports a given node if it violated this rule.
  43. * @param {ASTNode} node The node to check.
  44. * @returns {void}
  45. */
  46. function check(node) {
  47. if (!node.computed) {
  48. return;
  49. }
  50. const key = node.key,
  51. nodeType = typeof key.value;
  52. let allowedKey;
  53. if (node.type === "MethodDefinition") {
  54. allowedKey = node.static ? "prototype" : "constructor";
  55. } else {
  56. allowedKey = "__proto__";
  57. }
  58. if (key.type === "Literal" && (nodeType === "string" || nodeType === "number") && key.value !== allowedKey) {
  59. context.report({
  60. node,
  61. messageId: "unnecessarilyComputedProperty",
  62. data: { property: sourceCode.getText(key) },
  63. fix(fixer) {
  64. const leftSquareBracket = sourceCode.getTokenBefore(key, astUtils.isOpeningBracketToken);
  65. const rightSquareBracket = sourceCode.getTokenAfter(key, astUtils.isClosingBracketToken);
  66. // If there are comments between the brackets and the property name, don't do a fix.
  67. if (sourceCode.commentsExistBetween(leftSquareBracket, rightSquareBracket)) {
  68. return null;
  69. }
  70. const tokenBeforeLeftBracket = sourceCode.getTokenBefore(leftSquareBracket);
  71. // Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} })
  72. const needsSpaceBeforeKey = tokenBeforeLeftBracket.range[1] === leftSquareBracket.range[0] &&
  73. !astUtils.canTokensBeAdjacent(tokenBeforeLeftBracket, sourceCode.getFirstToken(key));
  74. const replacementKey = (needsSpaceBeforeKey ? " " : "") + key.raw;
  75. return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], replacementKey);
  76. }
  77. });
  78. }
  79. }
  80. return {
  81. Property: check,
  82. MethodDefinition: enforceForClassMembers ? check : lodash.noop
  83. };
  84. }
  85. };