-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add hashbang property to AST
#183
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,14 +22,19 @@ await run({ | |||||||||||||||||||
| let output = ""; | ||||||||||||||||||||
| for (const test of tests) { | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| const result = parseForESLint(test.content, { | ||||||||||||||||||||
| // Convert hashbang to normal line comment. | ||||||||||||||||||||
| // This is what ESLint does, before passing code to parser. | ||||||||||||||||||||
| let code = test.content; | ||||||||||||||||||||
| const hasHashbang = code.startsWith("#!"); | ||||||||||||||||||||
| if (hasHashbang) code = `//${code.slice(2)}`; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const result = parseForESLint(code, { | ||||||||||||||||||||
| filePath: path, | ||||||||||||||||||||
| sourceType: test.sourceType.module ? "module" : "script", | ||||||||||||||||||||
| ecmaFeatures: { | ||||||||||||||||||||
| 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 +78,10 @@ await run({ | |||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // 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; | ||||||||||||||||||||
|
||||||||||||||||||||
| 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; |
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.
Potential issue: The check
comments.length > 0 && code.startsWith("#!") && comments[0].type === "Line"doesn't verify thatcomments[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 thatcomments[0].start === 0to ensure it's the first thing in the file.