From 16036a8a5170030a95a56b28b29b1b355d0d0f80 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Tue, 19 Jan 2021 23:09:35 +0530 Subject: feat: add markdown support to deno fmt (#8887) This commit adds support for formatting markdown files with "deno fmt". Additionally "--ext={js|jsx|ts|tsx|md}" flag was added to "deno fmt" that allows to specify file type when providing contents over stdio. --- cli/fs_util.rs | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'cli/fs_util.rs') diff --git a/cli/fs_util.rs b/cli/fs_util.rs index 130a209ce..a6cd06e78 100644 --- a/cli/fs_util.rs +++ b/cli/fs_util.rs @@ -90,17 +90,30 @@ pub fn resolve_from_cwd(path: &Path) -> Result { /// Checks if the path has extension Deno supports. pub fn is_supported_ext(path: &Path) -> bool { - let lowercase_ext = path - .extension() - .and_then(|e| e.to_str()) - .map(|e| e.to_lowercase()); - if let Some(ext) = lowercase_ext { - ext == "ts" || ext == "tsx" || ext == "js" || ext == "jsx" || ext == "mjs" + if let Some(ext) = get_extension(path) { + matches!(ext.as_str(), "ts" | "tsx" | "js" | "jsx" | "mjs") + } else { + false + } +} + +/// This function is similar to is_supported_ext but also allows .md extension. +pub fn is_supported_ext_md(path: &Path) -> bool { + if let Some(ext) = get_extension(path) { + matches!(ext.as_str(), "ts" | "tsx" | "js" | "jsx" | "mjs" | "md") } else { false } } +/// Get the extension of a file in lowercase. +pub fn get_extension(file_path: &Path) -> Option { + return file_path + .extension() + .and_then(|e| e.to_str()) + .map(|e| e.to_lowercase()); +} + /// Collects file paths that satisfy the given predicate, by recursively walking `files`. /// If the walker visits a path that is listed in `ignore`, it skips descending into the directory. pub fn collect_files

( @@ -207,6 +220,24 @@ mod tests { assert!(!is_supported_ext(Path::new("foo.mjsx"))); } + #[test] + fn test_is_supported_ext_md() { + assert!(!is_supported_ext_md(Path::new("tests/subdir/redirects"))); + assert!(is_supported_ext_md(Path::new("README.md"))); + assert!(is_supported_ext_md(Path::new("readme.MD"))); + assert!(is_supported_ext_md(Path::new("lib/typescript.d.ts"))); + assert!(is_supported_ext_md(Path::new("cli/tests/001_hello.js"))); + assert!(is_supported_ext_md(Path::new("cli/tests/002_hello.ts"))); + assert!(is_supported_ext_md(Path::new("foo.jsx"))); + assert!(is_supported_ext_md(Path::new("foo.tsx"))); + assert!(is_supported_ext_md(Path::new("foo.TS"))); + assert!(is_supported_ext_md(Path::new("foo.TSX"))); + assert!(is_supported_ext_md(Path::new("foo.JS"))); + assert!(is_supported_ext_md(Path::new("foo.JSX"))); + assert!(is_supported_ext_md(Path::new("foo.mjs"))); + assert!(!is_supported_ext_md(Path::new("foo.mjsx"))); + } + #[test] fn test_collect_files() { fn create_files(dir_path: &PathBuf, files: &[&str]) { -- cgit v1.2.3