1 | 'use strict';
|
2 | const {isParenthesized} = require('eslint-utils');
|
3 | const getDocumentationUrl = require('./utils/get-documentation-url');
|
4 |
|
5 | const create = context => {
|
6 | const sourceCode = context.getSourceCode();
|
7 |
|
8 | return {
|
9 | ConditionalExpression: node => {
|
10 | const nodesToCheck = [node.alternate, node.consequent];
|
11 |
|
12 | for (const childNode of nodesToCheck) {
|
13 | if (childNode.type !== 'ConditionalExpression') {
|
14 | continue;
|
15 | }
|
16 |
|
17 | const message = 'Do not nest ternary expressions.';
|
18 |
|
19 |
|
20 | if (
|
21 | childNode.alternate.type === 'ConditionalExpression' ||
|
22 | childNode.consequent.type === 'ConditionalExpression'
|
23 | ) {
|
24 | context.report({node, message});
|
25 | break;
|
26 | } else if (!isParenthesized(childNode, sourceCode)) {
|
27 | context.report({
|
28 | node: childNode,
|
29 | message,
|
30 | fix: fixer => [
|
31 | fixer.insertTextBefore(childNode, '('),
|
32 | fixer.insertTextAfter(childNode, ')')
|
33 | ]
|
34 | });
|
35 | }
|
36 | }
|
37 | }
|
38 | };
|
39 | };
|
40 |
|
41 | module.exports = {
|
42 | create,
|
43 | meta: {
|
44 | type: 'suggestion',
|
45 | docs: {
|
46 | url: getDocumentationUrl(__filename)
|
47 | },
|
48 | fixable: 'code'
|
49 | }
|
50 | };
|