Skip to content

Commit 38a0914

Browse files
authored
feat: Allow hiding methods from base classes (#97)
1 parent 40b7fda commit 38a0914

File tree

7 files changed

+114
-38
lines changed

7 files changed

+114
-38
lines changed

src/test-utils/extractor.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function getInheritedFrom(declaration: ts.Declaration, currentClassName: string)
1919
if (parentName === currentClassName) {
2020
return undefined;
2121
}
22-
return { name: parentName + '.' + declaration.name.getText() };
22+
return { className: parentName, methodName: declaration.name.getText() };
2323
}
2424

2525
function getDefaultValue(declaration: ts.Declaration) {
@@ -35,7 +35,8 @@ function getDefaultValue(declaration: ts.Declaration) {
3535
export default function extractDocumentation(
3636
sourceFile: ts.SourceFile,
3737
checker: ts.TypeChecker,
38-
extraExports: Array<string>
38+
extraExports: Array<string>,
39+
includeCoreMethods: boolean
3940
): Array<TestUtilsDoc> {
4041
const moduleSymbol = checker.getSymbolAtLocation(sourceFile);
4142
if (!moduleSymbol) {
@@ -51,7 +52,7 @@ export default function extractDocumentation(
5152
continue;
5253
}
5354
const classType = checker.getDeclaredTypeOfSymbol(symbol);
54-
documentClass(definitions, symbol, classType, checker);
55+
documentClass(definitions, symbol, classType, checker, includeCoreMethods);
5556
}
5657

5758
return Array.from(definitions.values());
@@ -61,7 +62,8 @@ function documentClass(
6162
definitions: Map<string, TestUtilsDoc>,
6263
symbol: ts.Symbol,
6364
classType: ts.Type,
64-
checker: ts.TypeChecker
65+
checker: ts.TypeChecker,
66+
includeCoreMethods: boolean
6567
) {
6668
if (!classType.isClass()) {
6769
throw new Error(`Exported symbol is not a class, got ${checker.symbolToString(symbol)}`);
@@ -92,11 +94,20 @@ function documentClass(
9294
maybeReturnType.flags & ts.TypeFlags.Void ? maybeReturnType : maybeReturnType.getNonNullableType();
9395
const dependency = findDependencyType(returnType, checker);
9496
if (dependency && !definitions.has(dependency.typeName)) {
95-
documentClass(definitions, dependency.symbol, dependency.type, checker);
97+
documentClass(definitions, dependency.symbol, dependency.type, checker, includeCoreMethods);
9698
}
9799

98100
const { typeName, typeParameters } = extractTypeArguments(returnType, checker);
99101

102+
const inheritedFrom = getInheritedFrom(declaration, className);
103+
if (
104+
inheritedFrom &&
105+
!includeCoreMethods &&
106+
['AbstractWrapper', 'ElementWrapper'].includes(inheritedFrom?.className)
107+
) {
108+
continue;
109+
}
110+
100111
definition.methods.push({
101112
name: property.getName(),
102113
description: getDescription(property.getDocumentationComment(checker), declaration).text,
@@ -117,7 +128,7 @@ function documentClass(
117128
defaultValue: getDefaultValue(extractDeclaration(parameter)),
118129
};
119130
}),
120-
inheritedFrom: getInheritedFrom(declaration, className),
131+
inheritedFrom: inheritedFrom ? { name: `${inheritedFrom.className}.${inheritedFrom.methodName}` } : undefined,
121132
});
122133
}
123134
}

src/test-utils/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface TestUtilsVariantOptions {
1313

1414
export interface TestUtilsDocumenterOptions {
1515
tsconfigPath: string;
16+
includeCoreMethods?: boolean;
1617
domUtils: TestUtilsVariantOptions;
1718
selectorsUtils: TestUtilsVariantOptions;
1819
}
@@ -22,7 +23,7 @@ interface TestUtilsDefinitions {
2223
selectorsDefinitions: Array<TestUtilsDoc>;
2324
}
2425

25-
export function documentTestUtilsNew(options: TestUtilsDocumenterOptions): TestUtilsDefinitions {
26+
export function documentTestUtils(options: TestUtilsDocumenterOptions): TestUtilsDefinitions {
2627
const domUtilsRoot = pathe.resolve(options.domUtils.root);
2728
const selectorsUtilsRoot = pathe.resolve(options.selectorsUtils.root);
2829
const program = bootstrapTypescriptProject(options.tsconfigPath);
@@ -37,17 +38,29 @@ export function documentTestUtilsNew(options: TestUtilsDocumenterOptions): TestU
3738
if (!selectorsUtilsFile) {
3839
throw new Error(`File '${selectorsUtilsFile}' not found`);
3940
}
41+
// TODO: switch to false after all consumers updated
42+
const includeCoreMethods = options.includeCoreMethods ?? true;
4043
return {
41-
domDefinitions: extractDocumentation(domUtilsFile, checker, options.domUtils.extraExports ?? []),
42-
selectorsDefinitions: extractDocumentation(selectorsUtilsFile, checker, options.selectorsUtils.extraExports ?? []),
44+
domDefinitions: extractDocumentation(
45+
domUtilsFile,
46+
checker,
47+
options.domUtils.extraExports ?? [],
48+
includeCoreMethods
49+
),
50+
selectorsDefinitions: extractDocumentation(
51+
selectorsUtilsFile,
52+
checker,
53+
options.selectorsUtils.extraExports ?? [],
54+
includeCoreMethods
55+
),
4356
};
4457
}
4558

4659
export function writeTestUtilsDocumentation({
4760
outDir,
4861
...rest
4962
}: TestUtilsDocumenterOptions & { outDir: string }): void {
50-
const { domDefinitions, selectorsDefinitions } = documentTestUtilsNew(rest);
63+
const { domDefinitions, selectorsDefinitions } = documentTestUtils(rest);
5164
fs.mkdirSync(outDir, { recursive: true });
5265
fs.writeFileSync(
5366
pathe.join(outDir, 'dom.js'),

src/test-utils/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface TestUtilMethod {
2121
typeArguments?: Array<TypeArgument>;
2222
};
2323
parameters: Array<Parameter>;
24+
/* @deprecated All inherited methods should be filtered automatically */
2425
inheritedFrom?: {
2526
name: string;
2627
};

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ exports[`Generate documentation > For simple cases 1`] = `
1414
]
1515
`;
1616

17+
exports[`Generate documentation > allows to skip methods from core wrappers 1`] = `
18+
[
19+
{
20+
"methods": [
21+
{
22+
"description": undefined,
23+
"inheritedFrom": undefined,
24+
"name": "childClassMethod",
25+
"parameters": [],
26+
"returnType": {
27+
"isNullable": false,
28+
"name": "void",
29+
"typeArguments": undefined,
30+
},
31+
},
32+
],
33+
"name": "TestUtilWrapper",
34+
},
35+
]
36+
`;
37+
1738
exports[`Generate documentation > deal with more complex types 1`] = `
1839
[
1940
{
@@ -285,3 +306,37 @@ exports[`Generate documentation > deal with re-exports 1`] = `
285306
},
286307
]
287308
`;
309+
310+
exports[`Generate documentation > includes inherited methods from core wrappers 1`] = `
311+
[
312+
{
313+
"methods": [
314+
{
315+
"description": undefined,
316+
"inheritedFrom": undefined,
317+
"name": "childClassMethod",
318+
"parameters": [],
319+
"returnType": {
320+
"isNullable": false,
321+
"name": "void",
322+
"typeArguments": undefined,
323+
},
324+
},
325+
{
326+
"description": undefined,
327+
"inheritedFrom": {
328+
"name": "AbstractWrapper.inheritedMethod",
329+
},
330+
"name": "inheritedMethod",
331+
"parameters": [],
332+
"returnType": {
333+
"isNullable": false,
334+
"name": "void",
335+
"typeArguments": undefined,
336+
},
337+
},
338+
],
339+
"name": "TestUtilWrapper",
340+
},
341+
]
342+
`;

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Generate documentation', () => {
4949
});
5050

5151
test('deal with more complex types', () => {
52-
const results = buildTestUtilsProject('advanced-types', { extraExports: ['default'] });
52+
const results = buildTestUtilsProject('advanced-types', undefined, { extraExports: ['default'] });
5353

5454
expect(results.length).toBe(1);
5555
const classDoc = results[0];
@@ -61,26 +61,16 @@ describe('Generate documentation', () => {
6161
expect(methods).toMatchSnapshot();
6262
});
6363

64-
test('and deal with inheritance', () => {
65-
const results = buildTestUtilsProject('inheritance');
66-
67-
expect(results.length).toBe(1);
68-
const classDoc = results.find(classDoc => classDoc.name === 'TestUtilWrapper');
69-
70-
expect(classDoc).toBeDefined();
71-
72-
const methods = classDoc?.methods || [];
73-
expect(methods.length).toBe(2);
74-
75-
const inheritedMethod = methods.find(method => method.name === 'inheritedMethod');
76-
expect(inheritedMethod).toBeDefined();
77-
expect(inheritedMethod?.inheritedFrom).toEqual({
78-
name: 'AbstractWrapper.inheritedMethod',
79-
});
64+
test('includes inherited methods from core wrappers', () => {
65+
const results = buildTestUtilsProject('inheritance', { includeCoreMethods: true });
66+
expect(results[0].methods.find(method => method.name === 'inheritedMethod')).toBeTruthy();
67+
expect(results).toMatchSnapshot();
68+
});
8069

81-
const childClassMethod = methods.find(method => method.name === 'childClassMethod');
82-
expect(childClassMethod).toBeDefined();
83-
expect(childClassMethod?.inheritedFrom).toBeUndefined();
70+
test('allows to skip methods from core wrappers', () => {
71+
const results = buildTestUtilsProject('inheritance', { includeCoreMethods: false });
72+
expect(results[0].methods.find(method => method.name === 'inheritedMethod')).toBeFalsy();
73+
expect(results).toMatchSnapshot();
8474
});
8575

8676
test('deal with re-exports', () => {

test/test-utils/test-helpers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { documentTestUtilsNew, TestUtilsVariantOptions } from '../../src/test-utils';
3+
import { documentTestUtils, TestUtilsDocumenterOptions, TestUtilsVariantOptions } from '../../src/test-utils';
44
import { TestUtilsDoc } from '../../src/test-utils/interfaces';
55

66
export function buildTestUtilsProject(
77
name: string,
8-
configOverrides?: Partial<TestUtilsVariantOptions>
8+
configOverrides?: Partial<TestUtilsDocumenterOptions>,
9+
wrapperConfigOverrides?: Partial<TestUtilsVariantOptions>
910
): TestUtilsDoc[] {
10-
return documentTestUtilsNew({
11+
return documentTestUtils({
1112
tsconfigPath: require.resolve(`../../fixtures/test-utils/${name}/tsconfig.json`),
12-
domUtils: { root: `fixtures/test-utils/${name}/index.ts`, ...configOverrides },
13-
selectorsUtils: { root: `fixtures/test-utils/${name}/index.ts`, ...configOverrides },
13+
domUtils: { root: `fixtures/test-utils/${name}/index.ts`, ...wrapperConfigOverrides },
14+
selectorsUtils: { root: `fixtures/test-utils/${name}/index.ts`, ...wrapperConfigOverrides },
15+
...configOverrides,
1416
}).domDefinitions;
1517
}

test/test-utils/usage.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ describe('documentTestUtils throws error for ', () => {
2424

2525
test('having no input files because of a non-matching glob', () => {
2626
expect(() =>
27-
buildTestUtilsProject('simple', {
28-
root: 'fixtures/does-not-exist/index.ts',
29-
})
27+
buildTestUtilsProject(
28+
'simple',
29+
{},
30+
{
31+
root: 'fixtures/does-not-exist/index.ts',
32+
}
33+
)
3034
).toThrow(/File '.*fixtures\/does-not-exist\/index.ts' not found/);
3135
});
3236
});

0 commit comments

Comments
 (0)