From d2a86191626cec55622d18e0a277edf8cd1f0463 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 7 Jun 2024 13:02:47 -0400 Subject: [PATCH] fix: do not panic linting files with UTF-8 BOM (#24136) Closes #24122 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- cli/tools/lint/mod.rs | 4 ++-- tests/specs/lint/bom/__test__.jsonc | 5 +++++ tests/specs/lint/bom/mod.out | 12 ++++++++++++ tests/specs/lint/bom/mod.ts | 4 ++++ 6 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/specs/lint/bom/__test__.jsonc create mode 100644 tests/specs/lint/bom/mod.out create mode 100644 tests/specs/lint/bom/mod.ts diff --git a/Cargo.lock b/Cargo.lock index 142788eb30..0f29b9995e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1175,9 +1175,9 @@ dependencies = [ [[package]] name = "deno_ast" -version = "0.39.0" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32edef567e3090862e865c75628f4d35ace80ca90e0fc5263a7d10fa307ae899" +checksum = "042645e6a505a359b288723ded5c8b30fdc4f70514a3bcd7a49221cc89c1ba90" dependencies = [ "anyhow", "base64 0.21.7", diff --git a/Cargo.toml b/Cargo.toml index cfb00598c1..26f242036c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ license = "MIT" repository = "https://github.com/denoland/deno" [workspace.dependencies] -deno_ast = { version = "=0.39.0", features = ["transpiling"] } +deno_ast = { version = "=0.39.1", features = ["transpiling"] } deno_core = { version = "0.284.0" } deno_bench_util = { version = "0.149.0", path = "./bench_util" } diff --git a/cli/tools/lint/mod.rs b/cli/tools/lint/mod.rs index 151d0f11d1..712b95e340 100644 --- a/cli/tools/lint/mod.rs +++ b/cli/tools/lint/mod.rs @@ -230,7 +230,7 @@ async fn lint_files( deno_core::unsync::spawn(async move { run_parallelized(paths, { move |file_path| { - let file_text = fs::read_to_string(&file_path)?; + let file_text = deno_ast::strip_bom(fs::read_to_string(&file_path)?); // don't bother rechecking this file if it didn't have any diagnostics before if incremental_cache.is_file_same(&file_path, &file_text) { @@ -510,7 +510,7 @@ fn lint_stdin( linter .lint_file(LintFileOptions { specifier: specifier_from_file_path(file_path)?, - source_code: source_code.clone(), + source_code: deno_ast::strip_bom(source_code), media_type: MediaType::TypeScript, config, }) diff --git a/tests/specs/lint/bom/__test__.jsonc b/tests/specs/lint/bom/__test__.jsonc new file mode 100644 index 0000000000..f2ea579c49 --- /dev/null +++ b/tests/specs/lint/bom/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "lint mod.ts", + "exitCode": 1, + "output": "mod.out" +} diff --git a/tests/specs/lint/bom/mod.out b/tests/specs/lint/bom/mod.out new file mode 100644 index 0000000000..eea1e531d7 --- /dev/null +++ b/tests/specs/lint/bom/mod.out @@ -0,0 +1,12 @@ +error[no-unused-vars]: `t` is never used + --> [WILDLINE] + | +4 | const t = 5; + | ^ + = hint: If this is intentional, prefix it with an underscore like `_t` + + docs: https://lint.deno.land/rules/no-unused-vars + + +Found 1 problem +Checked 1 file diff --git a/tests/specs/lint/bom/mod.ts b/tests/specs/lint/bom/mod.ts new file mode 100644 index 0000000000..c2374d45d6 --- /dev/null +++ b/tests/specs/lint/bom/mod.ts @@ -0,0 +1,4 @@ +// this file is saved as UTF8 with BOM +console.log("Hello, world!"); + +const t = 5;