diff --git a/cli/config_file.rs b/cli/config_file.rs index 7ccd861626..70576afc01 100644 --- a/cli/config_file.rs +++ b/cli/config_file.rs @@ -885,14 +885,14 @@ mod tests { fn discover_inner_success() { // testdata/fmt/deno.jsonc exists let testdata = test_util::testdata_path(); - let c_md = testdata.join("fmt/fmt_with_config/c.md"); + let c_md = testdata.join("fmt/with_config/subdir/c.md"); let mut checked = HashSet::new(); let config_file = discover_inner(&c_md, &mut checked).unwrap().unwrap(); assert!(checked.contains(c_md.parent().unwrap())); assert!(!checked.contains(&testdata)); let fmt_config = config_file.to_fmt_config().unwrap().unwrap(); let expected_exclude = ModuleSpecifier::from_file_path( - testdata.join("fmt/fmt_with_config/b.ts"), + testdata.join("fmt/with_config/subdir/b.ts"), ) .unwrap(); assert_eq!(fmt_config.files.exclude, vec![expected_exclude]); diff --git a/cli/fs_util.rs b/cli/fs_util.rs index 20b20cccdb..339f6764f4 100644 --- a/cli/fs_util.rs +++ b/cli/fs_util.rs @@ -136,31 +136,6 @@ pub fn is_supported_ext(path: &Path) -> bool { } } -/// This function is similar to is_supported_ext but adds additional extensions -/// supported by `deno fmt`. -pub fn is_supported_ext_fmt(path: &Path) -> bool { - if let Some(ext) = get_extension(path) { - matches!( - ext.as_str(), - "ts" - | "tsx" - | "js" - | "jsx" - | "mjs" - | "json" - | "jsonc" - | "md" - | "mkd" - | "mkdn" - | "mdwn" - | "mdown" - | "markdown" - ) - } else { - false - } -} - /// Checks if the path has a basename and extension Deno supports for tests. pub fn is_supported_test_path(path: &Path) -> bool { use std::path::Component; @@ -446,43 +421,11 @@ mod tests { assert!(!is_supported_ext(Path::new("foo.mjsx"))); } - #[test] - fn test_is_supported_ext_fmt() { - assert!(!is_supported_ext_fmt(Path::new("tests/subdir/redirects"))); - assert!(is_supported_ext_fmt(Path::new("README.md"))); - assert!(is_supported_ext_fmt(Path::new("readme.MD"))); - assert!(is_supported_ext_fmt(Path::new("readme.mkd"))); - assert!(is_supported_ext_fmt(Path::new("readme.mkdn"))); - assert!(is_supported_ext_fmt(Path::new("readme.mdwn"))); - assert!(is_supported_ext_fmt(Path::new("readme.mdown"))); - assert!(is_supported_ext_fmt(Path::new("readme.markdown"))); - assert!(is_supported_ext_fmt(Path::new("lib/typescript.d.ts"))); - assert!(is_supported_ext_fmt(Path::new("testdata/001_hello.js"))); - assert!(is_supported_ext_fmt(Path::new("testdata/002_hello.ts"))); - assert!(is_supported_ext_fmt(Path::new("foo.jsx"))); - assert!(is_supported_ext_fmt(Path::new("foo.tsx"))); - assert!(is_supported_ext_fmt(Path::new("foo.TS"))); - assert!(is_supported_ext_fmt(Path::new("foo.TSX"))); - assert!(is_supported_ext_fmt(Path::new("foo.JS"))); - assert!(is_supported_ext_fmt(Path::new("foo.JSX"))); - assert!(is_supported_ext_fmt(Path::new("foo.mjs"))); - assert!(!is_supported_ext_fmt(Path::new("foo.mjsx"))); - assert!(is_supported_ext_fmt(Path::new("foo.jsonc"))); - assert!(is_supported_ext_fmt(Path::new("foo.JSONC"))); - assert!(is_supported_ext_fmt(Path::new("foo.json"))); - assert!(is_supported_ext_fmt(Path::new("foo.JsON"))); - } - #[test] fn test_is_supported_test_ext() { assert!(!is_supported_test_ext(Path::new("tests/subdir/redirects"))); assert!(is_supported_test_ext(Path::new("README.md"))); assert!(is_supported_test_ext(Path::new("readme.MD"))); - assert!(is_supported_ext_fmt(Path::new("readme.mkd"))); - assert!(is_supported_ext_fmt(Path::new("readme.mkdn"))); - assert!(is_supported_ext_fmt(Path::new("readme.mdwn"))); - assert!(is_supported_ext_fmt(Path::new("readme.mdown"))); - assert!(is_supported_ext_fmt(Path::new("readme.markdown"))); assert!(is_supported_test_ext(Path::new("lib/typescript.d.ts"))); assert!(is_supported_test_ext(Path::new("testdata/001_hello.js"))); assert!(is_supported_test_ext(Path::new("testdata/002_hello.ts"))); diff --git a/cli/tests/integration/fmt_tests.rs b/cli/tests/integration/fmt_tests.rs index f1847d3a72..df2fe182e7 100644 --- a/cli/tests/integration/fmt_tests.rs +++ b/cli/tests/integration/fmt_tests.rs @@ -177,18 +177,18 @@ itest!(fmt_stdin_check_not_formatted { }); itest!(fmt_with_config { - args: "fmt --config fmt/deno.jsonc fmt/fmt_with_config/", + args: "fmt --config fmt/with_config/deno.jsonc fmt/with_config/subdir", output: "fmt/fmt_with_config.out", }); itest!(fmt_with_config_default { - args: "fmt fmt/fmt_with_config/", + args: "fmt fmt/with_config/subdir", output: "fmt/fmt_with_config.out", }); // Check if CLI flags take precedence itest!(fmt_with_config_and_flags { - args: "fmt --config fmt/deno.jsonc --ignore=fmt/fmt_with_config/a.ts,fmt/fmt_with_config/b.ts", + args: "fmt --config fmt/with_config/deno.jsonc --ignore=fmt/with_config/subdir/a.ts,fmt/with_config/subdir/b.ts", output: "fmt/fmt_with_config_and_flags.out", }); diff --git a/cli/tests/testdata/fmt/deno.jsonc b/cli/tests/testdata/fmt/deno.jsonc deleted file mode 100644 index 9c330d34a8..0000000000 --- a/cli/tests/testdata/fmt/deno.jsonc +++ /dev/null @@ -1,15 +0,0 @@ -{ - "fmt": { - "files": { - "include": ["fmt_with_config/"], - "exclude": ["fmt_with_config/b.ts"] - }, - "options": { - "useTabs": true, - "lineWidth": 40, - "indentWidth": 8, - "singleQuote": true, - "proseWrap": "always" - } - } -} diff --git a/cli/tests/testdata/fmt/with_config/deno.jsonc b/cli/tests/testdata/fmt/with_config/deno.jsonc new file mode 100644 index 0000000000..3b9474e644 --- /dev/null +++ b/cli/tests/testdata/fmt/with_config/deno.jsonc @@ -0,0 +1,19 @@ +{ + "fmt": { + "files": { + "include": [ + "./subdir/" + ], + "exclude": [ + "./subdir/b.ts" + ] + }, + "options": { + "useTabs": true, + "lineWidth": 40, + "indentWidth": 8, + "singleQuote": true, + "proseWrap": "always" + } + } +} diff --git a/cli/tests/testdata/fmt/fmt_with_config/a.ts b/cli/tests/testdata/fmt/with_config/subdir/a.ts similarity index 100% rename from cli/tests/testdata/fmt/fmt_with_config/a.ts rename to cli/tests/testdata/fmt/with_config/subdir/a.ts diff --git a/cli/tests/testdata/fmt/fmt_with_config/b.ts b/cli/tests/testdata/fmt/with_config/subdir/b.ts similarity index 100% rename from cli/tests/testdata/fmt/fmt_with_config/b.ts rename to cli/tests/testdata/fmt/with_config/subdir/b.ts diff --git a/cli/tests/testdata/fmt/fmt_with_config/c.md b/cli/tests/testdata/fmt/with_config/subdir/c.md similarity index 100% rename from cli/tests/testdata/fmt/fmt_with_config/c.md rename to cli/tests/testdata/fmt/with_config/subdir/c.md diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 9172421b51..ebcdc6dd23 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -16,7 +16,7 @@ use crate::file_watcher; use crate::file_watcher::ResolutionResult; use crate::flags::FmtFlags; use crate::fs_util::specifier_to_file_path; -use crate::fs_util::{collect_files, get_extension, is_supported_ext_fmt}; +use crate::fs_util::{collect_files, get_extension}; use crate::text_encoding; use deno_ast::ParsedSource; use deno_core::anyhow::Context; @@ -91,6 +91,7 @@ pub async fn format( let result = collect_files(&include_files, &exclude_files, is_supported_ext_fmt).map( |files| { + eprintln!("fmt files {:?}", files); let refmt_files = if let Some(paths) = changed { if check { files @@ -597,3 +598,55 @@ where Ok(()) } } + +/// This function is similar to is_supported_ext but adds additional extensions +/// supported by `deno fmt`. +fn is_supported_ext_fmt(path: &Path) -> bool { + if let Some(ext) = get_extension(path) { + matches!( + ext.as_str(), + "ts" + | "tsx" + | "js" + | "jsx" + | "mjs" + | "json" + | "jsonc" + | "md" + | "mkd" + | "mkdn" + | "mdwn" + | "mdown" + | "markdown" + ) + } else { + false + } +} + +#[test] +fn test_is_supported_ext_fmt() { + assert!(!is_supported_ext_fmt(Path::new("tests/subdir/redirects"))); + assert!(is_supported_ext_fmt(Path::new("README.md"))); + assert!(is_supported_ext_fmt(Path::new("readme.MD"))); + assert!(is_supported_ext_fmt(Path::new("readme.mkd"))); + assert!(is_supported_ext_fmt(Path::new("readme.mkdn"))); + assert!(is_supported_ext_fmt(Path::new("readme.mdwn"))); + assert!(is_supported_ext_fmt(Path::new("readme.mdown"))); + assert!(is_supported_ext_fmt(Path::new("readme.markdown"))); + assert!(is_supported_ext_fmt(Path::new("lib/typescript.d.ts"))); + assert!(is_supported_ext_fmt(Path::new("testdata/001_hello.js"))); + assert!(is_supported_ext_fmt(Path::new("testdata/002_hello.ts"))); + assert!(is_supported_ext_fmt(Path::new("foo.jsx"))); + assert!(is_supported_ext_fmt(Path::new("foo.tsx"))); + assert!(is_supported_ext_fmt(Path::new("foo.TS"))); + assert!(is_supported_ext_fmt(Path::new("foo.TSX"))); + assert!(is_supported_ext_fmt(Path::new("foo.JS"))); + assert!(is_supported_ext_fmt(Path::new("foo.JSX"))); + assert!(is_supported_ext_fmt(Path::new("foo.mjs"))); + assert!(!is_supported_ext_fmt(Path::new("foo.mjsx"))); + assert!(is_supported_ext_fmt(Path::new("foo.jsonc"))); + assert!(is_supported_ext_fmt(Path::new("foo.JSONC"))); + assert!(is_supported_ext_fmt(Path::new("foo.json"))); + assert!(is_supported_ext_fmt(Path::new("foo.JsON"))); +}