Skip to content

Commit ed3ca20

Browse files
fralongoFrancesco Longo
andauthored
chore: Remove empty children from components tree representation and … (#139)
Co-authored-by: Francesco Longo <[email protected]>
1 parent c43a9f1 commit ed3ca20

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/internal/analytics-metadata/__tests__/page-scanner-utils.test.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('getComponentsTree', () => {
2323
);
2424
const target = container.querySelector('#outer-target') as HTMLElement;
2525
expect(getComponentsTree(target)).toEqual([
26-
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] },
26+
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' } },
2727
]);
2828
});
2929
test('only includes components inside the specified element', () => {
@@ -38,10 +38,10 @@ describe('getComponentsTree', () => {
3838
</>
3939
);
4040
expect(getComponentsTree(container.querySelector('#outer-target-1') as HTMLElement)).toEqual([
41-
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] },
41+
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' } },
4242
]);
4343
expect(getComponentsTree(container.querySelector('#outer-target-2') as HTMLElement)).toEqual([
44-
{ name: 'ComponentTwo', label: 'sub label', children: [] },
44+
{ name: 'ComponentTwo', label: 'sub label' },
4545
]);
4646
});
4747
test('can include multiple components', () => {
@@ -55,11 +55,11 @@ describe('getComponentsTree', () => {
5555
{
5656
name: 'ComponentThree',
5757
children: [
58-
{ name: 'ComponentTwo', label: 'sub label', children: [] },
59-
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] },
58+
{ name: 'ComponentTwo', label: 'sub label' },
59+
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' } },
6060
],
6161
},
62-
{ name: 'ComponentTwo', label: 'sub label', children: [] },
62+
{ name: 'ComponentTwo', label: 'sub label' },
6363
]);
6464
});
6565
test('can include multiple nested components', () => {
@@ -74,13 +74,13 @@ describe('getComponentsTree', () => {
7474
{
7575
name: 'ComponentThree',
7676
children: [
77-
{ name: 'ComponentTwo', label: 'sub label', children: [] },
78-
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] },
77+
{ name: 'ComponentTwo', label: 'sub label' },
78+
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' } },
7979
{
8080
name: 'ComponentThree',
8181
children: [
82-
{ name: 'ComponentTwo', label: 'sub label', children: [] },
83-
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] },
82+
{ name: 'ComponentTwo', label: 'sub label' },
83+
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' } },
8484
],
8585
},
8686
],
@@ -98,11 +98,11 @@ describe('getComponentsTree', () => {
9898
{
9999
name: 'ComponentThree',
100100
children: [
101-
{ name: 'ComponentTwo', label: 'sub label', children: [] },
102-
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] },
101+
{ name: 'ComponentTwo', label: 'sub label' },
102+
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' } },
103103
],
104104
},
105-
{ name: 'ComponentTwo', label: 'sub label', children: [] },
105+
{ name: 'ComponentTwo', label: 'sub label' },
106106
]);
107107
});
108108
test('skips malformed metadata', () => {
@@ -113,7 +113,7 @@ describe('getComponentsTree', () => {
113113
);
114114
const target = container.querySelector('#outer-target') as HTMLElement;
115115
expect(getComponentsTree(target)).toEqual([
116-
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' }, children: [] },
116+
{ name: 'ComponentOne', label: 'component label', properties: { multi: 'true' } },
117117
]);
118118
});
119119
});

src/internal/analytics-metadata/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface GeneratedAnalyticsMetadata {
1212
contexts: Array<GeneratedAnalyticsMetadataComponentContext>;
1313
}
1414

15-
export interface GeneratedAnalyticsMetadataComponent {
15+
interface GeneratedAnalyticsMetadataComponent {
1616
// name of the component. For example: "awsui.RadioGroup". We prefix the actual name with awsui to account for future tagging of custom components
1717
name: string;
1818

src/internal/analytics-metadata/page-scanner-utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
import { METADATA_ATTRIBUTE } from './attributes';
55
import { isNodeComponent } from './dom-utils';
6-
import { GeneratedAnalyticsMetadataComponent } from './interfaces';
76
import { getGeneratedAnalyticsMetadata } from './utils';
87

9-
interface GeneratedAnalyticsMetadataComponentTree extends GeneratedAnalyticsMetadataComponent {
8+
interface GeneratedAnalyticsMetadataComponentTree {
9+
name: string;
10+
label: string;
11+
properties?: Record<string, string>;
1012
children?: Array<GeneratedAnalyticsMetadataComponentTree>;
1113
}
1214

@@ -26,10 +28,14 @@ const getComponentsTreeRecursive = (
2628
return;
2729
}
2830
visited.add(componentNode);
29-
tree.push({
31+
const treeItem: GeneratedAnalyticsMetadataComponentTree = {
3032
...getGeneratedAnalyticsMetadata(componentNode).contexts[0].detail,
31-
children: getComponentsTreeRecursive(componentNode, visited),
32-
});
33+
};
34+
const children = getComponentsTreeRecursive(componentNode, visited);
35+
if (children.length > 0) {
36+
treeItem.children = children;
37+
}
38+
tree.push(treeItem);
3339
});
3440
return tree;
3541
};

0 commit comments

Comments
 (0)