From 42f4eea5c05aa0ac6b064b35307da0bef54b17d8 Mon Sep 17 00:00:00 2001 From: l3ops Date: Thu, 6 Oct 2022 11:02:09 +0200 Subject: [PATCH] fix(rome_wasm): fix the WASM diagnostic printer (#3349) * fix(rome_wasm): fix the WASM diagnostic printer and add a snapshot test for it * fix the formatting --- crates/rome_diagnostics/src/file.rs | 12 ++- crates/rome_wasm/src/utils.rs | 6 +- .../diagnosticPrinter.test.mjs.snap | 26 ++++++ .../tests/wasm/diagnosticPrinter.test.mjs | 80 +++++++++++++++++++ 4 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 npm/js-api/tests/wasm/__snapshots__/diagnosticPrinter.test.mjs.snap create mode 100644 npm/js-api/tests/wasm/diagnosticPrinter.test.mjs diff --git a/crates/rome_diagnostics/src/file.rs b/crates/rome_diagnostics/src/file.rs index a9a61ff9dc9..f5f1ec6ccc3 100644 --- a/crates/rome_diagnostics/src/file.rs +++ b/crates/rome_diagnostics/src/file.rs @@ -157,6 +157,13 @@ impl SimpleFile { pub fn empty() -> SimpleFile { SimpleFile::new(String::new(), String::new()) } + + pub fn source_code(&self) -> SourceCode<&'_ str, &'_ LineIndex> { + SourceCode { + text: &self.source, + line_starts: Some(&self.line_starts), + } + } } impl Files for SimpleFile { @@ -165,10 +172,7 @@ impl Files for SimpleFile { } fn source(&self, _id: FileId) -> Option> { - Some(SourceCode { - text: &self.source, - line_starts: Some(&self.line_starts), - }) + Some(self.source_code()) } } diff --git a/crates/rome_wasm/src/utils.rs b/crates/rome_wasm/src/utils.rs index aa1de965809..a5f170c5eb1 100644 --- a/crates/rome_wasm/src/utils.rs +++ b/crates/rome_wasm/src/utils.rs @@ -6,7 +6,8 @@ use wasm_bindgen::prelude::*; use rome_console::fmt::HTML; use rome_console::{fmt::Formatter, markup}; use rome_diagnostics::file::SimpleFile; -use rome_diagnostics::Diagnostic; +use rome_diagnostics::v2::serde::Diagnostic; +use rome_diagnostics::v2::{DiagnosticExt, PrintDiagnostic}; use super::IDiagnostic; @@ -39,10 +40,11 @@ impl DiagnosticPrinter { pub fn print(&mut self, diagnostic: IDiagnostic) -> Result<(), Error> { let diag: Diagnostic = diagnostic.into_serde().map_err(into_error)?; + let err = diag.with_file_source_code(self.file.source_code()); let mut html = HTML(&mut self.buffer); Formatter::new(&mut html) - .write_markup(markup!({ diag.display(&self.file) })) + .write_markup(markup!({ PrintDiagnostic(&err) })) .map_err(into_error)?; Ok(()) diff --git a/npm/js-api/tests/wasm/__snapshots__/diagnosticPrinter.test.mjs.snap b/npm/js-api/tests/wasm/__snapshots__/diagnosticPrinter.test.mjs.snap new file mode 100644 index 00000000000..104b6b3c0ad --- /dev/null +++ b/npm/js-api/tests/wasm/__snapshots__/diagnosticPrinter.test.mjs.snap @@ -0,0 +1,26 @@ +// Vitest Snapshot v1 + +exports[`Rome WebAssembly DiagnosticPrinter > should format content > HTML diagnostic 1`] = ` +"file.js:3:6 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + error message content + + 1 │ const variable = expr(); + 2 │ + > 3 │ if(expr()) { + ^^^^^^ + 4 │ statement(); + 5 │ } + +file.js:4:8 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + error message content + + 3 │ if(expr()) { + > 4 │ statement(); + ^^^^^^^^^ + > 5 │ } + ^ + +" +`; diff --git a/npm/js-api/tests/wasm/diagnosticPrinter.test.mjs b/npm/js-api/tests/wasm/diagnosticPrinter.test.mjs new file mode 100644 index 00000000000..ab324853253 --- /dev/null +++ b/npm/js-api/tests/wasm/diagnosticPrinter.test.mjs @@ -0,0 +1,80 @@ +import { describe, expect, it } from "vitest"; +import { DiagnosticPrinter } from "@rometools/wasm-nodejs"; + +describe("Rome WebAssembly DiagnosticPrinter", () => { + it("should format content", async () => { + const SOURCE_CODE = `const variable = expr(); + +if(expr()) { + statement(); +}`; + const printer = new DiagnosticPrinter("file.js", SOURCE_CODE); + + printer.print({ + advices: { + advices: [], + }, + category: "parse", + description: "error description content", + location: { + path: { + File: { + PathAndId: { + file_id: 0, + path: "file.js", + }, + }, + }, + source_code: null, + span: [31, 37], + }, + message: [ + { + content: "error message content", + elements: [], + }, + ], + severity: "Error", + source: null, + tags: [], + verbose_advices: { + advices: [], + }, + }); + + printer.print({ + advices: { + advices: [], + }, + category: "parse", + description: "error description content", + location: { + path: { + File: { + PathAndId: { + file_id: 0, + path: "file.js", + }, + }, + }, + source_code: null, + span: [46, 58], + }, + message: [ + { + content: "error message content", + elements: [], + }, + ], + severity: "Error", + source: null, + tags: [], + verbose_advices: { + advices: [], + }, + }); + + const html = printer.finish(); + expect(html).toMatchSnapshot("HTML diagnostic"); + }); +});