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

prefer-number-properties: Don't require by default for Infinity/-Infinity to be written as Number.POSITIVE_INFINITY/Number.NEGATIVE_INFINITY #2312

Merged
merged 6 commits into from Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions docs/rules/prefer-number-properties.md
Expand Up @@ -89,9 +89,9 @@ Type: `object`
### checkInfinity

Type: `boolean`\
Richienb marked this conversation as resolved.
Show resolved Hide resolved
Default: `true`
Default: `false`

Pass `checkInfinity: false` to disable check on `Infinity`.
Pass `checkInfinity: true` to enable check on `Infinity`.

#### Fail

Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-number-properties.js
Expand Up @@ -78,7 +78,7 @@ const create = context => {
const {
checkInfinity,
} = {
checkInfinity: true,
checkInfinity: false,
...context.options[0],
};
const {sourceCode} = context;
Expand Down
69 changes: 35 additions & 34 deletions test/prefer-number-properties.mjs
Expand Up @@ -182,6 +182,13 @@ const errorNaN = [
},
];

function withCheckInfinity(code) {
return {
code,
options: [{checkInfinity: true}],
};
}

test({
valid: [
'const foo = Number.NaN;',
Expand Down Expand Up @@ -252,14 +259,8 @@ test({
'function Infinity() {}',
'class Infinity {}',
'class Foo { Infinity(){}}',
{
code: 'const foo = Infinity;',
options: [{checkInfinity: false}],
},
{
code: 'const foo = -Infinity;',
options: [{checkInfinity: false}],
},
'const foo = Infinity;',
'const foo = -Infinity;',
],
invalid: [
{
Expand Down Expand Up @@ -307,6 +308,8 @@ test({
output: 'class Foo3 {[Number.NaN] = 1}',
errors: errorNaN,
},
withCheckInfinity('const foo = Infinity;'),
withCheckInfinity('const foo = -Infinity;'),
],
});

Expand Down Expand Up @@ -370,30 +373,28 @@ test.snapshot({
'foo[NaN] = 1;',
'class A {[NaN](){}}',
'foo = {[NaN]: 1}',

'const foo = Infinity;',
'if (Number.isNaN(Infinity)) {}',
'if (Object.is(foo, Infinity)) {}',
'const foo = bar[Infinity];',
'const foo = {Infinity};',
'const foo = {Infinity: Infinity};',
'const foo = {[Infinity]: -Infinity};',
'const foo = {[-Infinity]: Infinity};',
'const foo = {Infinity: -Infinity};',
'const {foo = Infinity} = {};',
'const {foo = -Infinity} = {};',
'const foo = Infinity.toString();',
'const foo = -Infinity.toString();',
'const foo = (-Infinity).toString();',
'const foo = +Infinity;',
'const foo = +-Infinity;',
'const foo = -Infinity;',
'const foo = -(-Infinity);',
'const foo = 1 - Infinity;',
'const foo = 1 - -Infinity;',
'const isPositiveZero = value => value === 0 && 1 / value === Infinity;',
'const isNegativeZero = value => value === 0 && 1 / value === -Infinity;',

withCheckInfinity('const foo = Infinity;'),
withCheckInfinity('if (Number.isNaN(Infinity)) {}'),
withCheckInfinity('if (Object.is(foo, Infinity)) {}'),
withCheckInfinity('const foo = bar[Infinity];'),
withCheckInfinity('const foo = {Infinity};'),
withCheckInfinity('const foo = {Infinity: Infinity};'),
withCheckInfinity('const foo = {[Infinity]: -Infinity};'),
withCheckInfinity('const foo = {[-Infinity]: Infinity};'),
withCheckInfinity('const foo = {Infinity: -Infinity};'),
withCheckInfinity('const {foo = Infinity} = {};'),
withCheckInfinity('const {foo = -Infinity} = {};'),
withCheckInfinity('const foo = Infinity.toString();'),
withCheckInfinity('const foo = -Infinity.toString();'),
withCheckInfinity('const foo = (-Infinity).toString();'),
withCheckInfinity('const foo = +Infinity;'),
withCheckInfinity('const foo = +-Infinity;'),
withCheckInfinity('const foo = -Infinity;'),
withCheckInfinity('const foo = -(-Infinity);'),
withCheckInfinity('const foo = 1 - Infinity;'),
withCheckInfinity('const foo = 1 - -Infinity;'),
withCheckInfinity('const isPositiveZero = value => value === 0 && 1 / value === Infinity;'),
withCheckInfinity('const isNegativeZero = value => value === 0 && 1 / value === -Infinity;'),
'const {a = NaN} = {};',
'const {[NaN]: a = NaN} = {};',
'const [a = NaN] = [];',
Expand All @@ -402,7 +403,7 @@ test.snapshot({
'function foo([a = NaN]) {}',

// Space after keywords
'function foo() {return-Infinity}',
withCheckInfinity('function foo() {return-Infinity}'),

'globalThis.isNaN(foo);',
'global.isNaN(foo);',
Expand All @@ -413,7 +414,7 @@ test.snapshot({
'window.parseFloat(foo);',
'self.parseFloat(foo);',
'globalThis.NaN',
'-globalThis.Infinity',
withCheckInfinity('-globalThis.Infinity'),

// Not a call
outdent`
Expand Down