Skip to content

Commit

Permalink
turn on by default
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Apr 11, 2024
1 parent f544b63 commit e86bef1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docs/rules/prefer-number-properties.md
Expand Up @@ -130,7 +130,7 @@ const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INF
### checkNaN

Type: `boolean`\
Default: `false`
Default: `true`

Pass `checkNaN: false` to disable check on `NaN`.

Expand Down
4 changes: 2 additions & 2 deletions rules/prefer-number-properties.js
Expand Up @@ -80,7 +80,7 @@ const create = context => {
checkNaN,
} = {
checkInfinity: false,
checkNaN: false,
checkNaN: true,
...context.options[0],
};
const {sourceCode} = context;
Expand Down Expand Up @@ -117,7 +117,7 @@ const schema = [
},
checkNaN: {
type: 'boolean',
default: false,
default: true,
},
},
},
Expand Down
59 changes: 27 additions & 32 deletions test/prefer-number-properties.mjs
Expand Up @@ -209,13 +209,6 @@ function withCheckInfinity(code) {
};
}

function withCheckNaN(code) {
return {
code,
options: [{checkNaN: true}],
};
}

test({
valid: [
'const foo = Number.NaN;',
Expand Down Expand Up @@ -288,52 +281,54 @@ test({
'class Foo { Infinity(){}}',
'const foo = Infinity;',
'const foo = -Infinity;',
'const foo = NaN;',
'const foo = -NaN;',
{
code: 'const foo = NaN',
options: [{checkNaN: false}],
},
],
invalid: [
{
...withCheckNaN('const foo = NaN;'),
code: 'const foo = NaN;',
output: 'const foo = Number.NaN;',
errors: errorNaN,
},
{
...withCheckNaN('if (Number.isNaN(NaN)) {}'),
code: 'if (Number.isNaN(NaN)) {}',
output: 'if (Number.isNaN(Number.NaN)) {}',
errors: errorNaN,
},
{
...withCheckNaN('if (Object.is(foo, NaN)) {}'),
code: 'if (Object.is(foo, NaN)) {}',
output: 'if (Object.is(foo, Number.NaN)) {}',
errors: errorNaN,
},
{
...withCheckNaN('const foo = bar[NaN];'),
code: 'const foo = bar[NaN];',
output: 'const foo = bar[Number.NaN];',
errors: errorNaN,
},
{
...withCheckNaN('const foo = {NaN};'),
code: 'const foo = {NaN};',
output: 'const foo = {NaN: Number.NaN};',
errors: errorNaN,
},
{
...withCheckNaN('const foo = {NaN: NaN};'),
code: 'const foo = {NaN: NaN};',
output: 'const foo = {NaN: Number.NaN};',
errors: errorNaN,
},
{
...withCheckNaN('const {foo = NaN} = {};'),
code: 'const {foo = NaN} = {};',
output: 'const {foo = Number.NaN} = {};',
errors: errorNaN,
},
{
...withCheckNaN('const foo = NaN.toString();'),
code: 'const foo = NaN.toString();',
output: 'const foo = Number.NaN.toString();',
errors: errorNaN,
},
{
...withCheckNaN('class Foo3 {[NaN] = 1}'),
code: 'class Foo3 {[NaN] = 1}',
output: 'class Foo3 {[Number.NaN] = 1}',
errors: errorNaN,
},
Expand All @@ -356,7 +351,7 @@ test.babel({
],
invalid: [
{
...withCheckNaN('class Foo2 {[NaN] = 1}'),
code: 'class Foo2 {[NaN] = 1}',
output: 'class Foo2 {[Number.NaN] = 1}',
errors: 1,
},
Expand Down Expand Up @@ -391,7 +386,7 @@ test.typescript({
],
invalid: [
{
...withCheckNaN('class Foo {[NaN] = 1}'),
code: 'class Foo {[NaN] = 1}',
output: 'class Foo {[Number.NaN] = 1}',
errors: 1,
},
Expand All @@ -405,11 +400,11 @@ test.snapshot({
'const foo = -(--Infinity);',
],
invalid: [
withCheckNaN('const foo = {[NaN]: 1}'),
withCheckNaN('const foo = {[NaN]() {}}'),
withCheckNaN('foo[NaN] = 1;'),
withCheckNaN('class A {[NaN](){}}'),
withCheckNaN('foo = {[NaN]: 1}'),
'const foo = {[NaN]: 1}',
'const foo = {[NaN]() {}}',
'foo[NaN] = 1;',
'class A {[NaN](){}}',
'foo = {[NaN]: 1}',
withCheckInfinity('const foo = Infinity;'),
withCheckInfinity('if (Number.isNaN(Infinity)) {}'),
withCheckInfinity('if (Object.is(foo, Infinity)) {}'),
Expand All @@ -432,12 +427,12 @@ test.snapshot({
withCheckInfinity('const foo = 1 - -Infinity;'),
withCheckInfinity('const isPositiveZero = value => value === 0 && 1 / value === Infinity;'),
withCheckInfinity('const isNegativeZero = value => value === 0 && 1 / value === -Infinity;'),
withCheckNaN('const {a = NaN} = {};'),
withCheckNaN('const {[NaN]: a = NaN} = {};'),
withCheckNaN('const [a = NaN] = [];'),
withCheckNaN('function foo({a = NaN}) {}'),
withCheckNaN('function foo({[NaN]: a = NaN}) {}'),
withCheckNaN('function foo([a = NaN]) {}'),
'const {a = NaN} = {};',
'const {[NaN]: a = NaN} = {};',
'const [a = NaN] = [];',
'function foo({a = NaN}) {}',
'function foo({[NaN]: a = NaN}) {}',
'function foo([a = NaN]) {}',

// Space after keywords
withCheckInfinity('function foo() {return-Infinity}'),
Expand All @@ -450,7 +445,7 @@ test.snapshot({
'global.parseFloat(foo);',
'window.parseFloat(foo);',
'self.parseFloat(foo);',
withCheckNaN('globalThis.NaN'),
'globalThis.NaN',
withCheckInfinity('-globalThis.Infinity'),

// Not a call
Expand Down

0 comments on commit e86bef1

Please sign in to comment.