Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/test262.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ await run({
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.
try {
ast = AcornParser.parse(code, {
ecmaVersion: "latest",
Expand All @@ -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 {
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.
try {
Expand All @@ -63,6 +65,7 @@ await run({
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.
});
} catch {
return;
Expand All @@ -71,6 +74,14 @@ await run({
fixMeriyahValue(ast);
}

// 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.
} else {
ast.hashbang = null;
}

// Parse tokens
const tokensJson = parseEspreeTokens(code, isModule, false);

Expand Down
15 changes: 14 additions & 1 deletion src/typescript-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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.

const astJson = stringifyWith(program, transformerTs);
output += "__ESTREE_TEST__:AST:\n```json\n" + astJson + "\n```\n";

Expand Down
2 changes: 0 additions & 2 deletions src/utils/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ export function transformerTs(_key, value) {
}

export function stringifyWith(ast, transformer) {
// Add `hashbang` field
ast.hashbang = null;
// Serialize to JSON, with modifications
let json = JSON.stringify(ast, transformer, 2);
json = json.replace(INFINITY_REGEXP, "1e+400");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
}
],
"sourceType": "script",
"hashbang": null,
"hashbang": {
"type": "Hashbang",
"value": " this comment ends with a Carriage Return (U+000D)",
"start": 0,
"end": 52
},
"start": 0,
"end": 637
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
}
],
"sourceType": "script",
"hashbang": null,
"hashbang": {
"type": "Hashbang",
"value": " this comment ends with a Line Separator (U+2028)",
"start": 0,
"end": 51
},
"start": 0,
"end": 635
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
}
],
"sourceType": "script",
"hashbang": null,
"hashbang": {
"type": "Hashbang",
"value": " this comment ends with a Paragraph Separator (U+2029)",
"start": 0,
"end": 56
},
"start": 0,
"end": 645
}
7 changes: 6 additions & 1 deletion tests/test262/test/language/comments/hashbang/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"type": "Program",
"body": [],
"sourceType": "module",
"hashbang": null,
"hashbang": {
"type": "Hashbang",
"value": "",
"start": 0,
"end": 2
},
"start": 0,
"end": 333
}
7 changes: 6 additions & 1 deletion tests/test262/test/language/comments/hashbang/not-empty.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"type": "Program",
"body": [],
"sourceType": "script",
"hashbang": null,
"hashbang": {
"type": "Hashbang",
"value": " these characters should be treated as a comment",
"start": 0,
"end": 50
},
"start": 0,
"end": 412
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
}
],
"sourceType": "script",
"hashbang": null,
"hashbang": {
"type": "Hashbang",
"value": "\"use strict\"",
"start": 0,
"end": 14
},
"start": 0,
"end": 391
}
Loading