summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/Cargo.toml10
-rw-r--r--cli/lockfile.rs5
-rw-r--r--cli/lsp/language_server.rs12
-rw-r--r--cli/tools/coverage/mod.rs6
-rw-r--r--cli/tools/fmt.rs76
5 files changed, 58 insertions, 51 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 118b7e7c9..978740f4f 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -37,7 +37,7 @@ deno_webgpu = { version = "0.45.0", path = "../ext/webgpu" }
deno_websocket = { version = "0.49.0", path = "../ext/websocket" }
deno_webstorage = { version = "0.39.0", path = "../ext/webstorage" }
regex = "=1.5.5"
-serde = { version = "=1.0.133", features = ["derive"] }
+serde = { version = "=1.0.136", features = ["derive"] }
zstd = '=0.9.2'
[target.'cfg(windows)'.build-dependencies]
@@ -62,9 +62,9 @@ clap_complete = "=3.1.1"
clap_complete_fig = "=3.1.4"
data-url = "=0.1.1"
dissimilar = "=1.0.2"
-dprint-plugin-json = "=0.14.1"
-dprint-plugin-markdown = "=0.12.2"
-dprint-plugin-typescript = "=0.65.1"
+dprint-plugin-json = "=0.15.0"
+dprint-plugin-markdown = "=0.13.0"
+dprint-plugin-typescript = "=0.66.0"
encoding_rs = "=0.8.29"
env_logger = "=0.8.4"
eszip = "=0.18.0"
@@ -87,7 +87,7 @@ ring = "=0.16.20"
rustyline = { version = "=9.1.2", default-features = false }
rustyline-derive = "=0.6.0"
semver-parser = "=0.10.2"
-serde = { version = "=1.0.133", features = ["derive"] }
+serde = { version = "=1.0.136", features = ["derive"] }
shell-escape = "=0.1.5"
sourcemap = "=6.0.1"
tempfile = "=3.2.0"
diff --git a/cli/lockfile.rs b/cli/lockfile.rs
index ea1429829..33f5768fb 100644
--- a/cli/lockfile.rs
+++ b/cli/lockfile.rs
@@ -45,7 +45,10 @@ impl Lockfile {
let j = json!(&self.map);
let s = serde_json::to_string_pretty(&j).unwrap();
- let format_s = format_json(&s, &Default::default()).unwrap_or(s);
+ let format_s = format_json(&s, &Default::default())
+ .ok()
+ .flatten()
+ .unwrap_or(s);
let mut f = std::fs::OpenOptions::new()
.write(true)
.create(true)
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs
index 1afcff8a6..56c0be3d2 100644
--- a/cli/lsp/language_server.rs
+++ b/cli/lsp/language_server.rs
@@ -1063,11 +1063,13 @@ impl Inner {
};
match format_result {
- Ok(new_text) => Some(text::get_edits(
- document.content().as_str(),
- &new_text,
- document.line_index().as_ref(),
- )),
+ Ok(new_text) => new_text.map(|new_text| {
+ text::get_edits(
+ document.content().as_str(),
+ &new_text,
+ document.line_index().as_ref(),
+ )
+ }),
Err(err) => {
// TODO(lucacasonato): handle error properly
warn!("Format error: {}", err);
diff --git a/cli/tools/coverage/mod.rs b/cli/tools/coverage/mod.rs
index 26f36adae..092f6c825 100644
--- a/cli/tools/coverage/mod.rs
+++ b/cli/tools/coverage/mod.rs
@@ -126,8 +126,10 @@ impl CoverageCollector {
let mut out = BufWriter::new(File::create(filepath)?);
let coverage = serde_json::to_string(&script_coverage)?;
- let formated_coverage =
- format_json(&coverage, &Default::default()).unwrap_or(coverage);
+ let formated_coverage = format_json(&coverage, &Default::default())
+ .ok()
+ .flatten()
+ .unwrap_or(coverage);
out.write_all(formated_coverage.as_bytes())?;
out.flush()?;
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 4fba9ea13..3c5ab523e 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -14,9 +14,11 @@ use crate::config_file::ProseWrap;
use crate::diff::diff;
use crate::file_watcher;
use crate::file_watcher::ResolutionResult;
-use crate::flags::{Flags, FmtFlags};
+use crate::flags::Flags;
+use crate::flags::FmtFlags;
+use crate::fs_util::collect_files;
+use crate::fs_util::get_extension;
use crate::fs_util::specifier_to_file_path;
-use crate::fs_util::{collect_files, get_extension};
use crate::text_encoding;
use deno_ast::ParsedSource;
use deno_core::anyhow::bail;
@@ -24,9 +26,9 @@ use deno_core::anyhow::Context;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::futures;
+use deno_core::parking_lot::Mutex;
use log::debug;
use log::info;
-use std::borrow::Cow;
use std::fs;
use std::io::stdin;
use std::io::stdout;
@@ -34,8 +36,9 @@ use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
-use std::sync::atomic::{AtomicUsize, Ordering};
-use std::sync::{Arc, Mutex};
+use std::sync::atomic::AtomicUsize;
+use std::sync::atomic::Ordering;
+use std::sync::Arc;
/// Format JavaScript/TypeScript files.
pub async fn format(
@@ -167,7 +170,7 @@ pub async fn format(
fn format_markdown(
file_text: &str,
fmt_options: &FmtOptionsConfig,
-) -> Result<String, AnyError> {
+) -> Result<Option<String>, AnyError> {
let markdown_config = get_resolved_markdown_config(fmt_options);
dprint_plugin_markdown::format_text(
file_text,
@@ -196,7 +199,7 @@ fn format_markdown(
if matches!(extension, "json" | "jsonc") {
let mut json_config = get_resolved_json_config(fmt_options);
json_config.line_width = line_width;
- dprint_plugin_json::format_text(text, &json_config).map(Cow::Owned)
+ dprint_plugin_json::format_text(text, &json_config)
} else {
let fake_filename =
PathBuf::from(format!("deno_fmt_stdin.{}", extension));
@@ -208,10 +211,9 @@ fn format_markdown(
text,
&codeblock_config,
)
- .map(Cow::Owned)
}
} else {
- Ok(Cow::Borrowed(text))
+ Ok(None)
}
},
)
@@ -223,7 +225,7 @@ fn format_markdown(
pub fn format_json(
file_text: &str,
fmt_options: &FmtOptionsConfig,
-) -> Result<String, AnyError> {
+) -> Result<Option<String>, AnyError> {
let config = get_resolved_json_config(fmt_options);
dprint_plugin_json::format_text(file_text, &config)
}
@@ -233,7 +235,7 @@ pub fn format_file(
file_path: &Path,
file_text: &str,
fmt_options: FmtOptionsConfig,
-) -> Result<String, AnyError> {
+) -> Result<Option<String>, AnyError> {
let ext = get_extension(file_path).unwrap_or_default();
if matches!(
ext.as_str(),
@@ -251,7 +253,7 @@ pub fn format_file(
pub fn format_parsed_source(
parsed_source: &ParsedSource,
fmt_options: FmtOptionsConfig,
-) -> Result<String, AnyError> {
+) -> Result<Option<String>, AnyError> {
dprint_plugin_typescript::format_parsed_source(
parsed_source,
&get_resolved_typescript_config(&fmt_options),
@@ -276,18 +278,17 @@ async fn check_source_files(
let file_text = read_file_contents(&file_path)?.text;
match format_file(&file_path, &file_text, fmt_options.clone()) {
- Ok(formatted_text) => {
- if formatted_text != file_text {
- not_formatted_files_count.fetch_add(1, Ordering::Relaxed);
- let _g = output_lock.lock().unwrap();
- let diff = diff(&file_text, &formatted_text);
- info!("");
- info!("{} {}:", colors::bold("from"), file_path.display());
- info!("{}", diff);
- }
+ Ok(Some(formatted_text)) => {
+ not_formatted_files_count.fetch_add(1, Ordering::Relaxed);
+ let _g = output_lock.lock();
+ let diff = diff(&file_text, &formatted_text);
+ info!("");
+ info!("{} {}:", colors::bold("from"), file_path.display());
+ info!("{}", diff);
}
+ Ok(None) => {}
Err(e) => {
- let _g = output_lock.lock().unwrap();
+ let _g = output_lock.lock();
eprintln!("Error checking: {}", file_path.to_string_lossy());
eprintln!(" {}", e);
}
@@ -330,22 +331,21 @@ async fn format_source_files(
let file_contents = read_file_contents(&file_path)?;
match format_file(&file_path, &file_contents.text, fmt_options.clone()) {
- Ok(formatted_text) => {
- if formatted_text != file_contents.text {
- write_file_contents(
- &file_path,
- FileContents {
- had_bom: file_contents.had_bom,
- text: formatted_text,
- },
- )?;
- formatted_files_count.fetch_add(1, Ordering::Relaxed);
- let _g = output_lock.lock().unwrap();
- info!("{}", file_path.to_string_lossy());
- }
+ Ok(Some(formatted_text)) => {
+ write_file_contents(
+ &file_path,
+ FileContents {
+ had_bom: file_contents.had_bom,
+ text: formatted_text,
+ },
+ )?;
+ formatted_files_count.fetch_add(1, Ordering::Relaxed);
+ let _g = output_lock.lock();
+ info!("{}", file_path.to_string_lossy());
}
+ Ok(None) => {}
Err(e) => {
- let _g = output_lock.lock().unwrap();
+ let _g = output_lock.lock();
eprintln!("Error formatting: {}", file_path.to_string_lossy());
eprintln!(" {}", e);
}
@@ -388,11 +388,11 @@ pub fn format_stdin(
let formatted_text = format_file(&file_path, &source, fmt_options)?;
if fmt_flags.check {
- if formatted_text != source {
+ if formatted_text.is_some() {
println!("Not formatted stdin");
}
} else {
- stdout().write_all(formatted_text.as_bytes())?;
+ stdout().write_all(formatted_text.unwrap_or(source).as_bytes())?;
}
Ok(())
}