Skip to content

Commit

Permalink
Make script for linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Dec 31, 2023
1 parent 34d7189 commit 13d7808
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 50 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,4 @@ jobs:
run: rustup component add clippy
- name: Lint
run: |
cargo clippy -- \
-Dwarnings \
-Wclippy::all \
-Wclippy::pedantic \
-Aclippy::comparison_chain \
-Aclippy::missing-panics-doc \
-Aclippy::module-name-repetitions \
-Aclippy::redundant-closure-for-method-calls
./scripts/lint.sh
39 changes: 19 additions & 20 deletions src/gen/nu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,24 @@ fn generate_cmd(cmd_name: &str, cmd: &CommandInfo, out: &mut Output) {
};

let type_str = if let Some(typ) = flag.typ.as_ref() {
match typ {
ArgType::Unknown => ": string".to_owned(),
_ => {
// Turn it into a valid Nu identifier
let first_form = if forms[0].starts_with("--") {
&forms[0][2..]
} else if forms[0].starts_with('-') {
&forms[0][1..]
} else {
&forms[0]
};
// This may cause collisions if there are flags with underscores, but
// that's unlikely
let first_form = first_form.replace("-", "_");
let res =
format!(r#": string@"nu-complete {} {}""#, cmd_name, &first_form);
complicated_flags.push((first_form, typ));
res
}
if typ == &ArgType::Unknown {
": string".to_owned()
} else {
// Turn it into a valid Nu identifier
let first_form = if forms[0].starts_with("--") {
&forms[0][2..]
} else if forms[0].starts_with('-') {
&forms[0][1..]
} else {
&forms[0]
};
// This may cause collisions if there are flags with underscores, but
// that's unlikely
let first_form = first_form.replace('-', "_");
let res =
format!(r#": string@"nu-complete {} {}""#, cmd_name, &first_form);
complicated_flags.push((first_form, typ));
res
}
} else {
String::new()
Expand All @@ -82,7 +81,7 @@ fn generate_cmd(cmd_name: &str, cmd: &CommandInfo, out: &mut Output) {
));
}

for form in long_forms.into_iter().chain(short_forms) {
for form in long_forms.chain(short_forms) {
flags_strs.push(format!("{form}{type_str}{desc_str}"));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This is a library for generating completions either by parsing manpages or
//! from KDL/JSON/YAML config files. If you're looking for the CLI tool, head to
//! https://crates.io/crates/gen-completions
//! <https://crates.io/crates/gen-completions>
//!
//! The [`parse_man`] module parses manpages, while the [`parse_deser`] module
//! deserializes a KDL/JSON/YAML file to get command information. Both produce
Expand Down
41 changes: 20 additions & 21 deletions src/parse_deser/kdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn kdl_to_cmd_info(node: &KdlNode) -> ParseResult<CommandInfo> {
help: None,
});
}
desc = Some(strip_quotes(&desc_node.entries()[0].to_string()));
desc = Some(strip_quotes(desc_node.entries()[0].to_string()));
}

if let Some(subcmds_doc) =
Expand Down Expand Up @@ -214,18 +214,18 @@ fn parse_flag(
let mut typ = None;

// The name of the node itself will be the first flag
let first_flag = strip_quotes(&node.name().to_string());
let first_flag = strip_quotes(node.name().to_string());
if let Some(prev_span) = flag_spans.get(&first_flag) {
return Err(ParseError::DuplicateFlag {
flag: first_flag,
span: *node.name().span(),
prev_span: *prev_span,
});
} else {
forms.push(first_flag.clone());
flag_spans.insert(first_flag, *node.name().span());
}

forms.push(first_flag.clone());
flag_spans.insert(first_flag, *node.name().span());

// The other flags will be parsed as entries
for flag_entry in node.entries() {
if let Some(name) = flag_entry.name() {
Expand All @@ -238,18 +238,18 @@ fn parse_flag(
msg: flag_entry.to_string(),
span: *flag_entry.span(),
});
} else {
let flag = strip_quotes(&flag_entry.value().to_string());
if let Some(prev_span) = flag_spans.get(&flag) {
return Err(ParseError::DuplicateFlag {
flag,
span: *flag_entry.span(),
prev_span: *prev_span,
});
}
forms.push(flag.clone());
flag_spans.insert(flag, *node.name().span());
}

let flag = strip_quotes(flag_entry.value().to_string());
if let Some(prev_span) = flag_spans.get(&flag) {
return Err(ParseError::DuplicateFlag {
flag,
span: *flag_entry.span(),
prev_span: *prev_span,
});
}
forms.push(flag.clone());
flag_spans.insert(flag, *node.name().span());
}

if let Some(doc) = node.children() {
Expand All @@ -258,7 +258,7 @@ fn parse_flag(
if let Some(desc_node) = nodes.get("desc") {
if desc_node.entries().len() == 1 {
// todo account for invalid entry with name
desc = Some(strip_quotes(&desc_node.entries()[0].value().to_string()));
desc = Some(strip_quotes(desc_node.entries()[0].value().to_string()));
} else {
todo!()
}
Expand Down Expand Up @@ -299,7 +299,7 @@ fn parse_type(node: &KdlNode) -> ParseResult<ArgType> {
error: "'strings' type should have no entries".to_owned(),
span: *node.span(),
label: "this stuff shouldn't be here".to_owned(),
help: Some(r#"Write out the strings like 'strings {...}' instead of 'strings ...'"#.to_owned()),
help: Some("Write out the strings like 'strings {...}' instead of 'strings ...'".to_owned()),
});
}

Expand Down Expand Up @@ -334,7 +334,7 @@ fn parse_type(node: &KdlNode) -> ParseResult<ArgType> {
cmd: node
.entries()
.iter()
.map(|entry| strip_quotes(&entry.to_string()))
.map(|entry| strip_quotes(entry.to_string()))
.collect::<Vec<_>>()
.join(" "),
sep: None,
Expand Down Expand Up @@ -392,8 +392,7 @@ fn strip_quotes(flag: impl AsRef<str>) -> String {
flag
.trim()
.strip_prefix('"')
.map(|s| s.strip_suffix('"').unwrap())
.unwrap_or(flag)
.map_or(flag, |s| s.strip_suffix('"').unwrap())
.to_string()
}

Expand Down

0 comments on commit 13d7808

Please sign in to comment.