-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add hashbang property to AST
#180
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,7 @@ await run({ | |||||||||
| const isModule = preamble.flags?.includes("module"); | ||||||||||
|
|
||||||||||
| let ast; | ||||||||||
| const comments = []; | ||||||||||
| try { | ||||||||||
| ast = AcornParser.parse(code, { | ||||||||||
| ecmaVersion: "latest", | ||||||||||
|
|
@@ -51,6 +52,7 @@ await run({ | |||||||||
| allowReturnOutsideFunction: true, | ||||||||||
| // Note: Do not specify `allowAwaitOutsideFunction` option. | ||||||||||
| // It defaults to `true` for modules, `false` for scripts, which is what we want. | ||||||||||
| onComment: comments, | ||||||||||
| }); | ||||||||||
| } catch { | ||||||||||
|
||||||||||
| } catch { | |
| } catch { | |
| // Clear any partial comments collected by Acorn before using the fallback parser. | |
| comments.length = 0; |
Copilot
AI
Feb 24, 2026
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.
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
AI
Feb 24, 2026
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.
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.
| ast.hashbang = { ...comments[0], type: "Hashbang" }; | |
| const { value, start, end } = comments[0]; | |
| ast.hashbang = { type: "Hashbang", value, start, end }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,6 @@ await run({ | |
| jsx: test.sourceType.jsx, | ||
| }, | ||
| }); | ||
| // oxlint-disable-next-line no-unused-vars | ||
| const { comments, tokens, ...program } = result.ast; | ||
|
|
||
| // TS-ESLint parser has no `unambiguous` option, so emulate it here | ||
|
|
@@ -73,6 +72,20 @@ await run({ | |
| } | ||
| } | ||
|
|
||
| // 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; | ||
| } | ||
|
Comment on lines
+75
to
+87
|
||
|
|
||
| const astJson = stringifyWith(program, transformerTs); | ||
| output += "__ESTREE_TEST__:AST:\n```json\n" + astJson + "\n```\n"; | ||
|
|
||
|
|
||
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.
The same
commentsarray 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 incommentsand can corrupt the subsequent hashbang detection. Consider using separate arrays per parse attempt, or clearingcommentsbefore invoking the fallback parser.