Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to flag nested ternary expressions |
| 3 | * @author Ian Christian Myers |
| 4 | */ |
| 5 | |
| 6 | "use strict"; |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | // Rule Definition |
| 10 | //------------------------------------------------------------------------------ |
| 11 | |
| 12 | module.exports = { |
| 13 | meta: { |
| 14 | type: "suggestion", |
| 15 | |
| 16 | docs: { |
| 17 | description: "disallow nested ternary expressions", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 18 | recommended: false, |
| 19 | url: "https://eslint.org/docs/rules/no-nested-ternary" |
| 20 | }, |
| 21 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 22 | schema: [], |
| 23 | |
| 24 | messages: { |
| 25 | noNestedTernary: "Do not nest ternary expressions." |
| 26 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 27 | }, |
| 28 | |
| 29 | create(context) { |
| 30 | |
| 31 | return { |
| 32 | ConditionalExpression(node) { |
| 33 | if (node.alternate.type === "ConditionalExpression" || |
| 34 | node.consequent.type === "ConditionalExpression") { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 35 | context.report({ |
| 36 | node, |
| 37 | messageId: "noNestedTernary" |
| 38 | }); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | }; |
| 42 | } |
| 43 | }; |