Skip to content

Commit 78bd0f8

Browse files
committed
exportResolver manages classes, structs and enums
1 parent c00ced8 commit 78bd0f8

File tree

2 files changed

+110
-172
lines changed

2 files changed

+110
-172
lines changed

packages/cli/src/languagePlugins2/csharp/exportResolver/index.test.ts

Lines changed: 107 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,149 @@
11
import { describe, test, expect, beforeEach } from "vitest";
22
import Parser from "tree-sitter";
33
import { CsharpExportResolver } from ".";
4-
import { csharpParser } from "../../../helpers/treeSitter/parsers";
4+
import { getCSharpFilesMap } from "../testFiles";
55

66
describe("CsharpExportResolver", () => {
77
let resolver: CsharpExportResolver;
88
let files: Map<string, { path: string; rootNode: Parser.SyntaxNode }>;
99

1010
beforeEach(() => {
1111
try {
12-
files = new Map([
13-
[
14-
"project/MyClass.cs",
15-
{
16-
path: "project/MyClass.cs",
17-
rootNode: csharpParser.parse(`
18-
public class MyClass
19-
{
20-
}
21-
`).rootNode,
22-
},
23-
],
24-
[
25-
"project/Models.cs",
26-
{
27-
path: "project/Models.cs",
28-
rootNode: csharpParser.parse(`
29-
public class User
30-
{
31-
public string Name { get; set; }
32-
}
33-
public struct Order
34-
{
35-
public int OrderId;
36-
public string Description;
37-
}
38-
public enum OrderStatus
39-
{
40-
Pending,
41-
Completed
42-
}
43-
`).rootNode,
44-
},
45-
],
46-
[
47-
"project/empty.cs",
48-
{
49-
path: "project/empty.cs",
50-
rootNode: csharpParser.parse("").rootNode,
51-
},
52-
],
53-
]);
12+
files = getCSharpFilesMap();
5413
resolver = new CsharpExportResolver(files);
5514
} catch (error) {
5615
console.error("Error in beforeEach:", error);
5716
throw error;
5817
}
5918
});
6019

61-
test("MyClass.cs", () => {
20+
test("Namespaced.cs", () => {
6221
try {
63-
const symbols = resolver.getSymbols("project/MyClass.cs");
64-
expect(symbols).toMatchObject([
65-
{
66-
id: "MyClass",
67-
identifierNode: { text: "MyClass" },
68-
type: "class",
69-
},
70-
]);
22+
const symbols = resolver.getSymbols("Namespaced.cs");
23+
expect(symbols).toEqual(
24+
expect.arrayContaining([
25+
{
26+
id: "MyClass",
27+
node: expect.objectContaining({ type: "class_declaration" }),
28+
identifierNode: expect.objectContaining({ text: "MyClass" }),
29+
type: "class",
30+
},
31+
]),
32+
);
7133
} catch (error) {
72-
console.error("Error in test MyClass.cs:", error);
34+
console.error("Error in test Namespaced.cs:", error);
7335
throw error;
7436
}
7537
});
7638

7739
test("Models.cs", () => {
7840
try {
79-
const symbols = resolver.getSymbols("project/Models.cs");
80-
expect(symbols).toMatchObject([
81-
{
82-
id: "User",
83-
identifierNode: { text: "User" },
84-
type: "class",
85-
},
86-
{
87-
id: "Order",
88-
identifierNode: { text: "Order" },
89-
type: "class",
90-
},
91-
{
92-
id: "OrderStatus",
93-
identifierNode: { text: "OrderStatus" },
94-
type: "class",
95-
},
96-
]);
41+
const symbols = resolver.getSymbols("Models.cs");
42+
expect(symbols).toEqual(
43+
expect.arrayContaining([
44+
{
45+
id: "User",
46+
node: expect.objectContaining({ type: "class_declaration" }),
47+
identifierNode: expect.objectContaining({ text: "User" }),
48+
type: "class",
49+
},
50+
{
51+
id: "Order",
52+
node: expect.objectContaining({ type: "struct_declaration" }),
53+
identifierNode: expect.objectContaining({ text: "Order" }),
54+
type: "struct",
55+
},
56+
{
57+
id: "OrderStatus",
58+
node: expect.objectContaining({ type: "enum_declaration" }),
59+
identifierNode: expect.objectContaining({ text: "OrderStatus" }),
60+
type: "enum",
61+
},
62+
]),
63+
);
9764
} catch (error) {
9865
console.error("Error in test Models.cs:", error);
9966
throw error;
10067
}
10168
});
10269

103-
test("empty.cs", () => {
70+
test("Variables.cs", () => {
71+
try {
72+
const symbols = resolver.getSymbols("Variables.cs");
73+
expect(symbols).toEqual(
74+
expect.arrayContaining([
75+
{
76+
id: "FarWest",
77+
node: expect.objectContaining({ type: "class_declaration" }),
78+
identifierNode: expect.objectContaining({ text: "FarWest" }),
79+
type: "class",
80+
},
81+
{
82+
id: "EastCoast",
83+
node: expect.objectContaining({ type: "class_declaration" }),
84+
identifierNode: expect.objectContaining({ text: "EastCoast" }),
85+
type: "class",
86+
},
87+
]),
88+
);
89+
} catch (error) {
90+
console.error("Error in test Variables.cs:", error);
91+
throw error;
92+
}
93+
});
94+
95+
test("SemiNamespaced.cs", () => {
10496
try {
105-
const symbols = resolver.getSymbols("project/empty.cs");
106-
expect(symbols).toEqual([]);
97+
const symbols = resolver.getSymbols("SemiNamespaced.cs");
98+
expect(symbols).toEqual(
99+
expect.arrayContaining([
100+
{
101+
id: "Gordon",
102+
node: expect.objectContaining({ type: "class_declaration" }),
103+
identifierNode: expect.objectContaining({ text: "Gordon" }),
104+
type: "class",
105+
},
106+
{
107+
id: "Freeman",
108+
node: expect.objectContaining({ type: "class_declaration" }),
109+
identifierNode: expect.objectContaining({ text: "Freeman" }),
110+
type: "class",
111+
},
112+
{
113+
id: "HeadCrab",
114+
node: expect.objectContaining({ type: "class_declaration" }),
115+
identifierNode: expect.objectContaining({ text: "HeadCrab" }),
116+
type: "class",
117+
},
118+
]),
119+
);
107120
} catch (error) {
108-
console.error("Error in test empty.cs:", error);
121+
console.error("Error in test SemiNamespaced.cs:", error);
109122
throw error;
110123
}
111124
});
112125

113-
test("Nested.cs", () => {
126+
test("IWillOnlyImportHalf.cs", () => {
114127
try {
115-
const symbols = resolver.getSymbols("project/Nested.cs");
116-
expect(symbols).toMatchObject([
117-
{
118-
id: "OuterClass",
119-
identifierNode: { text: "OuterClass" },
120-
type: "class",
121-
},
122-
{
123-
id: "InnerClass",
124-
identifierNode: { text: "InnerClass" },
125-
type: "class",
126-
},
127-
{
128-
id: "InnerMethod",
129-
identifierNode: { text: "InnerMethod" },
130-
type: "method",
131-
},
132-
{
133-
id: "OuterMethod",
134-
identifierNode: { text: "OuterMethod" },
135-
type: "method",
136-
},
137-
]);
128+
const symbols = resolver.getSymbols("IWillOnlyImportHalf.cs");
129+
expect(symbols).toEqual(
130+
expect.arrayContaining([
131+
{
132+
id: "Steak",
133+
node: expect.objectContaining({ type: "class_declaration" }),
134+
identifierNode: expect.objectContaining({ text: "Steak" }),
135+
type: "class",
136+
},
137+
{
138+
id: "Salad",
139+
node: expect.objectContaining({ type: "class_declaration" }),
140+
identifierNode: expect.objectContaining({ text: "Salad" }),
141+
type: "class",
142+
},
143+
]),
144+
);
138145
} catch (error) {
139-
console.error("Error in test Nested.cs:", error);
146+
console.error("Error in test IWillOnlyImportHalf.cs:", error);
140147
throw error;
141148
}
142149
});

packages/cli/src/languagePlugins2/csharp/exportResolver/index.ts

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import Parser from "tree-sitter";
22

3-
export const CSHARP_CLASS_TYPE = "class";
4-
export const CSHARP_METHOD_TYPE = "method";
5-
export const CSHARP_PROPERTY_TYPE = "property";
6-
export const CSHARP_FIELD_TYPE = "field";
7-
8-
export type SymbolType =
9-
| typeof CSHARP_CLASS_TYPE
10-
| typeof CSHARP_METHOD_TYPE
11-
| typeof CSHARP_PROPERTY_TYPE
12-
| typeof CSHARP_FIELD_TYPE;
3+
export type SymbolType = string;
134

145
export interface ExportedSymbol {
156
id: string;
@@ -34,7 +25,6 @@ export class CsharpExportResolver {
3425

3526
private getClass(fileNode: Parser.SyntaxNode) {
3627
const exportedSymbols: ExportedSymbol[] = [];
37-
console.log(fileNode);
3828
fileNode
3929
.descendantsOfType([
4030
"class_declaration",
@@ -53,66 +43,12 @@ export class CsharpExportResolver {
5343
id: identifierNode.text,
5444
node,
5545
identifierNode,
56-
type: CSHARP_CLASS_TYPE,
46+
type: node.type.split("_")[0],
5747
});
5848
});
5949
return exportedSymbols;
6050
}
6151

62-
private getMethod(fileNode: Parser.SyntaxNode) {
63-
const exportedSymbols: ExportedSymbol[] = [];
64-
fileNode.descendantsOfType("method_declaration").forEach((node) => {
65-
const identifierNode = node.childForFieldName("name");
66-
if (!identifierNode) {
67-
console.error("No identifier node found for method definition " + node);
68-
return;
69-
}
70-
exportedSymbols.push({
71-
id: identifierNode.text,
72-
node,
73-
identifierNode,
74-
type: CSHARP_METHOD_TYPE,
75-
});
76-
});
77-
return exportedSymbols;
78-
}
79-
80-
private getProperty(fileNode: Parser.SyntaxNode) {
81-
const exportedSymbols: ExportedSymbol[] = [];
82-
fileNode.descendantsOfType("property_declaration").forEach((node) => {
83-
const identifierNode = node.childForFieldName("name");
84-
if (!identifierNode) {
85-
console.error("No identifier node found for prop. definition " + node);
86-
return;
87-
}
88-
exportedSymbols.push({
89-
id: identifierNode.text,
90-
node,
91-
identifierNode,
92-
type: CSHARP_PROPERTY_TYPE,
93-
});
94-
});
95-
return exportedSymbols;
96-
}
97-
98-
private getField(fileNode: Parser.SyntaxNode) {
99-
const exportedSymbols: ExportedSymbol[] = [];
100-
fileNode.descendantsOfType("field_declaration").forEach((node) => {
101-
const identifierNode = node.childForFieldName("name");
102-
if (!identifierNode) {
103-
console.error("No identifier node found for field definition " + node);
104-
return;
105-
}
106-
exportedSymbols.push({
107-
id: identifierNode.text,
108-
node,
109-
identifierNode,
110-
type: CSHARP_FIELD_TYPE,
111-
});
112-
});
113-
return exportedSymbols;
114-
}
115-
11652
public getSymbols(filepath: string) {
11753
const cacheKey = filepath;
11854
const cachedSymbols = this.getFromExportedSymbolCache(cacheKey);
@@ -126,12 +62,7 @@ export class CsharpExportResolver {
12662
return [];
12763
}
12864

129-
const exportedSymbols: ExportedSymbol[] = [
130-
...this.getClass(file.rootNode),
131-
...this.getMethod(file.rootNode),
132-
...this.getProperty(file.rootNode),
133-
...this.getField(file.rootNode),
134-
];
65+
const exportedSymbols: ExportedSymbol[] = [...this.getClass(file.rootNode)];
13566

13667
this.exportedSymbolCache.set(cacheKey, exportedSymbols);
13768
return exportedSymbols;

0 commit comments

Comments
 (0)