Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions rules/prefer-ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const extendFixRange = require('./utils/extend-fix-range');
const needsSemicolon = require('./utils/needs-semicolon');
const isSameReference = require('./utils/is-same-reference');
const getIndentString = require('./utils/get-indent-string');
const {getParenthesizedText} = require('./utils/parentheses');
const shouldAddParenthesesToConditionalExpressionChild = require('./utils/should-add-parentheses-to-conditional-expression-child');

const messageId = 'prefer-ternary';

Expand Down Expand Up @@ -53,17 +55,16 @@ const create = context => {
return !generatedNames || !generatedNames.has(name);
});

const getParenthesizedText = node => {
const text = sourceCode.getText(node);
return (
isParenthesized(node, sourceCode) ||
node.type === 'AwaitExpression' ||
// Lower precedence, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table
node.type === 'AssignmentExpression' ||
node.type === 'YieldExpression' ||
node.type === 'SequenceExpression'
) ?
`(${text})` : text;
const getText = node => {
let text = getParenthesizedText(node, sourceCode);
if (
!isParenthesized(node, sourceCode) &&
shouldAddParenthesesToConditionalExpressionChild(node)
) {
text = `(${text})`;
}

return text;
};

const isSingleLineNode = node => {
Expand Down Expand Up @@ -206,13 +207,13 @@ const create = context => {
node,
messageId,
* fix(fixer) {
const testText = getParenthesizedText(node.test);
const testText = getText(node.test);
const consequentText = typeof result.consequent === 'string' ?
result.consequent :
getParenthesizedText(result.consequent);
getText(result.consequent);
const alternateText = typeof result.alternate === 'string' ?
result.alternate :
getParenthesizedText(result.alternate);
getText(result.alternate);

let {type, before, after} = result;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

/**
Check if parentheses should be added to a `node` when it's used as child of `ConditionalExpression`.

@param {Node} node - The AST node to check.
@returns {boolean}
*/
function shouldAddParenthesesToConditionalExpressionChild(node) {
return node.type === 'AwaitExpression' ||
// Lower precedence, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table
node.type === 'AssignmentExpression' ||
node.type === 'YieldExpression' ||
node.type === 'SequenceExpression';
}

module.exports = shouldAddParenthesesToConditionalExpressionChild;
6 changes: 5 additions & 1 deletion test/prefer-ternary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,11 @@ test({
a = bar;
}
`,
output: 'a = (test) ? foo : bar;',
output: outdent`
a = (
test
) ? foo : bar;
`,
options: onlySingleLineOptions,
errors
},
Expand Down