Skip to content

Commit 57c30af

Browse files
committed
Now body of each syntax type is getting extracted perfectly
1 parent 47bfaec commit 57c30af

File tree

9 files changed

+40
-58
lines changed

9 files changed

+40
-58
lines changed

rocket-chatter-ingestion-server/src/process/prepare/processor/core/treeNode.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ export type TreeNodeSourceLocation = {
22
start: {
33
line: number
44
column: number
5-
index: number
65
}
76
end: {
87
line: number
98
column: number
10-
index: number
119
}
1210
}
1311

rocket-chatter-ingestion-server/src/process/prepare/processor/file.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,18 @@ export class FileProcessor implements IFileProcessor {
8686
}
8787

8888
// Add the nodes to the global reference
89+
const fileContentLines = fileContent.split("\n")
90+
8991
for (const treeNode of treeNodes) {
92+
if (!treeNode.body) {
93+
const startLine = treeNode.location.start.line - 1
94+
const endLine = treeNode.location.end.line + 1
95+
treeNode.body = fileContentLines
96+
.slice(startLine, endLine)
97+
.join("\n")
98+
.trim()
99+
}
100+
90101
const dbNode = DBNode.fromTreeNode(treeNode)
91102
nodesRef[dbNode.id] = dbNode
92103
}

rocket-chatter-ingestion-server/src/process/prepare/processor/syntax/classes.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
import { namedTypes } from "ast-types"
22

3-
import { print } from "recast"
43
import { TreeNode } from "../core/treeNode"
54
import { TypeArgument } from "../core/typeArgument"
65
import { Functions } from "./functions"
76

87
export namespace Classes {
98
export function Handle(n: namedTypes.ClassDeclaration) {
10-
const node = new TreeNode(
11-
n.id?.name.toString() ?? "",
12-
"Class",
13-
n.body.body.map((e) => print(e).code).join("\n"),
14-
"",
15-
{
16-
start: {
17-
line: n.loc?.start.line ?? 0,
18-
column: n.loc?.start.column ?? 0,
19-
index: (n as any).start ?? 0,
20-
},
21-
end: {
22-
line: n.loc?.end.line ?? 0,
23-
column: n.loc?.end.column ?? 0,
24-
index: (n as any).end ?? 0,
25-
},
26-
}
27-
)
9+
const node = new TreeNode(n.id?.name.toString() ?? "", "Class", "", "", {
10+
start: {
11+
line: n.body.loc?.start.line ?? 0,
12+
column: n.body.loc?.start.column ?? 0,
13+
},
14+
end: {
15+
line: n.body.loc?.end.line ?? 0,
16+
column: n.body.loc?.end.column ?? 0,
17+
},
18+
})
2819

2920
// Check for type parameters
3021
const typeParameters: string[] = []

rocket-chatter-ingestion-server/src/process/prepare/processor/syntax/enums.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { namedTypes } from "ast-types"
2+
23
import { TreeNode } from "../core/treeNode"
34
import { Classes } from "./classes"
45
import { Functions } from "./functions"
@@ -9,12 +10,10 @@ export namespace Enums {
910
start: {
1011
line: n.loc?.start.line ?? 0,
1112
column: n.loc?.start.column ?? 0,
12-
index: (n as any).start ?? 0,
1313
},
1414
end: {
1515
line: n.loc?.end.line ?? 0,
1616
column: n.loc?.end.column ?? 0,
17-
index: (n as any).end ?? 0,
1817
},
1918
})
2019

rocket-chatter-ingestion-server/src/process/prepare/processor/syntax/functions.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
import { namedTypes } from "ast-types"
2-
import { print } from "recast"
2+
33
import { TreeNode } from "../core/treeNode"
44
import { TypeAnnotation } from "../core/typeAnnotation"
55
import { TypeArgument } from "../core/typeArgument"
66
import { Classes } from "./classes"
77

88
export namespace Functions {
99
export function Handle(n: namedTypes.FunctionDeclaration) {
10-
const node = new TreeNode(
11-
n.id?.name.toString() ?? "",
12-
"Function",
13-
n.body.body.map((e) => print(e).code).join("\n"),
14-
"",
15-
{
16-
start: {
17-
line: n.loc?.start.line ?? 0,
18-
column: n.loc?.start.column ?? 0,
19-
index: (n as any).start ?? 0,
20-
},
21-
end: {
22-
line: n.loc?.end.line ?? 0,
23-
column: n.loc?.end.column ?? 0,
24-
index: (n as any).end ?? 0,
25-
},
26-
}
27-
)
10+
const node = new TreeNode(n.id?.name.toString() ?? "", "Function", "", "", {
11+
start: {
12+
line: n.body.loc?.start.line ?? 0,
13+
column: n.body.loc?.start.column ?? 0,
14+
},
15+
end: {
16+
line: n.body.loc?.end.line ?? 0,
17+
column: n.body.loc?.end.column ?? 0,
18+
},
19+
})
2820

2921
// Check for type parameters
3022
const typeParameters: string[] = []

rocket-chatter-ingestion-server/src/process/prepare/processor/syntax/interface.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { namedTypes } from "ast-types"
2-
import { print } from "recast"
2+
33
import { TreeNode } from "../core/treeNode"
44
import { TypeAnnotation } from "../core/typeAnnotation"
55
import { TypeArgument } from "../core/typeArgument"
@@ -9,18 +9,16 @@ export namespace Interface {
99
const node = new TreeNode(
1010
n.id?.name.toString() ?? "",
1111
"Interface",
12-
(n.body as any).body.map((e: any) => print(e).code).join("\n"),
12+
"",
1313
"",
1414
{
1515
start: {
1616
line: n.loc?.start.line ?? 0,
1717
column: n.loc?.start.column ?? 0,
18-
index: (n as any).start ?? 0,
1918
},
2019
end: {
2120
line: n.loc?.end.line ?? 0,
2221
column: n.loc?.end.column ?? 0,
23-
index: (n as any).end ?? 0,
2422
},
2523
}
2624
)
@@ -32,7 +30,7 @@ export namespace Interface {
3230
}
3331

3432
// Check for extensions
35-
for (const e of n.extends) {
33+
for (const e of n.extends ?? []) {
3634
const name = (e as any).expression.name
3735
if (typeParameters.includes(name)) continue
3836

rocket-chatter-ingestion-server/src/process/prepare/processor/syntax/namespaces.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { namedTypes } from "ast-types/gen/namedTypes"
1+
import { namedTypes } from "ast-types"
2+
23
import { TreeNode } from "../core/treeNode"
34
import { Enums } from "./enums"
45
import { Functions } from "./functions"
@@ -16,12 +17,10 @@ export namespace Namespaces {
1617
start: {
1718
line: n.loc?.start.line ?? 0,
1819
column: n.loc?.start.column ?? 0,
19-
index: (n as any).start ?? 0,
2020
},
2121
end: {
2222
line: n.loc?.end.line ?? 0,
2323
column: n.loc?.end.column ?? 0,
24-
index: (n as any).end ?? 0,
2524
},
2625
}
2726
)

rocket-chatter-ingestion-server/src/process/prepare/processor/syntax/typeAlias.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { namedTypes } from "ast-types"
2-
import { writeFileSync } from "fs"
2+
33
import { TreeNode } from "../core/treeNode"
44
import { TypeAnnotation } from "../core/typeAnnotation"
55

@@ -14,19 +14,14 @@ export namespace TypeAlias {
1414
start: {
1515
line: n.loc?.start.line ?? 0,
1616
column: n.loc?.start.column ?? 0,
17-
index: (n as any).start ?? 0,
1817
},
1918
end: {
2019
line: n.loc?.end.line ?? 0,
2120
column: n.loc?.end.column ?? 0,
22-
index: (n as any).end ?? 0,
2321
},
2422
}
2523
)
2624

27-
// extract body of the type alias
28-
writeFileSync("test.json", JSON.stringify(n, null, 2))
29-
3025
// Check for type parameters
3126
const typeParameters: string[] = []
3227
for (const p of n.typeParameters?.params ?? []) {

rocket-chatter-ingestion-server/src/process/prepare/processor/syntax/variableDeclarations.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { namedTypes } from "ast-types"
2+
23
import { TreeNode } from "../core/treeNode"
34
import { Classes } from "./classes"
45
import { Functions } from "./functions"
@@ -14,12 +15,10 @@ export namespace VariableDeclarations {
1415
start: {
1516
line: n.loc?.start.line ?? 0,
1617
column: n.loc?.start.column ?? 0,
17-
index: (n as any).start ?? 0,
1818
},
1919
end: {
2020
line: n.loc?.end.line ?? 0,
2121
column: n.loc?.end.column ?? 0,
22-
index: (n as any).end ?? 0,
2322
},
2423
})
2524

0 commit comments

Comments
 (0)