Skip to content

Commit

Permalink
require-number-to-fixed-digits-argument: Ignore .toFixed from new…
Browse files Browse the repository at this point in the history
… expression (#1601)
  • Loading branch information
fisker committed Nov 11, 2021
1 parent f93b4be commit 20d61e7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
13 changes: 8 additions & 5 deletions rules/require-number-to-fixed-digits-argument.js
@@ -1,16 +1,19 @@
'use strict';
const {methodCallSelector} = require('./selectors/index.js');
const {methodCallSelector, not} = require('./selectors/index.js');
const {appendArgument} = require('./fix/index.js');

const MESSAGE_ID = 'require-number-to-fixed-digits-argument';
const messages = {
[MESSAGE_ID]: 'Missing the digits argument.',
};

const mathToFixed = methodCallSelector({
method: 'toFixed',
argumentsLength: 0,
});
const mathToFixed = [
methodCallSelector({
method: 'toFixed',
argumentsLength: 0,
}),
not('[callee.object.type="NewExpression"]'),
].join('');

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
Expand Down
8 changes: 8 additions & 0 deletions test/require-number-to-fixed-digits-argument.mjs
Expand Up @@ -13,9 +13,17 @@ test.snapshot({
'number?.toFixed()',
'number.toFixed?.()',
'number.notToFixed();',

// `callee` is a `NewExpression`
'new BigNumber(1).toFixed()',
'new Number(1).toFixed()',
],
invalid: [
'const string = number.toFixed();',
'const string = number.toFixed( /* comment */ );',
'Number(1).toFixed()',

// False positive cases
'const bigNumber = new BigNumber(1); const string = bigNumber.toFixed();',
],
});
32 changes: 32 additions & 0 deletions test/snapshots/require-number-to-fixed-digits-argument.mjs.md
Expand Up @@ -35,3 +35,35 @@ Generated by [AVA](https://avajs.dev).
> 1 | const string = number.toFixed( /* comment */ );␊
| ^^^^^^^^^^^^^^^^^ Missing the digits argument.␊
`

## Invalid #3
1 | Number(1).toFixed()

> Output
`␊
1 | Number(1).toFixed(0)␊
`

> Error 1/1
`␊
> 1 | Number(1).toFixed()␊
| ^^ Missing the digits argument.␊
`

## Invalid #4
1 | const bigNumber = new BigNumber(1); const string = bigNumber.toFixed();

> Output
`␊
1 | const bigNumber = new BigNumber(1); const string = bigNumber.toFixed(0);␊
`

> Error 1/1
`␊
> 1 | const bigNumber = new BigNumber(1); const string = bigNumber.toFixed();␊
| ^^ Missing the digits argument.␊
`
Binary file modified test/snapshots/require-number-to-fixed-digits-argument.mjs.snap
Binary file not shown.

0 comments on commit 20d61e7

Please sign in to comment.