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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build
lib
node_modules
/node_modules
coverage
17 changes: 17 additions & 0 deletions fixtures/components/complex-types/column-layout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import * as React from 'react';

export interface ColumnLayoutProps {
columns: ColumnLayoutProps.Columns;
widths: ColumnLayoutProps.Widths;
}

export namespace ColumnLayoutProps {
export type Columns = 1 | 2 | 3 | 4;
export type Widths = 25 | '50%' | 100 | '33%';
Comment on lines +11 to +12
Copy link
Member Author

Choose a reason for hiding this comment

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

Added new test case on number-only and on mixed string+number union types.

We do not have a real use-case yet, but let's make sure it works anyway

}

export default function ColumnLayout(props: ColumnLayoutProps) {

Check warning on line 15 in fixtures/components/complex-types/column-layout/index.tsx

View workflow job for this annotation

GitHub Actions / build / build

'props' is defined but never used

Check warning on line 15 in fixtures/components/complex-types/column-layout/index.tsx

View workflow job for this annotation

GitHub Actions / build / build

Missing return type on function
return <div></div>;
}
2 changes: 2 additions & 0 deletions fixtures/components/complex-types/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import * as React from 'react';
import { TableProps } from './interfaces';

export { TableProps };
Copy link
Member Author

Choose a reason for hiding this comment

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

The new implementation is more strict, it checks that the interface is actually exported in the index file (which was our requirement all the time)


export default function Table<T = any>(props: TableProps<T>) {

Check warning on line 8 in fixtures/components/complex-types/table/index.tsx

View workflow job for this annotation

GitHub Actions / build / build

'props' is defined but never used

Check warning on line 8 in fixtures/components/complex-types/table/index.tsx

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 8 in fixtures/components/complex-types/table/index.tsx

View workflow job for this annotation

GitHub Actions / build / build

Missing return type on function
return <table />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import * as React from 'react';

export default function Input() {
return <span>Surprise, I am not Button!</span>;
// should fail because ButtonProps export is missing
export default function Button() {
return <span>Test</span>;
}
7 changes: 7 additions & 0 deletions fixtures/components/error-not-a-component/button/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export default function Button() {
// if a function does not return JSX, we throw
return { type: 'button' };
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import * as React from 'react';

export namespace ButtonProps {
interface Ref {
export interface Ref {
value: string;
}
}

const Button = React.forwardRef((props, ref) => {
const Button = React.forwardRef<ButtonProps.Ref>((props, ref) => {
return <button />;
});

Expand Down
7 changes: 6 additions & 1 deletion fixtures/components/forward-ref/focusable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface FocusableProps {
children: React.ReactNode;
}

export namespace SomethingElse {
namespace SomethingElse {
export interface Ref {
/**
* Should be ignored
Expand All @@ -29,6 +29,11 @@ export namespace FocusableProps {
* Focuses element using the CSS-selector
*/
focusBySelector(selector: string): void;

/**
* Showcase for optional functions
*/
cancelEdit?(): void;
}
}

Expand Down
15 changes: 2 additions & 13 deletions fixtures/components/import-types/dependency/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import * as React from 'react';
import { DependencyProps } from './interfaces';

export interface DependencyProps {
name?: string;
variant?: DependencyProps.Variant;
}

// should not be included in the Main component API definition
export interface MainProps {
randomValue: string;
}

export namespace DependencyProps {
export type Variant = 'button' | 'link';
}
export { DependencyProps };

export default function Dependency({ name, variant = 'button' }: DependencyProps) {
return <div className={variant}>{name}</div>;
Expand Down
15 changes: 15 additions & 0 deletions fixtures/components/import-types/dependency/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
export interface DependencyProps {
name?: string;
variant?: DependencyProps.Variant;
}

// should not be included in the Main component API definition
export interface MainProps {
randomValue: string;
}

export namespace DependencyProps {
export type Variant = 'button' | 'link';
}
14 changes: 0 additions & 14 deletions fixtures/components/release-status/beta/index.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { IconProps } from '../node_modules_mock/icon';
import { IconProps } from 'icon';
Copy link
Member Author

Choose a reason for hiding this comment

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

Updated this test to more accurately represent node_modules resolution. node_modules_mock is a normal project folder, typescript has no special logic there.


export interface ButtonProps {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ function InternalSameFile() {
return <Internal name="test" />;
}

export default function WithInternals() {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface WithInternalsProps {
// nothing here
}

export default function WithInternals(props: WithInternalsProps) {
return <InternalSameFile />;
}
44 changes: 44 additions & 0 deletions src/bootstrap/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import ts from 'typescript';
import pathe from 'pathe';

function printDiagnostics(diagnostics: readonly ts.Diagnostic[]): void {
Copy link
Member Author

Choose a reason for hiding this comment

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

for (const diagnostic of diagnostics) {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
if (diagnostic.file) {
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!);
console.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.error(message);

Check warning on line 13 in src/bootstrap/typescript.ts

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/typescript.ts#L12-L13

Added lines #L12 - L13 were not covered by tests
}
}
}

function loadTSConfig(tsconfigPath: string): ts.ParsedCommandLine {
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
if (configFile.error) {
throw new Error('Failed to read tsconfig.json');

Check warning on line 21 in src/bootstrap/typescript.ts

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/typescript.ts#L21

Added line #L21 was not covered by tests
}
const config = ts.parseJsonConfigFileContent(configFile.config, ts.sys, pathe.dirname(tsconfigPath));
if (config.errors.length > 0) {
throw new Error('Failed to parse tsconfig.json');
}
// this prints a warning that incremental mode is not supported in programmatic API
config.options.incremental = false;
delete config.options.tsBuildInfoFile;
return config;
}

export function bootstrapTypescriptProject(tsconfigPath: string) {
const tsconfig = loadTSConfig(tsconfigPath);
const program = ts.createProgram(tsconfig.fileNames, tsconfig.options);

const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length > 0) {
printDiagnostics(diagnostics);
throw new Error('Compilation failed');
}

return program;
}
153 changes: 0 additions & 153 deletions src/components/build-definition.ts

This file was deleted.

Loading
Loading