Skip to content

Commit b192678

Browse files
authored
fix(vue): Correctly obtain component name (#13484)
In some cases, Vue components do not have `options.name` defined, but instead have `options.__name`. Such components will be displayed as anonymous in Sentry and currently won't be matched in `trackComponents`. The same fix was also done in Vue devtools (vuejs/devtools#2020). In my case, the problem were components from my own project, but this change also fixes that.
1 parent a428f85 commit b192678

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

packages/vue/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type ViewModel = {
2222
propsData?: { [key: string]: any };
2323
_componentTag?: string;
2424
__file?: string;
25+
__name?: string;
2526
};
2627
};
2728

packages/vue/src/vendor/components.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const formatComponentName = (vm?: ViewModel, includeFile?: boolean): stri
5151

5252
const options = vm.$options;
5353

54-
let name = options.name || options._componentTag;
54+
let name = options.name || options._componentTag || options.__name;
5555
const file = options.__file;
5656
if (!name && file) {
5757
const match = file.match(/([^/\\]+)\.vue$/);

packages/vue/test/vendor/components.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ describe('formatComponentName', () => {
8080
});
8181
});
8282

83+
describe('when the options have a __name', () => {
84+
it('returns the __name', () => {
85+
// arrange
86+
vm.$options.__name = 'my-component-name';
87+
88+
// act
89+
const formattedName = formatComponentName(vm);
90+
91+
// assert
92+
expect(formattedName).toEqual('<MyComponentName>');
93+
});
94+
});
95+
8396
describe('when the options have a __file', () => {
8497
describe('and we do not wish to include the filename', () => {
8598
it.each([

0 commit comments

Comments
 (0)