Skip to content

Conversation

@jakebailey
Copy link
Member

@jakebailey jakebailey commented Oct 23, 2025

Fixes #1526

#1938 sort of found the right area but I found some misports that fix way more baselines still.

@jakebailey jakebailey requested review from Copilot and weswigham and removed request for Copilot October 23, 2025 22:46
containerEnd := p.containerEnd
declarationListContainerEnd := p.declarationListContainerEnd
skipLeadingComments := emitFlags&EFNoLeadingComments == 0 && !ast.PositionIsSynthesized(detachedRange.Pos())
skipLeadingComments := ast.PositionIsSynthesized(detachedRange.Pos()) || emitFlags&EFNoLeadingComments != 0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a porting bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ol' "let's invert this condition" and then fail to invert the usage/name.

Comment on lines +4395 to +4397
if !p.writer.IsAtStartOfLine() {
p.writeLine()
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required. I am not sure where Strada did this particular newline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first line of emitSourceFile is an unconditional writeLine, which is after writeFile's emitShebangIfNeeded and emitPrologueDirectivesIfNeeded calls. Which puts this in exactly the right place.

Copilot AI review requested due to automatic review settings October 23, 2025 23:21
Copy link
Contributor

Copilot AI left a 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 bugs in the emission of leading source file comments, particularly addressing issues with comment placement when emitting JavaScript files. The fix ensures that leading comments (such as triple-slash references, JSDoc comments, and pinned comments) are emitted in the correct position relative to module boilerplate code.

Key changes:

  • Corrected the logic for when to emit detached comments in source files
  • Fixed the placement of comments to appear before Object.defineProperty exports statements rather than after
  • Ensured proper line breaks before emitting detached comments after prologue directives

Reviewed Changes

Copilot reviewed 300 out of 857 changed files in this pull request and generated no comments.

File Description
internal/printer/printer.go Fixed comment emission timing and logic in emitSourceFile to handle non-JSON files correctly, and corrected the conditional logic in emitDetachedCommentsBeforeStatementList
testdata/baselines/reference/submodule/compiler/*.js Updated baseline test outputs showing leading comments now appear before module boilerplate
testdata/baselines/reference/submodule/compiler/*.js.diff Removed diff entries as the output now matches TypeScript's expected behavior
testdata/baselines/reference/compiler/*.js Updated baseline for local test files with corrected comment placement

if node.IsDeclarationFile {
p.emitTripleSlashDirectives(node)
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a JSON document will ever have detached leading comments (since it only ever has one statement and no prologues), so this could probably be a hardcoded initial empty comment emit state, but it's probably fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no test change when I added this back (nil is fine), but since the original code did this unconditionally (both codebases) I thought I would add it back. I bet we just don't have any tests which reprint a JSON file without downleveling it...

containerEnd := p.containerEnd
declarationListContainerEnd := p.declarationListContainerEnd
skipLeadingComments := emitFlags&EFNoLeadingComments == 0 && !ast.PositionIsSynthesized(detachedRange.Pos())
skipLeadingComments := ast.PositionIsSynthesized(detachedRange.Pos()) || emitFlags&EFNoLeadingComments != 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ol' "let's invert this condition" and then fail to invert the usage/name.

@jakebailey jakebailey added this pull request to the merge queue Oct 24, 2025
Merged via the queue into main with commit 5c2cafa Oct 24, 2025
22 checks passed
@jakebailey jakebailey deleted the jabaile/fix-1938 branch October 24, 2025 16:51
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.

Emit adds generated code before file header/copyright comment

4 participants