From 106d47a0136c04ca219a81c3f91505116e13855e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 18 Nov 2024 22:54:28 +0000 Subject: [PATCH] feat: fmt and lint respect .gitignore file (#26897) Closes https://github.com/denoland/deno/issues/26573 --- cli/tools/fmt.rs | 1 + cli/tools/lint/mod.rs | 1 + tests/specs/fmt/gitignore/.gitignore | 1 + tests/specs/fmt/gitignore/__test__.jsonc | 6 ++++++ tests/specs/fmt/gitignore/dist/file1.js | 6 ++++++ tests/specs/fmt/gitignore/expected.out | 10 ++++++++++ tests/specs/fmt/gitignore/file2.ts | 3 +++ tests/specs/lint/gitignore/.gitignore | 1 + tests/specs/lint/gitignore/__test__.jsonc | 5 +++++ tests/specs/lint/gitignore/dist/file1.js | 3 +++ tests/specs/lint/gitignore/expected.out | 12 ++++++++++++ tests/specs/lint/gitignore/file2.ts | 6 ++++++ 12 files changed, 55 insertions(+) create mode 100644 tests/specs/fmt/gitignore/.gitignore create mode 100644 tests/specs/fmt/gitignore/__test__.jsonc create mode 100644 tests/specs/fmt/gitignore/dist/file1.js create mode 100644 tests/specs/fmt/gitignore/expected.out create mode 100644 tests/specs/fmt/gitignore/file2.ts create mode 100644 tests/specs/lint/gitignore/.gitignore create mode 100644 tests/specs/lint/gitignore/__test__.jsonc create mode 100644 tests/specs/lint/gitignore/dist/file1.js create mode 100644 tests/specs/lint/gitignore/expected.out create mode 100644 tests/specs/lint/gitignore/file2.ts diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 2221c0da97..d40abd5f50 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -228,6 +228,7 @@ fn collect_fmt_files( }) .ignore_git_folder() .ignore_node_modules() + .use_gitignore() .set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned)) .collect_file_patterns(&deno_config::fs::RealDenoConfigFs, files) } diff --git a/cli/tools/lint/mod.rs b/cli/tools/lint/mod.rs index a9c2d01528..fcefb45874 100644 --- a/cli/tools/lint/mod.rs +++ b/cli/tools/lint/mod.rs @@ -436,6 +436,7 @@ fn collect_lint_files( }) .ignore_git_folder() .ignore_node_modules() + .use_gitignore() .set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned)) .collect_file_patterns(&deno_config::fs::RealDenoConfigFs, files) } diff --git a/tests/specs/fmt/gitignore/.gitignore b/tests/specs/fmt/gitignore/.gitignore new file mode 100644 index 0000000000..838458f209 --- /dev/null +++ b/tests/specs/fmt/gitignore/.gitignore @@ -0,0 +1 @@ +/dist/ \ No newline at end of file diff --git a/tests/specs/fmt/gitignore/__test__.jsonc b/tests/specs/fmt/gitignore/__test__.jsonc new file mode 100644 index 0000000000..94804c3fe1 --- /dev/null +++ b/tests/specs/fmt/gitignore/__test__.jsonc @@ -0,0 +1,6 @@ +{ + "tempDir": true, + "args": "fmt --check", + "output": "expected.out", + "exitCode": 1 +} diff --git a/tests/specs/fmt/gitignore/dist/file1.js b/tests/specs/fmt/gitignore/dist/file1.js new file mode 100644 index 0000000000..3ecf5aa6a0 --- /dev/null +++ b/tests/specs/fmt/gitignore/dist/file1.js @@ -0,0 +1,6 @@ +// This file is in `.gitignore` simulating that it's generated by a build tool +// and should not be linted +function foo( ) { + console.log( "hello") + } + \ No newline at end of file diff --git a/tests/specs/fmt/gitignore/expected.out b/tests/specs/fmt/gitignore/expected.out new file mode 100644 index 0000000000..73568ff3a7 --- /dev/null +++ b/tests/specs/fmt/gitignore/expected.out @@ -0,0 +1,10 @@ + +from [WILDCARD]file2.ts: +1 | -function foo( ): any { +1 | +function foo(): any { +2 | - console.log( "hello") +2 | + console.log("hello"); +3 | - } +3 | +} + +error: Found 1 not formatted file in 1 file diff --git a/tests/specs/fmt/gitignore/file2.ts b/tests/specs/fmt/gitignore/file2.ts new file mode 100644 index 0000000000..0e5fdd36d2 --- /dev/null +++ b/tests/specs/fmt/gitignore/file2.ts @@ -0,0 +1,3 @@ +function foo( ): any { + console.log( "hello") + } diff --git a/tests/specs/lint/gitignore/.gitignore b/tests/specs/lint/gitignore/.gitignore new file mode 100644 index 0000000000..838458f209 --- /dev/null +++ b/tests/specs/lint/gitignore/.gitignore @@ -0,0 +1 @@ +/dist/ \ No newline at end of file diff --git a/tests/specs/lint/gitignore/__test__.jsonc b/tests/specs/lint/gitignore/__test__.jsonc new file mode 100644 index 0000000000..e777fb1c1e --- /dev/null +++ b/tests/specs/lint/gitignore/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "lint", + "output": "expected.out", + "exitCode": 1 +} diff --git a/tests/specs/lint/gitignore/dist/file1.js b/tests/specs/lint/gitignore/dist/file1.js new file mode 100644 index 0000000000..669c1e2c8c --- /dev/null +++ b/tests/specs/lint/gitignore/dist/file1.js @@ -0,0 +1,3 @@ +// This file is in `.gitignore` simulating that it's generated by a build tool +// and should not be linted +while (false) {} diff --git a/tests/specs/lint/gitignore/expected.out b/tests/specs/lint/gitignore/expected.out new file mode 100644 index 0000000000..8024b51d19 --- /dev/null +++ b/tests/specs/lint/gitignore/expected.out @@ -0,0 +1,12 @@ +error[no-empty]: Empty block statement + --> [WILDCARD]file2.ts:3:14 + | +3 | } catch (_e) {} + | ^^ + = hint: Add code or comment to the empty block + + docs: https://lint.deno.land/rules/no-empty + + +Found 1 problem +Checked 1 file diff --git a/tests/specs/lint/gitignore/file2.ts b/tests/specs/lint/gitignore/file2.ts new file mode 100644 index 0000000000..73c612c35f --- /dev/null +++ b/tests/specs/lint/gitignore/file2.ts @@ -0,0 +1,6 @@ +try { + await Deno.open("./some/file.txt"); +} catch (_e) {} + +// deno-lint-ignore no-explicit-any +function _foo(): any {}