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
Breaking: some rules recognize bigint literals (fixes #11803) #12701
Conversation
Does a separate new rule to disallow
Can this happen with some other rules as well? For example, it looks like if some new global gets added to
Just some thoughts, but probably neither of the following:
Also some rules if |
I'm not sure if disallowing
Yes. Pinning the In fact, I like RFC9's solution; deprecating
Thank you!
Right. It's not implicit type conversion.
Right. Probably,
Wait. Indeed, all of Node.js, Chrome, and Firefox throw syntax error. But I'm not sure if it's a defined behavior in the spec. PropertyName > LiteralPropertyName > NumericLiteral, then it contains BigInt literals. And I couldn't find any early errors nor runtime errors for BigInt literals. |
I think you're right, there is nothing to prevent a BigInt literal from being a property name in an object literal. What's an appropriate place to report an issue/question about the specification, such as this one? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
lib/rules/no-magic-numbers.js
Outdated
@@ -65,7 +65,7 @@ module.exports = { | |||
* @returns {boolean} true if the node is a number literal | |||
*/ | |||
function isNumber(node) { | |||
return typeof node.value === "number"; | |||
return typeof node.value === "number" || Boolean(node.bigint); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker: For my own understanding, is there a reason we're checking the presence of this property versus using typeof
like we are below? I wonder if it would be nice to add astUtils.isNumber()
and use it everywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have noticed after I wrote this, it's no problem if it's typeof node.value === "number" || typeof node.value === "bigint"
.
The reason for checking node.bigint
was that node.value
will be null if the runtime doesn't support bigint natively. However, Node.js 10.4.0 supports bigint and ESLint 7 requires 10.11.0, so it's not a problem.
astUtils.isNumeric()
or someting like is a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does bigint support matter tho? typeof x === 'bigint'
will just be false
in an engine that doesn't support bigint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a source code 1n
is given, ESLint expects the AST to be { type: "Literal", value: 1n }
. However, if the runtime doesn't support BigInt natively, the AST will be { type: "Literal", value: null }
then typeof node.value
becomes a wrong value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that seems like a pretty big flaw in the thing that generates the AST - it should probably supply enough information to know that the value isn't a literal null.
Maybe @ljharb could help us or point us to where we might be able to find this out? |
It would be treated as a string in the literal case, i believe - only strings can be non computed property keys. Why would the yoda rule changes require node 10.4? Syntactic bigint support should not be necessary for anything. |
As the language spec, if a property name is a In fact, Following the spec, it should allow
Because the |
Indeed, it would toString them, but since the toString of |
If A problem is that the rule would start to require (and produce in autofix) code that seems to be valid by the spec but doesn't work in real engines. |
Re |
# Conflicts: # lib/rules/utils/ast-utils.js # tests/lib/rules/yoda.js
In the /*eslint quote-props: ["error", "as-needed", { "numbers": true }]*/
var foo = {
123n: bar // no error at the moment
} |
I'm thinking about I'm leaning toward that |
|
Looks good except for JSON config files. I'm also not sure about yml and config comments, e.g., it throws a config error when I try this test case on this branch: {
code: "/*eslint no-magic-numbers: ['error', { 'ignore': [100n] }]*/ f(100n)",
parserOptions: { ecmaVersion: 2020 }
} Perhaps we could just say that the feature to ignore some bigints is supported in js configs only? Or maybe provide some alternative.
👍 major version looks like a good time to do this.
I agree that As for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just leaving one question about adopting the JSON metaschema, otherwise LGTM. Thanks!
For json configs, could the ignore list accept string versions of bigint literals? Like |
I think so. It's surprising a bit to me. |
A side note: I reported about BigInt literals as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Unrelated bug, no-magic-number allows 100 .toString()
as array index.
…slint#12701) * update no-magic-numbers to recognize bigint * update yoda to recognize bigint * add a no-extend-native test * update ci.yml temporary (this PR is blocked by eslint#12700) * add astUtils.isNumericLiteral and use it in some rules * update no-dupe-class-members * update no-magic-number to support bigint in options * update some rules to use getStaticPropertyName * update quote-props * revert no-useless-computed-key change * revert "allowing {type: 'bigint'}" and update no-magic-number * no-magic-number 'ignores' allows negative bigint
What is the purpose of this pull request? (put an "X" next to item)
[X] Changes existing rules (fixes #11803)
What changes did you make? (Give an overview)
This PR adds BigInt support to some rules with the manner that increases warnings.
From the list of #11803:
no-compare-neg-zero
rule: I didn't include the rule in this PR because of this question. Indeed, reporting BigInt literals is different from the purpose of this rule.no-extend-native
rule: As the above comment mentioned, the rule has been updated via a minor update ofglobals
package. I just added a test case. I'm wondering if we should update this rule to not useglobals
package.no-magic-number
rule: This rule got recognizing bigint literals.yoda
rule: This rule got recognizing ranges even if there are negative bigint literals. This change requires Node.js 10.4.0 or later. Therefore, this PR is blocked by Breaking: drop Node.js 8 support (refs eslint/rfcs#44) #12700.Is there anything you'd like reviewers to focus on?
no-extend-native
to prevent breaking changes via the minor updates ofglobals
package?