-
Notifications
You must be signed in to change notification settings - Fork 10
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
Generate Types #5
base: main
Are you sure you want to change the base?
Conversation
@@ -77,6 +81,7 @@ export default class { | |||
const lmfile = `${this.cwd}/@types/${basename(this.config.importPath)}/lastModified`; | |||
if (lm !== await Bun.file(lmfile).text()) { | |||
await this.build(); | |||
await this.initConfigTypes(); |
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.
This should maybe be in a separate PR, it just regenerates the config types if the source has changed
// Use Node to run tree-sitter due to Bun issue: | ||
// https://github.com/oven-sh/bun/issues/4188 | ||
const types = Bun.spawnSync([ | ||
"node", |
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.
Instead of node, tree-sitter provides wasm bindings which can be used https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/README.md
@@ -25,6 +25,10 @@ | |||
"url": "https://github.com/tr1ckydev/hyperimport/issues" | |||
}, | |||
"homepage": "https://github.com/tr1ckydev/hyperimport#readme", | |||
"dependencies": { | |||
"tree-sitter": "^0.20.5", | |||
"tree-sitter-rust": "^0.20.4" |
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.
we should probably conditionally import per-lang packages instead of including this in deps
@@ -0,0 +1,70 @@ | |||
const fs = require("fs"); | |||
const Parser = require("tree-sitter"); | |||
const Rust = require("tree-sitter-rust"); |
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.
conditional import + try to load for all the loaders specified in bunfig.toml
This uses tree sitter to generate types for the Rust loader.
The nice thing about tree sitter is that there are parsers for most languages, so it should be fairly easy to add new type mappings.
Unfortunately, there's an open issue in Bun that prevents tree sitter from working with Bun, so I'm spawning a node process instead to do the actual parsing. This would be pretty easy to move back to Bun though once the issue is fixed.
Here's an example of how it works:
Rust:
The resulting
config.ts
: