-
Notifications
You must be signed in to change notification settings - Fork 47
Add TSX support to UnifiedParser #4466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62a9021
chore(deps): update dependency TreesitterExcavationSite to v0.4.1
c8fbd92
feat(analysis): add TSX language support to UnifiedParser
9b3c4f2
fix(analysis): replace fixed sleep with polling in ProjectInputReader
812c88d
fix(analysis): resolve SonarCloud quality gate failures for TSX support
627034e
fix(analysis): complete mean_complexity_per_function descriptor in ts…
0769f32
fix(analysis): add missing link and analyzers to mean_complexity_per_…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
...de/maibornwolff/codecharta/analysers/parsers/unified/metriccollectors/TsxCollectorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| package de.maibornwolff.codecharta.analysers.parsers.unified.metriccollectors | ||
|
|
||
| import de.maibornwolff.treesitter.excavationsite.api.AvailableFileMetrics | ||
| import de.maibornwolff.treesitter.excavationsite.api.Language | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.Test | ||
| import java.io.File | ||
|
|
||
| class TsxCollectorTest { | ||
| private val collector = TreeSitterLibraryCollector(Language.TSX) | ||
|
|
||
| private fun createTestFile(content: String): File { | ||
| val tempFile = File.createTempFile("testFile", ".tsx") | ||
| tempFile.writeText(content) | ||
| tempFile.deleteOnExit() | ||
| return tempFile | ||
| } | ||
|
|
||
| @Test | ||
| fun `should parse a minimal tsx component and return non-zero loc`() { | ||
| // Arrange | ||
| val fileContent = """ | ||
| function Hello() { | ||
| return <div>Hello World</div>; | ||
| } | ||
| """.trimIndent() | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.LINES_OF_CODE.metricName] as Double).isGreaterThan(0.0) | ||
| assertThat(result.attributes[AvailableFileMetrics.NUMBER_OF_FUNCTIONS.metricName] as Double).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should count ternary operator inside JSX expression for complexity`() { | ||
| // Arrange | ||
| val fileContent = """const element = (<h1>{x < 10 ? "Banana" : "Apple"}</h1>);""" | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.COMPLEXITY.metricName]).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should count logical AND short-circuit in JSX expression for complexity`() { | ||
| // Arrange | ||
| val fileContent = """const element = (<div>{isLoggedIn && <Dashboard />}</div>);""" | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.COMPLEXITY.metricName]).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should not count inline arrow function in JSX prop as a function`() { | ||
| // Arrange | ||
| val fileContent = """ | ||
| function App() { | ||
| return (<Button onClick={() => doStuff()} />); | ||
| } | ||
| """.trimIndent() | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.NUMBER_OF_FUNCTIONS.metricName]).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should count arrow function component assigned to const as a function`() { | ||
| // Arrange | ||
| val fileContent = """ | ||
| const Fruit = () => ( | ||
| <div>Hello</div> | ||
| ); | ||
| """.trimIndent() | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.NUMBER_OF_FUNCTIONS.metricName]).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should count logical OR short-circuit in JSX expression for complexity`() { | ||
| // Arrange | ||
| val fileContent = """const element = (<div>{error || <Content />}</div>);""" | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.COMPLEXITY.metricName]).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should accumulate complexity across multiple JSX conditional expressions`() { | ||
| // Arrange | ||
| val fileContent = """ | ||
| function Card({ isAdmin, isActive, role }) { | ||
| return ( | ||
| <div> | ||
| {isAdmin && <AdminBadge />} | ||
| {isActive ? <ActiveIcon /> : <InactiveIcon />} | ||
| {role === "guest" || <Content />} | ||
| </div> | ||
| ); | ||
| } | ||
| """.trimIndent() | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| // 1 (function) + 1 (&&) + 1 (ternary) + 1 (||) = 4 | ||
| assertThat(result.attributes[AvailableFileMetrics.COMPLEXITY.metricName]).isEqualTo(4.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should not count map callback returning JSX as a function`() { | ||
| // Arrange | ||
| val fileContent = """ | ||
| function List({ items }) { | ||
| return (<ul>{items.map((item) => <li key={item.id}>{item.name}</li>)}</ul>); | ||
| } | ||
| """.trimIndent() | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.NUMBER_OF_FUNCTIONS.metricName]).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should count nullish coalescing operator in JSX expression for complexity`() { | ||
| // Arrange | ||
| val fileContent = """const element = (<div>{value ?? <Fallback />}</div>);""" | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.COMPLEXITY.metricName]).isEqualTo(1.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should count JSX block comment in comment lines`() { | ||
| // Arrange | ||
| val fileContent = """ | ||
| function App() { | ||
| return ( | ||
| <div> | ||
| {/* this is a JSX comment */} | ||
| <p>Hello</p> | ||
| </div> | ||
| ); | ||
| } | ||
| """.trimIndent() | ||
| val input = createTestFile(fileContent) | ||
|
|
||
| // Act | ||
| val result = collector.collectMetricsForFile(input) | ||
|
|
||
| // Assert | ||
| assertThat(result.attributes[AvailableFileMetrics.COMMENT_LINES.metricName]).isEqualTo(1.0) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.