ctb_formats_javascript/diagnostics.rs
1// SPDX-License-Identifier: AGPL-3.0-or-later AND MIT
2// SPDX-License-Identifier for parts derived from dlint: MIT
3/*
4This file is part of Collective Toolbox, a database and document workspace and utilities.
5Copyright (C) 2026 Collective Toolbox Developers
6Contact: info@collectivetoolbox.com
7
8This program is free software: you can redistribute it and/or modify it under
9the terms of the GNU Affero General Public License as published by the Free
10Software Foundation, either version 3 of the License, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
16
17You should have received a copy of the GNU Affero General Public License along
18with this program. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21// Derived from Deno's dlint (https://github.com/denoland/deno_lint).
22// For parts derived from dlint:
23// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
24
25// See additional licensing details at end of file.
26
27//! JavaScript and TypeScript diagnostic emission and formatting helpers.
28
29#[allow(
30 unused_imports,
31 clippy::wildcard_imports,
32 reason = "Standard workspace crate prelude"
33)]
34pub(crate) use ctb_utilities::*;
35
36use deno_ast::diagnostics::Diagnostic;
37use deno_lint::diagnostic::LintDiagnostic;
38
39pub fn display_diagnostics(
40 diagnostics: &[LintDiagnostic],
41 format: Option<&str>,
42) {
43 match format {
44 Some("compact") => print_compact(diagnostics),
45 Some("pretty") => print_pretty(diagnostics),
46 _ => unreachable!("Invalid output format specified"),
47 }
48}
49
50fn print_compact(diagnostics: &[LintDiagnostic]) {
51 for diagnostic in diagnostics {
52 match &diagnostic.range {
53 Some(range) => {
54 let display_index =
55 range.text_info.line_and_column_display(range.range.start);
56 eprintln!(
57 "{}: line {}, col {}, Error - {} ({})",
58 diagnostic.specifier,
59 display_index.line_number,
60 display_index.column_number,
61 diagnostic.details.message,
62 diagnostic.details.code
63 );
64 }
65 None => {
66 eprintln!(
67 "{}: {} ({})",
68 diagnostic.specifier,
69 diagnostic.message(),
70 diagnostic.code()
71 );
72 }
73 }
74 }
75}
76
77fn print_pretty(diagnostics: &[LintDiagnostic]) {
78 for diagnostic in diagnostics {
79 eprintln!("{}\n", diagnostic.display());
80 }
81}
82
83/*
84Code from dlint is used under the following license:
85======
86
87MIT License
88
89Copyright (c) 2018-2024 the Deno authors
90
91Permission is hereby granted, free of charge, to any person obtaining a copy
92of this software and associated documentation files (the "Software"), to deal
93in the Software without restriction, including without limitation the rights
94to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
95copies of the Software, and to permit persons to whom the Software is
96furnished to do so, subject to the following conditions:
97
98The above copyright notice and this permission notice shall be included in all
99copies or substantial portions of the Software.
100
101THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
102IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
103FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
104AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
105LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
106OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
107SOFTWARE.
108*/