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

refactor: move fixer to RuleCore #860

Merged
merged 6 commits into from Jan 15, 2024
Merged

refactor: move fixer to RuleCore #860

merged 6 commits into from Jan 15, 2024

Conversation

HerringtonDarkholme
Copy link
Member

@HerringtonDarkholme HerringtonDarkholme commented Jan 15, 2024

fix #723

Summary by CodeRabbit

  • New Features

    • Introduced a new rule core system for enhanced configuration management.
    • Added fixer capabilities to rules, allowing for automated suggestions and corrections.
  • Improvements

    • Improved internal rule matching and fixing logic for a more intuitive user experience.
    • Streamlined the configuration process with updated structures for rule definitions.
  • Refactor

    • Refactored the rule system, replacing the RuleWithConstraint with the more robust RuleCore.
    • Refined internal APIs to align with the new rule core architecture.
  • Removals

    • Removed the constraints module to simplify the configuration framework.
  • Bug Fixes

    • Fixed issues related to the assignment and usage of fixers in various components, ensuring consistent behavior across the application.

Copy link
Contributor

coderabbitai bot commented Jan 15, 2024

Walkthrough

The overarching change involves refactoring the codebase to support more granular rule application, particularly for transforming lists of meta variables, like import statements, into individual elements. This includes altering how fixers are assigned and accessed, introducing a new RuleCore module, and redefining the structure of rules and configurations to enable these transformations.

Changes

File Path Change Summary
crates/cli/src/print/colored_print/test.rs
crates/cli/src/scan.rs
crates/cli/src/verify/snapshot.rs
crates/lsp/src/lib.rs
Updated fixer variable assignment to use matcher.fixer.
crates/cli/src/print/interactive_print.rs Made matcher mutable and changed fixer to use take method.
crates/config/src/lib.rs
crates/napi/src/lib.rs
crates/pyo3/src/py_node.rs
Removed constraints module, added rule_core module, and updated references from RuleWithConstraint to RuleCore.
crates/config/src/rule/referent_rule.rs
crates/config/src/rule_config.rs
crates/config/src/rule_core.rs
Refactored rule types and methods, added SerializableRuleCore struct and associated methods, and updated SerializableRuleConfig struct.
crates/napi/src/doc.rs Added a fix field with a None value to the NapiConfig struct.

Assessment against linked issues

Objective Addressed Explanation
Enable applying rules/transformations to specific sub nodes in the matching process. (#723)
Introduce the capability to apply sub rules to matched variables/variable lists. (#723)
Support the transformation of import statements from a list to individual statements based on specific rules. (#723)
Allow the transformation of import statements from a list to individual statements with customized transformations for each import. (#723)
Provide the ability to apply rules to matched imports and generate transformed import statements. (#723)
Implement the functionality to apply custom rules to transform import statements and generate the desired output. (#723)

Poem

In the realm of code, a change takes flight,
A rabbit's touch makes refactors bright.
Meta lists transform with ease and grace,
As RuleCore weaves its intricate lace. 🐇✨

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-tests for this file.
  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0300ced and 5389e37.
Files selected for processing (3)
  • crates/config/src/rule_config.rs (3 hunks)
  • crates/napi/src/doc.rs (1 hunks)
  • crates/pyo3/src/py_node.rs (1 hunks)
Additional comments: 3
crates/napi/src/doc.rs (1)
  • 43-43: The addition of the fix field with a default value of None to the NapiConfig struct aligns with the PR's objectives to support transformations.
crates/pyo3/src/py_node.rs (1)
  • 309-309: The addition of the fix argument to the config_from_rule function is consistent with the PR's goal of enhancing transformation capabilities.
crates/config/src/rule_config.rs (1)
  • 56-56: The addition of the fix field to the SerializableRuleCore struct and its removal from the SerializableRuleConfig struct align with the PR's objectives. However, remember that the fixer field in SerializableRuleConfig is intended only for StrDoc as an optimization, as noted in a previous learning.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5389e37 and 1fe7e8e.
Files selected for processing (2)
  • crates/config/src/lib.rs (1 hunks)
  • crates/config/src/rule_config.rs (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/config/src/rule_config.rs
Additional comments: 2
crates/config/src/lib.rs (2)
  • 7-7: The addition of the rule_core module is consistent with the PR's objectives to enhance transformation capabilities.
  • 1-10: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [1-50]

The removal of the constraints module suggests a refactoring. Verify that the functionality previously provided by this module is either no longer needed or has been appropriately integrated into other parts of the system.

Verification successful

---



---


The 'rule_core' module contains functionality related to constraints, such as SerializeConstraintsError and RuleWithConstraint, indicating that the responsibilities of the removed 'constraints' module have been integrated into the rule system.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the constraints functionality has been migrated or refactored.
rg --type rust 'constraints::'

Length of output: 30



Script:

#!/bin/bash
# Since the direct search for 'constraints::' yielded no results, let's try to find if there are any files that might have contained the 'constraints' module or related functionality.
# We'll look for files that might have been renamed or modified in a way that replaced 'constraints' with another term.
rg --type rust --files-with-matches 'mod constraints'

Length of output: 55



Script:

#!/bin/bash
# Investigate the 'rule_core' module to see if it contains any functionality that might have been part of the 'constraints' module.
# We will look for any references to constraint-like functionality within the 'rule_core' module.
rg --type rust --files-with-matches 'rule_core'

Length of output: 105



Script:

#!/bin/bash
# Examine the contents of the 'rule_core' module to understand its functionality and to infer if it has taken over any responsibilities from the 'constraints' module.
# We will look at the 'rule_core' related code in the files identified.
rg --type rust -C 10 'mod rule_core' crates/config/src/rule_config.rs
rg --type rust -C 10 'mod rule_core' crates/config/src/lib.rs

Length of output: 507



Script:

#!/bin/bash
# Attempt to retrieve the contents of the 'rule_core' module file to inspect its functionality.
# We will use 'fd' to find the file and 'cat' to display its contents.
fd 'rule_core.rs' --exec cat {}

Length of output: 7023

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1fe7e8e and c94f59e.
Files selected for processing (2)
  • crates/config/src/rule/deserialize_env.rs (2 hunks)
  • crates/config/src/rule_config.rs (5 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/config/src/rule_config.rs
Additional comments: 1
crates/config/src/rule/deserialize_env.rs (1)
  • 147-147: The change in the parse_global_utils function parameter type from SerializableRuleCore to SerializableRuleCoreWithId aligns with the PR's objective to support transformations by applying rules to sub-nodes.

crates/config/src/rule/deserialize_env.rs Show resolved Hide resolved
Copy link

codecov bot commented Jan 15, 2024

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (f8ab6df) 71.31% compared to head (da7010b) 71.34%.
Report is 5 commits behind head on main.

Files Patch % Lines
crates/cli/src/scan.rs 0.00% 2 Missing ⚠️
crates/config/src/combined.rs 50.00% 1 Missing ⚠️
crates/config/src/rule/referent_rule.rs 50.00% 1 Missing ⚠️
crates/lsp/src/lib.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #860      +/-   ##
==========================================
+ Coverage   71.31%   71.34%   +0.03%     
==========================================
  Files          71       71              
  Lines        4594     4592       -2     
==========================================
  Hits         3276     3276              
+ Misses       1318     1316       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

BREAKING CHANGE: RuleConfig.fixer is not available now.
Use RuleConfig.matcher.fixer instead.
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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c94f59e and 315f40a.
Files selected for processing (8)
  • crates/cli/src/print/colored_print/test.rs (1 hunks)
  • crates/cli/src/print/interactive_print.rs (1 hunks)
  • crates/cli/src/scan.rs (2 hunks)
  • crates/cli/src/verify/snapshot.rs (1 hunks)
  • crates/config/src/combined.rs (2 hunks)
  • crates/config/src/rule_config.rs (9 hunks)
  • crates/config/src/rule_core.rs (4 hunks)
  • crates/lsp/src/lib.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/config/src/rule_config.rs
Additional comments: 15
crates/config/src/combined.rs (2)
  • 22-22: The sorting logic has been updated to sort rules based on the presence of a fix instead of a fixer. This change aligns with the PR objectives and the rest of the changes in the codebase.
  • 88-88: The conditional check for excluding fixable rules during scanning has been updated to check for rule.fix.is_some(). This is consistent with the new structure where the fix field is directly on the RuleConfig rather than nested within a fixer.
crates/cli/src/verify/snapshot.rs (1)
  • 94-94: The reference to fix has been updated from rule_config.fixer to rule_config.matcher.fixer, which is consistent with the changes in the PR and the new structure of the RuleConfig.
crates/cli/src/print/colored_print/test.rs (1)
  • 241-241: The assignment of the fixer variable has been updated to use matcher.fixer instead of rule.fixer. This change is in line with the new structure where the fixer is associated with the matcher.
crates/cli/src/print/interactive_print.rs (2)
  • 288-295: > Note: This review was outside the patches, and no patch overlapping with it was found. Original lines [251-251]

The logic for generating diffs has been updated to use rule.matcher.fixer instead of rule.fixer. This change is consistent with the new structure of the RuleConfig and the PR's objectives.

  • 288-295: > Note: This review was outside the patches, and no patch overlapping with it was found. Original lines [269-269]

The logic for matching rules on files has been updated to use rule.matcher.fixer instead of rule.fixer. This change is consistent with the new structure of the RuleConfig and the PR's objectives.

crates/config/src/rule_core.rs (7)
  • 70-79: The addition of the RuleConfigError enum and the RResult type alias is a good practice for error handling and provides a clear way to handle different error types that may occur during rule configuration.
  • 85-101: The SerializableRuleCore struct has been added with a new optional fix field of type SerializableFixer. This change aligns with the PR objectives to support transformations and is consistent with the new feature of applying transformations to sub-nodes.
  • 115-120: The method get_meta_var_matchers has been added to deserialize meta variable matchers from their serializable form. This method is necessary for the new functionality and is correctly implemented.
  • 123-134: The method get_fixer has been added to obtain a Fixer from the SerializableFixer. This method is necessary for the new functionality and is correctly implemented.
  • 136-149: The method get_matcher has been added to obtain a RuleWithConstraint from the SerializableRuleCore. This method is necessary for the new functionality and is correctly implemented.
  • 157-157: The fixer field has been added to the RuleWithConstraint struct. This change is consistent with the PR objectives and the new structure of the RuleConfig.
  • 188-191: The method with_fixer has been added to the RuleWithConstraint struct to allow setting the fixer. This change is consistent with the PR objectives and the new structure of the RuleConfig.
crates/cli/src/scan.rs (2)
  • 251-251: The logic for generating diffs has been updated to use rule.matcher.fixer instead of rule.fixer. This change is consistent with the new structure of the RuleConfig and the PR's objectives.
  • 269-269: The logic for matching rules on files has been updated to use rule.matcher.fixer instead of rule.fixer. This change is consistent with the new structure of the RuleConfig and the PR's objectives.

crates/lsp/src/lib.rs Show resolved Hide resolved
BREAKING CHANGE: RuleCore is the new name of RuleWithConstraints.
The renaming reflects RuleCore = Rule + trans + fix + constraints.

fix #862
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 315f40a and 70d4cb4.
Files selected for processing (7)
  • crates/config/src/lib.rs (2 hunks)
  • crates/config/src/rule/referent_rule.rs (6 hunks)
  • crates/config/src/rule_config.rs (9 hunks)
  • crates/config/src/rule_core.rs (4 hunks)
  • crates/napi/src/doc.rs (2 hunks)
  • crates/napi/src/lib.rs (3 hunks)
  • crates/pyo3/src/py_node.rs (3 hunks)
Files skipped from review as they are similar to previous changes (4)
  • crates/config/src/lib.rs
  • crates/config/src/rule_config.rs
  • crates/napi/src/doc.rs
  • crates/pyo3/src/py_node.rs
Additional comments: 17
crates/config/src/rule/referent_rule.rs (8)
  • 1-4: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [1-8]

The imports and module usage seem appropriate and there are no signs of unused imports or missing dependencies.

  • 30-30: The type alias GlobalRules is correctly defined and uses the new RuleCore type.
  • 27-36: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [33-42]

The insert method in GlobalRules has been updated to use RuleCore and includes proper error handling for duplicate and cyclic rules.

  • 58-58: The RuleRegistration struct has been updated to use RuleCore for the global field, aligning with the changes in the PR objectives.
  • 64-70: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [67-76]

The get_global method correctly returns a read guard for the global rules hashmap.

  • 110-110: The RegistrationRef struct has been updated to use RuleCore for the global field, which is consistent with the changes made in the RuleRegistration struct.
  • 159-159: The eval_global method in ReferentRule has been updated to use RuleCore, which is expected as per the changes in the PR.
  • 1-4: > Note: This review was outside the patches, and no patch overlapping with it was found. Original lines [200-214]

The Matcher implementation for ReferentRule has been updated to work with the new RuleCore type, ensuring that the matching process is consistent with the new rule structure.

crates/napi/src/lib.rs (3)
  • 7-7: The import of RuleCore is correct and replaces the previous RuleWithConstraint, aligning with the changes described in the PR objectives.
  • 227-227: The type alias FindInFiles has been correctly updated to use RuleCore instead of RuleWithConstraint.
  • 295-295: The call_sg_node function signature has been updated to use RuleCore, which is consistent with the changes made throughout the file.
crates/config/src/rule_core.rs (6)
  • 1-15: The imports and module usage are correctly organized, and the new imports for Fixer and related types are necessary for the changes in this file.
  • 69-79: The new error type RuleConfigError is well-defined and covers all potential error cases that could arise from the rule configuration process.
  • 83-102: The new struct SerializableRuleCore is correctly defined with all necessary fields to support the serialization and deserialization of rule configurations, including the new fix field.
  • 136-149: The get_matcher method in SerializableRuleCore correctly constructs a RuleCore instance, integrating the new fixer functionality as well as other matchers and transformations.
  • 66-165: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [152-191]

The RuleCore struct has been updated to include a fixer field, and the associated methods to manipulate the RuleCore object are correctly implemented.

  • 184-217: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [214-233]

The Matcher implementation for RuleCore correctly uses the new fields and respects the potential kinds, matchers, and transformations, ensuring that the matching process is consistent with the new rule structure.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 315f40a and da7010b.
Files selected for processing (7)
  • crates/config/src/lib.rs (2 hunks)
  • crates/config/src/rule/referent_rule.rs (6 hunks)
  • crates/config/src/rule_config.rs (9 hunks)
  • crates/config/src/rule_core.rs (4 hunks)
  • crates/napi/src/doc.rs (2 hunks)
  • crates/napi/src/lib.rs (3 hunks)
  • crates/pyo3/src/py_node.rs (3 hunks)
Files skipped from review as they are similar to previous changes (6)
  • crates/config/src/lib.rs
  • crates/config/src/rule/referent_rule.rs
  • crates/config/src/rule_config.rs
  • crates/napi/src/doc.rs
  • crates/napi/src/lib.rs
  • crates/pyo3/src/py_node.rs
Additional comments: 9
crates/config/src/rule_core.rs (9)
  • 1-15: The imports have been reorganized and new ones added to support the new features.
  • 69-79: The new RuleConfigError enum has been added to encapsulate various error types that can occur in the rule configuration process.
  • 81-81: The RResult type alias is a standard way to simplify the result type throughout the codebase.
  • 84-101: The SerializableRuleCore struct has been introduced with fields that align with the PR objectives, including the new fix field for transformations.
  • 104-150: The implementation of SerializableRuleCore includes methods for deserialization and fetching meta variable matchers, which are necessary for the new functionality.
  • 66-165: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [152-191]

The RuleCore struct has been updated with a new fixer field and associated methods, which is consistent with the PR objectives to allow transformations on specific sub-nodes.

  • 193-208: The Default implementation for RuleCore has been updated to include the default value for the new fixer field.
  • 184-217: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [214-233]

The Matcher implementation for RuleCore has been updated to include the new matching logic, which is crucial for the new feature's functionality.

  • 260-267: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [263-297]

The tests have been updated to reflect the new structures and functionalities, ensuring that the new features work as expected.

@HerringtonDarkholme HerringtonDarkholme changed the title feat: support applyRules transformation refactor: move fixer to RuleCore Jan 15, 2024
@HerringtonDarkholme HerringtonDarkholme added this pull request to the merge queue Jan 15, 2024
Merged via the queue into main with commit 1202618 Jan 15, 2024
4 checks passed
@HerringtonDarkholme HerringtonDarkholme deleted the feat-apply branch January 15, 2024 21:29
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.

[feature] transform specific meta variables
1 participant