Skip to content

Commit

Permalink
prefer-math-trunc: Improve fix logic (#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 29, 2021
1 parent b25962e commit 784c7a8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions rules/prefer-math-trunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ const create = context => {

if (!isAssignment || !hasSideEffect(left, sourceCode)) {
const fix = function * (fixer) {
let fixed = mathTruncFunctionCall(left);
const fixed = mathTruncFunctionCall(left);
if (isAssignment) {
// TODO[@fisker]: Improve this fix, don't touch left
fixed = `${sourceCode.getText(left)} = ${fixed}`;
const operatorToken = sourceCode.getTokenAfter(left, token => token.type === 'Punctuator' && token.value === operator);
yield fixer.replaceText(operatorToken, '=');
yield fixer.replaceText(right, fixed);
} else {
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
yield fixer.replaceText(node, fixed);
}

yield fixer.replaceText(node, fixed);
};

if (operator === '|') {
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/prefer-math-trunc.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ Generated by [AVA](https://avajs.dev).
`␊
1 | const foo = {a: {b: 3.4}};␊
2 | foo /* Comment 1 */ .a /* Comment 2 */ . /* Comment 3 */ b = Math.trunc(foo /* Comment 1 */ .a /* Comment 2 */ . /* Comment 3 */ b) /* Comment 5 */;␊
2 | foo /* Comment 1 */ .a /* Comment 2 */ . /* Comment 3 */ b = /* Comment 4 */ Math.trunc(foo /* Comment 1 */ .a /* Comment 2 */ . /* Comment 3 */ b) /* Comment 5 */;␊
`

> Error 1/1
Expand Down
Binary file modified test/snapshots/prefer-math-trunc.mjs.snap
Binary file not shown.

0 comments on commit 784c7a8

Please sign in to comment.