no-nested-ternary.js 960 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @fileoverview Rule to flag nested ternary expressions
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../types').Rule.RuleModule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Disallow nested ternary expressions",
  15. recommended: false,
  16. frozen: true,
  17. url: "https://eslint.org/docs/latest/rules/no-nested-ternary",
  18. },
  19. schema: [],
  20. messages: {
  21. noNestedTernary: "Do not nest ternary expressions.",
  22. },
  23. },
  24. create(context) {
  25. return {
  26. ConditionalExpression(node) {
  27. if (
  28. node.alternate.type === "ConditionalExpression" ||
  29. node.consequent.type === "ConditionalExpression"
  30. ) {
  31. context.report({
  32. node,
  33. messageId: "noNestedTernary",
  34. });
  35. }
  36. },
  37. };
  38. },
  39. };