Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: support detecting origin indent and newline to generate pages.json #170

Merged
merged 8 commits into from
Jun 25, 2024

Conversation

ModyQyW
Copy link
Member

@ModyQyW ModyQyW commented Jun 3, 2024

Description 描述

Close #133.

Linked Issues 关联的 Issues

#133

Additional context 额外上下文

As a feature, we don't have to rush for it.

I also want to find a way to handle #133 (comment). I think we can try to detect if pages.json or manifest.json exists and generate one if not in the constructor in the next PR. What do you think?

Summary by CodeRabbit

  • Chores

    • Updated packageManager, dependencies, and devDependencies versions in package.json.
    • Added detect-indent and detect-newline dependencies in packages/core/package.json.
  • New Features

    • Improved handling of JSON formatting in PageContext class, including indent and newline detection.
  • Tests

    • Expanded testing capabilities by adding it function import in test/files.spec.ts.

Copy link

coderabbitai bot commented Jun 3, 2024

Walkthrough

The updates enhance dependency versions, integrate new features for file formatting, and extend testing capabilities. Major improvements include updating package versions in package.json files across the project and ensuring JSON file formatting consistency using detect-indent and detect-newline. These adjustments resolve issues with newline characters in JSON files post-update, streamlining development workflows.

Changes

File(s) Change Summary
package.json Updated packageManager and several devDependencies versions.
packages/core/package.json Updated dependencies and added new ones (detect-indent, detect-newline).
packages/core/src/context.ts Introduced logic for handling JSON file formatting, including indent and newline detection.
test/files.spec.ts Extended imports to include it from 'vitest', enhancing test capabilities.

Sequence Diagram(s)

sequenceDiagram
    participant Editor as Editor
    participant PageContext as PageContext
    participant detectIndent as detectIndent
    participant detectNewline as detectNewline
    participant JSON as JSON

    Editor->>PageContext: Update pages.json
    PageContext->>detectIndent: Detect indent in pages.json
    detectIndent-->>PageContext: Return indent
    PageContext->>detectNewline: Detect newline in pages.json
    detectNewline-->>PageContext: Return newline style
    PageContext->>JSON: Stringify with detected indent & newline
    JSON-->>PageContext: Formatted JSON string
    PageContext->>Editor: Save formatted pages.json
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure pages.json maintains consistent file formatting including newline characters on save (#133)

Poem

In the realm of JSON's delight,
Where files gain structure, pure and bright,
Indents detected, newlines aligned,
Our code's now flawless, well-defined.
With pnpm and pnpm anew,
A coder's dreams do come true! ✨🐇


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.
    • @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 as 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.

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.

@ModyQyW ModyQyW requested review from KeJunMao, nei1ee and skiyee June 3, 2024 01:48
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 (3)
packages/core/src/context.ts (3)

Line range hint 91-91: Optimize array operations by using .flatMap().

- this.pagesPath = pageDirFiles.map(page => page.files).flat()
+ this.pagesPath = pageDirFiles.flatMap(page => page.files)

This change simplifies the code and potentially improves performance by reducing the complexity of array operations.


Line range hint 302-302: Use for...of instead of forEach for better performance and readability.

- pages.forEach(v => pagesMap.set(v.path, v))
+ for (const v of pages) {
+   pagesMap.set(v.path, v)
+ }

This change enhances the readability and performance of the loop, especially if asynchronous operations are involved.


Line range hint 119-119: Avoid reassigning function parameters.

- watcher.on('add', async (path) => {
+ watcher.on('add', async (originalPath) => {
+   let path = slash(originalPath);

This change avoids confusion and potential bugs related to the reassignment of function parameters by using a new variable.

Also applies to: 133-133, 155-155

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d97a8af and d4cc045.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !pnpm-lock.yaml
Files selected for processing (3)
  • packages/core/package.json (1 hunks)
  • packages/core/src/context.ts (5 hunks)
  • packages/core/src/files.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/core/src/files.ts
Additional context used
Biome
packages/core/src/context.ts

[error] 91-91: The call chain .map().flat() can be replaced with a single .flatMap() call.


[error] 302-302: Prefer for...of instead of forEach.


[error] 119-119: Reassigning a function parameter is confusing.


[error] 133-133: Reassigning a function parameter is confusing.


[error] 155-155: Reassigning a function parameter is confusing.

Additional comments not posted (5)
packages/core/package.json (1)

47-48: Addition of detect-indent and detect-newline dependencies.

These dependencies are crucial for implementing the feature to maintain the original indentation and newline settings in pages.json. This aligns with the PR's objectives to reduce unnecessary git diffs.

packages/core/src/context.ts (4)

10-11: Import statements for detect-indent and detect-newline.

These imports are necessary for the new functionality to detect and apply the original formatting of pages.json.


43-45: Initialization of properties for JSON formatting.

Proper initialization ensures that the default formatting is set, which is crucial for maintaining consistency if the detection fails.


67-70: Reading and setting the formatting based on the content of pages.json.

This change is essential for dynamically determining the indentation and newline characters based on the existing content of pages.json, which addresses the issue described in #133.


313-319: Use detected formatting settings when generating pages.json.

This implementation ensures that the generated pages.json respects the original formatting, thus minimizing unnecessary version control diffs.

@skiyee
Copy link
Member

skiyee commented Jun 3, 2024

LGTM

@ModyQyW ModyQyW changed the title feat: support auto using origin indent and newline to generate pages.json feat: support detecting origin indent and newline to generate pages.json Jun 4, 2024
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d4cc045 and 8bd980a.

Files selected for processing (2)
  • packages/core/src/files.ts (1 hunks)
  • test/files.spec.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/files.ts
Additional comments not posted (2)
test/files.spec.ts (2)

2-2: The addition of it from 'vitest' is appropriate for the expanded testing capabilities.


16-16: The snapshot test is well-implemented and ensures consistency in the output of getPageFiles.

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)
packages/core/src/context.ts (2)

Line range hint 91-91: Optimize array operations by replacing .map().flat() with .flatMap().

- this.pagesPath = pageDirFiles.map(page => page.files).flat()
+ this.pagesPath = pageDirFiles.flatMap(page => page.files)

Line range hint 302-302: Replace forEach with for...of for better performance and readability.

- pages.forEach(v => pagesMap.set(v.path, v))
+ for (const v of pages) {
+   pagesMap.set(v.path, v)
+ }
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8bd980a and 14b1aca.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !pnpm-lock.yaml
Files selected for processing (2)
  • packages/core/package.json (1 hunks)
  • packages/core/src/context.ts (5 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/package.json
Additional context used
Biome
packages/core/src/context.ts

[error] 91-91: The call chain .map().flat() can be replaced with a single .flatMap() call. (lint/complexity/useFlatMap)

Safe fix: Replace the chain with .flatMap().


[error] 302-302: Prefer for...of instead of forEach. (lint/complexity/noForEach)

forEach may lead to performance issues when working with large arrays. When combined with functions like filter or map, this causes multiple iterations over the same type.

Additional comments not posted (5)
packages/core/src/context.ts (5)

10-11: Added imports for detect-indent and detect-newline are appropriate for the new functionality.


24-24: Import of readFileSync aligns with the new functionality to read file content for detecting format.


43-45: Initialization of properties for JSON format detection is clear and concise.


67-70: Proper use of readFileSync and format detection functions within the constructor to set up the initial state based on the existing pages.json content.


313-319: The logic to conditionally append the newline character based on resolvedPagesJSONEofNewline is well-implemented.

@ModyQyW
Copy link
Member Author

ModyQyW commented Jun 20, 2024

我打算在两到三天内合并发布这个

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 14b1aca and e3c7ee0.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (6)
  • package.json (2 hunks)
  • packages/core/package.json (1 hunks)
  • packages/core/src/context.ts (5 hunks)
  • packages/playground/package.json (1 hunks)
  • packages/schema/package.json (1 hunks)
  • packages/volar/package.json (1 hunks)
Files skipped from review due to trivial changes (4)
  • package.json
  • packages/playground/package.json
  • packages/schema/package.json
  • packages/volar/package.json
Additional comments not posted (13)
packages/core/package.json (7)

43-43: Update to dependency @uni-helper/uni-env approved.

The version bump from ^0.1.1 to ^0.1.3 should include improvements or bug fixes that benefit the project.


44-44: Update to dependency @vue/compiler-sfc approved.

The minor version update from ^3.4.27 to ^3.4.30 is likely to include important fixes and optimizations.


46-46: Update to dependency debug approved.

The patch version update from ^4.3.4 to ^4.3.5 likely contains minor fixes and should not introduce breaking changes.


47-47: Addition of detect-indent dependency approved.

This new dependency supports the feature of detecting original indentation in pages.json, aligning with the PR objectives.


48-48: Addition of detect-newline dependency approved.

This new dependency supports the feature of detecting original newline format in pages.json, which is crucial for maintaining consistency and preventing frequent Git changes.


54-54: Update to dependency yaml approved.

The minor version update from ^2.4.2 to ^2.4.5 should include optimizations or bug fixes enhancing stability or performance.


59-59: Update to development dependency @types/node approved.

The version update from ^20.12.11 to ^20.14.8 ensures better type definitions aligning with the latest Node.js API changes, which can improve type safety and developer experience.

packages/core/src/context.ts (6)

10-10: Addition of detect-indent import approved.

This import is necessary for the implementation of detecting original indentation settings in pages.json, supporting the PR's functionality.


11-11: Addition of detect-newline import approved.

This import is essential for the new functionality of detecting newline characters, which helps maintain file consistency and minimize Git diffs.


24-24: Addition of readFileSync import approved.

Importing readFileSync from './files' supports reading the pages.json content to detect formatting settings, which is crucial for the feature being implemented.


43-45: Initialization of newline and indentation properties approved.

These lines correctly initialize the properties that will store the detected indentation and newline characters, which are central to the new feature.


67-70: Logic for setting formatting based on file content approved.

The logic here effectively reads the file content and sets the indentation and newline properties based on the actual content. This is a robust approach to ensure that the output matches the original formatting.


313-319: Enhanced JSON stringification logic approved.

This modification to the JSON.stringify usage, where indentation and newline settings are dynamically applied based on the detected values, is a crucial enhancement that supports the main objective of this PR.

@ModyQyW ModyQyW merged commit 434d8db into main Jun 25, 2024
@ModyQyW ModyQyW deleted the dev branch June 25, 2024 08:11
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
3 participants