Skip to content

Commit

Permalink
no-zero-fractions: Handle .0 correctly (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jul 26, 2021
1 parent 179b7df commit fcca35d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
9 changes: 6 additions & 3 deletions rules/no-zero-fractions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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' &&
Expand All @@ -55,7 +57,8 @@ const create = context => {
}
}

return fixer.replaceText(node, fixed);
yield fixer.replaceText(node, fixed);
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
},
};
},
Expand Down
8 changes: 8 additions & 0 deletions test/no-zero-fractions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
`,
],
});
83 changes: 83 additions & 0 deletions test/snapshots/no-zero-fractions.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.␊
`
Binary file modified test/snapshots/no-zero-fractions.mjs.snap
Binary file not shown.

0 comments on commit fcca35d

Please sign in to comment.