dev: add js-parse-and-output experiment

This commit is contained in:
KernelDeimos
2024-07-09 03:40:34 -04:00
parent 0b39c76c40
commit 50036e954d
5 changed files with 203 additions and 20 deletions
+29
View File
@@ -0,0 +1,29 @@
# Parsing Javascript and Generating it Back
## What Purpose?
This would be really useful for refactoring tools. I also want
automatic comments to be placed when certain features are used,
such as the [Sequence](../../src/backend/src/codex/Sequence.js)
class, since its usefulness won't be immediately apparent where
it appears in the source code.
I turns out the state of affairs with respect to generating a
CST for javascript... [kind of sucks](https://github.com/benjamn/recast/issues/1412).
I hope that further discussion on the issue I linked renders the
previous statement ironic.
## So, What Next?
The options I see are:
1. Add support to recast to make use of @babel/parser tokens when
the `tokens: true` option is set.
2. Add a format-preserving outputter to @babel/outputter.
[this is being worked on](https://github.com/babel/rfcs/pull/15)
3. Wait for someone else to do either of the previous two things.
4. Write a CST parser for javascript.
I'm going to start with option #3. It's very disappointing that
I don't have time to do #4, because I don't very much like solutions
#1 and #2; I like my CSTs to be more cohesive - a pyramid of meaning -
rather than lexer output dumped at the end of an AST.
@@ -0,0 +1,20 @@
{
"name": "js-parse-and-output",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "AGPL-3.0-only",
"dependencies": {
"@babel/generator": "^7.24.7",
"@babel/parser": "^7.24.7",
"acorn": "^8.12.1",
"acorn-static-class-features": "^1.0.0",
"escodegen": "^2.1.0",
"recast": "^0.23.9"
}
}
+37
View File
@@ -0,0 +1,37 @@
const babelParser = require('@babel/parser');
const generate = (require('@babel/generator')).default;
const fs = require('fs');
const recast = require('recast');
const example = fs.readFileSync('./src/backend/src/filesystem/ll_operations/ll_read.js');
{
const ast = recast.parse(example, {
parser: {
parse (source) {
return babelParser.parse(source, {
ranges: true,
tokens: true,
});
},
},
});
const { code } = recast.print(ast);
}
{
const ast = babelParser.parse('' + example, {
tokens: true,
});
console.log(JSON.stringify(ast, undefined, ' '));
}
/*
const { code } = generate(ast, {
retainLines: true,
});
*/
// console.log(code);