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 #1464: если в атрибуте не указан алиас, то им будет имя метода #1466

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

Mr-Rm
Copy link
Collaborator

@Mr-Rm Mr-Rm commented Oct 28, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced error reporting in the ContextMethodAttribute class constructor for invalid identifiers.
    • Added a new constructor overload in ContextMethodAttribute for improved flexibility.
  • Bug Fixes

    • Updated alias retrieval logic in the CreateMetadata method for better accuracy.
  • Refactor

    • Simplified method implementations in ContextMethodAttribute for cleaner code.

Copy link

coderabbitai bot commented Oct 28, 2024

Walkthrough

The changes in this pull request involve modifications to the ClassBuilder, ContextMethodAttribute, and ContextMethodMapper classes within the OneScript.Core and ScriptEngine libraries. Key updates include alterations to method constructors, specifically the handling of alias parameters and the introduction of new constructors for better error reporting. Additionally, the method for retrieving aliases has been simplified. These changes collectively enhance the clarity and functionality of the respective classes while maintaining their overall structure.

Changes

File Path Change Summary
src/OneScript.Core/Contexts/ClassBuilder.cs Modified ExportClassMethod(MethodInfo nativeMethod) to instantiate ContextMethodAttribute with an additional null alias parameter.
src/OneScript.Core/Contexts/ContextMethodAttribute.cs Updated ContextMethodAttribute constructor to require a non-null alias, added a new constructor overload, and simplified GetName and GetAlias methods.
src/ScriptEngine/Machine/Contexts/ContextMethodMapper.cs Changed CreateMetadata method to call GetAlias() without parameters, altering how method aliases are retrieved.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ClassBuilder
    participant ContextMethodAttribute
    participant ContextMethodMapper

    User->>ClassBuilder: Call ExportClassMethod(nativeMethod)
    ClassBuilder->>ContextMethodAttribute: Create instance with nativeMethod.Name, null
    ContextMethodAttribute-->>ClassBuilder: Return instance
    ClassBuilder-->>User: Return method export result

    User->>ContextMethodMapper: Call CreateMetadata()
    ContextMethodMapper->>ContextMethodAttribute: Call GetAlias()
    ContextMethodAttribute-->>ContextMethodMapper: Return alias
    ContextMethodMapper-->>User: Return metadata
Loading

🐇 "In the code where rabbits hop,
New aliases now will stop!
Constructors clear, methods bright,
Contexts dance in pure delight!
With each change, we do rejoice,
Hopping high, we lift our voice!" 🐇


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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

@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

🧹 Outside diff range and nitpick comments (2)
src/OneScript.Core/Contexts/ContextMethodAttribute.cs (2)

20-30: Consider enhancing error messages with more context.

The validation logic and immutability are well implemented. However, the error messages could be even more helpful.

Consider this enhancement:

-                throw new ArgumentException($"Name '{name}' must be a valid identifier");
+                throw new ArgumentException($"Name '{name}' must be a valid identifier", nameof(name));

-                throw new ArgumentException($"Alias '{alias}' must be a valid identifier");
+                throw new ArgumentException($"Alias '{alias}' must be a valid identifier", nameof(alias));

This change adds the parameter name to the exception, which helps with debugging and makes the error origin clearer.


32-36: Add XML documentation for better API understanding.

The constructor implementation is clever and aligns with the PR objective. However, it would benefit from documentation explaining its purpose.

Consider adding XML documentation:

+        /// <summary>
+        /// Creates a new context method attribute with an automatically derived alias from the native method name.
+        /// </summary>
+        /// <param name="name">The name of the context method</param>
+        /// <param name="_">Ignored parameter to distinguish from the primary constructor</param>
+        /// <param name="nativeMethodName">Automatically populated with the declaring method's name</param>
         public ContextMethodAttribute(string name, string _ = null, 
             [CallerMemberName] string nativeMethodName = null)
         : this(name, nativeMethodName)
         {
         }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 99e2ca3 and 8498477.

📒 Files selected for processing (3)
  • src/OneScript.Core/Contexts/ClassBuilder.cs (1 hunks)
  • src/OneScript.Core/Contexts/ContextMethodAttribute.cs (2 hunks)
  • src/ScriptEngine/Machine/Contexts/ContextMethodMapper.cs (1 hunks)
🔇 Additional comments (4)
src/OneScript.Core/Contexts/ContextMethodAttribute.cs (2)

9-9: LGTM!

The addition of System.Runtime.CompilerServices is necessary for the CallerMemberName attribute usage.


38-40: Verify removal of deprecated state handling.

The simplified getters improve readability, but let's verify that the removal of deprecated state handling doesn't break existing functionality.

src/OneScript.Core/Contexts/ClassBuilder.cs (1)

69-69: LGTM! The change implements the PR objective.

The modification correctly implements the requirement to use method name as alias when no custom alias is specified, by passing the method name as first parameter and explicitly indicating no custom alias with null.

Let's verify that the ContextMethodAttribute constructor accepts null for the alias parameter:

✅ Verification successful

Constructor accepts null alias parameter, change is safe

Looking at the ContextMethodAttribute constructor implementation in src/OneScript.Core/Contexts/ContextMethodAttribute.cs, it explicitly handles the case of null/empty alias through the condition if (!string.IsNullOrEmpty(alias)). The validation is only performed when alias has a value, making it safe to pass null.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for ContextMethodAttribute constructor definition
ast-grep --pattern 'class ContextMethodAttribute {
  $$$
  public ContextMethodAttribute($_, $_) {
    $$$
  }
  $$$
}'

Length of output: 121


Script:

#!/bin/bash
# Let's try a different approach to find ContextMethodAttribute constructor
# First, let's find the file containing ContextMethodAttribute
fd "ContextMethodAttribute" --type f

# Then use ripgrep to search for constructor definition with context
rg "ContextMethodAttribute\(.*\)" -A 5

Length of output: 1237

src/ScriptEngine/Machine/Contexts/ContextMethodMapper.cs (1)

166-166: LGTM! The change aligns with PR objective.

The modification to use binding.GetAlias() without parameters aligns with the PR objective of using method names as default aliases. This change suggests that the alias handling logic has been centralized in the GetAlias() method.

Let's verify the related changes in ContextMethodAttribute:

@EvilBeaver EvilBeaver merged commit 6c063ec into EvilBeaver:develop Nov 5, 2024
1 of 2 checks passed
@maximkin7
Copy link
Contributor

maximkin7 commented Nov 5, 2024 via email

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.

3 participants