From e86bef1663275e1fddb45d98b0a3223c19a18d7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 11 Apr 2024 20:55:40 +0200 Subject: [PATCH] turn on by default --- docs/rules/prefer-number-properties.md | 2 +- rules/prefer-number-properties.js | 4 +- test/prefer-number-properties.mjs | 59 ++++++++++++-------------- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/docs/rules/prefer-number-properties.md b/docs/rules/prefer-number-properties.md index 947e2a997c..6f46fd0442 100644 --- a/docs/rules/prefer-number-properties.md +++ b/docs/rules/prefer-number-properties.md @@ -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`. diff --git a/rules/prefer-number-properties.js b/rules/prefer-number-properties.js index 4346647ed9..e4acad1854 100644 --- a/rules/prefer-number-properties.js +++ b/rules/prefer-number-properties.js @@ -80,7 +80,7 @@ const create = context => { checkNaN, } = { checkInfinity: false, - checkNaN: false, + checkNaN: true, ...context.options[0], }; const {sourceCode} = context; @@ -117,7 +117,7 @@ const schema = [ }, checkNaN: { type: 'boolean', - default: false, + default: true, }, }, }, diff --git a/test/prefer-number-properties.mjs b/test/prefer-number-properties.mjs index b38f7428a3..9c218c3e43 100644 --- a/test/prefer-number-properties.mjs +++ b/test/prefer-number-properties.mjs @@ -209,13 +209,6 @@ function withCheckInfinity(code) { }; } -function withCheckNaN(code) { - return { - code, - options: [{checkNaN: true}], - }; -} - test({ valid: [ 'const foo = Number.NaN;', @@ -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, }, @@ -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, }, @@ -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, }, @@ -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)) {}'), @@ -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}'), @@ -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