Skip to content

Commit fcca35d

Browse files
authored
no-zero-fractions: Handle .0 correctly (#1444)
1 parent 179b7df commit fcca35d

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

rules/no-zero-fractions.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {isParenthesized} = require('eslint-utils');
33
const needsSemicolon = require('./utils/needs-semicolon.js');
44
const {isNumber, isDecimalInteger} = require('./utils/numeric.js');
55
const toLocation = require('./utils/to-location.js');
6+
const {fixSpaceAroundKeyword} = require('./fix/index.js');
67

78
const MESSAGE_ZERO_FRACTION = 'zero-fraction';
89
const MESSAGE_DANGLING_DOT = 'dangling-dot';
@@ -26,7 +27,8 @@ const create = context => {
2627
}
2728

2829
const {before, dotAndFractions, after} = match.groups;
29-
const formatted = before + dotAndFractions.replace(/[.0_]+$/g, '') + after;
30+
const fixedDotAndFractions = dotAndFractions.replace(/[.0_]+$/g, '');
31+
const formatted = ((before + fixedDotAndFractions) || '0') + after;
3032

3133
if (formatted === raw) {
3234
return;
@@ -40,7 +42,7 @@ const create = context => {
4042
return {
4143
loc: toLocation([start, end], sourceCode),
4244
messageId: isDanglingDot ? MESSAGE_DANGLING_DOT : MESSAGE_ZERO_FRACTION,
43-
fix: fixer => {
45+
* fix(fixer) {
4446
let fixed = formatted;
4547
if (
4648
node.parent.type === 'MemberExpression' &&
@@ -55,7 +57,8 @@ const create = context => {
5557
}
5658
}
5759

58-
return fixer.replaceText(node, fixed);
60+
yield fixer.replaceText(node, fixed);
61+
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
5962
},
6063
};
6164
},

test/no-zero-fractions.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,13 @@ test.snapshot({
8080
console.log()
8181
a[1.00e10].toString()
8282
`,
83+
'a = .0;',
84+
'a = .0.toString()',
85+
'function foo(){return.0}',
86+
'function foo(){return.0.toString()}',
87+
outdent`
88+
console.log()
89+
.0.toString()
90+
`,
8391
],
8492
});

test/snapshots/no-zero-fractions.mjs.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,3 +2687,86 @@ Generated by [AVA](https://avajs.dev).
26872687
> 2 | a[1.00e10].toString()␊
26882688
| ^^^ Don't use a zero fraction in the number.␊
26892689
`
2690+
2691+
## Invalid #168
2692+
1 | a = .0;
2693+
2694+
> Output
2695+
2696+
`␊
2697+
1 | a = 0;␊
2698+
`
2699+
2700+
> Error 1/1
2701+
2702+
`␊
2703+
> 1 | a = .0;␊
2704+
| ^ Don't use a zero fraction in the number.␊
2705+
`
2706+
2707+
## Invalid #169
2708+
1 | a = .0.toString()
2709+
2710+
> Output
2711+
2712+
`␊
2713+
1 | a = (0).toString()␊
2714+
`
2715+
2716+
> Error 1/1
2717+
2718+
`␊
2719+
> 1 | a = .0.toString()␊
2720+
| ^ Don't use a zero fraction in the number.␊
2721+
`
2722+
2723+
## Invalid #170
2724+
1 | function foo(){return.0}
2725+
2726+
> Output
2727+
2728+
`␊
2729+
1 | function foo(){return 0}␊
2730+
`
2731+
2732+
> Error 1/1
2733+
2734+
`␊
2735+
> 1 | function foo(){return.0}␊
2736+
| ^ Don't use a zero fraction in the number.␊
2737+
`
2738+
2739+
## Invalid #171
2740+
1 | function foo(){return.0.toString()}
2741+
2742+
> Output
2743+
2744+
`␊
2745+
1 | function foo(){return(0).toString()}␊
2746+
`
2747+
2748+
> Error 1/1
2749+
2750+
`␊
2751+
> 1 | function foo(){return.0.toString()}␊
2752+
| ^ Don't use a zero fraction in the number.␊
2753+
`
2754+
2755+
## Invalid #172
2756+
1 | console.log()
2757+
2 | .0.toString()
2758+
2759+
> Output
2760+
2761+
`␊
2762+
1 | console.log()␊
2763+
2 | ;(0).toString()␊
2764+
`
2765+
2766+
> Error 1/1
2767+
2768+
`␊
2769+
1 | console.log()␊
2770+
> 2 | .0.toString()␊
2771+
| ^ Don't use a zero fraction in the number.␊
2772+
`
241 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)