mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
refactor(cli): use PathBuf instead of String for lint and fmt subcommands (#8042)
This commit is contained in:
parent
0fb39f9176
commit
d6c824a6c3
4 changed files with 36 additions and 27 deletions
35
cli/flags.rs
35
cli/flags.rs
|
@ -40,8 +40,8 @@ pub enum DenoSubcommand {
|
||||||
},
|
},
|
||||||
Fmt {
|
Fmt {
|
||||||
check: bool,
|
check: bool,
|
||||||
files: Vec<String>,
|
files: Vec<PathBuf>,
|
||||||
ignore: Vec<String>,
|
ignore: Vec<PathBuf>,
|
||||||
},
|
},
|
||||||
Info {
|
Info {
|
||||||
json: bool,
|
json: bool,
|
||||||
|
@ -55,8 +55,8 @@ pub enum DenoSubcommand {
|
||||||
force: bool,
|
force: bool,
|
||||||
},
|
},
|
||||||
Lint {
|
Lint {
|
||||||
files: Vec<String>,
|
files: Vec<PathBuf>,
|
||||||
ignore: Vec<String>,
|
ignore: Vec<PathBuf>,
|
||||||
rules: bool,
|
rules: bool,
|
||||||
json: bool,
|
json: bool,
|
||||||
},
|
},
|
||||||
|
@ -106,7 +106,7 @@ pub struct Flags {
|
||||||
pub cached_only: bool,
|
pub cached_only: bool,
|
||||||
pub config_path: Option<String>,
|
pub config_path: Option<String>,
|
||||||
pub coverage: bool,
|
pub coverage: bool,
|
||||||
pub ignore: Vec<String>,
|
pub ignore: Vec<PathBuf>,
|
||||||
pub import_map_path: Option<String>,
|
pub import_map_path: Option<String>,
|
||||||
pub inspect: Option<SocketAddr>,
|
pub inspect: Option<SocketAddr>,
|
||||||
pub inspect_brk: Option<SocketAddr>,
|
pub inspect_brk: Option<SocketAddr>,
|
||||||
|
@ -357,11 +357,11 @@ fn types_parse(flags: &mut Flags, _matches: &clap::ArgMatches) {
|
||||||
|
|
||||||
fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
||||||
let files = match matches.values_of("files") {
|
let files = match matches.values_of("files") {
|
||||||
Some(f) => f.map(String::from).collect(),
|
Some(f) => f.map(PathBuf::from).collect(),
|
||||||
None => vec![],
|
None => vec![],
|
||||||
};
|
};
|
||||||
let ignore = match matches.values_of("ignore") {
|
let ignore = match matches.values_of("ignore") {
|
||||||
Some(f) => f.map(String::from).collect(),
|
Some(f) => f.map(PathBuf::from).collect(),
|
||||||
None => vec![],
|
None => vec![],
|
||||||
};
|
};
|
||||||
flags.subcommand = DenoSubcommand::Fmt {
|
flags.subcommand = DenoSubcommand::Fmt {
|
||||||
|
@ -639,11 +639,11 @@ fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
||||||
|
|
||||||
fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
||||||
let files = match matches.values_of("files") {
|
let files = match matches.values_of("files") {
|
||||||
Some(f) => f.map(String::from).collect(),
|
Some(f) => f.map(PathBuf::from).collect(),
|
||||||
None => vec![],
|
None => vec![],
|
||||||
};
|
};
|
||||||
let ignore = match matches.values_of("ignore") {
|
let ignore = match matches.values_of("ignore") {
|
||||||
Some(f) => f.map(String::from).collect(),
|
Some(f) => f.map(PathBuf::from).collect(),
|
||||||
None => vec![],
|
None => vec![],
|
||||||
};
|
};
|
||||||
let rules = matches.is_present("rules");
|
let rules = matches.is_present("rules");
|
||||||
|
@ -1781,7 +1781,10 @@ mod tests {
|
||||||
subcommand: DenoSubcommand::Fmt {
|
subcommand: DenoSubcommand::Fmt {
|
||||||
ignore: vec![],
|
ignore: vec![],
|
||||||
check: false,
|
check: false,
|
||||||
files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()],
|
files: vec![
|
||||||
|
PathBuf::from("script_1.ts"),
|
||||||
|
PathBuf::from("script_2.ts")
|
||||||
|
],
|
||||||
},
|
},
|
||||||
..Flags::default()
|
..Flags::default()
|
||||||
}
|
}
|
||||||
|
@ -1827,7 +1830,10 @@ mod tests {
|
||||||
r.unwrap(),
|
r.unwrap(),
|
||||||
Flags {
|
Flags {
|
||||||
subcommand: DenoSubcommand::Lint {
|
subcommand: DenoSubcommand::Lint {
|
||||||
files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()],
|
files: vec![
|
||||||
|
PathBuf::from("script_1.ts"),
|
||||||
|
PathBuf::from("script_2.ts")
|
||||||
|
],
|
||||||
rules: false,
|
rules: false,
|
||||||
json: false,
|
json: false,
|
||||||
ignore: vec![],
|
ignore: vec![],
|
||||||
|
@ -1850,7 +1856,10 @@ mod tests {
|
||||||
files: vec![],
|
files: vec![],
|
||||||
rules: false,
|
rules: false,
|
||||||
json: false,
|
json: false,
|
||||||
ignore: svec!["script_1.ts", "script_2.ts"],
|
ignore: vec![
|
||||||
|
PathBuf::from("script_1.ts"),
|
||||||
|
PathBuf::from("script_2.ts")
|
||||||
|
],
|
||||||
},
|
},
|
||||||
unstable: true,
|
unstable: true,
|
||||||
..Flags::default()
|
..Flags::default()
|
||||||
|
@ -1883,7 +1892,7 @@ mod tests {
|
||||||
r.unwrap(),
|
r.unwrap(),
|
||||||
Flags {
|
Flags {
|
||||||
subcommand: DenoSubcommand::Lint {
|
subcommand: DenoSubcommand::Lint {
|
||||||
files: vec!["script_1.ts".to_string()],
|
files: vec![PathBuf::from("script_1.ts")],
|
||||||
rules: false,
|
rules: false,
|
||||||
json: true,
|
json: true,
|
||||||
ignore: vec![],
|
ignore: vec![],
|
||||||
|
|
18
cli/fmt.rs
18
cli/fmt.rs
|
@ -32,11 +32,11 @@ const BOM_CHAR: char = '\u{FEFF}';
|
||||||
/// First argument and ignore supports globs, and if it is `None`
|
/// First argument and ignore supports globs, and if it is `None`
|
||||||
/// then the current directory is recursively walked.
|
/// then the current directory is recursively walked.
|
||||||
pub async fn format(
|
pub async fn format(
|
||||||
args: Vec<String>,
|
args: Vec<PathBuf>,
|
||||||
check: bool,
|
check: bool,
|
||||||
exclude: Vec<String>,
|
exclude: Vec<PathBuf>,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
if args.len() == 1 && args[0] == "-" {
|
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||||
return format_stdin(check);
|
return format_stdin(check);
|
||||||
}
|
}
|
||||||
// collect all files provided.
|
// collect all files provided.
|
||||||
|
@ -232,7 +232,7 @@ fn is_supported(path: &Path) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn collect_files(
|
pub fn collect_files(
|
||||||
files: Vec<String>,
|
files: Vec<PathBuf>,
|
||||||
) -> Result<Vec<PathBuf>, std::io::Error> {
|
) -> Result<Vec<PathBuf>, std::io::Error> {
|
||||||
let mut target_files: Vec<PathBuf> = vec![];
|
let mut target_files: Vec<PathBuf> = vec![];
|
||||||
|
|
||||||
|
@ -242,12 +242,12 @@ pub fn collect_files(
|
||||||
is_supported,
|
is_supported,
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
for arg in files {
|
for file in files {
|
||||||
let p = PathBuf::from(arg);
|
if file.is_dir() {
|
||||||
if p.is_dir() {
|
target_files
|
||||||
target_files.extend(files_in_subtree(p.canonicalize()?, is_supported));
|
.extend(files_in_subtree(file.canonicalize()?, is_supported));
|
||||||
} else {
|
} else {
|
||||||
target_files.push(p.canonicalize()?);
|
target_files.push(file.canonicalize()?);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,11 +40,11 @@ fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn lint_files(
|
pub async fn lint_files(
|
||||||
args: Vec<String>,
|
args: Vec<PathBuf>,
|
||||||
ignore: Vec<String>,
|
ignore: Vec<PathBuf>,
|
||||||
json: bool,
|
json: bool,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
if args.len() == 1 && args[0] == "-" {
|
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||||
return lint_stdin(json);
|
return lint_stdin(json);
|
||||||
}
|
}
|
||||||
let mut target_files = collect_files(args)?;
|
let mut target_files = collect_files(args)?;
|
||||||
|
|
|
@ -217,9 +217,9 @@ async fn install_command(
|
||||||
|
|
||||||
async fn lint_command(
|
async fn lint_command(
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
files: Vec<String>,
|
files: Vec<PathBuf>,
|
||||||
list_rules: bool,
|
list_rules: bool,
|
||||||
ignore: Vec<String>,
|
ignore: Vec<PathBuf>,
|
||||||
json: bool,
|
json: bool,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
if !flags.unstable {
|
if !flags.unstable {
|
||||||
|
|
Loading…
Reference in a new issue