Skip to content
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

fix(utils): collapse prefixes in PascalCase name #744

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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