Skip to content

Commit 211da04

Browse files
author
Ulrike Kiesel
committed
feat(analysis): add TSX language support to UnifiedParser
1 parent 845a53c commit 211da04

10 files changed

Lines changed: 542 additions & 2 deletions

File tree

analysis/analysers/parsers/UnifiedParser/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The Unified Parser is parser to generate code metrics from a source code file or
1010
|--------------|----------------------------------------|
1111
| Javascript | .js, .cjs, .mjs |
1212
| Typescript | .ts, .cts, .mts |
13+
| TSX | .tsx |
1314
| Java | .java |
1415
| Kotlin | .kt |
1516
| C# | .cs |
@@ -22,6 +23,7 @@ The Unified Parser is parser to generate code metrics from a source code file or
2223
| Ruby | .rb |
2324
| Swift | .swift |
2425
| Bash | .sh |
26+
| Vue | .vue |
2527

2628
## Supported Metrics
2729

analysis/analysers/parsers/UnifiedParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/unified/metriccollectors/AvailableCollectors.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import de.maibornwolff.treesitter.excavationsite.api.Language
55

66
enum class AvailableCollectors(val fileExtension: FileExtension, val collectorFactory: () -> TreeSitterLibraryCollector) {
77
TYPESCRIPT(FileExtension.TYPESCRIPT, { TreeSitterLibraryCollector(Language.TYPESCRIPT) }),
8+
TSX(FileExtension.TSX, { TreeSitterLibraryCollector(Language.TSX) }),
89
JAVASCRIPT(FileExtension.JAVASCRIPT, { TreeSitterLibraryCollector(Language.JAVASCRIPT) }),
910
KOTLIN(FileExtension.KOTLIN, { TreeSitterLibraryCollector(Language.KOTLIN) }),
1011
OBJECTIVE_C(FileExtension.OBJECTIVE_C, { TreeSitterLibraryCollector(Language.OBJECTIVE_C) }),

analysis/analysers/parsers/UnifiedParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/unified/metriccollectors/TreeSitterAdapter.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ object TreeSitterAdapter {
5151
FileExtension.JAVA -> Language.JAVA
5252
FileExtension.KOTLIN -> Language.KOTLIN
5353
FileExtension.TYPESCRIPT -> Language.TYPESCRIPT
54+
FileExtension.TSX -> Language.TSX
5455
FileExtension.JAVASCRIPT -> Language.JAVASCRIPT
5556
FileExtension.PYTHON -> Language.PYTHON
5657
FileExtension.GO -> Language.GO

analysis/analysers/parsers/UnifiedParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/unified/UnifiedParserTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class UnifiedParserTest {
4343
Arguments.of("ruby", ".rb"),
4444
Arguments.of("swift", ".swift"),
4545
Arguments.of("typescript", ".ts"),
46+
Arguments.of("tsx", ".tsx"),
4647
Arguments.of("vue", ".vue")
4748
)
4849
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package de.maibornwolff.codecharta.analysers.parsers.unified.metriccollectors
2+
3+
import de.maibornwolff.treesitter.excavationsite.api.AvailableFileMetrics
4+
import de.maibornwolff.treesitter.excavationsite.api.Language
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.junit.jupiter.api.Test
7+
import java.io.File
8+
9+
class TsxCollectorTest {
10+
private val collector = TreeSitterLibraryCollector(Language.TSX)
11+
12+
private fun createTestFile(content: String): File {
13+
val tempFile = File.createTempFile("testFile", ".txt")
14+
tempFile.writeText(content)
15+
tempFile.deleteOnExit()
16+
return tempFile
17+
}
18+
19+
@Test
20+
fun `should parse a minimal tsx component and return non-zero loc`() {
21+
// Arrange
22+
val fileContent = """
23+
function Hello() {
24+
return <div>Hello World</div>;
25+
}
26+
""".trimIndent()
27+
val input = createTestFile(fileContent)
28+
29+
// Act
30+
val result = collector.collectMetricsForFile(input)
31+
32+
// Assert
33+
assertThat(result.attributes[AvailableFileMetrics.LINES_OF_CODE.metricName] as Double).isGreaterThan(0.0)
34+
}
35+
}
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
{
2+
"data": {
3+
"projectName": "",
4+
"nodes": [
5+
{
6+
"name": "root",
7+
"type": "Folder",
8+
"attributes": {},
9+
"link": "",
10+
"children": [
11+
{
12+
"name": "tsxSample.tsx",
13+
"type": "File",
14+
"attributes": {
15+
"complexity": 23.0,
16+
"logic_complexity": 16.0,
17+
"comment_lines": 25.0,
18+
"number_of_functions": 3.0,
19+
"message_chains": 0.0,
20+
"rloc": 69.0,
21+
"loc": 109.0,
22+
"long_method": 2.0,
23+
"long_parameter_list": 0.0,
24+
"excessive_comments": 1.0,
25+
"comment_ratio": 0.36,
26+
"max_parameters_per_function": 2.0,
27+
"min_parameters_per_function": 1.0,
28+
"mean_parameters_per_function": 1.33,
29+
"median_parameters_per_function": 1.0,
30+
"max_complexity_per_function": 9.0,
31+
"min_complexity_per_function": 3.0,
32+
"mean_complexity_per_function": 6.67,
33+
"median_complexity_per_function": 8.0,
34+
"max_rloc_per_function": 18.0,
35+
"min_rloc_per_function": 8.0,
36+
"mean_rloc_per_function": 14.67,
37+
"median_rloc_per_function": 18.0
38+
},
39+
"link": "",
40+
"children": [],
41+
"checksum": "63beee9616698ee1"
42+
}
43+
]
44+
}
45+
],
46+
"apiVersion": "1.5",
47+
"edges": [],
48+
"attributeTypes": {},
49+
"attributeDescriptors": {
50+
"complexity": {
51+
"title": "Complexity",
52+
"description": "Complexity of the file representing how much cognitive load is needed to overview the whole file",
53+
"hintLowValue": "",
54+
"hintHighValue": "",
55+
"link": "https://codecharta.com/docs/parser/unified",
56+
"direction": -1,
57+
"analyzers": [
58+
"unifiedParser"
59+
]
60+
},
61+
"logic_complexity": {
62+
"title": "Logic complexity",
63+
"description": "Complexity of the file based on number of paths through the code, similar to cyclomatic complexity",
64+
"hintLowValue": "",
65+
"hintHighValue": "",
66+
"link": "https://en.wikipedia.org/wiki/Cyclomatic_complexity",
67+
"direction": -1,
68+
"analyzers": [
69+
"unifiedParser"
70+
]
71+
},
72+
"comment_lines": {
73+
"title": "Comment lines",
74+
"description": "Number of lines containing either a comment or commented-out code",
75+
"hintLowValue": "",
76+
"hintHighValue": "",
77+
"link": "https://codecharta.com/docs/parser/unified",
78+
"direction": -1,
79+
"analyzers": [
80+
"unifiedParser"
81+
]
82+
},
83+
"loc": {
84+
"title": "Lines of Code",
85+
"description": "Lines of code including empty lines and comments",
86+
"hintLowValue": "",
87+
"hintHighValue": "",
88+
"link": "https://codecharta.com/docs/parser/unified",
89+
"direction": -1,
90+
"analyzers": [
91+
"unifiedParser"
92+
]
93+
},
94+
"rloc": {
95+
"title": "Real Lines of Code",
96+
"description": "Number of lines that contain at least one character which is neither a whitespace nor a tabulation nor part of a comment",
97+
"hintLowValue": "",
98+
"hintHighValue": "",
99+
"link": "https://codecharta.com/docs/parser/unified",
100+
"direction": -1,
101+
"analyzers": [
102+
"unifiedParser"
103+
]
104+
},
105+
"number_of_functions": {
106+
"title": "Number of functions",
107+
"description": "The number of functions or methods present in the file. Does not include anonymous or lambda functions.",
108+
"hintLowValue": "",
109+
"hintHighValue": "",
110+
"link": "https://codecharta.com/docs/parser/unified",
111+
"direction": -1,
112+
"analyzers": [
113+
"unifiedParser"
114+
]
115+
},
116+
"max_parameters_per_function": {
117+
"title": "Maximum parameters per function",
118+
"description": "The maximum number of parameters a function or method has for this file.",
119+
"hintLowValue": "",
120+
"hintHighValue": "",
121+
"link": "https://codecharta.com/docs/parser/unified",
122+
"direction": -1,
123+
"analyzers": [
124+
"unifiedParser"
125+
]
126+
},
127+
"min_parameters_per_function": {
128+
"title": "Minimum parameters per function",
129+
"description": "The minimum number of parameters a function or method has for this file.",
130+
"hintLowValue": "",
131+
"hintHighValue": "",
132+
"link": "https://codecharta.com/docs/parser/unified",
133+
"direction": -1,
134+
"analyzers": [
135+
"unifiedParser"
136+
]
137+
},
138+
"median_parameters_per_function": {
139+
"title": "Median parameters per function",
140+
"description": "The median number of parameters a function or method has for this file.",
141+
"hintLowValue": "",
142+
"hintHighValue": "",
143+
"link": "https://codecharta.com/docs/parser/unified",
144+
"direction": -1,
145+
"analyzers": [
146+
"unifiedParser"
147+
]
148+
},
149+
"mean_parameters_per_function": {
150+
"title": "Mean parameters per function",
151+
"description": "The mean number of parameters a function or method has for this file.",
152+
"hintLowValue": "",
153+
"hintHighValue": "",
154+
"link": "https://codecharta.com/docs/parser/unified",
155+
"direction": -1,
156+
"analyzers": [
157+
"unifiedParser"
158+
]
159+
},
160+
"max_complexity_per_function": {
161+
"title": "Maximum complexity per function",
162+
"description": "The maximum complexity in the body of a function of this file.",
163+
"hintLowValue": "",
164+
"hintHighValue": "",
165+
"link": "https://codecharta.com/docs/parser/unified",
166+
"direction": -1,
167+
"analyzers": [
168+
"unifiedParser"
169+
]
170+
},
171+
"min_complexity_per_function": {
172+
"title": "Minimum complexity per function",
173+
"description": "The minimum number of complexity in the body of a function of this file.",
174+
"hintLowValue": "",
175+
"hintHighValue": "",
176+
"link": "https://codecharta.com/docs/parser/unified",
177+
"direction": -1,
178+
"analyzers": [
179+
"unifiedParser"
180+
]
181+
},
182+
"mean_complexity_per_function": {
183+
"title": "Mean complexity per function",
184+
"description": "The mean complexity found in the body of a function of this file.",
185+
"hintLowValue": "",
186+
"hintHighValue": "",
187+
"link": "",
188+
"direction": -1,
189+
"analyzers": []
190+
},
191+
"median_complexity_per_function": {
192+
"title": "Median complexity per function",
193+
"description": "The median complexity found in the body of a function of this file.",
194+
"hintLowValue": "",
195+
"hintHighValue": "",
196+
"link": "https://codecharta.com/docs/parser/unified",
197+
"direction": -1,
198+
"analyzers": [
199+
"unifiedParser"
200+
]
201+
},
202+
"max_rloc_per_function": {
203+
"title": "Maximum real lines of code in a function",
204+
"description": "The maximum number of real lines of code in a function of this file.",
205+
"hintLowValue": "",
206+
"hintHighValue": "",
207+
"link": "https://codecharta.com/docs/parser/unified",
208+
"direction": -1,
209+
"analyzers": [
210+
"unifiedParser"
211+
]
212+
},
213+
"min_rloc_per_function": {
214+
"title": "Minimum real lines of code in a function",
215+
"description": "The minimum number of real lines of code in a function of this file.",
216+
"hintLowValue": "",
217+
"hintHighValue": "",
218+
"link": "https://codecharta.com/docs/parser/unified",
219+
"direction": -1,
220+
"analyzers": [
221+
"unifiedParser"
222+
]
223+
},
224+
"mean_rloc_per_function": {
225+
"title": "Mean real lines of code in a function",
226+
"description": "The mean number of real lines of code in a function of this file.",
227+
"hintLowValue": "",
228+
"hintHighValue": "",
229+
"link": "https://codecharta.com/docs/parser/unified",
230+
"direction": -1,
231+
"analyzers": [
232+
"unifiedParser"
233+
]
234+
},
235+
"median_rloc_per_function": {
236+
"title": "Median real lines of code in a function",
237+
"description": "The median number of real lines of code in a function of this file.",
238+
"hintLowValue": "",
239+
"hintHighValue": "",
240+
"link": "https://codecharta.com/docs/parser/unified",
241+
"direction": -1,
242+
"analyzers": [
243+
"unifiedParser"
244+
]
245+
},
246+
"long_method": {
247+
"title": "Long Method",
248+
"description": "Code smell showing the number of functions with more than 10 real lines of code (rloc)",
249+
"hintLowValue": "",
250+
"hintHighValue": "",
251+
"link": "https://codecharta.com/docs/parser/unified",
252+
"direction": -1,
253+
"analyzers": [
254+
"unifiedParser"
255+
]
256+
},
257+
"long_parameter_list": {
258+
"title": "Long Parameter List",
259+
"description": "Code smell showing the number of functions with more than 4 parameters",
260+
"hintLowValue": "",
261+
"hintHighValue": "",
262+
"link": "https://codecharta.com/docs/parser/unified",
263+
"direction": -1,
264+
"analyzers": [
265+
"unifiedParser"
266+
]
267+
},
268+
"excessive_comments": {
269+
"title": "Excessive Comments",
270+
"description": "Code smell showing whether a file has more than 10 comment lines",
271+
"hintLowValue": "",
272+
"hintHighValue": "",
273+
"link": "https://codecharta.com/docs/parser/unified",
274+
"direction": -1,
275+
"analyzers": [
276+
"unifiedParser"
277+
]
278+
},
279+
"comment_ratio": {
280+
"title": "Comment Ratio",
281+
"description": "The ratio of comment lines to real lines of code (rloc)",
282+
"hintLowValue": "",
283+
"hintHighValue": "",
284+
"link": "https://codecharta.com/docs/parser/unified",
285+
"direction": 0,
286+
"analyzers": [
287+
"unifiedParser"
288+
]
289+
},
290+
"message_chains": {
291+
"title": "Message Chains",
292+
"description": "Code smell showing occurrences of method call chains with 4 or more consecutive calls suggesting tight coupling",
293+
"hintLowValue": "",
294+
"hintHighValue": "",
295+
"link": "https://codecharta.com/docs/parser/unified",
296+
"direction": -1,
297+
"analyzers": [
298+
"unifiedParser"
299+
]
300+
}
301+
},
302+
"blacklist": []
303+
},
304+
"checksum": "ecc4f379b952234dc24619bedda6a120"
305+
}

0 commit comments

Comments
 (0)