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.

no-useless-computed-key.js 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. const MESSAGE_UNNECESSARY_COMPUTED = "Unnecessarily computed property [{{property}}] found.";
  14. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "disallow unnecessary computed property keys in object literals",
  19. category: "ECMAScript 6",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/no-useless-computed-key"
  22. },
  23. schema: [],
  24. fixable: "code"
  25. },
  26. create(context) {
  27. const sourceCode = context.getSourceCode();
  28. return {
  29. Property(node) {
  30. if (!node.computed) {
  31. return;
  32. }
  33. const key = node.key,
  34. nodeType = typeof key.value;
  35. if (key.type === "Literal" && (nodeType === "string" || nodeType === "number") && key.value !== "__proto__") {
  36. context.report({
  37. node,
  38. message: MESSAGE_UNNECESSARY_COMPUTED,
  39. data: { property: sourceCode.getText(key) },
  40. fix(fixer) {
  41. const leftSquareBracket = sourceCode.getFirstToken(node, astUtils.isOpeningBracketToken);
  42. const rightSquareBracket = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isClosingBracketToken);
  43. const tokensBetween = sourceCode.getTokensBetween(leftSquareBracket, rightSquareBracket, 1);
  44. if (tokensBetween.slice(0, -1).some((token, index) =>
  45. sourceCode.getText().slice(token.range[1], tokensBetween[index + 1].range[0]).trim())) {
  46. // If there are comments between the brackets and the property name, don't do a fix.
  47. return null;
  48. }
  49. const tokenBeforeLeftBracket = sourceCode.getTokenBefore(leftSquareBracket);
  50. // Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} })
  51. const needsSpaceBeforeKey = tokenBeforeLeftBracket.range[1] === leftSquareBracket.range[0] &&
  52. !astUtils.canTokensBeAdjacent(tokenBeforeLeftBracket, sourceCode.getFirstToken(key));
  53. const replacementKey = (needsSpaceBeforeKey ? " " : "") + key.raw;
  54. return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], replacementKey);
  55. }
  56. });
  57. }
  58. }
  59. };
  60. }
  61. };