Skip to content

Commit

Permalink
no-nested-ternary: Use simple selector (#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 16, 2023
1 parent 2b05e38 commit 9a6012c
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions rules/no-nested-ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,41 @@ const messages = {
[MESSAGE_ID_SHOULD_PARENTHESIZED]: 'Nest ternary expression should be parenthesized.',
};

const nestTernarySelector = level => `:not(ConditionalExpression)${' > ConditionalExpression'.repeat(level)}`;

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const {sourceCode} = context;
const create = context => ({
ConditionalExpression(node) {
if ([
node.test,
node.consequent,
node.alternate,
].some(node => node.type === 'ConditionalExpression')) {
return;
}

return {
[nestTernarySelector(3)]: node =>
// Nesting more than one level not allowed.
({node, messageId: MESSAGE_ID_TOO_DEEP}),
[nestTernarySelector(2)](node) {
if (!isParenthesized(node, sourceCode)) {
return {
node,
messageId: MESSAGE_ID_SHOULD_PARENTHESIZED,
fix: fixer => [
fixer.insertTextBefore(node, '('),
fixer.insertTextAfter(node, ')'),
],
};
}
},
};
};
const {sourceCode} = context;
const ancestors = sourceCode.getAncestors(node).reverse();
const nestLevel = ancestors.findIndex(node => node.type !== 'ConditionalExpression');

if (nestLevel === 1 && !isParenthesized(node, sourceCode)) {
return {
node,
messageId: MESSAGE_ID_SHOULD_PARENTHESIZED,
fix: fixer => [
fixer.insertTextBefore(node, '('),
fixer.insertTextAfter(node, ')'),
],
};
}

// Nesting more than one level not allowed
if (nestLevel > 1) {
return {
node: nestLevel > 2 ? ancestors[nestLevel - 3] : node,
messageId: MESSAGE_ID_TOO_DEEP,
};
}
},
});

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
Expand Down

0 comments on commit 9a6012c

Please sign in to comment.