Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-ternary: Skip fix if there are comments #1763

Merged
merged 2 commits into from
Mar 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 53 additions & 49 deletions rules/prefer-ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,61 +194,65 @@ const create = context => {
return;
}

const scope = context.getScope();

return {
node,
messageId,
* fix(fixer) {
const testText = getText(node.test);
const consequentText = typeof result.consequent === 'string'
? result.consequent
: getText(result.consequent);
const alternateText = typeof result.alternate === 'string'
? result.alternate
: getText(result.alternate);

let {type, before, after} = result;

let generateNewVariables = false;
if (type === 'ThrowStatement') {
const scopes = getScopes(scope);
const errorName = avoidCapture('error', scopes, isSafeName);

for (const scope of scopes) {
if (!scopeToNamesGeneratedByFixer.has(scope)) {
scopeToNamesGeneratedByFixer.set(scope, new Set());
}

const generatedNames = scopeToNamesGeneratedByFixer.get(scope);
generatedNames.add(errorName);
}
const problem = {node, messageId};

const indentString = getIndentString(node, sourceCode);
// Don't fix if there are comments
if (sourceCode.getCommentsInside(node).length > 0) {
return problem;
}

after = after
.replace('{{INDENT_STRING}}', indentString)
.replace('{{ERROR_NAME}}', errorName);
before = before
.replace('{{INDENT_STRING}}', indentString)
.replace('{{ERROR_NAME}}', errorName);
generateNewVariables = true;
}
const scope = context.getScope();
problem.fix = function * (fixer) {
const testText = getText(node.test);
const consequentText = typeof result.consequent === 'string'
? result.consequent
: getText(result.consequent);
const alternateText = typeof result.alternate === 'string'
? result.alternate
: getText(result.alternate);

let {type, before, after} = result;

let generateNewVariables = false;
if (type === 'ThrowStatement') {
const scopes = getScopes(scope);
const errorName = avoidCapture('error', scopes, isSafeName);

for (const scope of scopes) {
if (!scopeToNamesGeneratedByFixer.has(scope)) {
scopeToNamesGeneratedByFixer.set(scope, new Set());
}

let fixed = `${before}${testText} ? ${consequentText} : ${alternateText}${after}`;
const tokenBefore = sourceCode.getTokenBefore(node);
const shouldAddSemicolonBefore = needsSemicolon(tokenBefore, sourceCode, fixed);
if (shouldAddSemicolonBefore) {
fixed = `;${fixed}`;
const generatedNames = scopeToNamesGeneratedByFixer.get(scope);
generatedNames.add(errorName);
}

yield fixer.replaceText(node, fixed);

if (generateNewVariables) {
yield * extendFixRange(fixer, sourceCode.ast.range);
}
},
const indentString = getIndentString(node, sourceCode);

after = after
.replace('{{INDENT_STRING}}', indentString)
.replace('{{ERROR_NAME}}', errorName);
before = before
.replace('{{INDENT_STRING}}', indentString)
.replace('{{ERROR_NAME}}', errorName);
generateNewVariables = true;
}

let fixed = `${before}${testText} ? ${consequentText} : ${alternateText}${after}`;
const tokenBefore = sourceCode.getTokenBefore(node);
const shouldAddSemicolonBefore = needsSemicolon(tokenBefore, sourceCode, fixed);
if (shouldAddSemicolonBefore) {
fixed = `;${fixed}`;
}

yield fixer.replaceText(node, fixed);

if (generateNewVariables) {
yield * extendFixRange(fixer, sourceCode.ast.range);
}
};

return problem;
},
};
};
Expand Down
8 changes: 7 additions & 1 deletion test/prefer-ternary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,9 @@ test({
{
code: outdent`
function unicorn() {
// There is an empty block inside consequent
if (test) {
; // Empty block
;
return a;
} else {
return b;
Expand All @@ -1176,6 +1177,7 @@ test({
`,
output: outdent`
function unicorn() {
// There is an empty block inside consequent
return test ? a : b;
}
`,
Expand Down Expand Up @@ -1319,5 +1321,9 @@ test({
`,
errors,
},
{
code: 'if (test) {foo = /* comment */1;} else {foo = 2;}',
errors,
},
],
});