symbol-description.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @fileoverview Rule to enforce description with the `Symbol` object
  3. * @author Jarek Rencz
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "suggestion",
  16. docs: {
  17. description: "require symbol descriptions",
  18. category: "ECMAScript 6",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/symbol-description"
  21. },
  22. fixable: null,
  23. schema: [],
  24. messages: {
  25. expected: "Expected Symbol to have a description."
  26. }
  27. },
  28. create(context) {
  29. /**
  30. * Reports if node does not conform the rule in case rule is set to
  31. * report missing description
  32. * @param {ASTNode} node A CallExpression node to check.
  33. * @returns {void}
  34. */
  35. function checkArgument(node) {
  36. if (node.arguments.length === 0) {
  37. context.report({
  38. node,
  39. messageId: "expected"
  40. });
  41. }
  42. }
  43. return {
  44. "Program:exit"() {
  45. const scope = context.getScope();
  46. const variable = astUtils.getVariableByName(scope, "Symbol");
  47. if (variable && variable.defs.length === 0) {
  48. variable.references.forEach(reference => {
  49. const node = reference.identifier;
  50. if (astUtils.isCallee(node)) {
  51. checkArgument(node.parent);
  52. }
  53. });
  54. }
  55. }
  56. };
  57. }
  58. };