Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(complier): support type import aliasing #5836

Merged
merged 2 commits into from
Jun 18, 2024
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
6 changes: 3 additions & 3 deletions src/compiler/types/generate-app-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ const generateComponentTypesFile = (
return `{ ${typeData
.sort(sortImportNames)
.map((td) => {
if (td.localName === td.importName) {
return `${td.importName}`;
if (td.originalName === td.importName) {
return `${td.originalName}`;
} else {
return `${td.localName} as ${td.importName}`;
return `${td.originalName} as ${td.importName}`;
}
})
.join(`, `)} } from "${importFilePath}";`;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types/tests/ComponentCompilerEvent.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const stubComponentCompilerEvent = (
resolved: '"foo" | "bar"',
references: {
UserImplementedEventType: {
id: 'placeholder',
id: './resources.ts::UserImplementedEventType',
location: 'import',
path: './resources',
},
Expand Down
124 changes: 108 additions & 16 deletions src/compiler/types/tests/generate-app-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ declare module "@stencil/core" {
resolved: '"wee" | "woo"',
references: {
SecondUserImplementedEventType: {
id: 'placeholder',
id: './resources.ts::SecondUserImplementedEventType',
location: 'import',
path: './resources',
},
Expand Down Expand Up @@ -310,7 +310,7 @@ declare module "@stencil/core" {
references: {
UserImplementedEventType: {
location: 'import',
id: 'placeholder',
id: './resources.ts::UserImplementedEventType',
path: './resources',
},
},
Expand Down Expand Up @@ -479,7 +479,7 @@ declare module "@stencil/core" {
UserImplementedEventType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources::UserImplementedEventType',
},
},
},
Expand All @@ -502,7 +502,7 @@ declare module "@stencil/core" {
UserImplementedEventType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources::UserImplementedEventType',
},
},
},
Expand Down Expand Up @@ -649,7 +649,7 @@ declare module "@stencil/core" {
UserImplementedEventType: {
location: 'local',
path: '/some/stubbed/path/a/my-component.tsx',
id: 'placeholder',
id: '/some/stubbed/path/a/my-component.tsx::UserImplementedEventType',
},
},
},
Expand All @@ -671,7 +671,7 @@ declare module "@stencil/core" {
references: {
UserImplementedEventType: {
location: 'local',
id: 'placeholder',
id: '/some/stubbed/path/b/my-new-component.tsx::UserImplementedEventType',
path: '/some/stubbed/path/b/my-new-component.tsx',
},
},
Expand Down Expand Up @@ -819,7 +819,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources::UserImplementedPropType',
},
},
},
Expand Down Expand Up @@ -907,7 +907,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources::UserImplementedPropType',
},
},
},
Expand All @@ -921,7 +921,7 @@ declare module "@stencil/core" {
SecondUserImplementedPropType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources::SecondUserImplementedPropType',
},
},
},
Expand Down Expand Up @@ -1011,7 +1011,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources::UserImplementedPropType',
},
},
},
Expand All @@ -1035,7 +1035,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'import',
path: '../resources',
id: 'placeholder',
id: '../resources::UserImplementedPropType',
},
},
},
Expand Down Expand Up @@ -1152,7 +1152,7 @@ declare module "@stencil/core" {
references: {
UserImplementedPropType: {
location: 'import',
id: 'placeholder',
id: './resources.ts::UserImplementedPropType',
path: './resources',
},
},
Expand All @@ -1177,7 +1177,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources.ts::UserImplementedPropType',
},
},
},
Expand Down Expand Up @@ -1297,7 +1297,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'local',
path: '/some/stubbed/path/a/my-component.tsx',
id: 'placeholder',
id: '/some/stubbed/path/a/my-component.tsx::UserImplementedPropType',
},
},
},
Expand All @@ -1321,7 +1321,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'local',
path: '/some/stubbed/path/b/my-new-component.tsx',
id: 'placeholder',
id: '/some/stubbed/path/b/my-new-component.tsx::UserImplementedPropType',
},
},
},
Expand Down Expand Up @@ -1444,7 +1444,7 @@ declare module "@stencil/core" {
UserImplementedPropType: {
location: 'import',
path: './resources',
id: 'placeholder',
id: './resources.ts::UserImplementedPropType',
},
},
},
Expand Down Expand Up @@ -1720,6 +1720,98 @@ declare module "@stencil/core" {
}
}
}
`,
{
immediateWrite: true,
},
);
});

it('should handle type import aliases', async () => {
const compilerComponentMeta = stubComponentCompilerMeta({
tagName: 'my-component',
componentClassName: 'MyComponent',
jsFilePath: '/some/stubbed/path/a/my-component.js',
sourceFilePath: '/some/stubbed/path/a/my-component.tsx',
sourceMapPath: '/some/stubbed/path/a/my-component.js.map',
hasProp: true,
properties: [
stubComponentCompilerProperty({
name: 'name',
complexType: {
original: 'UserImplementedPropType',
resolved: '"foo" | "bar"',
references: {
UserImplementedPropType: {
id: 'some-file.ts::MyType',
location: 'import',
path: '@utils',
},
},
},
}),
],
});
buildCtx.components = [compilerComponentMeta];
config.tsCompilerOptions = {};

await generateAppTypes(config, compilerCtx, buildCtx, 'src');

expect(mockWriteFile).toHaveBeenCalledWith(
'/components.d.ts',
`/* eslint-disable */
/* tslint:disable */
/**
* This is an autogenerated file created by the Stencil compiler.
* It contains typing information for all components that exist in this project.
*/
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
import { MyType as UserImplementedPropType } from "@utils";
export { MyType as UserImplementedPropType } from "@utils";
export namespace Components {
/**
* docs
*/
interface MyComponent {
"name": UserImplementedPropType;
}
}
declare global {
/**
* docs
*/
interface HTMLMyComponentElement extends Components.MyComponent, HTMLStencilElement {
}
var HTMLMyComponentElement: {
prototype: HTMLMyComponentElement;
new (): HTMLMyComponentElement;
};
interface HTMLElementTagNameMap {
"my-component": HTMLMyComponentElement;
}
}
declare namespace LocalJSX {
/**
* docs
*/
interface MyComponent {
"name"?: UserImplementedPropType;
}
interface IntrinsicElements {
"my-component": MyComponent;
}
}
export { LocalJSX as JSX };
declare module "@stencil/core" {
export namespace JSX {
interface IntrinsicElements {
/**
* docs
*/
"my-component": LocalJSX.MyComponent & JSXBase.HTMLAttributes<HTMLMyComponentElement>;
}
}
}
`,
{
immediateWrite: true,
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/types/tests/stencil-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe('stencil-types', () => {
[typePath]: [
{
localName: 'SomeOtherType',
originalName: 'SomeOtherType',
},
],
});
Expand Down Expand Up @@ -176,6 +177,7 @@ describe('stencil-types', () => {
[typePath]: [
{
localName: initialType,
originalName: initialType,
importName: expectedType,
},
],
Expand All @@ -198,6 +200,7 @@ describe('stencil-types', () => {
const typeMemberNames = [
{
localName: 'SomeType',
originalName: 'SomeType',
importName: 'SomeOtherType',
},
];
Expand All @@ -211,6 +214,7 @@ describe('stencil-types', () => {
const typeMemberNames = [
{
localName: 'Ar',
originalName: 'Ar',
importName: 'Ar1',
},
];
Expand All @@ -224,6 +228,7 @@ describe('stencil-types', () => {
const typeMemberNames = [
{
localName: 'Ar',
originalName: 'Ar',
importName: 'Ar1',
},
];
Expand All @@ -237,10 +242,12 @@ describe('stencil-types', () => {
const typeMemberNames = [
{
localName: 'SomeType',
originalName: 'SomeType',
importName: 'SomeType1',
},
{
localName: 'AnotherType',
originalName: 'AnotherType',
importName: 'AnotherType1',
},
];
Expand All @@ -254,6 +261,7 @@ describe('stencil-types', () => {
const typeMemberNames = [
{
localName: 'SomeType',
originalName: 'SomeType',
importName: 'SomeType1',
},
];
Expand All @@ -279,6 +287,7 @@ describe('stencil-types', () => {
const typeMemberNames = [
{
localName: 'SomeType',
originalName: 'SomeType',
importName: 'SomeType1',
},
];
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types/update-import-refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ const updateImportReferenceFactory = (

const newTypeName = getIncrementTypeName(typeName);
existingTypeImportData[importResolvedFile].push({
// Since we create a unique ID for each type for documentation generation purposes, we can parse
// that ID to get the original name for the export
originalName: typeReference.id.split('::').pop(),
localName: typeName,
importName: newTypeName,
});
Expand Down
9 changes: 9 additions & 0 deletions src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,15 @@ export interface TypesImportData {
* as generating `components.d.ts` files.
*/
export interface TypesMemberNameData {
/**
* The original name of the import before any aliasing was applied.
*
* i.e. if a component imports a type as follows:
* `import { MyType as MyCoolType } from './my-type';`
*
* the `originalName` would be 'MyType'. If the import is not aliased, then `originalName` and `localName` will be the same.
*/
originalName: string;
/**
* The name of the type as it's used within a file.
*/
Expand Down