Skip to content

Conversation

@baevm
Copy link
Contributor

@baevm baevm commented Oct 21, 2025

Fixes #2733, added support for BigInt('+1'), BigInt('-1'), BigInt(+1), BigInt(-1). Not sure about BigInt(!1) and BigInt(~1), looks like those also valid?

image

@github-actions github-actions bot changed the title fix (prefer-bigint-literals): fix add support for numbers/strings with operators fix (prefer-bigint-literals): fix add support for numbers/strings with operators Oct 21, 2025
@fisker
Copy link
Collaborator

fisker commented Oct 23, 2025

I opened #2733, because I think there are maybe people writes BigInt("-1"), but I don't think anyone would write BigInt(!1), it should be a mistake.

@fisker fisker closed this Oct 23, 2025
@fisker
Copy link
Collaborator

fisker commented Oct 23, 2025

I didn't notice, this is a PR, not an issue. Sorry!

@fisker fisker reopened this Oct 23, 2025

// BigInt("+1") -> 1n
if (raw[0] === '+') {
raw = raw.slice(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add test for BigInt("++1")?

Copy link
Collaborator

Choose a reason for hiding this comment

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

And BigInt("+ 1")

Copy link
Collaborator

@fisker fisker Oct 23, 2025

Choose a reason for hiding this comment

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

BigInt(" -1 ")
BigInt(" +1 ")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added tests:
BigInt("++1") - valid
BigInt("+ 1") - valid

BigInt(" -1 ") - invalid
BigInt(" +1 ") - invalid with suggestion

@fisker
Copy link
Collaborator

fisker commented Oct 23, 2025

Need test -BigInt(-1) and -BigInt("-1"), I think they will fail.

@fisker
Copy link
Collaborator

fisker commented Oct 23, 2025

There can also ASI issue since the fixed code inserts -.

foo // <-- no semicolon here
BigInt("-1")

@baevm
Copy link
Contributor Author

baevm commented Oct 27, 2025

Need test -BigInt(-1) and -BigInt("-1"), I think they will fail.

fixed, added tests

There can also ASI issue since the fixed code inserts -.

foo // <-- no semicolon here
BigInt("-1")

not sure about this, but i think it is working fine? added test for this

@fisker
Copy link
Collaborator

fisker commented Oct 27, 2025

Another test 2n - BigInt("-1")

@sindresorhus
Copy link
Owner

Bump :)

@baevm baevm force-pushed the fix-prefer-bigint-literals branch from 59c1ea2 to 1bd819c Compare January 29, 2026 12:41
@baevm baevm force-pushed the fix-prefer-bigint-literals branch from 1bd819c to b8460db Compare January 29, 2026 12:50
@baevm
Copy link
Contributor Author

baevm commented Jan 29, 2026

Another test 2n - BigInt("-1")

added test case 2n - BigInt("-1") -> 2n - -1n

image

@fisker
Copy link
Collaborator

fisker commented Jan 29, 2026

Sorry, I mean 2n -BigInt("-1"), but should already fixed .

@baevm
Copy link
Contributor Author

baevm commented Jan 30, 2026

Sorry, I mean 2n -BigInt("-1"), but should already fixed .

fixed, 2n -BigInt("-1") -> 2n -(-1n)

also added check for cases like this: +BigInt(1), it throws TypeError so i think it should not report on these

image

@fisker fisker self-assigned this Jan 30, 2026
@fisker
Copy link
Collaborator

fisker commented Jan 30, 2026

I'm taking a final look.

@fisker
Copy link
Collaborator

fisker commented Jan 30, 2026

I pushed some simplification, can you take a look?

'BigInt(1) + BigInt(2)',
'BigInt(1) ** 2n',
'condition ? BigInt(1) : BigInt(2)',
'+BigInt(1)',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same runtime error, this is not what we should care.

+BigInt(1)
VM782:1 Uncaught TypeError: Cannot convert a BigInt value to a number
    at <anonymous>:1:1
(anonymous) @ VM782:1

+1n
VM792:1 Uncaught TypeError: Cannot convert a BigInt value to a number
    at <anonymous>:1:1

@fisker fisker changed the title fix (prefer-bigint-literals): fix add support for numbers/strings with operators prefer-bigint-literals: Support signed numbers and strings Jan 30, 2026
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.

prefer-bigint-literal: Support number with +/- sign

3 participants