Skip to content

Commit 2f77326

Browse files
committed
feat(ast) a good bunch going, found a way to represent all useful bits of information
1 parent cb3808a commit 2f77326

File tree

5 files changed

+55
-50
lines changed

5 files changed

+55
-50
lines changed

dist/main/atom/views/astView.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function runCode(rootNode, mainContent) {
7878
.attr("dy", 3.5)
7979
.attr("dx", 5.5)
8080
.text(function (d) {
81-
return 'Kind: ' + d.kind;
81+
return d.kind;
8282
});
8383
nodeEnter.transition()
8484
.duration(duration)

dist/main/lang/modules/astToText.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Inspired by `ts.forEachChild`:
66
// https://github.com/Microsoft/TypeScript/blob/65cbd91667acf890f21a3527b3647c7bc994ca32/src/compiler/parser.ts#L43-L320
77
var ts = require("typescript");
8-
var tsconfig_1 = require("../../tsconfig/tsconfig");
98
function astToText(srcFile) {
109
//// A useful function for debugging
1110
// aggregate(srcFile, 0);
@@ -15,11 +14,7 @@ function astToText(srcFile) {
1514
// }
1615
var nodeIndex = 0;
1716
function nodeToNodeDisplay(node, depth) {
18-
if (node.parent) {
19-
delete node.parent;
20-
}
2117
var kind = syntaxKindToString(node.kind);
22-
var details = keyDetails(node, kind);
2318
var children = [];
2419
ts.forEachChild(node, function (cNode) {
2520
var child = nodeToNodeDisplay(cNode, depth + 1);
@@ -28,10 +23,11 @@ function astToText(srcFile) {
2823
var ret = {
2924
kind: kind,
3025
children: children,
26+
pos: node.pos,
27+
end: node.end,
3128
depth: depth,
3229
nodeIndex: nodeIndex,
33-
details: details,
34-
rawJson: tsconfig_1.prettyJSON(node)
30+
rawJson: prettyJSONNoParent(node)
3531
};
3632
nodeIndex++;
3733
return ret;
@@ -40,20 +36,23 @@ function astToText(srcFile) {
4036
return root;
4137
}
4238
exports.default = astToText;
43-
function keyDetails(node, kind) {
44-
var n = node;
45-
var details = undefined;
46-
if (node.kind == 227) {
47-
details = { fileName: node.fileName };
48-
}
49-
else if (n.name && n.name.text) {
50-
details = { 'name.text: ': (n.name.text) };
51-
}
52-
return details;
53-
}
5439
function syntaxKindToString(syntaxKind) {
5540
return ts.SyntaxKind[syntaxKind];
5641
}
57-
function clearParent(obj) {
58-
return obj;
42+
function prettyJSONNoParent(object) {
43+
var cache = [];
44+
var value = JSON.stringify(object, function (key, value) {
45+
if (key == 'parent') {
46+
return;
47+
}
48+
if (typeof value === 'object' && value !== null) {
49+
if (cache.indexOf(value) !== -1) {
50+
return;
51+
}
52+
cache.push(value);
53+
}
54+
return value;
55+
}, 4);
56+
cache = null;
57+
return value;
5958
}

lib/globals.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ interface NodeDisplay {
136136
kind: string;
137137
children: NodeDisplay[];
138138

139+
pos: number;
140+
end: number;
141+
139142
/** Represents how many parents it has */
140143
depth: number;
141144
/** If we had a flat structure this is where this item would belong */
@@ -144,6 +147,8 @@ interface NodeDisplay {
144147
/** Key Details I understand */
145148
details?: any;
146149

147-
/** Best attempt serialization of original node */
150+
/** Best attempt serialization of original node
151+
* I also remove `parent`
152+
*/
148153
rawJson: any;
149154
}

lib/main/atom/views/astView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function runCode(rootNode:NodeDisplay, mainContent:HTMLElement){
103103
.attr("dy", 3.5)
104104
.attr("dx", 5.5)
105105
.text(function(d:NodeDisplay) {
106-
return 'Kind: ' + d.kind;
106+
return d.kind;
107107
});
108108

109109
// Transition nodes to their new position.

lib/main/lang/modules/astToText.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
import * as ts from "typescript";
1010
import {SyntaxKind, Node} from "typescript";
11-
import {prettyJSON} from "../../tsconfig/tsconfig";
1211

13-
/** Important notes:
14-
* I delete `.parent`. As its noisy :)
12+
/**
13+
* Important notes:
1514
*/
1615
export default function astToText(srcFile: ts.SourceFile) {
1716

@@ -24,12 +23,8 @@ export default function astToText(srcFile: ts.SourceFile) {
2423

2524
var nodeIndex = 0;
2625
function nodeToNodeDisplay(node: ts.Node, depth: number): NodeDisplay {
27-
if (node.parent) {
28-
delete node.parent;
29-
}
3026

3127
var kind = syntaxKindToString(node.kind);
32-
var details = keyDetails(node, kind);
3328

3429
var children = [];
3530
ts.forEachChild(node, (cNode) => {
@@ -40,10 +35,11 @@ export default function astToText(srcFile: ts.SourceFile) {
4035
var ret: NodeDisplay = {
4136
kind,
4237
children,
38+
pos: node.pos,
39+
end: node.end,
4340
depth,
4441
nodeIndex,
45-
details,
46-
rawJson: prettyJSON(node)
42+
rawJson: prettyJSONNoParent(node)
4743
};
4844

4945
// each time we get called index is incremented
@@ -56,29 +52,34 @@ export default function astToText(srcFile: ts.SourceFile) {
5652
return root;
5753
}
5854

59-
60-
function keyDetails(node: Node, kind: string): any {
61-
let n: any = node;
62-
var details = undefined;
63-
64-
if (node.kind == SyntaxKind.SourceFile) {
65-
details = { fileName: (<ts.SourceFile>node).fileName };
66-
}
67-
else if (n.name && n.name.text) {
68-
details = { 'name.text: ': (n.name.text) };
69-
}
70-
71-
return details;
72-
}
73-
7455
function syntaxKindToString(syntaxKind: ts.SyntaxKind): string {
7556
return (<any>ts).SyntaxKind[syntaxKind];
7657
}
7758

78-
function clearParent<T>(obj: T): T {
79-
return obj;
80-
}
8159

60+
function prettyJSONNoParent(object: any): string {
61+
var cache = [];
62+
var value = JSON.stringify(object,
63+
// fixup circular reference
64+
function(key, value) {
65+
if (key == 'parent'){
66+
return;
67+
}
68+
if (typeof value === 'object' && value !== null) {
69+
if (cache.indexOf(value) !== -1) {
70+
// Circular reference found, discard key
71+
return;
72+
}
73+
// Store value in our collection
74+
cache.push(value);
75+
}
76+
return value;
77+
},
78+
// indent 4 spaces
79+
4);
80+
cache = null;
81+
return value;
82+
}
8283

8384
// import {Node,SyntaxKind,visitNode} from "typescript";
8485
//

0 commit comments

Comments
 (0)