Skip to content

fix: add hashbang property to AST#183

Merged
overlookmotel merged 1 commit intomainfrom
om/02-24-fix_add_hashbang_property_to_ast
Feb 24, 2026
Merged

fix: add hashbang property to AST#183
overlookmotel merged 1 commit intomainfrom
om/02-24-fix_add_hashbang_property_to_ast

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Feb 24, 2026

Add hashbang property to AST for test cases which include one.

Oxc's AST has a non-standard Hashbang property. We already added hashbang: null to ASTs in snapshots. The PR fills in that property correctly where the file does have a hashbang. We now don't need to skip these test cases in ESTree conformance.

(2nd attempt after #180)

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel force-pushed the om/02-24-fix_add_hashbang_property_to_ast branch from 3328282 to ab37605 Compare February 24, 2026 15:15
@overlookmotel overlookmotel marked this pull request as ready for review February 24, 2026 15:18
Copilot AI review requested due to automatic review settings February 24, 2026 15:18
@overlookmotel overlookmotel self-assigned this Feb 24, 2026
@overlookmotel overlookmotel merged commit 49d5664 into main Feb 24, 2026
3 checks passed
@overlookmotel overlookmotel deleted the om/02-24-fix_add_hashbang_property_to_ast branch February 24, 2026 15:19
Copy link

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 implements proper handling of the hashbang property in AST snapshots for test cases that include a hashbang (shebang). Oxc's AST has a non-standard Hashbang property, and this change populates it correctly instead of always setting it to null. This allows the repository to avoid skipping these test cases in ESTree conformance testing.

Changes:

  • Removed the global ast.hashbang = null initialization from stringifyWith function
  • Added hashbang detection and property generation in both the TypeScript-ESLint and Test262 parsers
  • Updated snapshot files to include proper hashbang objects with type, value, start, and end properties where files contain shebangs

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/utils/json.js Removed the automatic hashbang: null assignment since parsers now handle this property
src/typescript-eslint.js Added logic to detect hashbangs, convert them to comments for parsing, then reconstruct the hashbang property from the first comment
src/test262.js Added comment collection and hashbang property generation from the first comment when code starts with #!
tests/typescript/tests/cases/compiler/shebang.ts.md Updated snapshot with correct hashbang object for /usr/bin/env node
tests/typescript/tests/cases/compiler/shebangBeforeReferences.ts.md Updated snapshot with correct hashbang object for /usr/bin/env node
tests/typescript/tests/cases/compiler/emitBundleWithShebang1.ts.md Updated snapshot with correct hashbang object for /usr/bin/env gjs
tests/typescript/tests/cases/compiler/emitBundleWithShebang2.ts.md Updated snapshots (2 instances) with correct hashbang objects for /usr/bin/env gjs and /usr/bin/env js
tests/typescript/tests/cases/compiler/emitBundleWithShebangAndPrologueDirectives1.ts.md Updated snapshot with correct hashbang object for /usr/bin/env gjs
tests/typescript/tests/cases/compiler/emitBundleWithShebangAndPrologueDirectives2.ts.md Updated snapshots (2 instances) with correct hashbang object for /usr/bin/env gjs
tests/test262/test/language/comments/hashbang/module.json Updated snapshot with correct hashbang object for empty hashbang
tests/test262/test/language/comments/hashbang/not-empty.json Updated snapshot with correct hashbang object for hashbang with comment text
tests/test262/test/language/comments/hashbang/use-strict.json Updated snapshot with correct hashbang object for "use strict" hashbang
tests/test262/test/language/comments/hashbang/line-terminator-carriage-return.json Updated snapshot with correct hashbang object for hashbang ending with carriage return
tests/test262/test/language/comments/hashbang/line-terminator-line-separator.json Updated snapshot with correct hashbang object for hashbang ending with line separator
tests/test262/test/language/comments/hashbang/line-terminator-paragraph-separator.json Updated snapshot with correct hashbang object for hashbang ending with paragraph separator

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// Add `hashbang` property to AST if file starts with a hashbang.
// This property is non-standard and exclusive to Oxc.
program.hashbang = hasHashbang ? { ...comments[0], type: "Hashbang" } : null;
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

Potential issue: Assuming that comments[0] is the hashbang comment may not be safe. If the code has leading whitespace or other comments before the hashbang is converted to a comment, this could fail or assign the wrong comment. Consider checking that comments[0].start === 0 to ensure it's actually the first thing in the file.

Suggested change
program.hashbang = hasHashbang ? { ...comments[0], type: "Hashbang" } : null;
let hashbangComment = null;
if (hasHashbang && Array.isArray(comments) && comments.length > 0) {
// Prefer a comment that starts at the very beginning of the file.
const firstCommentAtStart = comments.find((comment) => comment.start === 0);
const baseComment = firstCommentAtStart ?? comments[0];
hashbangComment = { ...baseComment, type: "Hashbang" };
}
program.hashbang = hashbangComment;

Copilot uses AI. Check for mistakes.

// Add `hashbang` property to AST if file starts with a hashbang.
// This property is non-standard and exclusive to Oxc.
if (comments.length > 0 && code.startsWith("#!") && comments[0].type === "Line") {
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

Potential issue: The check comments.length > 0 && code.startsWith("#!") && comments[0].type === "Line" doesn't verify that comments[0] is actually the hashbang comment at position 0. If there's leading whitespace or other comments, comments[0] might not correspond to the hashbang. Consider adding a check that comments[0].start === 0 to ensure it's the first thing in the file.

Suggested change
if (comments.length > 0 && code.startsWith("#!") && comments[0].type === "Line") {
if (
comments.length > 0 &&
code.startsWith("#!") &&
comments[0].type === "Line" &&
comments[0].start === 0
) {

Copilot uses AI. Check for mistakes.
graphite-app bot pushed a commit to oxc-project/oxc that referenced this pull request Feb 24, 2026
Oxc's AST on JS side has a non-standard `hashbang` property. Previously in ESTree conformance tests, we skipped files with a hashbang.

Bump `estree-conformance` submodule to include oxc-project/estree-conformance#183 which adds `hashbang` property to AST in snapshots.

We can now enable the tests which we previously skipped.
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