-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsparser
More file actions
executable file
·30 lines (26 loc) · 903 Bytes
/
jsparser
File metadata and controls
executable file
·30 lines (26 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env node
const acorn = require("acorn");
const dashAst = require('dash-ast')
const XmlWriter = require('xml-writer');
const fs = require('fs')
const code = fs.readFileSync(process.argv[2], 'utf8')
const ast = acorn.parse(code, {ecmaVersion: 2020, sourceType: 'module'});
const xml = new XmlWriter(true);
xml.startDocument();
dashAst(ast, {
enter: function(node, parent) {
xml.startElement('tree');
xml.writeAttribute('type', node.type);
xml.writeAttribute('pos', node.start);
xml.writeAttribute('length', node.end - node.start);
if (node.type == 'Identifier')
xml.writeAttribute('label', node.name);
else if (node.type == 'Literal')
xml.writeAttribute('label', node.raw);
},
leave: function(node, parent) {
xml.endElement();
}
});
xml.endDocument();
console.log(xml.toString());