Skip to content

Commit

Permalink
fix(utils): collapse prefixes in PascalCase name (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynzh authored May 1, 2024
1 parent aeaf51d commit 027128c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export default {
name: 'KebabCaseFile',
}
</script>

<template>
<h3>KebabCaseFile Component: <code>kebab-case/KebabCaseFile.vue</code></h3>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export default {
name: 'KebabCaseCollapseFile',
}
</script>

<template>
<h3>KebabCaseCollapseFile Component: <code>kebab-case/kebab-case-collapse/KebabCaseCollapseFile.vue</code></h3>
</template>
9 changes: 5 additions & 4 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ export function getNameFromFilePath(filePath: string, options: ResolvedOptions):
for (const fileOrFolderName of namespaced) {
let cumulativePrefix = ''
let didCollapse = false
const pascalCasedName = pascalCase(fileOrFolderName)

for (const parentFolder of [...collapsed].reverse()) {
cumulativePrefix = `${capitalize(parentFolder)}${cumulativePrefix}`
cumulativePrefix = `${parentFolder}${cumulativePrefix}`

if (pascalCase(fileOrFolderName).startsWith(pascalCase(cumulativePrefix))) {
const collapseSamePrefix = fileOrFolderName.slice(cumulativePrefix.length)
if (pascalCasedName.startsWith(cumulativePrefix)) {
const collapseSamePrefix = pascalCasedName.slice(cumulativePrefix.length)

collapsed.push(collapseSamePrefix)

Expand All @@ -174,7 +175,7 @@ export function getNameFromFilePath(filePath: string, options: ResolvedOptions):
}

if (!didCollapse)
collapsed.push(fileOrFolderName)
collapsed.push(pascalCasedName)
}

namespaced = collapsed
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/search.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ exports[`search > should with namespace & collapse 1`] = `
"as": "ComponentD",
"from": "src/components/ComponentD.vue",
},
{
"as": "KebabCaseCollapseFile",
"from": "src/components/kebab-case/kebab-case-collapse/KebabCaseCollapseFile.vue",
},
{
"as": "KebabCaseFile",
"from": "src/components/kebab-case/KebabCaseFile.vue",
},
{
"as": "Recursive",
"from": "src/components/Recursive.vue",
Expand Down Expand Up @@ -91,6 +99,14 @@ exports[`search > should with namespace 1`] = `
"as": "ComponentD",
"from": "src/components/ComponentD.vue",
},
{
"as": "KebabCaseKebabCaseCollapseKebabCaseCollapseFile",
"from": "src/components/kebab-case/kebab-case-collapse/KebabCaseCollapseFile.vue",
},
{
"as": "KebabCaseKebabCaseFile",
"from": "src/components/kebab-case/KebabCaseFile.vue",
},
{
"as": "Recursive",
"from": "src/components/Recursive.vue",
Expand Down Expand Up @@ -152,6 +168,14 @@ exports[`search > should work 1`] = `
"as": "FolderAndComponentPartially",
"from": "src/components/collapse/collapseFolder/FolderAndComponentPartially.vue",
},
{
"as": "KebabCaseCollapseFile",
"from": "src/components/kebab-case/kebab-case-collapse/KebabCaseCollapseFile.vue",
},
{
"as": "KebabCaseFile",
"from": "src/components/kebab-case/KebabCaseFile.vue",
},
{
"as": "Recursive",
"from": "src/components/Recursive.vue",
Expand Down

0 comments on commit 027128c

Please sign in to comment.