From 66becbbd6db9c3829f47f4239e4dc8f92dd62b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Sun, 1 Sep 2024 10:37:16 +0200 Subject: [PATCH 1/2] feat: Include version number in all --json based outputs --- cli/tools/bench/reporters.rs | 4 + cli/tools/doc.rs | 15 ++- cli/tools/info.rs | 15 ++- cli/tools/lint/mod.rs | 27 +++-- cli/tools/lint/reporters.rs | 4 + tests/specs/bench/json_output/pass.json.out | 1 + tests/specs/doc/json/__test__.jsonc | 4 + tests/specs/doc/json/json.js | 2 + tests/specs/doc/json/json.out | 26 +++++ tests/specs/doc/json/types.d.ts | 2 + tests/testdata/doc/lint_success_json.out | 101 +++++++++--------- .../info/076_info_json_deps_order.out | 1 + tests/testdata/info/info_json.out | 1 + tests/testdata/info/info_json_location.out | 1 + tests/testdata/info/json_output/main.out | 1 + .../lint/expected_from_stdin_json.out | 1 + tests/testdata/lint/expected_json.out | 1 + .../lint/with_report_config_override.out | 1 + .../npm/cjs_with_deps/main_info_json.out | 1 + tests/testdata/npm/info/chalk_json.out | 1 + .../main_info_json.out | 1 + 21 files changed, 142 insertions(+), 69 deletions(-) create mode 100644 tests/specs/doc/json/__test__.jsonc create mode 100644 tests/specs/doc/json/json.js create mode 100644 tests/specs/doc/json/json.out create mode 100644 tests/specs/doc/json/types.d.ts diff --git a/cli/tools/bench/reporters.rs b/cli/tools/bench/reporters.rs index 250655be738acf..9aabd760b37310 100644 --- a/cli/tools/bench/reporters.rs +++ b/cli/tools/bench/reporters.rs @@ -18,8 +18,11 @@ pub trait BenchReporter { fn report_uncaught_error(&mut self, origin: &str, error: Box); } +const JSON_SCHEMA_VERSION: u8 = 1; + #[derive(Debug, Serialize)] struct JsonReporterOutput { + version: u8, runtime: String, cpu: String, benches: Vec, @@ -28,6 +31,7 @@ struct JsonReporterOutput { impl Default for JsonReporterOutput { fn default() -> Self { Self { + version: JSON_SCHEMA_VERSION, runtime: format!( "{} {}", version::DENO_VERSION_INFO.user_agent, diff --git a/cli/tools/doc.rs b/cli/tools/doc.rs index ce31c8068476bf..0ba3b84fbba3b7 100644 --- a/cli/tools/doc.rs +++ b/cli/tools/doc.rs @@ -5,8 +5,7 @@ use crate::args::DocHtmlFlag; use crate::args::DocSourceFileFlag; use crate::args::Flags; use crate::colors; -use crate::display::write_json_to_stdout; -use crate::display::write_to_stdout_ignore_sigpipe; +use crate::display; use crate::factory::CliFactory; use crate::graph_util::graph_exit_lock_errors; use crate::tsc::get_types_declaration_file_text; @@ -17,6 +16,7 @@ use deno_config::glob::PathOrPatternSet; use deno_core::anyhow::bail; use deno_core::anyhow::Context; use deno_core::error::AnyError; +use deno_core::serde_json; use deno_doc as doc; use deno_doc::html::UrlResolveKind; use deno_graph::source::NullFileSystem; @@ -31,6 +31,8 @@ use std::collections::BTreeMap; use std::rc::Rc; use std::sync::Arc; +const JSON_SCHEMA_VERSION: u8 = 1; + async fn generate_doc_nodes_for_builtin_types( doc_flags: DocFlags, parser: &dyn ModuleParser, @@ -228,7 +230,11 @@ pub async fn doc( doc_nodes_by_url.into_values().flatten().collect::>(); if doc_flags.json { - write_json_to_stdout(&doc_nodes) + let json_output = serde_json::json!({ + "version": JSON_SCHEMA_VERSION, + "nodes": &doc_nodes + }); + display::write_json_to_stdout(&json_output) } else if doc_flags.lint { // don't output docs if running with only the --lint flag log::info!( @@ -553,7 +559,8 @@ fn print_docs_to_stdout( ) }; - write_to_stdout_ignore_sigpipe(details.as_bytes()).map_err(AnyError::from) + display::write_to_stdout_ignore_sigpipe(details.as_bytes()) + .map_err(AnyError::from) } fn check_diagnostics(diagnostics: &[DocDiagnostic]) -> Result<(), AnyError> { diff --git a/cli/tools/info.rs b/cli/tools/info.rs index a32f9dc453b173..6aa044a924d05a 100644 --- a/cli/tools/info.rs +++ b/cli/tools/info.rs @@ -11,7 +11,6 @@ use deno_core::anyhow::bail; use deno_core::error::AnyError; use deno_core::resolve_url_or_path; use deno_core::serde_json; -use deno_core::serde_json::json; use deno_graph::Dependency; use deno_graph::GraphKind; use deno_graph::Module; @@ -35,6 +34,8 @@ use crate::npm::CliNpmResolver; use crate::npm::ManagedCliNpmResolver; use crate::util::checksum; +const JSON_SCHEMA_VERSION: u8 = 1; + pub async fn info( flags: Arc, info_flags: InfoFlags, @@ -79,7 +80,10 @@ pub async fn info( } if info_flags.json { - let mut json_graph = json!(graph); + let mut json_graph = serde_json::json!(graph); + if let Some(output) = json_graph.as_object_mut() { + output.insert("version".to_string(), JSON_SCHEMA_VERSION.into()); + } add_npm_packages_to_json(&mut json_graph, npm_resolver.as_ref()); display::write_json_to_stdout(&json_graph)?; } else { @@ -121,7 +125,8 @@ fn print_cache_info( let local_storage_dir = origin_dir.join("local_storage"); if json { - let mut output = json!({ + let mut json_output = serde_json::json!({ + "version": JSON_SCHEMA_VERSION, "denoDir": deno_dir, "modulesCache": modules_cache, "npmCache": npm_cache, @@ -131,10 +136,10 @@ fn print_cache_info( }); if location.is_some() { - output["localStorage"] = serde_json::to_value(local_storage_dir)?; + json_output["localStorage"] = serde_json::to_value(local_storage_dir)?; } - display::write_json_to_stdout(&output) + display::write_json_to_stdout(&json_output) } else { println!("{} {}", colors::bold("DENO_DIR location:"), deno_dir); println!( diff --git a/cli/tools/lint/mod.rs b/cli/tools/lint/mod.rs index 5b51b2b40c045b..d5d174bf75252a 100644 --- a/cli/tools/lint/mod.rs +++ b/cli/tools/lint/mod.rs @@ -45,6 +45,7 @@ use crate::colors; use crate::factory::CliFactory; use crate::graph_util::ModuleGraphCreator; use crate::tools::fmt::run_parallelized; +use crate::util::display; use crate::util::file_watcher; use crate::util::fs::canonicalize_path; use crate::util::path::is_script_ext; @@ -60,6 +61,8 @@ pub use rules::collect_no_slow_type_diagnostics; pub use rules::ConfiguredRules; pub use rules::LintRuleProvider; +const JSON_SCHEMA_VERSION: u8 = 1; + static STDIN_FILE_NAME: &str = "$deno$stdin.ts"; pub async fn lint( @@ -440,18 +443,20 @@ pub fn print_rules_list(json: bool, maybe_rules_tags: Option>) { .rules; if json { - let json_rules: Vec = lint_rules - .iter() - .map(|rule| { - serde_json::json!({ - "code": rule.code(), - "tags": rule.tags(), - "docs": rule.docs(), + let json_output = serde_json::json!({ + "version": JSON_SCHEMA_VERSION, + "rules": lint_rules + .iter() + .map(|rule| { + serde_json::json!({ + "code": rule.code(), + "tags": rule.tags(), + "docs": rule.docs(), + }) }) - }) - .collect(); - let json_str = serde_json::to_string_pretty(&json_rules).unwrap(); - println!("{json_str}"); + .collect::>(), + }); + display::write_json_to_stdout(&json_output).unwrap(); } else { // The rules should still be printed even if `--quiet` option is enabled, // so use `println!` here instead of `info!`. diff --git a/cli/tools/lint/reporters.rs b/cli/tools/lint/reporters.rs index cb00ad29624978..bf80be9f20ed58 100644 --- a/cli/tools/lint/reporters.rs +++ b/cli/tools/lint/reporters.rs @@ -12,6 +12,8 @@ use crate::args::LintReporterKind; use super::LintError; +const JSON_SCHEMA_VERSION: u8 = 1; + pub fn create_reporter(kind: LintReporterKind) -> Box { match kind { LintReporterKind::Pretty => Box::new(PrettyLintReporter::new()), @@ -170,6 +172,7 @@ struct JsonLintDiagnostic { #[derive(Serialize)] struct JsonLintReporter { + version: u8, diagnostics: Vec, errors: Vec, } @@ -177,6 +180,7 @@ struct JsonLintReporter { impl JsonLintReporter { fn new() -> JsonLintReporter { JsonLintReporter { + version: JSON_SCHEMA_VERSION, diagnostics: Vec::new(), errors: Vec::new(), } diff --git a/tests/specs/bench/json_output/pass.json.out b/tests/specs/bench/json_output/pass.json.out index 53259e758571d0..7276158ac3dae5 100644 --- a/tests/specs/bench/json_output/pass.json.out +++ b/tests/specs/bench/json_output/pass.json.out @@ -1,5 +1,6 @@ Check file:///[WILDCARD]/pass.ts { + "version": 1, "runtime": "Deno/[WILDCARD]", "cpu": "[WILDCARD]", "benches": [ diff --git a/tests/specs/doc/json/__test__.jsonc b/tests/specs/doc/json/__test__.jsonc new file mode 100644 index 00000000000000..12005bc46dfef6 --- /dev/null +++ b/tests/specs/doc/json/__test__.jsonc @@ -0,0 +1,4 @@ +{ + "args": "doc --json json.js", + "output": "json.out" +} diff --git a/tests/specs/doc/json/json.js b/tests/specs/doc/json/json.js new file mode 100644 index 00000000000000..84d4ff4a08f83d --- /dev/null +++ b/tests/specs/doc/json/json.js @@ -0,0 +1,2 @@ +/// +export const foo = "foo"; diff --git a/tests/specs/doc/json/json.out b/tests/specs/doc/json/json.out new file mode 100644 index 00000000000000..91f02abe32d3b6 --- /dev/null +++ b/tests/specs/doc/json/json.out @@ -0,0 +1,26 @@ +{ + "version": 1, + "nodes": [ + { + "kind": "variable", + "name": "foo", + "location": { + "filename": "file:///[WILDCARD]/types.d.ts", + "line": 2, + "col": 13 + }, + "declarationKind": "export", + "jsDoc": { + "doc": "An exported value." + }, + "variableDef": { + "tsType": { + "repr": "string", + "kind": "keyword", + "keyword": "string" + }, + "kind": "const" + } + } + ] +} \ No newline at end of file diff --git a/tests/specs/doc/json/types.d.ts b/tests/specs/doc/json/types.d.ts new file mode 100644 index 00000000000000..ce39201e1bf2ab --- /dev/null +++ b/tests/specs/doc/json/types.d.ts @@ -0,0 +1,2 @@ +/** An exported value. */ +export const foo: string; diff --git a/tests/testdata/doc/lint_success_json.out b/tests/testdata/doc/lint_success_json.out index 19f04c6a86e429..1c82680f8ab9d5 100644 --- a/tests/testdata/doc/lint_success_json.out +++ b/tests/testdata/doc/lint_success_json.out @@ -1,51 +1,54 @@ -[ - { - "name": "Test", - "isDefault": false, - "location": { - "filename": "file:///[WILDCARD]/lint_success.ts", - "line": 2, - "col": 0, - "byteIndex": 22 - }, - "declarationKind": "export", - "jsDoc": { - "doc": "My test class." - }, - "kind": "class", - "classDef": { - "isAbstract": false, - "constructors": [], - "properties": [ - { - "jsDoc": { - "doc": "My property." - }, - "tsType": { - "repr": "string", - "kind": "keyword", - "keyword": "string" - }, - "readonly": false, - "accessibility": null, - "optional": false, - "isAbstract": false, - "isStatic": false, - "name": "prop", - "location": { - "filename": "file:///[WILDCARD]/lint_success.ts", - "line": 4, - "col": 2, - "byteIndex": 66 +{ + "version": 1, + "nodes": [ + { + "name": "Test", + "isDefault": false, + "location": { + "filename": "file:///[WILDCARD]/lint_success.ts", + "line": 2, + "col": 0, + "byteIndex": 22 + }, + "declarationKind": "export", + "jsDoc": { + "doc": "My test class." + }, + "kind": "class", + "classDef": { + "isAbstract": false, + "constructors": [], + "properties": [ + { + "jsDoc": { + "doc": "My property." + }, + "tsType": { + "repr": "string", + "kind": "keyword", + "keyword": "string" + }, + "readonly": false, + "accessibility": null, + "optional": false, + "isAbstract": false, + "isStatic": false, + "name": "prop", + "location": { + "filename": "file:///[WILDCARD]/lint_success.ts", + "line": 4, + "col": 2, + "byteIndex": 66 + } } - } - ], - "indexSignatures": [], - "methods": [], - "extends": null, - "implements": [], - "typeParams": [], - "superTypeParams": [] + ], + "indexSignatures": [], + "methods": [], + "extends": null, + "implements": [], + "typeParams": [], + "superTypeParams": [] + } } - } -] + ] +} diff --git a/tests/testdata/info/076_info_json_deps_order.out b/tests/testdata/info/076_info_json_deps_order.out index a1b15e00c09203..33d58da16a64a4 100644 --- a/tests/testdata/info/076_info_json_deps_order.out +++ b/tests/testdata/info/076_info_json_deps_order.out @@ -160,5 +160,6 @@ } ], "redirects": {}, + "version": 1, "npmPackages": {} } diff --git a/tests/testdata/info/info_json.out b/tests/testdata/info/info_json.out index 3215af742b9bfe..607489ca39b10b 100644 --- a/tests/testdata/info/info_json.out +++ b/tests/testdata/info/info_json.out @@ -1,4 +1,5 @@ { + "version": 1, "denoDir": "[WILDCARD]", "modulesCache": "[WILDCARD]deps", "npmCache": "[WILDCARD]npm", diff --git a/tests/testdata/info/info_json_location.out b/tests/testdata/info/info_json_location.out index 510fa77494235a..004bf03db1f860 100644 --- a/tests/testdata/info/info_json_location.out +++ b/tests/testdata/info/info_json_location.out @@ -1,4 +1,5 @@ { + "version": 1, "denoDir": "[WILDCARD]", "modulesCache": "[WILDCARD]deps", "npmCache": "[WILDCARD]npm", diff --git a/tests/testdata/info/json_output/main.out b/tests/testdata/info/json_output/main.out index 5a89d5cab037bd..43c4f7398da321 100644 --- a/tests/testdata/info/json_output/main.out +++ b/tests/testdata/info/json_output/main.out @@ -87,5 +87,6 @@ } ], "redirects": {}, + "version": 1, "npmPackages": {} } diff --git a/tests/testdata/lint/expected_from_stdin_json.out b/tests/testdata/lint/expected_from_stdin_json.out index 9e1188bcdbb1f5..27b215b43d2550 100644 --- a/tests/testdata/lint/expected_from_stdin_json.out +++ b/tests/testdata/lint/expected_from_stdin_json.out @@ -1,4 +1,5 @@ { + "version": 1, "diagnostics": [ { "filename": "[WILDCARD]$deno$stdin.ts", diff --git a/tests/testdata/lint/expected_json.out b/tests/testdata/lint/expected_json.out index 95c3d30ba48012..6712c891a592dd 100644 --- a/tests/testdata/lint/expected_json.out +++ b/tests/testdata/lint/expected_json.out @@ -1,4 +1,5 @@ { + "version": 1, "diagnostics": [ { "filename": "[WILDCARD]file1.js", diff --git a/tests/testdata/lint/with_report_config_override.out b/tests/testdata/lint/with_report_config_override.out index 7ca748158308f0..ad32e31236f25f 100644 --- a/tests/testdata/lint/with_report_config_override.out +++ b/tests/testdata/lint/with_report_config_override.out @@ -1,4 +1,5 @@ { + "version": 1, "diagnostics": [ { "filename": "[WILDCARD]a.ts", diff --git a/tests/testdata/npm/cjs_with_deps/main_info_json.out b/tests/testdata/npm/cjs_with_deps/main_info_json.out index fd850b8a1c80e5..4d8c1a5bea5986 100644 --- a/tests/testdata/npm/cjs_with_deps/main_info_json.out +++ b/tests/testdata/npm/cjs_with_deps/main_info_json.out @@ -53,6 +53,7 @@ "npm:chai@4.3": "npm:/chai@4.3.6", "npm:chalk@4": "npm:/chalk@4.1.2" }, + "version": 1, "npmPackages": { "ansi-styles@4.3.0": { "name": "ansi-styles", diff --git a/tests/testdata/npm/info/chalk_json.out b/tests/testdata/npm/info/chalk_json.out index bffed4ad446a96..d54155270b1679 100644 --- a/tests/testdata/npm/info/chalk_json.out +++ b/tests/testdata/npm/info/chalk_json.out @@ -12,6 +12,7 @@ "redirects": { "npm:chalk@4": "npm:/chalk@4.1.2" }, + "version": 1, "npmPackages": { "ansi-styles@4.3.0": { "name": "ansi-styles", diff --git a/tests/testdata/npm/peer_deps_with_copied_folders/main_info_json.out b/tests/testdata/npm/peer_deps_with_copied_folders/main_info_json.out index a4306a6d5fb738..48cb1f992037c3 100644 --- a/tests/testdata/npm/peer_deps_with_copied_folders/main_info_json.out +++ b/tests/testdata/npm/peer_deps_with_copied_folders/main_info_json.out @@ -53,6 +53,7 @@ "npm:@denotest/peer-dep-test-child@1": "npm:/@denotest/peer-dep-test-child@1.0.0", "npm:@denotest/peer-dep-test-child@2": "npm:/@denotest/peer-dep-test-child@2.0.0" }, + "version": 1, "npmPackages": { "@denotest/peer-dep-test-child@1.0.0_@denotest+peer-dep-test-peer@1.0.0": { "name": "@denotest/peer-dep-test-child", From c36d900eda3c17f5f2fef4af1700b9b279aa3d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 5 Sep 2024 10:11:39 +0200 Subject: [PATCH 2/2] fix tests --- tests/specs/doc/json/json.out | 8 +++++--- tests/specs/info/multiple_redirects/main.out | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/specs/doc/json/json.out b/tests/specs/doc/json/json.out index 91f02abe32d3b6..ae1b02596e7fcd 100644 --- a/tests/specs/doc/json/json.out +++ b/tests/specs/doc/json/json.out @@ -2,17 +2,19 @@ "version": 1, "nodes": [ { - "kind": "variable", "name": "foo", + "isDefault": false, "location": { "filename": "file:///[WILDCARD]/types.d.ts", "line": 2, - "col": 13 + "col": 13, + "byteIndex": 39 }, "declarationKind": "export", "jsDoc": { "doc": "An exported value." }, + "kind": "variable", "variableDef": { "tsType": { "repr": "string", @@ -23,4 +25,4 @@ } } ] -} \ No newline at end of file +} diff --git a/tests/specs/info/multiple_redirects/main.out b/tests/specs/info/multiple_redirects/main.out index bd18c17288a8d3..31123be778dc60 100644 --- a/tests/specs/info/multiple_redirects/main.out +++ b/tests/specs/info/multiple_redirects/main.out @@ -47,5 +47,6 @@ Download http://localhost:4545/subdir/redirects/redirect1.js "http://localhost:4546/subdir/redirects/redirect1.js": "http://localhost:4545/subdir/redirects/redirect1.js", "http://localhost:4548/subdir/redirects/redirect1.js": "http://localhost:4546/subdir/redirects/redirect1.js" }, + "version": 1, "npmPackages": {} }