mirror of
https://github.com/denoland/deno.git
synced 2025-01-09 07:39:15 -05:00
feat(fmt): Add deno-fmt-ignore
and deno-fmt-ignore-file
comment support (#5075)
This commit is contained in:
parent
8c509bd885
commit
60f2d57fb7
5 changed files with 101 additions and 61 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -618,18 +618,18 @@ checksum = "52ba6eb47c2131e784a38b726eb54c1e1484904f013e576a25354d0124161af6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dprint-core"
|
name = "dprint-core"
|
||||||
version = "0.16.0"
|
version = "0.17.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2fe2ae2e02c20dcb77d422c6326db6c2a11a3dd37eb012b1cf69703685d272fb"
|
checksum = "51503534b100175b33c3a7ed71839c8027ae16be1092ad93c7bb7cb2f8a06bcb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dprint-plugin-typescript"
|
name = "dprint-plugin-typescript"
|
||||||
version = "0.14.1"
|
version = "0.16.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d8f6bd2fcf216220b940683d47aa9a6231a3f039ae3303067ed3af60e606eb04"
|
checksum = "afad8c8794d11dca59f4257f2901252e3e566704ac9810dad0eee694f1dc2ce7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"dprint-core",
|
"dprint-core",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -33,7 +33,7 @@ byteorder = "1.3.4"
|
||||||
clap = "2.33.0"
|
clap = "2.33.0"
|
||||||
dirs = "2.0.2"
|
dirs = "2.0.2"
|
||||||
dlopen = "0.1.8"
|
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"] }
|
futures = { version = "0.3.4", features = ["compat", "io-compat"] }
|
||||||
glob = "0.3.0"
|
glob = "0.3.0"
|
||||||
http = "0.2.1"
|
http = "0.2.1"
|
||||||
|
|
|
@ -601,7 +601,13 @@ fn fmt_subcommand<'a, 'b>() -> App<'a, 'b> {
|
||||||
deno fmt --check
|
deno fmt --check
|
||||||
|
|
||||||
Format stdin and write to stdout:
|
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(
|
||||||
Arg::with_name("check")
|
Arg::with_name("check")
|
||||||
|
|
110
cli/fmt.rs
110
cli/fmt.rs
|
@ -21,21 +21,38 @@ use std::path::PathBuf;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
fn is_supported(path: &Path) -> bool {
|
/// Format JavaScript/TypeScript files.
|
||||||
let lowercase_ext = path
|
///
|
||||||
.extension()
|
/// First argument supports globs, and if it is `None`
|
||||||
.and_then(|e| e.to_str())
|
/// then the current directory is recursively walked.
|
||||||
.map(|e| e.to_lowercase());
|
pub async fn format(args: Vec<String>, check: bool) -> Result<(), ErrBox> {
|
||||||
if let Some(ext) = lowercase_ext {
|
if args.len() == 1 && args[0] == "-" {
|
||||||
ext == "ts" || ext == "tsx" || ext == "js" || ext == "jsx"
|
return format_stdin(check);
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn get_config() -> dprint::configuration::Configuration {
|
let mut target_files: Vec<PathBuf> = vec![];
|
||||||
use dprint::configuration::*;
|
|
||||||
ConfigurationBuilder::new().deno().build()
|
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(
|
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(
|
async fn format_source_files(
|
||||||
config: dprint::configuration::Configuration,
|
config: dprint::configuration::Configuration,
|
||||||
paths: Vec<PathBuf>,
|
paths: Vec<PathBuf>,
|
||||||
|
@ -134,40 +143,6 @@ async fn format_source_files(
|
||||||
Ok(())
|
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<String>, check: bool) -> Result<(), ErrBox> {
|
|
||||||
if args.len() == 1 && args[0] == "-" {
|
|
||||||
return format_stdin(check);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut target_files: Vec<PathBuf> = 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.
|
/// Format stdin and write result to stdout.
|
||||||
/// Treats input as TypeScript.
|
/// Treats input as TypeScript.
|
||||||
/// Compatible with `--check` flag.
|
/// Compatible with `--check` flag.
|
||||||
|
@ -196,6 +171,31 @@ fn format_stdin(check: bool) -> Result<(), ErrBox> {
|
||||||
Ok(())
|
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<F>(
|
async fn run_parallelized<F>(
|
||||||
file_paths: Vec<PathBuf>,
|
file_paths: Vec<PathBuf>,
|
||||||
f: F,
|
f: F,
|
||||||
|
|
|
@ -1298,6 +1298,40 @@ All listeners added using `window.addEventListener` were run, but
|
||||||
`window.onload` and `window.onunload` defined in `main.ts` overridden handlers
|
`window.onload` and `window.onunload` defined in `main.ts` overridden handlers
|
||||||
defined in `imported.ts`.
|
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:
|
||||||
|
|
||||||
|
<!-- prettier-ignore-start -->
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// deno-fmt-ignore
|
||||||
|
export const identity = [
|
||||||
|
1, 0, 0,
|
||||||
|
0, 1, 0,
|
||||||
|
0, 0, 1,
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
<!-- prettier-ignore-end -->
|
||||||
|
|
||||||
|
Or ignore an entire file by adding a `// deno-fmt-ignore-file` comment at the
|
||||||
|
top of the file.
|
||||||
|
|
||||||
## Internal details
|
## Internal details
|
||||||
|
|
||||||
### Deno and Linux analogy
|
### Deno and Linux analogy
|
||||||
|
|
Loading…
Reference in a new issue