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

Add prefer-number-is-integer rule #1509

Conversation

tristanHessell
Copy link
Contributor

Fixes #1460.

One thing to note is that I couldn't run npm run integration to completion as my computer only has 4GB RAM.

'[left.callee.type="MemberExpression"]',
'[left.callee.object.name="Math"]',
'[left.callee.property.name="round"]',
'[left.arguments.0.type="Identifier"]',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should limit this to Identifier, it should work for anything, just make sure the argument and the right side are the same reference, you can use isSameReference.

And don't auto-fix if the expression has side effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker I've updated the PR to use isSameReference.

Can you give me an example of an expression with a side effect that fits here? So I can TDD against it :)

@tristanHessell
Copy link
Contributor Author

Thanks for the feedback on PR, I'll get onto updating this PR over the next week :)

@fisker
Copy link
Collaborator

fisker commented Nov 5, 2021

Sorry for the long delay, overall looks good. But I'm not sure if we want check == cases.

Can you fix tests?

@TristanHessellHyper
Copy link

Happy to make the changes! Will get it done over the next week or so

equalsSelector,
].join('');

// ParseInt(value,10) === value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// ParseInt(value,10) === value
// `Number.parseInt(value, 10) === value`

Let's forget about parseInt, only check Number.parseInt

equalsSelector,
].join('');

// _.isInteger(value)
Copy link
Collaborator

@fisker fisker Dec 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use methodCallSelector, search how to use it in other rules.

'[callee.property.name="isInteger"]',
].join('');

// Math.round(value) === value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use methodCallSelector,

@sindresorhus
Copy link
Owner

Bump :)

@tristanHessell
Copy link
Contributor Author

@sindresorhus appreciated! I had forgot about this PR.

Working on it over the next couple of days


default: {
return '';
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus what should we do here?

I've added the default case as to appease linting, but the calling code protects us from this branch being taken.

How should I approach appeasing Codecov?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use an ignore comment:

/* c8 ignore next 3 */

@tristanHessell
Copy link
Contributor Author

@sindresorhus et al, this is ready or review :)

return node => {
const variable = variableGetter(node);

if (!variable) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not limit this to Identifier and MemberExpression, should allow anything

_.isInteger(a ? b : c)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus
Copy link
Owner

@tristanHessell Bump

@sindresorhus
Copy link
Owner

Closing for lack of response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: prefer-number-is-integer
5 participants