Skip to content

Commit ae64be3

Browse files
committed
Made processing better in every syntax type
1 parent 70fcea7 commit ae64be3

File tree

10 files changed

+66
-30
lines changed

10 files changed

+66
-30
lines changed

rocket-chatter-ingestion-server/src/process/prepare/codebase.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ export class Codebase {
116116
let nodes: Record<string, DBNode> = {}
117117

118118
/* Step 1 */
119-
try {
119+
// try {
120120
const files = this._files.slice(start, end)
121121
const jobs = files.map((x) => this._fileProcessor.process(x, nodes))
122122
await Promise.all(jobs)
123-
} catch {
124-
console.error(`Error in processing ${start}-${end} files`)
125-
}
123+
// } catch {
124+
// console.error(`Error in processing ${start}-${end} files`)
125+
// }
126126

127127
/* Step 2 */
128128
for (let i = 0; i < Object.keys(nodes).length; i += this._batchSize) {

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,41 @@ import { Functions } from "./syntax/functions"
1212
import { Interface } from "./syntax/interface"
1313
import { Namespaces } from "./syntax/namespaces"
1414
import { TypeAlias } from "./syntax/typeAlias"
15+
import { VariableDeclarations } from "./syntax/variableDeclarations"
1516

1617
export class FileProcessor implements IFileProcessor {
1718
process(sourceFile: ISourceFile, nodesRef: Record<string, DBNode>): void {
1819
const fileContent = sourceFile.read()
19-
2020
const ast = parse(fileContent)
2121

22-
for (const node of ast.body) {
23-
let treeNode: TreeNode | null = null
22+
const imports = ast.body.filter((node) =>
23+
namedTypes.ImportDeclaration.check(node)
24+
)
25+
26+
let treeNodes: TreeNode[] = []
27+
for (let node of ast.body) {
28+
if (namedTypes.ExportNamedDeclaration.check(node)) {
29+
node = (node as any).declaration
30+
}
31+
2432
if (namedTypes.FunctionDeclaration.check(node)) {
25-
treeNode = Functions.Handle(node)
33+
treeNodes.push(Functions.Handle(node))
2634
} else if (namedTypes.TSInterfaceDeclaration.check(node)) {
27-
treeNode = Interface.Handle(node)
35+
treeNodes.push(Interface.Handle(node))
2836
} else if (namedTypes.TSTypeAliasDeclaration.check(node)) {
29-
treeNode = TypeAlias.Handle(node)
37+
treeNodes.push(TypeAlias.Handle(node))
3038
} else if (namedTypes.TSEnumDeclaration.check(node)) {
31-
treeNode = Enums.Handle(node)
32-
} else if (
33-
namedTypes.TSModuleDeclaration.check(node) ||
34-
namedTypes.ExportNamedDeclaration.check(node)
35-
) {
36-
treeNode = Namespaces.Handle(node)
39+
treeNodes.push(Enums.Handle(node))
3740
} else if (namedTypes.ClassDeclaration.check(node)) {
38-
treeNode = Classes.Handle(node)
41+
treeNodes.push(Classes.Handle(node))
42+
} else if (namedTypes.TSModuleDeclaration.check(node)) {
43+
treeNodes.push(Namespaces.Handle(node))
44+
} else if (namedTypes.VariableDeclaration.check(node)) {
45+
treeNodes.push(...VariableDeclarations.Handle(node))
3946
}
40-
41-
console.log(treeNode)
4247
}
43-
44-
// throw new Error("Method not implemented.")
48+
for (const treeNode of treeNodes) {
49+
console.log(`${sourceFile.getFullPath()}:${treeNode.getID()}`)
50+
}
4551
}
4652
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { Functions } from "./functions"
55

66
export namespace Classes {
77
export function Handle(n: namedTypes.ClassDeclaration) {
8-
const node = new TreeNode(n.id?.name.toString() ?? "", "Class", "")
8+
const node = new TreeNode(
9+
n.id?.name.toString() ?? "",
10+
"Class",
11+
"",
12+
n.loc?.start.line ?? 0,
13+
n.loc?.start.column ?? 0
14+
)
915

1016
// Check for type parameters
1117
const typeParameters: string[] = []

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { Functions } from "./functions"
55

66
export namespace Enums {
77
export function Handle(n: namedTypes.EnumDeclaration) {
8-
const node = new TreeNode(n.id?.name.toString() ?? "", "Enums", "")
8+
const node = new TreeNode(
9+
n.id?.name.toString() ?? "",
10+
"Enum",
11+
"",
12+
n.loc?.start.line ?? 0,
13+
n.loc?.start.column ?? 0
14+
)
915

1016
// Check for external references while initializing the enum members
1117
for (const m of (n as any).members as namedTypes.TSEnumMember[]) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export namespace Functions {
99
export function Handle(n: namedTypes.FunctionDeclaration) {
1010
const node = new TreeNode(
1111
n.id?.name.toString() ?? "",
12-
"function",
13-
n.body.body.map((e) => print(e).code).join("\n")
12+
"Function",
13+
n.body.body.map((e) => print(e).code).join("\n"),
14+
n.loc?.start.line ?? 0,
15+
n.loc?.start.column ?? 0
1416
)
1517

1618
// Handle type annotations

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ 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+
(n.body as any).body.map((e: any) => print(e).code).join("\n"),
13+
n.loc?.start.line ?? 0,
14+
n.loc?.start.column ?? 0
1315
)
1416

1517
// Check for type parameters

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ export namespace Namespaces {
1010
const node = new TreeNode(
1111
(n.id as any)?.name.toString() ?? "",
1212
"Namespace",
13-
""
13+
"",
14+
n.loc?.start.line ?? 0,
15+
n.loc?.start.column ?? 0
1416
)
1517

16-
const body = (n as any).declaration.body.body as namedTypes.TSModuleBlock[]
18+
const body = ((n as any).declaration?.body?.body ??
19+
[]) as namedTypes.TSModuleBlock[]
1720
for (const b of body) {
1821
let d = b
1922
if (namedTypes.ExportNamedDeclaration.check(b)) d = (b as any).declaration

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { TypeAnnotation } from "../core/typeAnnotation"
44

55
export namespace TypeAlias {
66
export function Handle(n: namedTypes.TSTypeAliasDeclaration) {
7-
const node = new TreeNode(n.id?.name.toString() ?? "", "TypeAlias", "")
7+
const node = new TreeNode(
8+
n.id?.name.toString() ?? "",
9+
"TypeAlias",
10+
"",
11+
n.loc?.start.line ?? 0,
12+
n.loc?.start.column ?? 0
13+
)
814

915
// Check for type parameters
1016
const typeParameters: string[] = []

rocket-chatter-ingestion-server/src/process/prepare/sourceFile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ export class SourceFile implements ISourceFile {
55
private _path: string
66

77
constructor(path: string) {
8-
this._path = path
8+
this._path = path.replace(/\\/g, "/")
99
}
1010

1111
read(): string {
1212
const content = readFileSync(this._path, "utf-8")
1313
return content
1414
}
15+
16+
getFullPath(): string {
17+
return this._path
18+
}
1519
}

rocket-chatter-ingestion-server/src/process/prepare/sourceFile.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export enum SourceFileType {
1313

1414
export interface ISourceFile {
1515
read(): string
16+
getFullPath(): string
1617
}

0 commit comments

Comments
 (0)