Skip to content

Refactor body_literals to eliminate unnecessary helper functions and simplify logic #91

@sourcery-ai

Description

@sourcery-ai

The current implementation of the body_literals function relies on several small helper functions (extract_literals_from_body, process_body_element, process_token, and add_literal_if_not_empty) to parse and collect body literals. This approach increases code complexity and can be simplified.

Suggested Improvement:
Refactor body_literals to perform a single pass that collects and splits the body text, eliminating the need for the four helper functions. The new implementation would:

  • Skip tokens up to and including the :- token.
  • Collect all tokens until the terminating ..
  • Concatenate the text, split on commas, trim whitespace, and discard empty pieces.

Example Implementation:

#[must_use]
pub fn body_literals(&self) -> Vec<String> {
    use rowan::NodeOrToken;
    use crate::SyntaxKind::{T_DOT, T_IMPLIES};

    // Skip up through the “:-” token, take everything until the terminating “.”
    let body_text: String = self.syntax
        .children_with_tokens()
        .skip_while(|e| e.kind() != T_IMPLIES)
        .skip(1)  // drop the “:-”
        .take_while(|e| e.kind() != T_DOT)
        .map(|e| match e {
            NodeOrToken::Node(n) => n.text().to_string(),
            NodeOrToken::Token(t) => t.text().to_string(),
        })
        .collect();

    // Split on commas, trim, and discard any empty pieces
    body_text
        .split(',')
        .map(str::trim)
        .filter(|lit| !lit.is_empty())
        .map(str::to_string)
        .collect()
}

Action Items:

  • Refactor body_literals as described above.
  • Remove the now-unnecessary helper functions.
  • Ensure all tests pass and output remains unchanged.

This change will simplify the codebase and improve maintainability.


I created this issue for @leynos from #89 (comment).

Tips and commands

Getting Help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions