Skip to content

⚡️ Stop writing // dart format width=80 to the generated freezed files#1256

Merged
rrousselGit merged 10 commits intorrousselGit:masterfrom
AlexV525:feat/not-writing-fixed-format-page-width
Jul 2, 2025
Merged

⚡️ Stop writing // dart format width=80 to the generated freezed files#1256
rrousselGit merged 10 commits intorrousselGit:masterfrom
AlexV525:feat/not-writing-fixed-format-page-width

Conversation

@AlexV525
Copy link
Copy Markdown
Contributor

@AlexV525 AlexV525 commented Jun 8, 2025

The builder of freezed does not specify the formatOutput, which will produce the default width=80 header in generated files. #1160 removes the handling, so the default implementation is being used. The PR prevents it from being generated.

See also:

Where it got inserted:

How json_serializable handles it:

Summary by CodeRabbit

  • New Features

    • Integrated automatic Dart code formatting into the build process for generated files.
  • Bug Fixes

    • Ensured generated files no longer include the unnecessary // dart format width=80 comment.
  • Tests

    • Added tests to verify proper header comments and absence of the old formatting comment in generated files.
  • Documentation

    • Updated changelog to reflect these changes.
  • Chores

    • Updated dependencies to include required formatting and versioning packages.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jun 8, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

A new Dart code formatting step was integrated into the Freezed code generation process, using the dart_style and pub_semver packages. The changelog and dependencies were updated accordingly. Additionally, a test was added to ensure generated files have the correct headers and do not include the old formatting comment.

Changes

File(s) Change Summary
packages/freezed/CHANGELOG.md Added unreleased patch note about removing // dart format width=80 from generated files.
packages/freezed/lib/builder.dart Integrated Dart code formatting using DartFormatter, added formatOutput parameter, and updated imports.
packages/freezed/pubspec.yaml Added dart_style and pub_semver as dependencies.
packages/freezed/test/source_gen_test.dart Added test to verify generated files' headers and absence of the old format comment; updated imports.

Sequence Diagram(s)

sequenceDiagram
    participant Builder
    participant DartFormatter
    participant FileSystem

    Builder->>DartFormatter: Format generated Dart code
    DartFormatter-->>Builder: Return formatted code
    Builder->>FileSystem: Write formatted code to .freezed.dart file
Loading

Possibly related PRs

  • CI #1145: Modified packages/freezed/lib/builder.dart to enhance Dart code formatting and adjust formatOutput logic, closely related to this PR’s changes.

Poem

In fields of code, the Freezed winds blow,
Now formatted neat, with a tidy flow.
Old comments gone, new headers in place,
Each file wears a freshly pressed face.
With tests to check and dependencies new—
This bunny hops forward, proud of the view!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 401ceda and d5a8538.

📒 Files selected for processing (2)
  • packages/freezed/CHANGELOG.md (1 hunks)
  • packages/freezed/pubspec.yaml (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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
Copy Markdown

@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

🧹 Nitpick comments (1)
packages/freezed/lib/builder.dart (1)

30-31: Consider adding error handling to the formatter function.

The _defaultFormatOutput function correctly implements the required signature for source_gen's formatOutput parameter. However, consider adding error handling since DartFormatter.format() could potentially throw exceptions on malformed code.

-String _defaultFormatOutput(String code, Version version) =>
-    DartFormatter(languageVersion: version).format(code);
+String _defaultFormatOutput(String code, Version version) {
+  try {
+    return DartFormatter(languageVersion: version).format(code);
+  } catch (e) {
+    // Fall back to unformatted code if formatting fails
+    return code;
+  }
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 011e231 and 8ded69b.

📒 Files selected for processing (3)
  • packages/freezed/CHANGELOG.md (1 hunks)
  • packages/freezed/lib/builder.dart (3 hunks)
  • packages/freezed/pubspec.yaml (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: freezed (packages/freezed, master, get)
🔇 Additional comments (4)
packages/freezed/CHANGELOG.md (1)

1-4: Documentation accurately reflects the change.

The changelog entry clearly documents the removal of the // dart format width=80 comment from generated files, which aligns with the PR objectives and the implementation changes in the other files.

packages/freezed/pubspec.yaml (1)

16-16:

Details

✅ Verification successful

Verify dependency versions for security and currency.

The addition of dart_style and pub_semver dependencies is necessary for the new formatting functionality. However, please verify that these versions are current and free from known security vulnerabilities.

Also applies to: 22-22


🏁 Script executed:

#!/bin/bash
# Description: Check for latest versions and security advisories for the new dependencies

echo "Checking dart_style latest version:"
curl -s https://pub.dev/api/packages/dart_style | jq '.latest.version'

echo "Checking pub_semver latest version:"  
curl -s https://pub.dev/api/packages/pub_semver | jq '.latest.version'

echo "Checking for security advisories:"
gh api graphql -f query='
{
  securityVulnerabilities(first: 10, ecosystem: PUB) {
    nodes {
      advisory {
        summary
        severity
        publishedAt
      }
      package {
        name
      }
      vulnerableVersionRange
      firstPatchedVersion {
        identifier
      }
    }
  }
}' | jq '.data.securityVulnerabilities.nodes[] | select(.package.name == "dart_style" or .package.name == "pub_semver")'

Length of output: 823


Dependencies up-to-date and secure.

  • dart_style constraint ^3.0.0 already allows the latest 3.1.0 release.
  • pub_semver pinned at ^2.2.0 matches the latest 2.2.0.
  • No known security advisories were found for either package in the PUB ecosystem.

No further changes needed. Approving these dependency additions.

packages/freezed/lib/builder.dart (2)

2-2: Good use of specific imports.

Using show to import only the specific classes needed (DartFormatter and Version) is a good practice that keeps the namespace clean and makes dependencies explicit.

Also applies to: 4-4


19-19: Correct implementation of formatOutput override.

Adding the formatOutput: _defaultFormatOutput parameter to PartBuilder is the correct approach to override source_gen's default behavior and prevent the // dart format width=80 comment from being added to generated files.

@AlexV525 AlexV525 marked this pull request as draft June 8, 2025 14:19
@AlexV525 AlexV525 marked this pull request as ready for review June 8, 2025 14:27
@rrousselGit
Copy link
Copy Markdown
Owner

Some tests are necesary here :)

Copy link
Copy Markdown

@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

🧹 Nitpick comments (1)
packages/freezed/test/builder_test.dart (1)

64-104: Consider clarifying the purpose of the explicit format option.

This test uses {'format': true} while the previous test uses empty options {}. If both scenarios should behave identically regarding the dart format comment removal, consider adding a comment explaining why both configurations are tested.

    await testBuilder(
-      freezed(const BuilderOptions({'format': true})),
+      freezed(const BuilderOptions({'format': true})), // Test explicit format option
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0303732 and 9a9661a.

📒 Files selected for processing (2)
  • packages/freezed/lib/builder.dart (3 hunks)
  • packages/freezed/test/builder_test.dart (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/freezed/lib/builder.dart
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: freezed (packages/freezed, master, get)
  • GitHub Check: freezed (packages/freezed/example, master, get)
🔇 Additional comments (6)
packages/freezed/test/builder_test.dart (6)

1-8: LGTM! Clean and appropriate imports.

The imports are well-organized and include all necessary dependencies for testing the formatting functionality.


18-21: Good test of direct formatting function.

The test correctly verifies that the defaultFormatOutput function formats code properly while avoiding the dart format width comment. The use of Version(3, 0, 0) for testing is appropriate.


29-62: Excellent integration test coverage.

This test thoroughly validates the core PR objective by:

  • Testing the full builder pipeline with realistic freezed code
  • Verifying absence of // dart format width= comments
  • Confirming presence of expected generated file headers

The test structure using InMemoryAssetWriter and testBuilder is appropriate for build system testing.


95-95: Consistent assertion pattern.

Good use of contains('// dart format') instead of the more specific contains('// dart format width=') used in other tests. This broader check ensures no dart format comments are present regardless of the specific variant.


10-27:

Details

✅ Verification successful

Verify that defaultFormatOutput is exported from the builder module.

The test directly imports and uses defaultFormatOutput from package:freezed/builder.dart. Ensure this function is properly exported in the public API.


🏁 Script executed:

#!/bin/bash
# Description: Verify that defaultFormatOutput is exported from the builder module
# Expected: Function should be found in the builder.dart file exports

# Check if defaultFormatOutput is exported from builder.dart
rg -A 5 "defaultFormatOutput" packages/freezed/lib/builder.dart

Length of output: 672


No action needed: defaultFormatOutput is already exported from builder.dart.
The function is defined and available in packages/freezed/lib/builder.dart, so the test import is valid.


101-103:

Details

❓ Verification inconclusive

Verify the expected mixin and getter patterns.

The test asserts specific code structure patterns. Ensure these assertions align with the actual output format of the freezed generator.


🏁 Script executed:

#!/bin/bash
# Description: Verify the expected patterns in generated freezed files match test assertions
# Expected: Generated files should contain the patterns being tested

# Check for similar patterns in existing freezed generated files or examples
fd "\.freezed\.dart$" --exec rg -l "mixin.*\$.*\{" {} \;
fd "\.freezed\.dart$" --exec rg -l "get.*;" {} \; | head -5

Length of output: 272


🏁 Script executed:

#!/bin/bash
# Inspect mixin and getters indentation in generated freezed files
for file in benchmarks/lib/src/*.freezed.dart; do
  echo "=== $file ==="
  # Find the first mixin declaration
  line=$(grep -n "mixin _\$" "$file" | cut -d: -f1 | head -n1)
  if [ -z "$line" ]; then
    echo "No mixin found"
    continue
  fi
  # Print the mixin line and the next 5 lines to capture getters
  sed -n "${line},$((line+5))p" "$file"
  echo
done

Length of output: 631


🏁 Script executed:

#!/bin/bash
# Locate and display the test file with context around the assertions
file=$(fd builder_test.dart)
echo "Found test file: $file"
echo "Displaying lines 80-120:"
sed -n '80,120p' "$file"

Length of output: 1146


🏁 Script executed:

#!/bin/bash
# Display the first 30 lines of the example generated file to inspect mixin and getters
file="packages/freezed_lint/example/lib/missing_mixin.freezed.dart"
if [ -f "$file" ]; then
  echo "=== $file ==="
  sed -n '1,30p' "$file"
else
  echo "File not found: $file"
fi

Length of output: 1833


Verify Freezed Output for Mixin and Getter Syntax

Please confirm that the generated simple_class.freezed.dart file matches the test’s expectations:

  • It declares the mixin exactly as
    mixin _$SimpleClass {
  • The getters are indented two spaces and terminated with semicolons:
    String get name;
    int get age;

Affected test: packages/freezed/test/builder_test.dart lines 101–103.

@AlexV525
Copy link
Copy Markdown
Contributor Author

@rrousselGit PTAL

Copy link
Copy Markdown

@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 details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between edd560e and f18e852.

📒 Files selected for processing (1)
  • packages/freezed/test/source_gen_test.dart (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: freezed (packages/freezed/example, master, get)
  • GitHub Check: freezed (packages/freezed, master, get)
  • GitHub Check: freezed (packages/freezed_lint, master, get)
  • GitHub Check: freezed (packages/freezed_annotation, master, get)
🔇 Additional comments (1)
packages/freezed/test/source_gen_test.dart (1)

2-2: LGTM - necessary imports for the new test.

The added imports are appropriate for the file system operations and test framework usage in the new test case.

Also applies to: 7-7

@AlexV525 AlexV525 requested a review from rrousselGit June 10, 2025 12:55
@AlexV525
Copy link
Copy Markdown
Contributor Author

@rrousselGit Would you take another look?

@rrousselGit
Copy link
Copy Markdown
Owner

LGTM

@rrousselGit rrousselGit merged commit 71b51d2 into rrousselGit:master Jul 2, 2025
4 of 5 checks passed
@AlexV525 AlexV525 deleted the feat/not-writing-fixed-format-page-width branch July 2, 2025 21:21
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.

2 participants