Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require-number-to-fixed-digits-argument: Ignore .toFixed from new expression #1601

Merged
merged 2 commits into from Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.