summaryrefslogtreecommitdiff
path: root/cli/tools/fmt.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-01-31 22:15:22 -0500
committerGitHub <noreply@github.com>2024-02-01 03:15:22 +0000
commit4b7c6049ef9d40394eb823859c82cbf8d293430d (patch)
tree61e6de7c69c9d00faeef0ff7e6c223224a53de9e /cli/tools/fmt.rs
parent830d096b66696ad9f4e67b3ed8460fb1ff7a9170 (diff)
refactor: load bytes in deno_graph (#22212)
Upgrades deno_graph to 0.64 where deno_graph is now responsible for turning bytes into a string. This is in preparation for Wasm modules.
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r--cli/tools/fmt.rs19
1 files changed, 7 insertions, 12 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index ad35615a0..86fc9700e 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -20,7 +20,6 @@ use crate::util::file_watcher;
use crate::util::fs::canonicalize_path;
use crate::util::fs::FileCollector;
use crate::util::path::get_extension;
-use crate::util::text_encoding;
use deno_ast::ParsedSource;
use deno_config::glob::FilePatterns;
use deno_core::anyhow::anyhow;
@@ -607,28 +606,24 @@ struct FileContents {
fn read_file_contents(file_path: &Path) -> Result<FileContents, AnyError> {
let file_bytes = fs::read(file_path)
.with_context(|| format!("Error reading {}", file_path.display()))?;
- let charset = text_encoding::detect_charset(&file_bytes);
- let file_text = text_encoding::convert_to_utf8(&file_bytes, charset)
- .map_err(|_| {
+ let had_bom = file_bytes.starts_with(&[0xEF, 0xBB, 0xBF]);
+ // will have the BOM stripped
+ let text = deno_graph::source::decode_owned_file_source(file_bytes)
+ .with_context(|| {
anyhow!("{} is not a valid UTF-8 file", file_path.display())
})?;
- let had_bom = file_text.starts_with(text_encoding::BOM_CHAR);
- let text = if had_bom {
- text_encoding::strip_bom(&file_text).to_string()
- } else {
- file_text.to_string()
- };
Ok(FileContents { text, had_bom })
}
fn write_file_contents(
file_path: &Path,
- file_contents: FileContents,
+ mut file_contents: FileContents,
) -> Result<(), AnyError> {
let file_text = if file_contents.had_bom {
// add back the BOM
- format!("{}{}", text_encoding::BOM_CHAR, file_contents.text)
+ file_contents.text.insert(0, '\u{FEFF}');
+ file_contents.text
} else {
file_contents.text
};