Skip to content

Commit

Permalink
fix(sfc): avoid auto name inference leading to unwanted recursion
Browse files Browse the repository at this point in the history
fix #5965
fix #6027
fix #6029
  • Loading branch information
yyx990803 committed Jun 6, 2022
1 parent 11e17a1 commit 9734b31
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`SFC analyze <script> bindings auto name inference basic 1`] = `
"export default {
name: 'FooBar',
__name: 'FooBar',
setup(__props, { expose }) {
expose();
const a = 1
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ describe('SFC analyze <script> bindings', () => {
}
)
expect(content).toMatch(`export default {
name: 'FooBar'`)
__name: 'FooBar'`)
assertCode(content)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ export function compileScript(
if (!hasDefaultExportName && filename && filename !== DEFAULT_FILENAME) {
const match = filename.match(/([^/\\]+)\.\w+$/)
if (match) {
runtimeOptions += `\n name: '${match[1]}',`
runtimeOptions += `\n __name: '${match[1]}',`
}
}
if (hasInlinedSsrRenderFn) {
Expand Down
11 changes: 8 additions & 3 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export interface ComponentInternalOptions {
* This one should be exposed so that devtools can make use of it
*/
__file?: string
/**
* name inferred from filename
*/
__name?: string
}

export interface FunctionalComponent<P = {}, E extends EmitsOptions = {}>
Expand Down Expand Up @@ -949,11 +953,12 @@ const classify = (str: string): string =>
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')

export function getComponentName(
Component: ConcreteComponent
): string | undefined {
Component: ConcreteComponent,
includeInferred = true
): string | false | undefined {
return isFunction(Component)
? Component.displayName || Component.name
: Component.name
: Component.name || (includeInferred && Component.__name)
}

/* istanbul ignore next */
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-core/src/helpers/resolveAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ function resolveAsset(

// explicit self name has highest priority
if (type === COMPONENTS) {
const selfName = getComponentName(Component)
const selfName = getComponentName(
Component,
false /* do not include inferred name to avoid breaking existing code */
)
if (
selfName &&
(selfName === name ||
Expand Down

0 comments on commit 9734b31

Please sign in to comment.