From 60f2d57fb7a01a438e99c5d2ea655cfa44641755 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 4 May 2020 15:17:15 -0400 Subject: [PATCH] feat(fmt): Add `deno-fmt-ignore` and `deno-fmt-ignore-file` comment support (#5075) --- Cargo.lock | 8 ++-- cli/Cargo.toml | 2 +- cli/flags.rs | 8 +++- cli/fmt.rs | 110 ++++++++++++++++++++++++------------------------- std/manual.md | 34 +++++++++++++++ 5 files changed, 101 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a70630592d..09542b7e05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -618,18 +618,18 @@ checksum = "52ba6eb47c2131e784a38b726eb54c1e1484904f013e576a25354d0124161af6" [[package]] name = "dprint-core" -version = "0.16.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fe2ae2e02c20dcb77d422c6326db6c2a11a3dd37eb012b1cf69703685d272fb" +checksum = "51503534b100175b33c3a7ed71839c8027ae16be1092ad93c7bb7cb2f8a06bcb" dependencies = [ "serde", ] [[package]] name = "dprint-plugin-typescript" -version = "0.14.1" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8f6bd2fcf216220b940683d47aa9a6231a3f039ae3303067ed3af60e606eb04" +checksum = "afad8c8794d11dca59f4257f2901252e3e566704ac9810dad0eee694f1dc2ce7" dependencies = [ "dprint-core", "serde", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 728856b9cd..fc899e2d9f 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -33,7 +33,7 @@ byteorder = "1.3.4" clap = "2.33.0" dirs = "2.0.2" dlopen = "0.1.8" -dprint-plugin-typescript = "0.14.1" +dprint-plugin-typescript = "0.16.0" futures = { version = "0.3.4", features = ["compat", "io-compat"] } glob = "0.3.0" http = "0.2.1" diff --git a/cli/flags.rs b/cli/flags.rs index decf3cb175..f87c338c4c 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -601,7 +601,13 @@ fn fmt_subcommand<'a, 'b>() -> App<'a, 'b> { deno fmt --check Format stdin and write to stdout: - cat file.ts | deno fmt -", + cat file.ts | deno fmt - + +Ignore formatting code by preceding it with an ignore comment: + // deno-fmt-ignore + +Ignore formatting a file by adding an ignore comment at the top of the file: + // deno-fmt-ignore-file", ) .arg( Arg::with_name("check") diff --git a/cli/fmt.rs b/cli/fmt.rs index d6f2efd960..74d3d0eea7 100644 --- a/cli/fmt.rs +++ b/cli/fmt.rs @@ -21,21 +21,38 @@ use std::path::PathBuf; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; -fn is_supported(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" - } else { - false +/// Format JavaScript/TypeScript files. +/// +/// First argument supports globs, and if it is `None` +/// then the current directory is recursively walked. +pub async fn format(args: Vec, check: bool) -> Result<(), ErrBox> { + if args.len() == 1 && args[0] == "-" { + return format_stdin(check); } -} -fn get_config() -> dprint::configuration::Configuration { - use dprint::configuration::*; - ConfigurationBuilder::new().deno().build() + let mut target_files: Vec = vec![]; + + if args.is_empty() { + target_files.extend(files_in_subtree( + std::env::current_dir().unwrap(), + is_supported, + )); + } else { + for arg in args { + let p = PathBuf::from(arg); + if p.is_dir() { + target_files.extend(files_in_subtree(p, is_supported)); + } else { + target_files.push(p); + }; + } + } + let config = get_config(); + if check { + check_source_files(config, target_files).await + } else { + format_source_files(config, target_files).await + } } async fn check_source_files( @@ -84,14 +101,6 @@ async fn check_source_files( } } -fn files_str(len: usize) -> &'static str { - if len == 1 { - "file" - } else { - "files" - } -} - async fn format_source_files( config: dprint::configuration::Configuration, paths: Vec, @@ -134,40 +143,6 @@ async fn format_source_files( Ok(()) } -/// Format JavaScript/TypeScript files. -/// -/// First argument supports globs, and if it is `None` -/// then the current directory is recursively walked. -pub async fn format(args: Vec, check: bool) -> Result<(), ErrBox> { - if args.len() == 1 && args[0] == "-" { - return format_stdin(check); - } - - let mut target_files: Vec = vec![]; - - if args.is_empty() { - target_files.extend(files_in_subtree( - std::env::current_dir().unwrap(), - is_supported, - )); - } else { - for arg in args { - let p = PathBuf::from(arg); - if p.is_dir() { - target_files.extend(files_in_subtree(p, is_supported)); - } else { - target_files.push(p); - }; - } - } - let config = get_config(); - if check { - check_source_files(config, target_files).await - } else { - format_source_files(config, target_files).await - } -} - /// Format stdin and write result to stdout. /// Treats input as TypeScript. /// Compatible with `--check` flag. @@ -196,6 +171,31 @@ fn format_stdin(check: bool) -> Result<(), ErrBox> { Ok(()) } +fn files_str(len: usize) -> &'static str { + if len == 1 { + "file" + } else { + "files" + } +} + +fn is_supported(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" + } else { + false + } +} + +fn get_config() -> dprint::configuration::Configuration { + use dprint::configuration::*; + ConfigurationBuilder::new().deno().build() +} + async fn run_parallelized( file_paths: Vec, f: F, diff --git a/std/manual.md b/std/manual.md index 29abe2c661..8df8adfd3b 100644 --- a/std/manual.md +++ b/std/manual.md @@ -1298,6 +1298,40 @@ All listeners added using `window.addEventListener` were run, but `window.onload` and `window.onunload` defined in `main.ts` overridden handlers defined in `imported.ts`. +## `deno fmt` + +Deno ships with a built in code formatter that auto-formats TypeScript and +JavaScript code. + +```shell +# format all JS/TS files in the current directory and subdirectories +deno fmt +# format specific files +deno fmt myfile1.ts myfile2.ts +# check if all the JS/TS files in the current directory and subdirectories are formatted +deno fmt --check +# format stdin and write to stdout +cat file.ts | deno fmt - +``` + +Ignore formatting code by preceding it with a `// deno-fmt-ignore` comment: + + + +```ts +// deno-fmt-ignore +export const identity = [ + 1, 0, 0, + 0, 1, 0, + 0, 0, 1, +]; +``` + + + +Or ignore an entire file by adding a `// deno-fmt-ignore-file` comment at the +top of the file. + ## Internal details ### Deno and Linux analogy