Typescript ast extraction improvements.
This commit is contained in:
@@ -42,5 +42,5 @@ export interface TreeNode<TExtraData> {
|
||||
name: string;
|
||||
children: TreeNode<TExtraData>[];
|
||||
data: TExtraData;
|
||||
isSelected: boolean;
|
||||
isMarked: boolean;
|
||||
}
|
||||
|
||||
@@ -14,13 +14,73 @@ export class TypeScriptAstDataExtractor
|
||||
return;
|
||||
}
|
||||
|
||||
const tsApi = require("typescript") as typeof ts | undefined;
|
||||
const tsApi = require("typescript") as typeof ts;
|
||||
if (!tsApi) {
|
||||
return;
|
||||
}
|
||||
|
||||
function findKey(value: any, object: any): string | null {
|
||||
for (var key in object) {
|
||||
if (key.startsWith("_")) continue;
|
||||
|
||||
var member = object[key];
|
||||
if (member === value) return key;
|
||||
|
||||
if (Array.isArray(member) && member.indexOf(value) !== -1)
|
||||
return key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function toTreeNode(
|
||||
node: ts.Node,
|
||||
memberName: string,
|
||||
marked: ts.Node
|
||||
): CommonDataTypes.AstData["root"] {
|
||||
const name = tsApi.SyntaxKind[node.kind];
|
||||
const children = node
|
||||
.getChildren()
|
||||
.map((childNode, idx) => {
|
||||
let parentPropertyName = findKey(childNode, node) || "";
|
||||
|
||||
if (childNode.kind == tsApi.SyntaxKind.SyntaxList) {
|
||||
childNode.getChildren().some(c => {
|
||||
parentPropertyName = findKey(c, node) || "";
|
||||
return !!parentPropertyName;
|
||||
});
|
||||
|
||||
if (childNode.getChildren().length === 0) return null!;
|
||||
}
|
||||
|
||||
if (node.kind == tsApi.SyntaxKind.SyntaxList) {
|
||||
parentPropertyName = "" + idx;
|
||||
}
|
||||
|
||||
return toTreeNode(childNode, parentPropertyName, marked);
|
||||
})
|
||||
.filter(c => c !== null);
|
||||
|
||||
return {
|
||||
name: name,
|
||||
id: memberName,
|
||||
children: children,
|
||||
data: {
|
||||
length: 0,
|
||||
position: 0,
|
||||
},
|
||||
isMarked: node === marked,
|
||||
// startPos: node.pos,
|
||||
// endPos: node.end
|
||||
};
|
||||
}
|
||||
|
||||
if ((tsApi as any).isNode(data)) {
|
||||
const n = data as ts.Node;
|
||||
let root = data as ts.Node;
|
||||
const marked = root;
|
||||
while (root.parent) {
|
||||
root = root.parent;
|
||||
}
|
||||
collector.addExtraction({
|
||||
id: "ts-ast",
|
||||
name: "TypeScript AST",
|
||||
@@ -28,8 +88,8 @@ export class TypeScriptAstDataExtractor
|
||||
extractData() {
|
||||
return {
|
||||
kind: { text: true, tree: true, ast: true },
|
||||
root: toTreeNode(data, "root"),
|
||||
text: n.getSourceFile().text,
|
||||
root: toTreeNode(root, "root", marked),
|
||||
text: root.getSourceFile().text,
|
||||
fileType: "ts",
|
||||
};
|
||||
},
|
||||
@@ -37,57 +97,3 @@ export class TypeScriptAstDataExtractor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findKey(value: any, object: any): string | null {
|
||||
for (var key in object) {
|
||||
if (key.startsWith("_")) continue;
|
||||
|
||||
var member = object[key];
|
||||
if (member === value) return key;
|
||||
|
||||
if (Array.isArray(member) && member.indexOf(value) !== -1) return key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function toTreeNode(
|
||||
node: ts.Node,
|
||||
memberName: string
|
||||
): CommonDataTypes.AstData["root"] {
|
||||
const name = ts.SyntaxKind[node.kind];
|
||||
const children = node
|
||||
.getChildren()
|
||||
.map((childNode, idx) => {
|
||||
let parentPropertyName = findKey(childNode, node) || "";
|
||||
|
||||
if (childNode.kind == ts.SyntaxKind.SyntaxList) {
|
||||
childNode.getChildren().some(c => {
|
||||
parentPropertyName = findKey(c, node) || "";
|
||||
return !!parentPropertyName;
|
||||
});
|
||||
|
||||
if (childNode.getChildren().length === 0) return null!;
|
||||
}
|
||||
|
||||
if (node.kind == ts.SyntaxKind.SyntaxList) {
|
||||
parentPropertyName = "" + idx;
|
||||
}
|
||||
|
||||
return toTreeNode(childNode, parentPropertyName);
|
||||
})
|
||||
.filter(c => c !== null);
|
||||
|
||||
return {
|
||||
name: name,
|
||||
id: memberName,
|
||||
children: children,
|
||||
data: {
|
||||
length: 0,
|
||||
position: 0,
|
||||
},
|
||||
isSelected: false,
|
||||
// startPos: node.pos,
|
||||
// endPos: node.end
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { TreeViewModel, TreeNodeViewModel } from "./Views";
|
||||
import { TreeNodeData } from "@hediet/debug-visualizer-data-extraction";
|
||||
import { CommonDataTypes } from "@hediet/debug-visualizer-data-extraction";
|
||||
|
||||
export function createTreeViewModelFromTreeNodeData(
|
||||
root: TreeNodeData["root"]
|
||||
root: CommonDataTypes.TreeNodeData["root"]
|
||||
): TreeViewModel {
|
||||
const m = new TreeViewModel();
|
||||
m.root = recurse(root, m);
|
||||
return m;
|
||||
|
||||
function recurse(
|
||||
root: TreeNodeData["root"],
|
||||
root: CommonDataTypes.TreeNodeData["root"],
|
||||
viewModel: TreeViewModel
|
||||
): TreeNodeViewModel {
|
||||
const children: TreeNodeViewModel[] = root.children.map(c =>
|
||||
@@ -21,6 +21,7 @@ export function createTreeViewModelFromTreeNodeData(
|
||||
root.name,
|
||||
children
|
||||
);
|
||||
model.isMarked = root.isMarked;
|
||||
for (const c of children) {
|
||||
c.parent = model;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user