Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/component-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import ts from 'typescript';

import { extractDeclaration, stringifyType } from './type-utils';
import { extractDeclaration, stringifyType } from '../shared/type-utils';
import type {
ComponentDefinition,
ComponentFunction,
Expand Down
47 changes: 47 additions & 0 deletions src/components/extract-value-descriptions.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not new code, that's how Github sees it after the rest of the code moved into ../shared

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import ts from 'typescript';
import { extractDeclaration } from '../shared/type-utils';
import { ValueDescription } from './interfaces';

export function extractValueDescriptions(type: ts.UnionOrIntersectionType, typeNode: ts.TypeNode | undefined) {
if (type.aliasSymbol) {
// Traverse from "variant: ButtonProps.Variant" to "type Variant = ..."
const aliasDeclaration = extractDeclaration(type.aliasSymbol);
if (ts.isTypeAliasDeclaration(aliasDeclaration)) {
typeNode = aliasDeclaration.type;
}
}

if (!typeNode) {
return [];
}

const maybeList = typeNode.getChildren()[0];
// based on similar code in typedoc
// https://github.com/TypeStrong/typedoc/blob/6090b3e31471cea3728db1b03888bca5703b437e/src/lib/converter/symbols.ts#L406-L438
if (maybeList.kind !== ts.SyntaxKind.SyntaxList) {
return [];
}
const rawComments: Array<string | undefined> = [];
let memberIndex = 0;
for (const child of maybeList.getChildren()) {
const text = child.getFullText();
if (text.includes('/**')) {
rawComments[memberIndex] = (rawComments[memberIndex] ?? '') + child.getFullText();
}

if (child.kind !== ts.SyntaxKind.BarToken) {
memberIndex++;
}
}
// Array.from to fix sparse array
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#array_methods_and_empty_slots
return Array.from(rawComments).map((comment): ValueDescription | undefined => {
if (!comment) {
return undefined;
}
const systemTags = Array.from(comment.matchAll(/@awsuiSystem\s+(\w+)/g), ([_, system]) => system);
return systemTags.length > 0 ? { systemTags } : undefined;
});
}
2 changes: 1 addition & 1 deletion src/components/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isOptional,
stringifyType,
unwrapNamespaceDeclaration,
} from './type-utils';
} from '../shared/type-utils';

export interface ExtractedDescription {
text: string | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { buildComponentDefinition } from './component-definition';
import { extractDefaultValues, extractExports, extractFunctions, extractProps } from './extractor';
import type { ComponentDefinition } from './interfaces';
import { bootstrapTypescriptProject } from '../shared/bootstrap';
import { extractDeclaration, getDescription } from './type-utils';
import { extractDeclaration, getDescription } from '../shared/type-utils';

function componentNameFromPath(componentPath: string) {
const dashCaseName = pathe.basename(pathe.dirname(componentPath));
Expand Down
3 changes: 2 additions & 1 deletion src/components/object-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
import ts from 'typescript';
import type { TypeDefinition, UnionTypeDefinition, ValueDescription } from './interfaces';
import { extractDeclaration, extractValueDescriptions, isOptional, stringifyType } from './type-utils';
import { extractDeclaration, isOptional, stringifyType } from '../shared/type-utils';
import { extractValueDescriptions } from './extract-value-descriptions';

function isArrayType(type: ts.Type) {
const symbol = type.getSymbol();
Expand Down
43 changes: 0 additions & 43 deletions src/components/type-utils.ts → src/shared/type-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import ts from 'typescript';
import { ValueDescription } from './interfaces';

export function isOptional(type: ts.Type) {
if (!type.isUnionOrIntersection()) {
Expand Down Expand Up @@ -62,48 +61,6 @@ export function getDescription(docComment: Array<ts.SymbolDisplayPart>, declarat
};
}

export function extractValueDescriptions(type: ts.UnionOrIntersectionType, typeNode: ts.TypeNode | undefined) {
if (type.aliasSymbol) {
// Traverse from "variant: ButtonProps.Variant" to "type Variant = ..."
const aliasDeclaration = extractDeclaration(type.aliasSymbol);
if (ts.isTypeAliasDeclaration(aliasDeclaration)) {
typeNode = aliasDeclaration.type;
}
}

if (!typeNode) {
return [];
}

const maybeList = typeNode.getChildren()[0];
// based on similar code in typedoc
// https://github.com/TypeStrong/typedoc/blob/6090b3e31471cea3728db1b03888bca5703b437e/src/lib/converter/symbols.ts#L406-L438
if (maybeList.kind !== ts.SyntaxKind.SyntaxList) {
return [];
}
const rawComments: Array<string | undefined> = [];
let memberIndex = 0;
for (const child of maybeList.getChildren()) {
const text = child.getFullText();
if (text.includes('/**')) {
rawComments[memberIndex] = (rawComments[memberIndex] ?? '') + child.getFullText();
}

if (child.kind !== ts.SyntaxKind.BarToken) {
memberIndex++;
}
}
// Array.from to fix sparse array
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#array_methods_and_empty_slots
return Array.from(rawComments).map((comment): ValueDescription | undefined => {
if (!comment) {
return undefined;
}
const systemTags = Array.from(comment.matchAll(/@awsuiSystem\s+(\w+)/g), ([_, system]) => system);
return systemTags.length > 0 ? { systemTags } : undefined;
});
}

export function extractDeclaration(symbol: ts.Symbol) {
const declarations = symbol.getDeclarations();
if (!declarations || declarations.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isNullable,
isOptional,
stringifyType,
} from '../components/type-utils';
} from '../shared/type-utils';
import { TestUtilsDoc } from './interfaces';

function getInheritedFrom(declaration: ts.Declaration, currentClassName: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/test-utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
export interface Parameter {
name: string;
typeName?: string;
typeName: string;
description?: string;
flags: { isOptional?: boolean };
defaultValue?: string;
Expand All @@ -15,7 +15,7 @@ interface TypeArgument {
export interface TestUtilMethod {
name: string;
description?: string;
returnType?: {
returnType: {
name: string;
isNullable: boolean;
typeArguments?: Array<TypeArgument>;
Expand Down
3 changes: 2 additions & 1 deletion test/components/value-descriptions-extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
import { expect, test } from 'vitest';
import ts from 'typescript';
import { extractDeclaration, extractValueDescriptions } from '../../src/components/type-utils';
import { extractDeclaration } from '../../src/shared/type-utils';
import { extractValueDescriptions } from '../../src/components/extract-value-descriptions';
import { getInMemoryProject } from './test-helpers';

function extractFromSource(source: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

test('serialises node flags', () => {
const flags = printFlags(
Expand Down
Loading