summaryrefslogtreecommitdiff
path: root/cli/tools
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2021-08-16 09:28:29 +0200
committerGitHub <noreply@github.com>2021-08-16 09:28:29 +0200
commit163f2ef57117afd410f03be7e7519fc89bd18173 (patch)
tree03080b2306927efbb2af64869b418c009abd92f5 /cli/tools
parent02b23e057561f11daf004b4ed8d455081dbeac65 (diff)
fix: parse error when transpiling code with BOM (#11688)
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/tools')
-rw-r--r--cli/tools/fmt.rs11
1 files changed, 4 insertions, 7 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index a02b86b17..33c3599d7 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -28,8 +28,6 @@ use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
-const BOM_CHAR: char = '\u{FEFF}';
-
/// Format JavaScript/TypeScript files.
pub async fn format(
args: Vec<PathBuf>,
@@ -350,12 +348,11 @@ fn read_file_contents(file_path: &Path) -> Result<FileContents, AnyError> {
let file_bytes = fs::read(&file_path)?;
let charset = text_encoding::detect_charset(&file_bytes);
let file_text = text_encoding::convert_to_utf8(&file_bytes, charset)?;
- let had_bom = file_text.starts_with(BOM_CHAR);
+ let had_bom = file_text.starts_with(text_encoding::BOM_CHAR);
let text = if had_bom {
- // remove the BOM
- String::from(&file_text[BOM_CHAR.len_utf8()..])
+ text_encoding::strip_bom(&file_text).to_string()
} else {
- String::from(file_text)
+ file_text.to_string()
};
Ok(FileContents { text, had_bom })
@@ -367,7 +364,7 @@ fn write_file_contents(
) -> Result<(), AnyError> {
let file_text = if file_contents.had_bom {
// add back the BOM
- format!("{}{}", BOM_CHAR, file_contents.text)
+ format!("{}{}", text_encoding::BOM_CHAR, file_contents.text)
} else {
file_contents.text
};