Skip to content

Commit

Permalink
feat: Svelte support (sourcerer-io#524)
Browse files Browse the repository at this point in the history
* feat(Extractor): Svelte support sourcerer-io#523

* feat(Extractor): Adding tests for Svelte
support sourcerer-io#523
  • Loading branch information
edgarberlinck authored and yaronskaya committed Jul 19, 2019
1 parent f3af264 commit f0193a6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/app/extractors/Heuristics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,9 @@ val HeuristicsMap = mapOf<String, (String) -> ExtractorInterface?>(
"vue" to { _ ->
JavascriptExtractor()
},
"svelte" to { _ ->
JavascriptExtractor()
},
"vw" to { _ ->
CommonExtractor(Lang.PLSQL)
},
Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/app/extractors/JavascriptExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class JavascriptExtractor : ExtractorInterface {

override fun extractLibStats(files: List<DiffFile>): List<CommitStats> {
val vueExtension = ".vue"
val svelteExtension = ".svelte"
val vueFiles = files.filter { it.path.endsWith(vueExtension) }
val otherFiles = files.filter { !it.path.endsWith(vueExtension) }
val svelteFiles = files.filter { it.path.endsWith(svelteExtension) }
val otherFiles = files.filter { !it.path.endsWith(vueExtension) && !it.path.endsWith(svelteExtension) }

// Add stats from *.vue files.
val vueStats = listOf(CommitStats(
Expand All @@ -36,7 +38,16 @@ class JavascriptExtractor : ExtractorInterface {
type = ExtractorInterface.TYPE_LIBRARY,
tech = "js.vue"
)).filter { it.numLinesAdded > 0 || it.numLinesDeleted > 0 }
return vueStats + super.extractLibStats(otherFiles)

// Add stats from *.svelte files.
val svelteStats = listOf(CommitStats(
numLinesAdded = svelteFiles.map { it.getAllAdded().size }.sum(),
numLinesDeleted = svelteFiles.map { it.getAllDeleted().size }.sum(),
type = ExtractorInterface.TYPE_LIBRARY,
tech = "js.svelte"
)).filter { it.numLinesAdded > 0 || it.numLinesDeleted > 0 }

return vueStats + svelteStats + super.extractLibStats(otherFiles)
}

override fun tokenize(line: String): List<String> {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/app/extractors/Languages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ object Lang {
const val VHDL = "vhdl" // VHDL
const val VIML = "viml" // Vim L
const val VUE = "vue" // Vue
const val SVELTE = "svelte" // Svelte
const val WOLFRAM = "wolframlanguage" // Wolfram Language
const val XML = "xml" // XML
const val XPM = "xpm" // XPM
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/data/libraries/js_libraries.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
react
d3
vue
svelte
angular
jquery
meteor
Expand Down
44 changes: 44 additions & 0 deletions src/test/kotlin/test/tests/hashers/CommitHasherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,50 @@ class CommitHasherTest : Spek({
}

}
given("commits with svelte files") {
val lines = listOf("line 1", "line 2")

val author = Author(userName, userEmail)

val testRepoPath = "../testrepo-extractor-"
val testRepo = TestRepo(testRepoPath + "svelte")

val mockApi = MockApi(mockRepo = repo)
val observable = CommitCrawler.getObservable(testRepo.git, repo)

it("sends stats") {
for (i in 0..lines.size - 1) {
val line = lines[i]
val fileName = "file$i.svelte"
testRepo.createFile(fileName, listOf(line))
testRepo.commit(message = "$line in $fileName", author = author)
}

val errors = mutableListOf<Throwable>()

val rehashes = (0..lines.size - 1).map { "r$it" }

CommitHasher(repo, mockApi, rehashes, emails)
.updateFromObservable(observable, { e -> errors.add(e) })

assertEquals(0, errors.size)

val syntaxStats = mockApi.receivedAddedCommits
.fold(mutableListOf<CommitStats>()) { allStats, commit ->
allStats.addAll(commit.stats)
allStats
}.filter { it.type == ExtractorInterface.TYPE_LIBRARY }

val svelteStats = syntaxStats.filter { it.tech == "js.svelte" }
assertEquals(2, svelteStats.size)
assertEquals(2, svelteStats.map { it.numLinesAdded }.sum())
assertEquals(0, svelteStats.map { it.numLinesDeleted }.sum())
}

afterGroup {
testRepo.destroy()
}
}

given("commits with scss stats") {

Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/samples/JavaScript/basic.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import { onMount } from 'svelte';
export let param;
onMount(() => console.log('App ready ' + param));
</script>

<style>
.my_div {
background-color: red;
}
</style>

<div class='my_div'>
</div>

0 comments on commit f0193a6

Please sign in to comment.