Skip to content

Commit 40b7fda

Browse files
authored
chore: Update files structure (#96)
1 parent 34643d4 commit 40b7fda

File tree

10 files changed

+58
-52
lines changed

10 files changed

+58
-52
lines changed

src/components/component-definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import ts from 'typescript';
44

5-
import { extractDeclaration, stringifyType } from './type-utils';
5+
import { extractDeclaration, stringifyType } from '../shared/type-utils';
66
import type {
77
ComponentDefinition,
88
ComponentFunction,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import ts from 'typescript';
4+
import { extractDeclaration } from '../shared/type-utils';
5+
import { ValueDescription } from './interfaces';
6+
7+
export function extractValueDescriptions(type: ts.UnionOrIntersectionType, typeNode: ts.TypeNode | undefined) {
8+
if (type.aliasSymbol) {
9+
// Traverse from "variant: ButtonProps.Variant" to "type Variant = ..."
10+
const aliasDeclaration = extractDeclaration(type.aliasSymbol);
11+
if (ts.isTypeAliasDeclaration(aliasDeclaration)) {
12+
typeNode = aliasDeclaration.type;
13+
}
14+
}
15+
16+
if (!typeNode) {
17+
return [];
18+
}
19+
20+
const maybeList = typeNode.getChildren()[0];
21+
// based on similar code in typedoc
22+
// https://github.com/TypeStrong/typedoc/blob/6090b3e31471cea3728db1b03888bca5703b437e/src/lib/converter/symbols.ts#L406-L438
23+
if (maybeList.kind !== ts.SyntaxKind.SyntaxList) {
24+
return [];
25+
}
26+
const rawComments: Array<string | undefined> = [];
27+
let memberIndex = 0;
28+
for (const child of maybeList.getChildren()) {
29+
const text = child.getFullText();
30+
if (text.includes('/**')) {
31+
rawComments[memberIndex] = (rawComments[memberIndex] ?? '') + child.getFullText();
32+
}
33+
34+
if (child.kind !== ts.SyntaxKind.BarToken) {
35+
memberIndex++;
36+
}
37+
}
38+
// Array.from to fix sparse array
39+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#array_methods_and_empty_slots
40+
return Array.from(rawComments).map((comment): ValueDescription | undefined => {
41+
if (!comment) {
42+
return undefined;
43+
}
44+
const systemTags = Array.from(comment.matchAll(/@awsuiSystem\s+(\w+)/g), ([_, system]) => system);
45+
return systemTags.length > 0 ? { systemTags } : undefined;
46+
});
47+
}

src/components/extractor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isOptional,
99
stringifyType,
1010
unwrapNamespaceDeclaration,
11-
} from './type-utils';
11+
} from '../shared/type-utils';
1212

1313
export interface ExtractedDescription {
1414
text: string | undefined;

src/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { buildComponentDefinition } from './component-definition';
99
import { extractDefaultValues, extractExports, extractFunctions, extractProps } from './extractor';
1010
import type { ComponentDefinition } from './interfaces';
1111
import { bootstrapTypescriptProject } from '../shared/bootstrap';
12-
import { extractDeclaration, getDescription } from './type-utils';
12+
import { extractDeclaration, getDescription } from '../shared/type-utils';
1313

1414
function componentNameFromPath(componentPath: string) {
1515
const dashCaseName = pathe.basename(pathe.dirname(componentPath));

src/components/object-definition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import ts from 'typescript';
44
import type { TypeDefinition, UnionTypeDefinition, ValueDescription } from './interfaces';
5-
import { extractDeclaration, extractValueDescriptions, isOptional, stringifyType } from './type-utils';
5+
import { extractDeclaration, isOptional, stringifyType } from '../shared/type-utils';
6+
import { extractValueDescriptions } from './extract-value-descriptions';
67

78
function isArrayType(type: ts.Type) {
89
const symbol = type.getSymbol();
Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
import ts from 'typescript';
4-
import { ValueDescription } from './interfaces';
54

65
export function isOptional(type: ts.Type) {
76
if (!type.isUnionOrIntersection()) {
@@ -62,48 +61,6 @@ export function getDescription(docComment: Array<ts.SymbolDisplayPart>, declarat
6261
};
6362
}
6463

65-
export function extractValueDescriptions(type: ts.UnionOrIntersectionType, typeNode: ts.TypeNode | undefined) {
66-
if (type.aliasSymbol) {
67-
// Traverse from "variant: ButtonProps.Variant" to "type Variant = ..."
68-
const aliasDeclaration = extractDeclaration(type.aliasSymbol);
69-
if (ts.isTypeAliasDeclaration(aliasDeclaration)) {
70-
typeNode = aliasDeclaration.type;
71-
}
72-
}
73-
74-
if (!typeNode) {
75-
return [];
76-
}
77-
78-
const maybeList = typeNode.getChildren()[0];
79-
// based on similar code in typedoc
80-
// https://github.com/TypeStrong/typedoc/blob/6090b3e31471cea3728db1b03888bca5703b437e/src/lib/converter/symbols.ts#L406-L438
81-
if (maybeList.kind !== ts.SyntaxKind.SyntaxList) {
82-
return [];
83-
}
84-
const rawComments: Array<string | undefined> = [];
85-
let memberIndex = 0;
86-
for (const child of maybeList.getChildren()) {
87-
const text = child.getFullText();
88-
if (text.includes('/**')) {
89-
rawComments[memberIndex] = (rawComments[memberIndex] ?? '') + child.getFullText();
90-
}
91-
92-
if (child.kind !== ts.SyntaxKind.BarToken) {
93-
memberIndex++;
94-
}
95-
}
96-
// Array.from to fix sparse array
97-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#array_methods_and_empty_slots
98-
return Array.from(rawComments).map((comment): ValueDescription | undefined => {
99-
if (!comment) {
100-
return undefined;
101-
}
102-
const systemTags = Array.from(comment.matchAll(/@awsuiSystem\s+(\w+)/g), ([_, system]) => system);
103-
return systemTags.length > 0 ? { systemTags } : undefined;
104-
});
105-
}
106-
10764
export function extractDeclaration(symbol: ts.Symbol) {
10865
const declarations = symbol.getDeclarations();
10966
if (!declarations || declarations.length === 0) {

src/test-utils/extractor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isNullable,
99
isOptional,
1010
stringifyType,
11-
} from '../components/type-utils';
11+
} from '../shared/type-utils';
1212
import { TestUtilsDoc } from './interfaces';
1313

1414
function getInheritedFrom(declaration: ts.Declaration, currentClassName: string) {

src/test-utils/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
export interface Parameter {
44
name: string;
5-
typeName?: string;
5+
typeName: string;
66
description?: string;
77
flags: { isOptional?: boolean };
88
defaultValue?: string;
@@ -15,7 +15,7 @@ interface TypeArgument {
1515
export interface TestUtilMethod {
1616
name: string;
1717
description?: string;
18-
returnType?: {
18+
returnType: {
1919
name: string;
2020
isNullable: boolean;
2121
typeArguments?: Array<TypeArgument>;

test/components/value-descriptions-extractor.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import { expect, test } from 'vitest';
44
import ts from 'typescript';
5-
import { extractDeclaration, extractValueDescriptions } from '../../src/components/type-utils';
5+
import { extractDeclaration } from '../../src/shared/type-utils';
6+
import { extractValueDescriptions } from '../../src/components/extract-value-descriptions';
67
import { getInMemoryProject } from './test-helpers';
78

89
function extractFromSource(source: string) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import ts from 'typescript';
55
import { test, expect } from 'vitest';
6-
import { printFlags } from '../../lib/components/type-utils';
6+
import { printFlags } from '../../lib/shared/type-utils';
77

88
test('serialises node flags', () => {
99
const flags = printFlags(

0 commit comments

Comments
 (0)