-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types): components.d.ts type resolution for duplicate types (#3337)
this commit updates the type resolution of types used by components when generating a `components.d.ts` file. types that have been deduplicated are now piped to functions used to generate the types for class members that are decorated with `@Prop()`, `@Event()`, and `@Method()`, and used when generating the types for each.
- Loading branch information
1 parent
ed1de3c
commit 31eae6e
Showing
11 changed files
with
627 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,43 @@ | ||
import type * as d from '../../declarations'; | ||
import { getTextDocs } from '@utils'; | ||
import { updateTypeIdentifierNames } from './stencil-types'; | ||
|
||
/** | ||
* Generates the individual event types for all @Method() decorated events in a component | ||
* @param cmpMeta component runtime metadata for a single component | ||
* @param typeImportData locally/imported/globally used type names, which may be used to prevent naming collisions | ||
* @returns the generated type metadata | ||
*/ | ||
export const generateMethodTypes = (cmpMeta: d.ComponentCompilerMeta): d.TypeInfo => { | ||
export const generateMethodTypes = ( | ||
cmpMeta: d.ComponentCompilerMeta, | ||
typeImportData: d.TypesImportData | ||
): d.TypeInfo => { | ||
return cmpMeta.methods.map((cmpMethod) => ({ | ||
name: cmpMethod.name, | ||
type: cmpMethod.complexType.signature, | ||
type: getType(cmpMethod, typeImportData, cmpMeta.sourceFilePath), | ||
optional: false, | ||
required: false, | ||
internal: cmpMethod.internal, | ||
jsdoc: getTextDocs(cmpMethod.docs), | ||
})); | ||
}; | ||
|
||
/** | ||
* Determine the correct type name for all type(s) used by a class member annotated with `@Method()` | ||
* @param cmpMethod the compiler metadata for a single `@Method()` | ||
* @param typeImportData locally/imported/globally used type names, which may be used to prevent naming collisions | ||
* @param componentSourcePath the path to the component on disk | ||
* @returns the type associated with a `@Method()` | ||
*/ | ||
function getType( | ||
cmpMethod: d.ComponentCompilerMethod, | ||
typeImportData: d.TypesImportData, | ||
componentSourcePath: string | ||
): string { | ||
return updateTypeIdentifierNames( | ||
cmpMethod.complexType.references, | ||
typeImportData, | ||
componentSourcePath, | ||
cmpMethod.complexType.signature | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as d from '@stencil/core/declarations'; | ||
|
||
/** | ||
* Generates a stub {@link TypesImportData}. | ||
* @param overrides a partial implementation of `TypesImportData`. Any provided fields will override the defaults | ||
* provided by this function. | ||
* @returns the stubbed `TypesImportData` | ||
*/ | ||
export const stubTypesImportData = (overrides: Partial<d.TypesImportData> = {}): d.TypesImportData => { | ||
/** | ||
* By design, we do not provide any default values. the keys used in this data structure will be highly dependent on | ||
* the tests being written, and providing default values may lead to unexpected behavior when enumerating the returned | ||
* stub | ||
*/ | ||
const defaults: d.TypesImportData = {}; | ||
|
||
return { ...defaults, ...overrides }; | ||
}; |
Oops, something went wrong.