-
-
Notifications
You must be signed in to change notification settings - Fork 923
feat(json_analyze): implement noTopLevelLiterals #9367
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`noTopLevelLiterals`](https://biomejs.dev/linter/rules/no-top-level-literals/). It requires the root-level value to be an array or object. | ||
|
|
||
| **Invalid:** | ||
|
|
||
| ```json | ||
| "just a string" | ||
| ``` |
12 changes: 12 additions & 0 deletions
12
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
crates/biome_json_analyze/src/lint/nursery/no_top_level_literals.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_json_syntax::{AnyJsonValue, JsonRoot}; | ||
| use biome_rowan::AstNode; | ||
| use biome_rule_options::no_top_level_literals::NoTopLevelLiteralsOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Require the JSON top-level value to be an array or object. | ||
| /// | ||
| /// The JSON specification technically allows any JSON value (object, array, string, number, boolean, or null) to be used as the top-level element of a JSON document. | ||
| /// However, some older JSON parsers, especially those created before [RFC 7158](https://datatracker.ietf.org/doc/html/rfc7158)/[4627](https://datatracker.ietf.org/doc/html/rfc4627) was fully adopted, only support objects or arrays as the root element. | ||
| /// | ||
| /// Additionally, some security practices (such as those preventing JSON hijacking attacks) rely on the assumption that the top-level value is an object or array. | ||
| /// Using an object or array at the top level also provides better extensibility for your data structures over time. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```json,expect_diagnostic | ||
| /// "just a string" | ||
| /// ``` | ||
| /// | ||
| /// ```json,expect_diagnostic | ||
| /// 42 | ||
| /// ``` | ||
| /// | ||
| /// ```json,expect_diagnostic | ||
| /// true | ||
| /// ``` | ||
| /// | ||
| /// ```json,expect_diagnostic | ||
| /// null | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```json | ||
| /// { | ||
| /// "property": "value", | ||
| /// "otherProperty": 123 | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ```json | ||
| /// ["element", "anotherElement"] | ||
| /// ``` | ||
| /// | ||
| /// ```json | ||
| /// {} | ||
| /// ``` | ||
| /// | ||
| /// ```json | ||
| /// [] | ||
| /// ``` | ||
| /// | ||
| pub NoTopLevelLiterals { | ||
| version: "next", | ||
| name: "noTopLevelLiterals", | ||
| language: "json", | ||
| recommended: false, | ||
| sources: &[RuleSource::EslintJson("top-level-interop").same()], | ||
| } | ||
| } | ||
|
|
||
| impl Rule for NoTopLevelLiterals { | ||
| type Query = Ast<JsonRoot>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = NoTopLevelLiteralsOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
| let value = node.value().ok()?; | ||
|
|
||
| match value { | ||
| AnyJsonValue::JsonObjectValue(_) => None, | ||
| AnyJsonValue::JsonArrayValue(_) => None, | ||
| _ => Some(()), | ||
| } | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let span = ctx.query().range(); | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| span, | ||
| markup! { | ||
| "Expected the top-level value to be an array or object." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility." | ||
| }), | ||
| ) | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/boolean.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| true |
26 changes: 26 additions & 0 deletions
26
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/boolean.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: boolean.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| true | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| boolean.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Expected the top-level value to be an array or object. | ||
|
|
||
| > 1 │ true | ||
| │ ^^^^ | ||
| 2 │ | ||
|
|
||
| i Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
1 change: 1 addition & 0 deletions
1
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/null.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| null |
26 changes: 26 additions & 0 deletions
26
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/null.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: null.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| null | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| null.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| i Expected the top-level value to be an array or object. | ||
| > 1 │ null | ||
| │ ^^^^ | ||
| 2 │ | ||
| i Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility. | ||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
| ``` |
1 change: 1 addition & 0 deletions
1
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/number.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 42 |
26 changes: 26 additions & 0 deletions
26
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/number.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: number.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| 42 | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| number.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| i Expected the top-level value to be an array or object. | ||
| > 1 │ 42 | ||
| │ ^^ | ||
| 2 │ | ||
| i Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility. | ||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
| ``` |
1 change: 1 addition & 0 deletions
1
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/string.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| "just a string" |
26 changes: 26 additions & 0 deletions
26
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/invalid/string.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: string.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| "just a string" | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| string.json:1:1 lint/nursery/noTopLevelLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Expected the top-level value to be an array or object. | ||
|
|
||
| > 1 │ "just a string" | ||
| │ ^^^^^^^^^^^^^^^ | ||
| 2 │ | ||
|
|
||
| i Some JSON parsers only support objects or arrays as the root element. Wrap your value in an array or object to ensure compatibility. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/array.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [ | ||
| "one", | ||
| "two", | ||
| "three" | ||
| ] |
13 changes: 13 additions & 0 deletions
13
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/array.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: array.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| [ | ||
| "one", | ||
| "two", | ||
| "three" | ||
| ] | ||
| ``` |
1 change: 1 addition & 0 deletions
1
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/empty-array.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [] |
9 changes: 9 additions & 0 deletions
9
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/empty-array.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: empty-array.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| [] | ||
| ``` |
1 change: 1 addition & 0 deletions
1
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/empty-object.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
9 changes: 9 additions & 0 deletions
9
...es/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/empty-object.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: empty-object.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| {} | ||
| ``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/object.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "one": "value", | ||
| "two": "value", | ||
| "three": "value" | ||
| } |
13 changes: 13 additions & 0 deletions
13
crates/biome_json_analyze/tests/specs/nursery/noTopLevelLiterals/valid/object.json.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| source: crates/biome_json_analyze/tests/spec_tests.rs | ||
| expression: object.json | ||
| --- | ||
| # Input | ||
| ```json | ||
| { | ||
| "one": "value", | ||
| "two": "value", | ||
| "three": "value" | ||
| } | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| use biome_deserialize_macros::{Deserializable, Merge}; | ||
| use serde::{Deserialize, Serialize}; | ||
| #[derive(Default, Clone, Debug, Deserialize, Deserializable, Merge, Eq, PartialEq, Serialize)] | ||
| #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
| #[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
| pub struct NoTopLevelLiteralsOptions {} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion | 🟠 Major
Add rustdoc for the options type.
NoTopLevelLiteralsOptionsshould have inline rustdoc so generated docs stay complete and consistent.Suggested patch
+/// Options for the `noTopLevelLiterals` rule. pub struct NoTopLevelLiteralsOptions {}As per coding guidelines, "**/*.rs: Use inline rustdoc documentation for rules, assists, and their options".
📝 Committable suggestion
🤖 Prompt for AI Agents