Skip to content

fix: add hashbang property to AST#180

Merged
overlookmotel merged 2 commits intomainfrom
om/02-24-fix_add_hashbang_property_to_ast
Feb 24, 2026
Merged

fix: add hashbang property to AST#180
overlookmotel merged 2 commits 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.

Copy link
Member Author

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

@overlookmotel overlookmotel marked this pull request as ready for review February 24, 2026 13:41
Copilot AI review requested due to automatic review settings February 24, 2026 13:41
@overlookmotel overlookmotel merged commit 24eb190 into main Feb 24, 2026
2 of 3 checks passed
@overlookmotel overlookmotel deleted the om/02-24-fix_add_hashbang_property_to_ast branch February 24, 2026 13:42
overlookmotel added a commit that referenced this pull request Feb 24, 2026
overlookmotel added a commit that referenced this pull request Feb 24, 2026
I made a mistake in #179 and #180. The snapshot changes were inaccurate. Reverting and I'll try again.
@overlookmotel
Copy link
Member Author

The snapshots in this PR were wrong as my dependencies were out of date. Reverted in #181 and done again correctly in #183.

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

Adds support for populating Oxc’s non-standard hashbang field in generated AST snapshots so hashbang-related Test262 cases no longer need to be skipped for ESTree conformance.

Changes:

  • Update Test262 AST JSON fixtures to include a populated hashbang node where applicable
  • Stop force-inserting hashbang: null during JSON stringify, and instead set hashbang during parsing/AST construction
  • Add hashbang detection in both the TS-ESLint and Test262 runners

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/test262/test/language/comments/hashbang/use-strict.json Populate hashbang object for this fixture instead of null.
tests/test262/test/language/comments/hashbang/not-empty.json Populate hashbang object for this fixture instead of null.
tests/test262/test/language/comments/hashbang/module.json Populate hashbang object for this fixture instead of null.
tests/test262/test/language/comments/hashbang/line-terminator-paragraph-separator.json Populate hashbang object for this fixture instead of null.
tests/test262/test/language/comments/hashbang/line-terminator-line-separator.json Populate hashbang object for this fixture instead of null.
tests/test262/test/language/comments/hashbang/line-terminator-carriage-return.json Populate hashbang object for this fixture instead of null.
src/utils/json.js Remove the implicit mutation that always set ast.hashbang = null during stringify.
src/typescript-eslint.js Add hashbang extraction from TS-ESLint “Shebang” comment into program.hashbang.
src/test262.js Capture comments during parsing and synthesize ast.hashbang when the file starts with #!.

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

const isModule = preamble.flags?.includes("module");

let ast;
const comments = [];
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.

The same comments array is reused across the Acorn parse attempt and the Meriyah fallback. If the first parse throws after pushing any comments, those partial results will remain in comments and can corrupt the subsequent hashbang detection. Consider using separate arrays per parse attempt, or clearing comments before invoking the fallback parser.

Copilot uses AI. Check for mistakes.
// It defaults to `true` for modules, `false` for scripts, which is what we want.
onComment: comments,
});
} catch {
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.

The same comments array is reused across the Acorn parse attempt and the Meriyah fallback. If the first parse throws after pushing any comments, those partial results will remain in comments and can corrupt the subsequent hashbang detection. Consider using separate arrays per parse attempt, or clearing comments before invoking the fallback parser.

Suggested change
} catch {
} catch {
// Clear any partial comments collected by Acorn before using the fallback parser.
comments.length = 0;

Copilot uses AI. Check for mistakes.
globalReturn: true,
webcompat: true, // I think this enables support for Annex B
next: true, // Enable parsing decorators and import attributes
onComment: comments,
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.

The same comments array is reused across the Acorn parse attempt and the Meriyah fallback. If the first parse throws after pushing any comments, those partial results will remain in comments and can corrupt the subsequent hashbang detection. Consider using separate arrays per parse attempt, or clearing comments before invoking the fallback parser.

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") {
ast.hashbang = { ...comments[0], type: "Hashbang" };
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.

Constructing hashbang via { ...comments[0], ... } makes the emitted AST shape depend on whatever extra fields the parser includes on comment objects (which can vary across parsers/versions). To keep snapshots stable, consider explicitly picking only the expected fields (e.g., value, start, end) when building ast.hashbang.

Suggested change
ast.hashbang = { ...comments[0], type: "Hashbang" };
const { value, start, end } = comments[0];
ast.hashbang = { type: "Hashbang", value, start, end };

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +87
// Add `hashbang` property to AST if file starts with a hashbang.
// This property is non-standard and exclusive to Oxc.
if (comments.length > 0 && comments[0].type === "Shebang") {
const comment = comments[0];
program.hashbang = {
type: "Hashbang",
value: comment.value,
start: comment.range[0],
end: comment.range[1],
};
} else {
program.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.

The hashbang synthesis logic is now duplicated here and in src/test262.js (with slightly different comment/input shapes). Consider extracting a small helper (e.g., normalize-first-comment-to-hashbang) to keep behavior consistent and reduce the chance of future divergence.

Copilot uses AI. Check for mistakes.
overlookmotel added a commit that referenced this pull request 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)
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