From 8d3b03b2f0e23c1d685eb568dacd1615d3b6686c Mon Sep 17 00:00:00 2001 From: electrohyun Date: Sat, 5 Sep 2026 10:52:00 +0900 Subject: [PATCH 1/4] fix: report JSON5 `Infinity` values in `no-unsafe-values` --- src/rules/no-unsafe-values.js | 9 ++++++++ tests/rules/no-unsafe-values.test.js | 32 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/rules/no-unsafe-values.js b/src/rules/no-unsafe-values.js index 75007d43..3a7931b6 100644 --- a/src/rules/no-unsafe-values.js +++ b/src/rules/no-unsafe-values.js @@ -127,6 +127,15 @@ export default /** @satisfies {NoUnsafeValuesRuleDefinition} */ ({ } } }, + Infinity(node) { + const value = context.sourceCode.getText(node); + + context.report({ + loc: node.loc, + messageId: "unsafeNumber", + data: { value }, + }); + }, String(node) { if (node.value.isWellFormed) { if (node.value.isWellFormed()) { diff --git a/tests/rules/no-unsafe-values.test.js b/tests/rules/no-unsafe-values.test.js index 93e9f6c3..4374d8f7 100644 --- a/tests/rules/no-unsafe-values.test.js +++ b/tests/rules/no-unsafe-values.test.js @@ -106,6 +106,38 @@ ruleTester.run("no-unsafe-values", rule, { }, ], }, + { + code: "Infinity", + language: "json/json5", + errors: [ + { + messageId: "unsafeNumber", + data: { + value: "Infinity", + }, + line: 1, + column: 1, + endLine: 1, + endColumn: 9, + }, + ], + }, + { + code: "-Infinity", + language: "json/json5", + errors: [ + { + messageId: "unsafeNumber", + data: { + value: "-Infinity", + }, + line: 1, + column: 1, + endLine: 1, + endColumn: 10, + }, + ], + }, { code: '"\ud83d"', errors: [ From 00020af4cc7762c80c83262171b8b523fb2731dd Mon Sep 17 00:00:00 2001 From: electrohyun Date: Tue, 15 Sep 2026 09:37:12 +0900 Subject: [PATCH 2/4] docs: clarify JSON5 `Infinity` and `NaN` handling in `no-unsafe-values` --- docs/rules/no-unsafe-values.md | 2 + src/rules/no-unsafe-values.js | 9 ----- tests/rules/no-unsafe-values.test.js | 56 ++++++++++++---------------- 3 files changed, 26 insertions(+), 41 deletions(-) diff --git a/docs/rules/no-unsafe-values.md b/docs/rules/no-unsafe-values.md index 49c817a0..8f902aac 100644 --- a/docs/rules/no-unsafe-values.md +++ b/docs/rules/no-unsafe-values.md @@ -18,6 +18,8 @@ These issues can lead to data corruption, silent failures, or inconsistent behav This rule warns on values that are unsafe for interchange, such as strings with unmatched [surrogates](https://en.wikipedia.org/wiki/UTF-16), numbers that evaluate to Infinity, numbers that evaluate to zero unintentionally, numbers that look like integers but are too large, and [subnormal numbers](https://en.wikipedia.org/wiki/Subnormal_number). +For JSON5, this rule does not report explicit `Infinity` and `NaN` values because the specification defines how they are interpreted. These values differ from numeric literals such as `1e400`, which evaluate to `Infinity` because they exceed JavaScript's number range. + Examples of **incorrect** code for this rule: diff --git a/src/rules/no-unsafe-values.js b/src/rules/no-unsafe-values.js index 3a7931b6..75007d43 100644 --- a/src/rules/no-unsafe-values.js +++ b/src/rules/no-unsafe-values.js @@ -127,15 +127,6 @@ export default /** @satisfies {NoUnsafeValuesRuleDefinition} */ ({ } } }, - Infinity(node) { - const value = context.sourceCode.getText(node); - - context.report({ - loc: node.loc, - messageId: "unsafeNumber", - data: { value }, - }); - }, String(node) { if (node.value.isWellFormed) { if (node.value.isWellFormed()) { diff --git a/tests/rules/no-unsafe-values.test.js b/tests/rules/no-unsafe-values.test.js index 4374d8f7..11c8114e 100644 --- a/tests/rules/no-unsafe-values.test.js +++ b/tests/rules/no-unsafe-values.test.js @@ -74,6 +74,30 @@ ruleTester.run("no-unsafe-values", rule, { code: "0.", language: "json/json5", }, + { + code: "Infinity", + language: "json/json5", + }, + { + code: "+Infinity", + language: "json/json5", + }, + { + code: "-Infinity", + language: "json/json5", + }, + { + code: "NaN", + language: "json/json5", + }, + { + code: "+NaN", + language: "json/json5", + }, + { + code: "-NaN", + language: "json/json5", + }, ], invalid: [ { @@ -106,38 +130,6 @@ ruleTester.run("no-unsafe-values", rule, { }, ], }, - { - code: "Infinity", - language: "json/json5", - errors: [ - { - messageId: "unsafeNumber", - data: { - value: "Infinity", - }, - line: 1, - column: 1, - endLine: 1, - endColumn: 9, - }, - ], - }, - { - code: "-Infinity", - language: "json/json5", - errors: [ - { - messageId: "unsafeNumber", - data: { - value: "-Infinity", - }, - line: 1, - column: 1, - endLine: 1, - endColumn: 10, - }, - ], - }, { code: '"\ud83d"', errors: [ From 5f8cb2018578f32ac1728ce7225bfcd52373b841 Mon Sep 17 00:00:00 2001 From: electrohyun Date: Tue, 15 Sep 2026 13:42:03 +0900 Subject: [PATCH 3/4] docs: clarify JSON5 special values note Co-authored-by: lumir --- docs/rules/no-unsafe-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-unsafe-values.md b/docs/rules/no-unsafe-values.md index 8f902aac..dbac5574 100644 --- a/docs/rules/no-unsafe-values.md +++ b/docs/rules/no-unsafe-values.md @@ -18,7 +18,7 @@ These issues can lead to data corruption, silent failures, or inconsistent behav This rule warns on values that are unsafe for interchange, such as strings with unmatched [surrogates](https://en.wikipedia.org/wiki/UTF-16), numbers that evaluate to Infinity, numbers that evaluate to zero unintentionally, numbers that look like integers but are too large, and [subnormal numbers](https://en.wikipedia.org/wiki/Subnormal_number). -For JSON5, this rule does not report explicit `Infinity` and `NaN` values because the specification defines how they are interpreted. These values differ from numeric literals such as `1e400`, which evaluate to `Infinity` because they exceed JavaScript's number range. +NOTE: For JSON5, this rule does not report explicit [`Infinity` and `NaN`](https://spec.json5.org/#summary-of-features-numbers) values because the specification defines how they are interpreted. These values differ from numeric literals such as `1e400`, which evaluate to `Infinity` because they exceed JavaScript's number range. Examples of **incorrect** code for this rule: From 73d2587d8801188f2ed2a18327d6efde36babcf3 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 23 Sep 2026 13:23:53 -0400 Subject: [PATCH 4/4] Apply suggestion from @nzakas --- docs/rules/no-unsafe-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-unsafe-values.md b/docs/rules/no-unsafe-values.md index dbac5574..b97abb09 100644 --- a/docs/rules/no-unsafe-values.md +++ b/docs/rules/no-unsafe-values.md @@ -18,7 +18,7 @@ These issues can lead to data corruption, silent failures, or inconsistent behav This rule warns on values that are unsafe for interchange, such as strings with unmatched [surrogates](https://en.wikipedia.org/wiki/UTF-16), numbers that evaluate to Infinity, numbers that evaluate to zero unintentionally, numbers that look like integers but are too large, and [subnormal numbers](https://en.wikipedia.org/wiki/Subnormal_number). -NOTE: For JSON5, this rule does not report explicit [`Infinity` and `NaN`](https://spec.json5.org/#summary-of-features-numbers) values because the specification defines how they are interpreted. These values differ from numeric literals such as `1e400`, which evaluate to `Infinity` because they exceed JavaScript's number range. +When run on JSON5 code, this rule does not report explicit [`Infinity` and `NaN`](https://spec.json5.org/#summary-of-features-numbers) values because the specification defines how they are interpreted. These values differ from numeric literals such as `1e400`, which evaluate to `Infinity` because they exceed JavaScript's number range. Examples of **incorrect** code for this rule: