Skip to content

Commit 0dc32a0

Browse files
authored
fix: Read the declared class name instead of imported (#90)
1 parent f1d4104 commit 0dc32a0

File tree

8 files changed

+172
-13
lines changed

8 files changed

+172
-13
lines changed

fixtures/test-utils/exports/alert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { ElementWrapper } from './core';
55

6-
export class AlertWrapper extends ElementWrapper {
6+
export default class AlertWrapper extends ElementWrapper {
77
findContent() {
88
return new ElementWrapper();
99
}

fixtures/test-utils/exports/dropdown.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { ElementWrapper } from './core';
5+
import OptionWrapper from './internal/option';
56

67
export class DropdownWrapper extends ElementWrapper {
7-
findItems(): Array<ElementWrapper> {
8+
findItems(): Array<OptionWrapper> {
89
return [];
910
}
1011

12+
findHighlightedOption() {
13+
return new OptionWrapper();
14+
}
15+
1116
// test for circular dependency
1217
findItemGroup() {
1318
return new DropdownWrapper();
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
export { AlertWrapper } from './alert';
3+
import AlertWrapper from './alert';
4+
export { AlertWrapper };
45
export { ButtonWrapper } from './button';
56
export { CardsWrapper } from './cards';
67
export { DropdownWrapper } from './dropdown';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { ElementWrapper } from '../core';
4+
5+
export default class OptionWrapper extends ElementWrapper {
6+
findLabel(): ElementWrapper | null {
7+
return new ElementWrapper();
8+
}
9+
}

src/components/type-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,5 @@ export function extractTypeArguments(type: ts.Type, checker: ts.TypeChecker) {
132132
if (!symbol) {
133133
throw new Error(`Unknown generic type without symbol: ${stringifyType(type, checker)}`);
134134
}
135-
return { typeParameters, typeName: symbol.getName() };
135+
return { typeParameters, typeName: checker.symbolToString(symbol) };
136136
}

src/test-utils-new/extractor.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function documentClass(
6666
if (!classType.isClass()) {
6767
throw new Error(`Exported symbol is not a class, got ${checker.symbolToString(symbol)}`);
6868
}
69-
const className = symbol.getName();
69+
const className = checker.symbolToString(symbol);
7070
const definition: TestUtilsDoc = { name: className, methods: [] };
7171
definitions.set(className, definition);
7272

@@ -91,7 +91,7 @@ function documentClass(
9191
const returnType =
9292
maybeReturnType.flags & ts.TypeFlags.Void ? maybeReturnType : maybeReturnType.getNonNullableType();
9393
const dependency = findDependencyType(returnType, checker);
94-
if (dependency && !definitions.has(dependency.symbol.getName())) {
94+
if (dependency && !definitions.has(dependency.typeName)) {
9595
documentClass(definitions, dependency.symbol, dependency.type, checker);
9696
}
9797

@@ -124,13 +124,16 @@ function documentClass(
124124
definition.methods.sort((a, b) => a.name.localeCompare(b.name));
125125
}
126126

127-
function findDependencyType(type: ts.Type, checker: ts.TypeChecker): { type: ts.Type; symbol: ts.Symbol } | undefined {
127+
function findDependencyType(
128+
type: ts.Type,
129+
checker: ts.TypeChecker
130+
): { typeName: string; type: ts.Type; symbol: ts.Symbol } | undefined {
128131
const symbol = type.getSymbol();
129132
if (!symbol) {
130133
return;
131134
}
132135

133-
const typeName = symbol.getName();
136+
const typeName = checker.symbolToString(symbol);
134137
if (typeName === 'Array') {
135138
const itemType = checker.getTypeArguments(type as ts.TypeReference)[0];
136139
return findDependencyType(itemType, checker);
@@ -144,6 +147,7 @@ function findDependencyType(type: ts.Type, checker: ts.TypeChecker): { type: ts.
144147
}
145148

146149
return {
150+
typeName,
147151
type,
148152
symbol,
149153
};

test/test-utils/__snapshots__/doc-generation.test.ts.snap

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,140 @@ exports[`Generate documentation > deal with more complex types 1`] = `
107107
},
108108
]
109109
`;
110+
111+
exports[`Generate documentation > deal with re-exports > alert wrapper methods 1`] = `
112+
[
113+
{
114+
"description": undefined,
115+
"inheritedFrom": undefined,
116+
"name": "findContent",
117+
"parameters": [],
118+
"returnType": {
119+
"isNullable": false,
120+
"name": "ElementWrapper",
121+
"typeArguments": undefined,
122+
},
123+
},
124+
]
125+
`;
126+
127+
exports[`Generate documentation > deal with re-exports > button wrapper methods 1`] = `
128+
[
129+
{
130+
"description": undefined,
131+
"inheritedFrom": undefined,
132+
"name": "findText",
133+
"parameters": [],
134+
"returnType": {
135+
"isNullable": false,
136+
"name": "ElementWrapper",
137+
"typeArguments": undefined,
138+
},
139+
},
140+
]
141+
`;
142+
143+
exports[`Generate documentation > deal with re-exports > card item wrapper methods 1`] = `
144+
[
145+
{
146+
"description": undefined,
147+
"inheritedFrom": undefined,
148+
"name": "findContent",
149+
"parameters": [],
150+
"returnType": {
151+
"isNullable": false,
152+
"name": "ElementWrapper",
153+
"typeArguments": undefined,
154+
},
155+
},
156+
{
157+
"description": undefined,
158+
"inheritedFrom": undefined,
159+
"name": "findHeader",
160+
"parameters": [],
161+
"returnType": {
162+
"isNullable": false,
163+
"name": "ElementWrapper",
164+
"typeArguments": undefined,
165+
},
166+
},
167+
]
168+
`;
169+
170+
exports[`Generate documentation > deal with re-exports > cards wrapper methods 1`] = `
171+
[
172+
{
173+
"description": undefined,
174+
"inheritedFrom": undefined,
175+
"name": "findItems",
176+
"parameters": [],
177+
"returnType": {
178+
"isNullable": false,
179+
"name": "Array",
180+
"typeArguments": [
181+
{
182+
"name": "CardWrapper",
183+
},
184+
],
185+
},
186+
},
187+
]
188+
`;
189+
190+
exports[`Generate documentation > deal with re-exports > dropdown wrapper methods 1`] = `
191+
[
192+
{
193+
"description": undefined,
194+
"inheritedFrom": undefined,
195+
"name": "findHighlightedOption",
196+
"parameters": [],
197+
"returnType": {
198+
"isNullable": false,
199+
"name": "OptionWrapper",
200+
"typeArguments": undefined,
201+
},
202+
},
203+
{
204+
"description": undefined,
205+
"inheritedFrom": undefined,
206+
"name": "findItemGroup",
207+
"parameters": [],
208+
"returnType": {
209+
"isNullable": false,
210+
"name": "DropdownWrapper",
211+
"typeArguments": undefined,
212+
},
213+
},
214+
{
215+
"description": undefined,
216+
"inheritedFrom": undefined,
217+
"name": "findItems",
218+
"parameters": [],
219+
"returnType": {
220+
"isNullable": false,
221+
"name": "Array",
222+
"typeArguments": [
223+
{
224+
"name": "OptionWrapper",
225+
},
226+
],
227+
},
228+
},
229+
]
230+
`;
231+
232+
exports[`Generate documentation > deal with re-exports > option wrapper methods 1`] = `
233+
[
234+
{
235+
"description": undefined,
236+
"inheritedFrom": undefined,
237+
"name": "findLabel",
238+
"parameters": [],
239+
"returnType": {
240+
"isNullable": true,
241+
"name": "ElementWrapper",
242+
"typeArguments": undefined,
243+
},
244+
},
245+
]
246+
`;

test/test-utils/doc-generation.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,20 @@ describe('Generate documentation', () => {
9191
'CardsWrapper',
9292
'CardWrapper',
9393
'DropdownWrapper',
94+
'OptionWrapper',
9495
]);
9596
const alertWrapper = results.find(classDoc => classDoc.name === 'AlertWrapper')!;
96-
expect(alertWrapper.methods.map(method => method.name)).toEqual(['findContent']);
97+
expect(alertWrapper.methods).toMatchSnapshot('alert wrapper methods');
9798
const buttonWrapper = results.find(classDoc => classDoc.name === 'ButtonWrapper')!;
98-
expect(buttonWrapper.methods.map(method => method.name)).toEqual(['findText']);
99+
expect(buttonWrapper.methods).toMatchSnapshot('button wrapper methods');
99100
const cardsWrapper = results.find(classDoc => classDoc.name === 'CardsWrapper')!;
100-
expect(cardsWrapper.methods.map(method => method.name)).toEqual(['findItems']);
101+
expect(cardsWrapper.methods).toMatchSnapshot('cards wrapper methods');
101102
const cardItemWrapper = results.find(classDoc => classDoc.name === 'CardWrapper')!;
102-
expect(cardItemWrapper.methods.map(method => method.name)).toEqual(['findContent', 'findHeader']);
103+
expect(cardItemWrapper.methods).toMatchSnapshot('card item wrapper methods');
103104
const dropdownWrapper = results.find(classDoc => classDoc.name === 'DropdownWrapper')!;
104-
expect(dropdownWrapper.methods.map(method => method.name)).toEqual(['findItemGroup', 'findItems']);
105+
expect(dropdownWrapper.methods).toMatchSnapshot('dropdown wrapper methods');
106+
const optionWrapper = results.find(classDoc => classDoc.name === 'OptionWrapper')!;
107+
expect(optionWrapper.methods).toMatchSnapshot('option wrapper methods');
105108
});
106109

107110
test('default value rendering', () => {

0 commit comments

Comments
 (0)