-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(es/codegen): Emit comments of all nodes #11314
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
Conversation
🦋 Changeset detectedLatest commit: 02981bc The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Code Review for PR #11314SummaryThis PR refines the comment emission logic introduced in PR #11302 to better handle the case where Module/Script nodes share the same span starting position as their first statement. The change adds a conditional check to prevent double emission of comments. ✅ Positive Aspects
🔍 Areas for Improvement1. Comment Accuracy (Minor)// If the first statement and module span are the same, then do not take the
// comment of the module.This comment says "comment of the module" but could be more precise. Consider: // If the first statement starts at the same position as the Module/Script span,
// skip emitting leading comments here. The first statement will handle them instead.Location: 2. Performance Consideration (Minor)The code calls let should_skip = first_stmt_span_lo
.map(|lo| lo == self.span.lo)
.unwrap_or(false);This is fine, but you could make it slightly more direct: let should_skip = first_stmt_span_lo == Some(self.span.lo);This eliminates the closure and Location: 3. Test CoverageThe PR doesn't appear to include new tests or reference existing tests that validate this behavior. According to the project's CLAUDE.md guidelines (#5: "Write unit tests for your code"), it would be good to:
Suggestion: Add a test in 🎯 Code Quality Assessment
🔒 Security & SafetyNo security concerns identified. The change is purely about comment emission logic and doesn't affect runtime behavior or introduce vulnerabilities. 📋 Recommendations
📝 Additional ContextThis PR builds on PR #11302 which itself fixed issue #11167. The progression is:
The fix makes sense in this context and addresses a real edge case that could cause issues. ✅ ConclusionOverall: APPROVE with suggestions The code change is sound and addresses a legitimate edge case. The main improvement needed is adding test coverage to prevent regressions. The code quality is good and follows Rust best practices. Great work on tracking down and fixing this edge case! 👍 |
Code Review for PR #11314SummaryThis PR refines the comment emission logic introduced in PR #11302 to better handle the case where Module/Script nodes share the same span starting position as their first statement. The change adds a conditional check to prevent double emission of comments. ✅ Positive Aspects
🔍 Areas for Improvement1. Comment Accuracy (Minor)// If the first statement and module span are the same, then do not take the
// comment of the module.This comment says "comment of the module" but could be more precise. Consider: // If the first statement starts at the same position as the Module/Script span,
// skip emitting leading comments here. The first statement will handle them instead.Location: 2. Performance Consideration (Minor)The code calls let should_skip = first_stmt_span_lo
.map(|lo| lo == self.span.lo)
.unwrap_or(false);This is fine, but you could make it slightly more direct: let should_skip = first_stmt_span_lo == Some(self.span.lo);This eliminates the closure and Location: 3. Test CoverageThe PR does not appear to include new tests or reference existing tests that validate this behavior. According to the project's CLAUDE.md guidelines (#5: "Write unit tests for your code"), it would be good to:
Suggestion: Add a test in 🎯 Code Quality Assessment
🔒 Security & SafetyNo security concerns identified. The change is purely about comment emission logic and does not affect runtime behavior or introduce vulnerabilities. 📋 Recommendations
📝 Additional ContextThis PR builds on PR #11302 which itself fixed issue #11167. The progression is:
The fix makes sense in this context and addresses a real edge case that could cause issues. ✅ ConclusionOverall: APPROVE with suggestions The code change is sound and addresses a legitimate edge case. The main improvement needed is adding test coverage to prevent regressions. The code quality is good and follows Rust best practices. Great work on tracking down and fixing this edge case! 👍 |
Code ReviewSummaryThis PR fixes comment emission in the ECMAScript code generator by ensuring that comments associated with Positive Aspects ✅
Concerns and Suggestions 🔍1. Comment Accuracy (Minor)The comment says "comment of the module" in both // In Script impl:
// If the first statement and script span are the same, then do not take the
// comment of the script.2. Performance Consideration (Minor)The code calls 3. Code Duplication (Suggestion)There's identical logic in both fn should_skip_leading_comments<S: Spanned>(body: &[S], container_span: Span) -> bool {
body.first()
.map(|s| s.span().lo == container_span.lo)
.unwrap_or(false)
}Though this might be over-engineering for just 4 lines of code used twice. 4. Test Coverage
|
Code ReviewSummaryThis PR fixes the handling of leading comments in the ECMAScript codegen. The change ensures that comments at the beginning of modules/scripts are properly emitted in all cases, not just for empty bodies. Changes**File: ** (+18/-12) The PR modifies the Previous behavior:
New behavior:
Code Quality ✅The code follows good practices:
Test Coverage ✅Excellent test coverage with 554 test files updated showing the fix works across many scenarios:
Example test case from // Input:
/* @ngInject */
class TestClass { ... }
// Before: Comment lost, import appeared first
import { _ as s } from "@swc/helpers/_/_define_property";
class e { ... }
// After: Comment preserved correctly
/* @ngInject */ import { _ as s } from "@swc/helpers/_/_define_property";
class e { ... }Performance Considerations ✅The change has minimal performance impact:
Potential Issues & Questions
|
Code Review: PR #11314SummaryThis PR fixes a critical issue where top-level comments (like copyright headers and JSDoc annotations) were being lost during code generation. The fix modifies the ✅ Positive Aspects1. Correct Problem Identification 2. Elegant Solution
3. Comprehensive Test Coverage
4. Code Quality
|
Code Review for PR #11314: fix(es/codegen): Emit comments of all nodesSummaryThis PR fixes an issue where leading comments on modules/scripts were not being emitted when they should be. The fix ensures comments appear at the correct location - particularly important when helper imports are injected. ✅ Positives
🔍 Code Quality ObservationsGood Practices
Minor Concerns1. Performance Consideration (Minor) let should_skip_leading_comments = self.body.iter().any(|s| s.span().lo == self.span.lo);
let should_skip_leading_comments = self.body.first()
.map(|s| s.span().lo == self.span.lo)
.unwrap_or(false);This is more explicit that you're checking the first statement, matches your comment better, and avoids unnecessary iteration. 2. Edge Cases
3. Comment Clarity // If the first statement and module span are the same, then do not take the
// comment of the module.Suggested improvement: // Skip emitting leading comments at module level if the first statement starts
// at the same position - that statement will emit them instead. This ensures
// comments appear after any injected imports (e.g., helper imports) rather than
// before them.🔒 Security Considerations✅ No security concerns identified. This is a pure codegen/formatting change. 🐛 Potential Bugs✅ No bugs identified. The logic appears sound for the intended use case. 📊 Performance Analysis✅ Overall: Minimal performance impact
🧪 Test Coverage✅ Excellent: 343 line changes across multiple test fixtures demonstrate thorough validation:
All test changes show comments now appearing in the correct location. 📋 Adherence to CLAUDE.md GuidelinesBased on
💡 RecommendationsHigh Priority:
Medium Priority: let should_skip_leading_comments = self.body.first()
.map(|s| s.span().lo == self.span.lo)
.unwrap_or(false);Low Priority: ✅ ConclusionOverall Assessment: ✅ Approve with minor suggestions This is a solid fix for a real issue. The implementation is clean, focused, and well-tested. The suggestions above are minor improvements that would enhance code clarity and maintainability but don't block merging. The test updates comprehensively demonstrate the fix works correctly across many scenarios, which is excellent. Action Items Before Merge:
Great work! 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug in the SWC code generator where leading comments of Module and Script nodes were not being emitted correctly. The previous logic only emitted comments for empty modules/scripts, causing comments to be lost when code was present. The new logic checks whether any body statement starts at the same span position as the module/script itself, and only skips leading comment emission in that case.
Key changes:
- Updated comment emission logic for both
ModuleandScriptimplementations - Comments are now emitted unless a body statement shares the exact same starting position
- Numerous test files updated to reflect the corrected comment output
Reviewed Changes
Copilot reviewed 182 out of 188 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/swc_ecma_codegen/src/lib.rs | Updated Module and Script implementations to fix leading comment emission logic |
| crates/swc/tests/tsc-references/*.js | Test output files updated to show correctly emitted comments |
| crates/swc/tests/fixture/**/*.ts | Test fixture outputs updated with correctly positioned comments |
| crates/swc/tests/fixture/**/*.map | Source map files updated to reflect new comment positions |
| .changeset/stupid-numbers-hang.md | Changeset documenting the fix |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Binary Sizes
Commit: 526f588 |
CodSpeed Performance ReportMerging #11314 will not alter performanceComparing Summary
|
Code ReviewI've reviewed PR #11314 which fixes comment emission in the codegen. Here's my analysis: SummaryThis PR fixes an issue where comments at the beginning of files were being lost during code generation. The fix changes how Code Quality ✅Strengths:
Suggestions:
Potential Issues ❌
Performance ⚡The current implementation is acceptable but see optimization suggestion above. The Security 🔒No security concerns identified. This is a pure code generation change. Test Coverage ✅Excellent test coverage! The PR includes:
Adherence to CLAUDE.md Guidelines ✅
RecommendationsBefore merging:
Overall Assessment: ✅ APPROVE with minor suggestions This is a solid fix that addresses the root cause. The extensive test coverage gives confidence the change works correctly across many scenarios. The suggested optimizations are nice-to-haves but not blocking issues. |
Description:
We want to emit all comments, but at the same time, we don't want
ModuleorScriptto take the comment of a statement.