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 {
|
||||
check: bool,
|
||||
files: Vec<String>,
|
||||
ignore: Vec<String>,
|
||||
files: Vec<PathBuf>,
|
||||
ignore: Vec<PathBuf>,
|
||||
},
|
||||
Info {
|
||||
json: bool,
|
||||
|
@ -55,8 +55,8 @@ pub enum DenoSubcommand {
|
|||
force: bool,
|
||||
},
|
||||
Lint {
|
||||
files: Vec<String>,
|
||||
ignore: Vec<String>,
|
||||
files: Vec<PathBuf>,
|
||||
ignore: Vec<PathBuf>,
|
||||
rules: bool,
|
||||
json: bool,
|
||||
},
|
||||
|
@ -106,7 +106,7 @@ pub struct Flags {
|
|||
pub cached_only: bool,
|
||||
pub config_path: Option<String>,
|
||||
pub coverage: bool,
|
||||
pub ignore: Vec<String>,
|
||||
pub ignore: Vec<PathBuf>,
|
||||
pub import_map_path: Option<String>,
|
||||
pub inspect: 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) {
|
||||
let files = match matches.values_of("files") {
|
||||
Some(f) => f.map(String::from).collect(),
|
||||
Some(f) => f.map(PathBuf::from).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let ignore = match matches.values_of("ignore") {
|
||||
Some(f) => f.map(String::from).collect(),
|
||||
Some(f) => f.map(PathBuf::from).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
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) {
|
||||
let files = match matches.values_of("files") {
|
||||
Some(f) => f.map(String::from).collect(),
|
||||
Some(f) => f.map(PathBuf::from).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let ignore = match matches.values_of("ignore") {
|
||||
Some(f) => f.map(String::from).collect(),
|
||||
Some(f) => f.map(PathBuf::from).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let rules = matches.is_present("rules");
|
||||
|
@ -1781,7 +1781,10 @@ mod tests {
|
|||
subcommand: DenoSubcommand::Fmt {
|
||||
ignore: vec![],
|
||||
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()
|
||||
}
|
||||
|
@ -1827,7 +1830,10 @@ mod tests {
|
|||
r.unwrap(),
|
||||
Flags {
|
||||
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,
|
||||
json: false,
|
||||
ignore: vec![],
|
||||
|
@ -1850,7 +1856,10 @@ mod tests {
|
|||
files: vec![],
|
||||
rules: 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,
|
||||
..Flags::default()
|
||||
|
@ -1883,7 +1892,7 @@ mod tests {
|
|||
r.unwrap(),
|
||||
Flags {
|
||||
subcommand: DenoSubcommand::Lint {
|
||||
files: vec!["script_1.ts".to_string()],
|
||||
files: vec![PathBuf::from("script_1.ts")],
|
||||
rules: false,
|
||||
json: true,
|
||||
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`
|
||||
/// then the current directory is recursively walked.
|
||||
pub async fn format(
|
||||
args: Vec<String>,
|
||||
args: Vec<PathBuf>,
|
||||
check: bool,
|
||||
exclude: Vec<String>,
|
||||
exclude: Vec<PathBuf>,
|
||||
) -> Result<(), AnyError> {
|
||||
if args.len() == 1 && args[0] == "-" {
|
||||
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||
return format_stdin(check);
|
||||
}
|
||||
// collect all files provided.
|
||||
|
@ -232,7 +232,7 @@ fn is_supported(path: &Path) -> bool {
|
|||
}
|
||||
|
||||
pub fn collect_files(
|
||||
files: Vec<String>,
|
||||
files: Vec<PathBuf>,
|
||||
) -> Result<Vec<PathBuf>, std::io::Error> {
|
||||
let mut target_files: Vec<PathBuf> = vec![];
|
||||
|
||||
|
@ -242,12 +242,12 @@ pub fn collect_files(
|
|||
is_supported,
|
||||
));
|
||||
} else {
|
||||
for arg in files {
|
||||
let p = PathBuf::from(arg);
|
||||
if p.is_dir() {
|
||||
target_files.extend(files_in_subtree(p.canonicalize()?, is_supported));
|
||||
for file in files {
|
||||
if file.is_dir() {
|
||||
target_files
|
||||
.extend(files_in_subtree(file.canonicalize()?, is_supported));
|
||||
} 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(
|
||||
args: Vec<String>,
|
||||
ignore: Vec<String>,
|
||||
args: Vec<PathBuf>,
|
||||
ignore: Vec<PathBuf>,
|
||||
json: bool,
|
||||
) -> Result<(), AnyError> {
|
||||
if args.len() == 1 && args[0] == "-" {
|
||||
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||
return lint_stdin(json);
|
||||
}
|
||||
let mut target_files = collect_files(args)?;
|
||||
|
|
|
@ -217,9 +217,9 @@ async fn install_command(
|
|||
|
||||
async fn lint_command(
|
||||
flags: Flags,
|
||||
files: Vec<String>,
|
||||
files: Vec<PathBuf>,
|
||||
list_rules: bool,
|
||||
ignore: Vec<String>,
|
||||
ignore: Vec<PathBuf>,
|
||||
json: bool,
|
||||
) -> Result<(), AnyError> {
|
||||
if !flags.unstable {
|
||||
|
|
Loading…
Reference in a new issue