Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't give empty documentation to starlark-rust #44

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,4 +1277,37 @@ mod tests {

Ok(())
}

#[test]
/// Empty summary in DocString strings break starlark-rust. See #41. Here
/// we ensure that instead of generating an empty summary, the whole
/// DocString is None.
fn no_empty_documentation_is_produced() -> anyhow::Result<()> {
let fixture = TestFixture::new("simple")?;
let context = fixture.context()?;

let module = context.get_environment(&LspUrl::File(PathBuf::from("/foo/bar/defs.bzl")));

fn check_doc_not_empty(doc: Option<&DocString>) {
if let Some(doc) = doc {
assert!(!doc.summary.trim().is_empty());
}
}

for (name, member) in module.members {
match member {
DocMember::Function(function) => {
check_doc_not_empty(function.docs.as_ref());
for param in function.params {
check_doc_not_empty(param.get_doc_string());
}
}
DocMember::Property(property) => {
check_doc_not_empty(property.docs.as_ref());
}
}
}

Ok(())
}
}
41 changes: 24 additions & 17 deletions src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ pub fn build_language_to_doc_members<'a>(

pub fn rule_to_doc_member(rule: &RuleDefinition) -> DocMember {
DocMember::Function(DocFunction {
docs: rule.documentation.as_ref().map(|doc| DocString {
summary: doc.clone(),
details: None,
}),
docs: rule
.documentation
.as_ref()
.and_then(|doc| create_docstring(doc)),
params: rule
.attribute
.iter()
.map(|attribute| DocParam::Arg {
name: attribute.name.clone(),
docs: attribute.documentation.as_ref().map(|doc| DocString {
summary: doc.clone(),
details: None,
}),
docs: attribute
.documentation
.as_ref()
.and_then(|doc| create_docstring(doc)),
typ: Ty::any(),
default_value: None,
})
Expand All @@ -55,11 +55,8 @@ pub fn builtins_to_doc_members<'a>(
})
}

pub fn value_to_doc_member(value: &Value) -> DocMember {
let docs = Some(DocString {
summary: value.doc.clone(),
details: None,
});
fn value_to_doc_member(value: &Value) -> DocMember {
let docs = create_docstring(&value.doc);

if let Some(callable) = &value.callable {
DocMember::Function(DocFunction {
Expand All @@ -69,10 +66,7 @@ pub fn value_to_doc_member(value: &Value) -> DocMember {
.iter()
.map(|param| DocParam::Arg {
name: param.name.clone(),
docs: Some(DocString {
summary: param.doc.clone(),
details: None,
}),
docs: create_docstring(&param.doc),
typ: Ty::any(),
default_value: if param.is_mandatory {
None
Expand All @@ -90,3 +84,16 @@ pub fn value_to_doc_member(value: &Value) -> DocMember {
})
}
}

fn create_docstring(summary: &str) -> Option<DocString> {
let summary = summary.trim();

if summary.is_empty() {
None
} else {
Some(DocString {
summary: summary.to_string(),
details: None,
})
}
}