项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

77 lines
3.2 KiB

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