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

chore: extract more node compilers (I/L) #177

Closed
wants to merge 6 commits into from

Conversation

arendjr
Copy link
Contributor

@arendjr arendjr commented Apr 7, 2024

Applies the node compiler pattern from #164 to yet more node types.

See also: #173

Summary by CodeRabbit

  • New Features

    • Introduced compiler structures for If, Includes, Like, Limit, and ListIndex patterns, enhancing pattern compilation and processing.
  • Refactor

    • Modified visibility and construction logic for If, Includes, Like, Limit, and ListIndex structures, improving module access control and pattern handling.
    • Reorganized import statements and removed unused methods, streamlining code structure and dependencies.
  • Chores

    • Added new compiler modules to the public and crate scope, expanding the codebase's functionality and modular organization.

Copy link
Contributor

coderabbitai bot commented Apr 7, 2024

Walkthrough

This update transforms pattern handling by introducing specialized compiler classes for various patterns, enhancing modularity and code encapsulation. The transition signifies a strategic architectural shift towards more sophisticated and maintainable code, focusing on pattern compilation and visibility adjustments across structures.

Changes

Files Change Summary
.../src/pattern/{container, list_index}.rs Replaced direct pattern creation with compiler instances; adjusted visibility and structure.
.../src/pattern/{if, includes, like, limit}.rs Modified field visibility; removed from_node methods. Shifted pattern construction to compiler classes.
.../src/pattern/patterns.rs Updated to utilize compiler classes for pattern creation.
.../src/pattern_compiler/{if, includes, like, limit, list_index}_compiler.rs, .../pattern_compiler/mod.rs Introduced compiler structs implementing NodeCompiler trait for various patterns; added modules to public and crate scope.

"In a codebase, wide and vast,
Changes swift, none shall outlast.
🐇 With a hop and a compiler's grace,
Patterns form with newfound space.
Fields now public, structures tight,
Our code leaps forth into the light.
🌟 For every line that we refine,
A rabbit's touch, both bold and fine."

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 95bb648 and 7ffcd5b.
Files selected for processing (13)
  • crates/core/src/pattern/container.rs (2 hunks)
  • crates/core/src/pattern/if.rs (2 hunks)
  • crates/core/src/pattern/includes.rs (2 hunks)
  • crates/core/src/pattern/like.rs (1 hunks)
  • crates/core/src/pattern/limit.rs (2 hunks)
  • crates/core/src/pattern/list_index.rs (1 hunks)
  • crates/core/src/pattern/patterns.rs (6 hunks)
  • crates/core/src/pattern_compiler/if_compiler.rs (1 hunks)
  • crates/core/src/pattern_compiler/includes_compiler.rs (1 hunks)
  • crates/core/src/pattern_compiler/like_compiler.rs (1 hunks)
  • crates/core/src/pattern_compiler/limit_compiler.rs (1 hunks)
  • crates/core/src/pattern_compiler/list_index_compiler.rs (1 hunks)
  • crates/core/src/pattern_compiler/mod.rs (1 hunks)
Additional comments not posted (13)
crates/core/src/pattern_compiler/mod.rs (1)

10-14: The addition of new compiler modules enhances the project's modularity and structure.

crates/core/src/pattern_compiler/includes_compiler.rs (1)

8-36: Correct implementation of IncludesCompiler adhering to best practices and project structure.

crates/core/src/pattern/like.rs (1)

14-15: Changes in field visibility and the removal of the from_node method align with the PR's objectives to enhance modularity and maintainability.

crates/core/src/pattern_compiler/limit_compiler.rs (1)

8-42: Correct implementation of LimitCompiler adhering to best practices and project structure.

crates/core/src/pattern_compiler/like_compiler.rs (1)

10-52: Correct implementation of LikeCompiler adhering to best practices and project structure.

crates/core/src/pattern/limit.rs (1)

Line range hint 1-1: Removal of the from_node function aligns with the PR's objectives to centralize pattern compilation, enhancing modularity and maintainability.

crates/core/src/pattern_compiler/if_compiler.rs (1)

10-64: Correct implementation of IfCompiler adhering to best practices and project structure.

crates/core/src/pattern_compiler/list_index_compiler.rs (1)

13-76: Correct implementation of ListIndexCompiler adhering to best practices and project structure.

crates/core/src/pattern/if.rs (1)

18-20: Changing the visibility of the If struct fields from pub(crate) to pub increases their accessibility. Ensure that this change aligns with the intended encapsulation and usage patterns of the If struct within the broader codebase.

crates/core/src/pattern/container.rs (1)

73-73: Replacing direct instantiation of ListIndex with ListIndexCompiler::from_node aligns with the PR's goal of enhancing modularity and maintainability by using compiler patterns. This change centralizes the logic for creating ListIndex instances, which can simplify future modifications and ensure consistency.

crates/core/src/pattern/includes.rs (1)

4-7: The removal of unused imports, such as variable::VariableSourceLocations and Node, along with the adjustment of the State import, cleans up the code and aligns with Rust best practices for managing dependencies. This change contributes to the maintainability and readability of the code.

crates/core/src/pattern/list_index.rs (1)

18-32: Changing the visibility of the ListOrContainer and ContainerOrIndex enums, as well as the ListIndex struct fields from pub(crate) to pub, increases their accessibility. This change should be carefully considered to ensure it aligns with the intended encapsulation and usage patterns within the broader codebase. Additionally, ensure that the removal of the from_node method from the ListIndex struct is consistent with the PR's goal of centralizing node compilation logic.

crates/core/src/pattern/patterns.rs (1)

827-833: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [718-987]

Replacing direct calls to constructors with calls to specific compiler classes (LimitCompiler, IncludesCompiler, IfCompiler, ListIndexCompiler, and LikeCompiler) for creating Limit, Includes, If, ListIndex, and Like patterns, respectively, is a significant improvement. This approach enhances the modularity and maintainability of the code by centralizing the logic for pattern creation. Ensure that these changes are thoroughly tested to confirm that the behavior of pattern creation remains consistent.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7ffcd5b and 9f40b57.
Files selected for processing (5)
  • crates/core/src/pattern/log.rs (2 hunks)
  • crates/core/src/pattern/patterns.rs (7 hunks)
  • crates/core/src/pattern/predicates.rs (2 hunks)
  • crates/core/src/pattern_compiler/log_compiler.rs (1 hunks)
  • crates/core/src/pattern_compiler/mod.rs (1 hunks)
Additional comments not posted (5)
crates/core/src/pattern_compiler/mod.rs (1)

10-15: Ensure the newly added compiler modules are utilized appropriately within the codebase.

Verification successful

The verification process confirms that the newly added compiler modules (if_compiler, includes_compiler, like_compiler, limit_compiler, list_index_compiler, log_compiler) are utilized appropriately within the codebase, as evidenced by their references in various parts of the project.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the new compiler modules are imported and used in other parts of the project.
for module in if_compiler includes_compiler like_compiler limit_compiler list_index_compiler log_compiler; do
  echo "Checking usage of $module..."
  rg --type rust --count "$module::"
done

Length of output: 1698

crates/core/src/pattern/log.rs (2)

13-22: Making VariableInfo public enhances modularity by allowing external access to variable information within logs.


26-31: Public visibility of the Log constructor and methods aligns with the goal of improving code modularity and maintainability.

crates/core/src/pattern/predicates.rs (1)

130-130: Replacing Log with LogCompiler in the Predicate::Log variant is a key step towards centralizing compilation logic.

crates/core/src/pattern/patterns.rs (1)

827-833: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [718-987]

Replacing direct constructor calls with compiler class calls for Limit, Includes, If, ListIndex, and Like patterns enhances the code's modularity and maintainability.

Comment on lines +12 to +59
pub(crate) struct LogCompiler;

impl NodeCompiler for LogCompiler {
type TargetPattern = Log;

fn from_node(
node: &Node,
context: &CompilationContext,
vars: &mut BTreeMap<String, usize>,
vars_array: &mut Vec<Vec<VariableSourceLocations>>,
scope_index: usize,
global_vars: &mut BTreeMap<String, usize>,
logs: &mut AnalysisLogs,
) -> Result<Self::TargetPattern> {
let message = node.child_by_field_name("message");
let message = if let Some(message) = message {
Some(Pattern::from_node(
&message,
context,
vars,
vars_array,
scope_index,
global_vars,
false,
logs,
)?)
} else {
None
};
let variable_node = node.child_by_field_name("variable");
let variable = variable_node
.map(|n| {
let name = n.utf8_text(context.src.as_bytes()).unwrap().to_string();
let variable = Variable::from_node(
&n,
context.file,
context.src,
vars,
global_vars,
vars_array,
scope_index,
)?;
Ok(VariableInfo::new(name, variable))
})
.map_or(Ok(None), |v: Result<VariableInfo>| v.map(Some))?;

Ok(Log::new(variable, message))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of LogCompiler aligns with the compiler pattern expansion objective. However, consider adding error handling for the unwrap calls to prevent potential panics.

- let name = n.utf8_text(context.src.as_bytes()).unwrap().to_string();
+ let name = n.utf8_text(context.src.as_bytes()).ok_or_else(|| anyhow!("Failed to get utf8 text for variable node"))?.to_string();

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
pub(crate) struct LogCompiler;
impl NodeCompiler for LogCompiler {
type TargetPattern = Log;
fn from_node(
node: &Node,
context: &CompilationContext,
vars: &mut BTreeMap<String, usize>,
vars_array: &mut Vec<Vec<VariableSourceLocations>>,
scope_index: usize,
global_vars: &mut BTreeMap<String, usize>,
logs: &mut AnalysisLogs,
) -> Result<Self::TargetPattern> {
let message = node.child_by_field_name("message");
let message = if let Some(message) = message {
Some(Pattern::from_node(
&message,
context,
vars,
vars_array,
scope_index,
global_vars,
false,
logs,
)?)
} else {
None
};
let variable_node = node.child_by_field_name("variable");
let variable = variable_node
.map(|n| {
let name = n.utf8_text(context.src.as_bytes()).unwrap().to_string();
let variable = Variable::from_node(
&n,
context.file,
context.src,
vars,
global_vars,
vars_array,
scope_index,
)?;
Ok(VariableInfo::new(name, variable))
})
.map_or(Ok(None), |v: Result<VariableInfo>| v.map(Some))?;
Ok(Log::new(variable, message))
}
pub(crate) struct LogCompiler;
impl NodeCompiler for LogCompiler {
type TargetPattern = Log;
fn from_node(
node: &Node,
context: &CompilationContext,
vars: &mut BTreeMap<String, usize>,
vars_array: &mut Vec<Vec<VariableSourceLocations>>,
scope_index: usize,
global_vars: &mut BTreeMap<String, usize>,
logs: &mut AnalysisLogs,
) -> Result<Self::TargetPattern> {
let message = node.child_by_field_name("message");
let message = if let Some(message) = message {
Some(Pattern::from_node(
&message,
context,
vars,
vars_array,
scope_index,
global_vars,
false,
logs,
)?)
} else {
None
};
let variable_node = node.child_by_field_name("variable");
let variable = variable_node
.map(|n| {
let name = n.utf8_text(context.src.as_bytes()).ok_or_else(|| anyhow!("Failed to get utf8 text for variable node"))?.to_string();
let variable = Variable::from_node(
&n,
context.file,
context.src,
vars,
global_vars,
vars_array,
scope_index,
)?;
Ok(VariableInfo::new(name, variable))
})
.map_or(Ok(None), |v: Result<VariableInfo>| v.map(Some))?;
Ok(Log::new(variable, message))
}

@arendjr
Copy link
Contributor Author

arendjr commented Apr 8, 2024

Grouped in #179 to avoid further merge issues.

@arendjr arendjr closed this Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant