Skip to content

Commit 784c7a8

Browse files
authored
prefer-math-trunc: Improve fix logic (#1670)
1 parent b25962e commit 784c7a8

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

rules/prefer-math-trunc.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ const create = context => {
6161

6262
if (!isAssignment || !hasSideEffect(left, sourceCode)) {
6363
const fix = function * (fixer) {
64-
let fixed = mathTruncFunctionCall(left);
64+
const fixed = mathTruncFunctionCall(left);
6565
if (isAssignment) {
66-
// TODO[@fisker]: Improve this fix, don't touch left
67-
fixed = `${sourceCode.getText(left)} = ${fixed}`;
66+
const operatorToken = sourceCode.getTokenAfter(left, token => token.type === 'Punctuator' && token.value === operator);
67+
yield fixer.replaceText(operatorToken, '=');
68+
yield fixer.replaceText(right, fixed);
6869
} else {
6970
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
71+
yield fixer.replaceText(node, fixed);
7072
}
71-
72-
yield fixer.replaceText(node, fixed);
7373
};
7474

7575
if (operator === '|') {

test/snapshots/prefer-math-trunc.mjs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ Generated by [AVA](https://avajs.dev).
683683
684684
`␊
685685
1 | const foo = {a: {b: 3.4}};␊
686-
2 | foo /* Comment 1 */ .a /* Comment 2 */ . /* Comment 3 */ b = Math.trunc(foo /* Comment 1 */ .a /* Comment 2 */ . /* Comment 3 */ b) /* Comment 5 */;␊
686+
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 */;␊
687687
`
688688

689689
> Error 1/1
3 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)