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: unify the logic of saving ts files #1874

Merged
merged 1 commit into from
Nov 18, 2024
Merged

chore: unify the logic of saving ts files #1874

merged 1 commit into from
Nov 18, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 18, 2024

No description provided.

Copy link
Contributor

coderabbitai bot commented Nov 18, 2024

📝 Walkthrough

Walkthrough

The pull request introduces modifications to the EnhancerGenerator and PolicyGenerator classes, as well as the generate function in the model-meta-generator.ts file. The key change is the transition of several methods from asynchronous to synchronous execution, specifically the removal of the async keyword and the await operator. This affects how the saveSourceFile function is called, shifting from a promise-based approach to a direct invocation. The overall structure of the classes remains unchanged, focusing primarily on the file-saving mechanism.

Changes

File Path Change Summary
packages/schema/src/plugins/enhancer/enhance/index.ts - Modified saveSourceFile method from private async saveSourceFile(sf: SourceFile) to private saveSourceFile(sf: SourceFile).
- Added import for saveSourceFile from @zenstackhq/sdk.
- Removed await from the method, changing its control flow.
packages/schema/src/plugins/enhancer/policy/policy-guard-generator.ts - Modified generate method from async generate(project: Project, model: Model, output: string) to generate(project: Project, model: Model, output: string).
- Added import for saveSourceFile from @zenstackhq/sdk.
- Replaced await sf.save() with saveSourceFile(sf).
packages/sdk/src/model-meta-generator.ts - Modified generate function from export async function generate(...) to export function generate(...).
- Added import for saveSourceFile and replaced await sf.save() with saveSourceFile(sf).
- Introduced saveSourceFile method.

Possibly related PRs


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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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
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: 2

🧹 Outside diff range and nitpick comments (1)
packages/sdk/src/model-meta-generator.ts (1)

Inconsistent file saving patterns detected across the codebase

The verification reveals mixed usage of file saving methods that need to be unified:

  • Direct .save() calls are still present in multiple files, particularly in error handling paths in code-gen.ts
  • saveSourceFile implementation in code-gen.ts appears to be the standardized approach, but it's not consistently used
  • Some files like enhance/index.ts have both patterns

Key locations requiring attention:

  • packages/schema/src/plugins/enhancer/enhance/index.ts: Contains both save() and saveSourceFile usage
  • packages/sdk/src/code-gen.ts: Has multiple project.save() calls in error handling paths
  • packages/schema/tests/generator/expression-writer.test.ts: Uses direct save() call
  • packages/misc/redwood/src/commands/setup.ts: Uses direct project.save() call
🔗 Analysis chain

Line range hint 70-88: LGTM: Function changes align with unified file saving approach

The transition from async to sync execution and the use of saveSourceFile is consistent with the PR's objective of unifying TypeScript file saving logic.

Let's verify the consistent usage of saveSourceFile across the codebase:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent usage of saveSourceFile and absence of direct .save() calls

# Check for any remaining direct .save() calls
rg -l "\.save\(\)" --type ts

# Check for consistent usage of saveSourceFile
rg -l "saveSourceFile\(" --type ts

Length of output: 446


Script:

#!/bin/bash
# Let's examine the actual usage context of .save() and saveSourceFile in these files

# Check .save() usage context
echo "=== .save() usage in enhance/index.ts ==="
rg "\.save\(\)" packages/schema/src/plugins/enhancer/enhance/index.ts -B 2 -A 2

echo -e "\n=== .save() usage in code-gen.ts ==="
rg "\.save\(\)" packages/sdk/src/code-gen.ts -B 2 -A 2

echo -e "\n=== .save() usage in expression-writer.test.ts ==="
rg "\.save\(\)" packages/schema/tests/generator/expression-writer.test.ts -B 2 -A 2

echo -e "\n=== .save() usage in setup.ts ==="
rg "\.save\(\)" packages/misc/redwood/src/commands/setup.ts -B 2 -A 2

# Check saveSourceFile usage context
echo -e "\n=== saveSourceFile usage in code-gen.ts ==="
rg "saveSourceFile\(" packages/sdk/src/code-gen.ts -B 2 -A 2

echo -e "\n=== saveSourceFile usage in enhance/index.ts ==="
rg "saveSourceFile\(" packages/schema/src/plugins/enhancer/enhance/index.ts -B 2 -A 2

echo -e "\n=== saveSourceFile usage in policy-guard-generator.ts ==="
rg "saveSourceFile\(" packages/schema/src/plugins/enhancer/policy/policy-guard-generator.ts -B 2 -A 2

Length of output: 2943

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9b56b1c and c46fa6b.

📒 Files selected for processing (3)
  • packages/schema/src/plugins/enhancer/enhance/index.ts (3 hunks)
  • packages/schema/src/plugins/enhancer/policy/policy-guard-generator.ts (3 hunks)
  • packages/sdk/src/model-meta-generator.ts (3 hunks)
🔇 Additional comments (5)
packages/sdk/src/model-meta-generator.ts (1)

41-41: LGTM: Import addition aligns with unified file saving approach

The addition of saveSourceFile import is consistent with the PR's objective of unifying TypeScript file saving logic.

packages/schema/src/plugins/enhancer/policy/policy-guard-generator.ts (2)

29-29: LGTM: Import of saveSourceFile is properly placed

The addition of saveSourceFile to the existing imports from @zenstackhq/sdk is well-organized and aligns with the PR's objective of unifying the ts file saving logic.


90-90: Consider error handling for synchronous file saving

With the transition to synchronous file saving, ensure that potential file system errors are properly handled. The saveSourceFile function should include appropriate error handling mechanisms.

#!/bin/bash
# Check implementation of saveSourceFile for error handling
rg -A 5 "export function saveSourceFile"
packages/schema/src/plugins/enhancer/enhance/index.ts (2)

14-14: LGTM: Import statement added correctly.

The import of saveSourceFile from '@zenstackhq/sdk' aligns with the transition to synchronous file operations.


141-141: Verify the removal of await.

The change from await this.saveSourceFile(enhanceTs) to this.saveSourceFile(enhanceTs) aligns with making the operation synchronous. However, we should verify that this doesn't affect error handling.

✅ Verification successful

Removal of await is correct and safe

The verification shows that saveSourceFile is a synchronous function that performs file text replacement operations. Looking at its implementation in packages/sdk/src/code-gen.ts, it doesn't contain any asynchronous operations. The function is consistently used synchronously across the codebase, including in other plugins. The removal of await aligns with its synchronous nature and won't affect error handling since there were no try-catch blocks found around its usage.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any error handling related to saveSourceFile
rg -A 5 "saveSourceFile.*catch" 

# Search for any async/await usage patterns with saveSourceFile
rg -A 5 "await.*saveSourceFile"

Length of output: 67


Script:

#!/bin/bash
# Let's first check the implementation of saveSourceFile
ast-grep --pattern 'saveSourceFile($_) { $$$ }'

# Also check if there are any try-catch blocks around saveSourceFile calls
ast-grep --pattern 'try {
  $$$
  saveSourceFile($_)
  $$$
}'

# Let's see the method declaration and its usage context
rg -B 5 -A 5 "saveSourceFile"

Length of output: 9540

@ymc9 ymc9 merged commit 768d94c into dev Nov 18, 2024
13 checks passed
@ymc9 ymc9 deleted the chore/unify-sf-saving branch November 18, 2024 21:02
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