Repository: sindresorhus/eslint-plugin-unicorn (JavaScript)

Issue:
`no-zero-fractions`:  Incorrect fix
<!-- ✨ Thanks for reporting a bug! ➡️ Please don't ignore this template -->

<!-- 1️⃣ Explain here what's wrong -->

```js
function foo(){return .0;};
```

Was fixed to

<!-- 2️⃣ Specify which rule is buggy here and in the title -->

<!-- 3️⃣ Add some examples where the issue appears -->

```js
function foo(){return ;};
```

I'll fix it after #1436, because it could be

```js
function foo(){return.0;};
```

Which need add space after `return`.




Discussion:

Patch:
diff --git a/rules/no-zero-fractions.js b/rules/no-zero-fractions.js
index 37d31d1c6c..49ea90dbcc 100644
--- a/rules/no-zero-fractions.js
+++ b/rules/no-zero-fractions.js
@@ -3,6 +3,7 @@ const {isParenthesized} = require('eslint-utils');
 const needsSemicolon = require('./utils/needs-semicolon.js');
 const {isNumber, isDecimalInteger} = require('./utils/numeric.js');
 const toLocation = require('./utils/to-location.js');
+const {fixSpaceAroundKeyword} = require('./fix/index.js');
 
 const MESSAGE_ZERO_FRACTION = 'zero-fraction';
 const MESSAGE_DANGLING_DOT = 'dangling-dot';
@@ -26,7 +27,8 @@ const create = context => {
 			}
 
 			const {before, dotAndFractions, after} = match.groups;
-			const formatted = before + dotAndFractions.replace(/[.0_]+$/g, '') + after;
+			const fixedDotAndFractions = dotAndFractions.replace(/[.0_]+$/g, '');
+			const formatted = ((before + fixedDotAndFractions) || '0') + after;
 
 			if (formatted === raw) {
 				return;
@@ -40,7 +42,7 @@ const create = context => {
 			return {
 				loc: toLocation([start, end], sourceCode),
 				messageId: isDanglingDot ? MESSAGE_DANGLING_DOT : MESSAGE_ZERO_FRACTION,
-				fix: fixer => {
+				* fix(fixer) {
 					let fixed = formatted;
 					if (
 						node.parent.type === 'MemberExpression' &&
@@ -55,7 +57,8 @@ const create = context => {
 						}
 					}
 
-					return fixer.replaceText(node, fixed);
+					yield fixer.replaceText(node, fixed);
+					yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
 				},
 			};
 		},


Test patch:
diff --git a/test/no-zero-fractions.mjs b/test/no-zero-fractions.mjs
index e7db01af50..217dd1dfff 100644
--- a/test/no-zero-fractions.mjs
+++ b/test/no-zero-fractions.mjs
@@ -80,5 +80,13 @@ test.snapshot({
 			console.log()
 			a[1.00e10].toString()
 		`,
+		'a = .0;',
+		'a = .0.toString()',
+		'function foo(){return.0}',
+		'function foo(){return.0.toString()}',
+		outdent`
+			console.log()
+			.0.toString()
+		`,
 	],
 });
diff --git a/test/snapshots/no-zero-fractions.mjs.md b/test/snapshots/no-zero-fractions.mjs.md
index 79583f4d6a..e91b617b82 100644
--- a/test/snapshots/no-zero-fractions.mjs.md
+++ b/test/snapshots/no-zero-fractions.mjs.md
@@ -2687,3 +2687,86 @@ Generated by [AVA](https://avajs.dev).
     > 2 | a[1.00e10].toString()␊
         |    ^^^ Don't use a zero fraction in the number.␊
     `
+
+## Invalid #168
+      1 | a = .0;
+
+> Output
+
+    `␊
+      1 | a = 0;␊
+    `
+
+> Error 1/1
+
+    `␊
+    > 1 | a = .0;␊
+        |      ^ Don't use a zero fraction in the number.␊
+    `
+
+## Invalid #169
+      1 | a = .0.toString()
+
+> Output
+
+    `␊
+      1 | a = (0).toString()␊
+    `
+
+> Error 1/1
+
+    `␊
+    > 1 | a = .0.toString()␊
+        |      ^ Don't use a zero fraction in the number.␊
+    `
+
+## Invalid #170
+      1 | function foo(){return.0}
+
+> Output
+
+    `␊
+      1 | function foo(){return 0}␊
+    `
+
+> Error 1/1
+
+    `␊
+    > 1 | function foo(){return.0}␊
+        |                       ^ Don't use a zero fraction in the number.␊
+    `
+
+## Invalid #171
+      1 | function foo(){return.0.toString()}
+
+> Output
+
+    `␊
+      1 | function foo(){return(0).toString()}␊
+    `
+
+> Error 1/1
+
+    `␊
+    > 1 | function foo(){return.0.toString()}␊
+        |                       ^ Don't use a zero fraction in the number.␊
+    `
+
+## Invalid #172
+      1 | console.log()
+      2 | .0.toString()
+
+> Output
+
+    `␊
+      1 | console.log()␊
+      2 | ;(0).toString()␊
+    `
+
+> Error 1/1
+
+    `␊
+      1 | console.log()␊
+    > 2 | .0.toString()␊
+        |  ^ Don't use a zero fraction in the number.␊
+    `
diff --git a/test/snapshots/no-zero-fractions.mjs.snap b/test/snapshots/no-zero-fractions.mjs.snap
index a33da6eed8..91eb924e6c 100644
Binary files a/test/snapshots/no-zero-fractions.mjs.snap and b/test/snapshots/no-zero-fractions.mjs.snap differ