Skip to content

Commit

Permalink
fix(vue): Correctly obtain component name (#13484)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
filips123 authored Aug 28, 2024
1 parent a428f85 commit b192678
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/vue/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type ViewModel = {
propsData?: { [key: string]: any };
_componentTag?: string;
__file?: string;
__name?: string;
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/vendor/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const formatComponentName = (vm?: ViewModel, includeFile?: boolean): stri

const options = vm.$options;

let name = options.name || options._componentTag;
let name = options.name || options._componentTag || options.__name;
const file = options.__file;
if (!name && file) {
const match = file.match(/([^/\\]+)\.vue$/);
Expand Down
13 changes: 13 additions & 0 deletions packages/vue/test/vendor/components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ describe('formatComponentName', () => {
});
});

describe('when the options have a __name', () => {
it('returns the __name', () => {
// arrange
vm.$options.__name = 'my-component-name';

// act
const formattedName = formatComponentName(vm);

// assert
expect(formattedName).toEqual('<MyComponentName>');
});
});

describe('when the options have a __file', () => {
describe('and we do not wish to include the filename', () => {
it.each([
Expand Down

0 comments on commit b192678

Please sign in to comment.