Skip to content

Commit

Permalink
chore: add nodeModulesInputFilePaths
Browse files Browse the repository at this point in the history
  • Loading branch information
cansuaa committed Aug 7, 2024
1 parent 2ea6ef7 commit a11d44c
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 18 deletions.
14 changes: 14 additions & 0 deletions fixtures/components/third-party-import-types/button/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import * as React from 'react';

import { ButtonProps } from './interfaces';

export { ButtonProps };

/**
* Component-level description
*/
export default function Button({ iconName }: ButtonProps) {
return <div className={iconName}>{iconName}</div>;
}
10 changes: 10 additions & 0 deletions fixtures/components/third-party-import-types/button/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { IconProps } from '../node_modules/icon';

export interface ButtonProps {
/**
* This is icon name
*/
iconName: IconProps.Name;
}
7 changes: 7 additions & 0 deletions fixtures/components/third-party-import-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "button"
},
"include": ["button"]
}
10 changes: 9 additions & 1 deletion src/bootstrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { TypeDocAndTSOptions, Application, TSConfigReader, ProjectReflection } f
import { matcher } from 'micromatch';
import { resolve } from 'pathe';

export function bootstrapProject(options: Partial<TypeDocAndTSOptions>, filteringGlob?: string): ProjectReflection {
export function bootstrapProject(
options: Partial<TypeDocAndTSOptions>,
filteringGlob?: string,
nodeModulesInputFilePaths?: string[]
): ProjectReflection {
const app = new Application();
app.options.addReader(new TSConfigReader());

Expand All @@ -14,6 +18,10 @@ export function bootstrapProject(options: Partial<TypeDocAndTSOptions>, filterin
throw new Error('Errors during parsing configuration');
}

if (nodeModulesInputFilePaths?.length) {
inputFiles.push(...nodeModulesInputFilePaths);

Check warning on line 22 in src/bootstrap/index.ts

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/index.ts#L22

Added line #L22 was not covered by tests
}

const filteredInputFiles = filterFiles(inputFiles, filteringGlob);
if (!filteredInputFiles.length) {
throw new Error('No input files to convert');
Expand Down
10 changes: 1 addition & 9 deletions src/components/components-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ function findProps(allDefinitions: DeclarationReflection[], propsName: string, d
};
}

export default function extractComponents(
publicFilesGlob: string,
project: ProjectReflection,
hasCoreComponentTypeDependency?: boolean
): ComponentDefinition[] {
export default function extractComponents(publicFilesGlob: string, project: ProjectReflection): ComponentDefinition[] {
const definitions: ComponentDefinition[] = [];
const isMatch = matcher(resolve(publicFilesGlob));

Expand All @@ -100,10 +96,6 @@ export default function extractComponents(

const allDefinitions = project.children.flatMap(module => {
if (!module.children) {
if (hasCoreComponentTypeDependency) {
return [];
}

throw new Error(`Module ${module.originalName} does not contain a definition.`);
}

Expand Down
18 changes: 12 additions & 6 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import { bootstrapProject } from '../bootstrap';
export function documentComponents(
tsconfigPath: string,
publicFilesGlob: string,
hasCoreComponentTypeDependency?: boolean
nodeModulesInputFilePaths?: string[]
): ComponentDefinition[] {
const project = bootstrapProject({
tsconfig: tsconfigPath,
includeDeclarations: hasCoreComponentTypeDependency,
});
return extractComponents(publicFilesGlob, project, hasCoreComponentTypeDependency);
const includeNodeModulePaths = Boolean(nodeModulesInputFilePaths?.length);
const project = bootstrapProject(
{
tsconfig: tsconfigPath,
includeDeclarations: includeNodeModulePaths,
excludeExternals: includeNodeModulePaths,
},
undefined,
nodeModulesInputFilePaths
);
return extractComponents(publicFilesGlob, project);
}
5 changes: 3 additions & 2 deletions test/components/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { TestUtilsDoc } from '../../src/test-utils/interfaces';

// TODO: Move this file into common location, improve naming

export function buildProject(name: string): ComponentDefinition[] {
export function buildProject(name: string, nodeModulesInputFilePaths?: string[]): ComponentDefinition[] {
return documentComponents(
require.resolve(`../../fixtures/components/${name}/tsconfig.json`),
`fixtures/components/${name}/*/index.tsx`
`fixtures/components/${name}/*/index.tsx`,
nodeModulesInputFilePaths
);
}

Expand Down
46 changes: 46 additions & 0 deletions test/components/third-party-import-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { buildProject } from './test-helpers';
import { ComponentDefinition } from '../../src';
import process from 'node:process';
const cwd = process.cwd();

test('should resolve object type to string', () => {
const resultBefore = buildProject('third-party-import-types');
const buttonBefore: ComponentDefinition | undefined = resultBefore.find(component => component.name === 'Button');

expect(buttonBefore?.properties).toEqual([
{
name: 'iconName',
type: 'IconProps.Name',
inlineType: undefined,
optional: false,
description: 'This is icon name',
defaultValue: undefined,
visualRefreshTag: undefined,
deprecatedTag: undefined,
i18nTag: undefined,
analyticsTag: undefined,
},
]);

const resultAfter = buildProject('third-party-import-types', [
`${cwd}/fixtures/components/third-party-import-types/node_modules/icon/interfaces.d.ts`,
]);
const buttonAfter: ComponentDefinition | undefined = resultAfter.find(component => component.name === 'Button');

expect(buttonAfter?.properties).toEqual([
{
name: 'iconName',
type: 'string',
inlineType: { name: 'IconProps.Name', type: 'union', values: ['icon1', 'icon2', 'icon3'] },
optional: false,
description: 'This is icon name',
defaultValue: undefined,
visualRefreshTag: undefined,
deprecatedTag: undefined,
i18nTag: undefined,
analyticsTag: undefined,
},
]);
});

0 comments on commit a11d44c

Please sign in to comment.