Skip to content

Emit shebangs when printing #1193

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

Merged
merged 3 commits into from
Jun 16, 2025
Merged

Conversation

AlCalzone
Copy link
Contributor

Fixes: #1038

Ideally I'd like to add a test for this, but I have no clue how this works in this repo.

@Copilot Copilot AI review requested due to automatic review settings June 15, 2025 19:58
Copy link
Contributor

@Copilot 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 ensures that a file’s shebang (e.g., #!/usr/bin/env node) is preserved when printing source files.

  • Added GetShebang helper to extract the shebang line from text
  • Updated Printer.emitShebangIfNeeded to call GetShebang and emit the line
  • No tests added for the new functionality

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
internal/scanner/scanner.go Added GetShebang function to return the shebang line if present
internal/printer/printer.go Updated emitShebangIfNeeded to call GetShebang and write the shebang to output
Comments suppressed due to low confidence (2)

internal/scanner/scanner.go:2263

  • There are no unit tests for GetShebang or the updated emitShebangIfNeeded logic. Adding tests that cover files both with and without a shebang will ensure this feature remains correct.
func GetShebang(text string) string {

internal/printer/printer.go:4341

  • Using writeComment may prefix the shebang with comment markers (e.g., //). To emit the shebang exactly as written (including #!), consider using a raw write method such as p.write(shebang).
p.writeComment(shebang)

@jakebailey
Copy link
Member

Ideally I'd like to add a test for this, but I have no clue how this works in this repo.

Most likely we already have a test, such that running hereby test and then hereby baseline-accept will update the baselines.

Otherwise, there are printer tests in the printer package you could add to.

@AlCalzone
Copy link
Contributor Author

Done. I wasn't sure how to interpret the diff, but after accepting the output files look correct to me.

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

Yeah, this looks great to me, thank you!

@jakebailey jakebailey enabled auto-merge June 16, 2025 07:58
@jakebailey jakebailey added this pull request to the merge queue Jun 16, 2025
Merged via the queue into microsoft:main with commit dd7b320 Jun 16, 2025
22 checks passed
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.

Hashbang comments are stripped from JS output
2 participants