-
-
Notifications
You must be signed in to change notification settings - Fork 461
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
oxc-parser
NPM package return AST as JS object, not JSON
#4295
Comments
@Boshen and I discussed on video call just now. He's given his 👍 to this. |
@overlookmotel Supporting |
Hi @dalisoft. Sorry but I don't quite understand what you mean. I'm not much of a TypeScript user, so perhaps that's the reason. Could you possibly elaborate? |
Inputconst funcString =
/**
*
* @returns {{status: 'success' | 'error'}}
*/
(async (req, res) => {
const var1 = req.headers['X-Telemetry-ID'];
const var2 = req.cookies['X-User-ID'];
useEffect(() => {
let unload;
const initTelemetry = async () => {
await xUserConnect(req.params['x-user-id']);
({ unload } = await loadTelemetryApp(var1));
};
initTelemetry();
return unload;
}, [var1, var2]);
const result = await someExpensiveTask();
const confirm = await waitToConfirm(req, result);
const { cookies } = req;
const { send } = res;
const queries = req.queries;
const { compress } = req.queries;
const { check_zstd } = queries;
const values = [req.body];
const foo = {
bar: req.body.bar
};
const template = `${req.params.mailId}`;
await testMe(queries.elemId);
shouldSync(req.params);
await checkAuth(req.headers, req.cookies);
await itWorks(result, confirm);
itWorksSync(confirm);
await (async () => {
await someSQLTask();
})();
return { status: 'success' };
}).toString();
const oxcParserResult = await oxcParser.parseAsync(funcString, {
sourceType: 'unambiguous',
sourceFilename: `route-${i}.ts`,
preserveParens: true
});
const { errors, comments, program } = oxcParserResult;
// program is `string` at here, but as this issue propose is using `JS` instead of `JSON` string, i would like to expect `TypeScript types` too Propose 1 (TypeScript types)// This is for example, this interface/type could be improved
// with type-based properties like `body` or `properties`
interface ASTNode {
type:
| 'Program'
| 'ExpressionStatement'
| 'ArrowFunctionExpression'
| 'CallExpression' /* so on other types */;
start: number;
end: number;
body?: ASTNode[];
}
const { errors, comments, program } = oxcParserResult;
// program is `ASTNode` at here for better completion Propose 2 (TypeScript
|
Ah ha. Thanks for clarifying. We are working on infrastructure to generate TS type definitions from our internal Rust types. It'll take a little time to do that (not quite as simple as it sounds!), but it is coming. |
Thank you @overlookmotel if there need any help at JavaScript/TypeScript-side or any testing, i would like to help with that |
Thanks very much for the offer. I've made a note to call on you when we have type def generation working. |
@dalisoft could you please help me understand how Propose 2 can work with dynamic value? |
@escaton It will work as expected because all values (even dynamic) will be as types so it gets as |
In order for TS to infer something with
|
@escaton Seems i don’t understand what you means by dynamic values. |
What happens when AST is parsed by Rust and then transferred to JS is conceptually not different from
|
For such case I don’t know but with for my own project i am did
|
Indeed, that will work, but this is not quite the mainstream usage of the ast parsers :) |
I've been looking at using the node bindings for |
@danielroe Thanks for the offer. I'm afraid that's going to be a while, because generating types is tied up with other major changes we're making to how AST data is transported from Rust to JS. And for the same reasons, it's unfortunately not something that's really accessible to contributors to work on either. I'm really sorry that's a rubbish answer! We would dearly like to receive help from the community, but it's just a lot more complicated than it might appear. It goes deep! When we get movement on this and have type generation working, I hope you don't mind if we take up your offer to help with testing. |
#6796 alters We should still add a |
Will defer when requirement arises. |
@danielroe |
At present, NPM package oxc-parser's
parse*
methods return an object including AST as JSON string.I propose:
parseSync
andparseAsync
to callJSON.parse
on the JSON by default, so they return AST as a JS object.json: true
to return JSON string instead (as at present).This is a breaking change, and will require bumping the package minor version (i.e. 0.21.0).
This is motivated by oxc-project/backlog#47.
The text was updated successfully, but these errors were encountered: