-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): generate component custom event types with HTML target (
#3296) with this commit, the compiler will generate new exported interfaces in `components.d.ts` for all web components that have custom events. the generated interfaces include the typings to the generated HTML element type for the event target and accepts a generic for the custom event detail. the compiler will generate the web component custom event type with the new type, allowing usage with the custom event and the generated HTML element type for the target.
- Loading branch information
1 parent
2f8a6c0
commit 846740f
Showing
14 changed files
with
307 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type * as d from '../../declarations'; | ||
import { dashToPascalCase } from '@utils'; | ||
|
||
/** | ||
* Generates the custom event interface for each component that combines the `CustomEvent` interface with | ||
* the HTMLElement target. This is used to allow implementers to use strict typings on event handlers. | ||
* | ||
* The generated interface accepts a generic for the event detail type. This allows implementers to use | ||
* custom typings for individual events without Stencil needing to generate an interface for each event. | ||
* | ||
* @param cmp The component compiler metadata | ||
* @returns The generated interface type definition. | ||
*/ | ||
export const generateEventDetailTypes = (cmp: d.ComponentCompilerMeta): d.TypesModule => { | ||
const tagName = cmp.tagName.toLowerCase(); | ||
const tagNameAsPascal = dashToPascalCase(tagName); | ||
const htmlElementName = `HTML${tagNameAsPascal}Element`; | ||
|
||
const isDep = cmp.isCollectionDependency; | ||
|
||
const cmpEventInterface = `${tagNameAsPascal}CustomEvent`; | ||
const cmpInterface = [ | ||
`export interface ${cmpEventInterface}<T> extends CustomEvent<T> {`, | ||
` detail: T;`, | ||
` target: ${htmlElementName};`, | ||
`}`, | ||
]; | ||
return { | ||
isDep, | ||
tagName, | ||
tagNameAsPascal, | ||
htmlElementName, | ||
component: cmpInterface.join('\n'), | ||
jsx: cmpInterface.join('\n'), | ||
element: cmpInterface.join('\n'), | ||
}; | ||
}; |
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
32 changes: 32 additions & 0 deletions
32
src/compiler/types/tests/generate-event-detail-types.spec.ts
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,32 @@ | ||
import type * as d from '../../../declarations'; | ||
import { stubComponentCompilerMeta } from './ComponentCompilerMeta.stub'; | ||
import { generateEventDetailTypes } from '../generate-event-detail-types'; | ||
|
||
describe('generate-event-detail-types', () => { | ||
describe('generateEventDetailTypes', () => { | ||
it('returns the correct type module data for a component', () => { | ||
const tagName = 'event-detail-test-tag'; | ||
const tagNameAsPascal = 'EventDetailTestTag'; | ||
|
||
const expectedTypeInfo = `export interface ${tagNameAsPascal}CustomEvent<T> extends CustomEvent<T> { | ||
detail: T; | ||
target: HTML${tagNameAsPascal}Element; | ||
}`; | ||
const componentMeta = stubComponentCompilerMeta({ | ||
tagName, | ||
}); | ||
|
||
const actualEventDetailTypes = generateEventDetailTypes(componentMeta); | ||
|
||
expect(actualEventDetailTypes).toEqual<d.TypesModule>({ | ||
component: expectedTypeInfo, | ||
element: expectedTypeInfo, | ||
htmlElementName: `HTML${tagNameAsPascal}Element`, | ||
isDep: false, | ||
jsx: expectedTypeInfo, | ||
tagName, | ||
tagNameAsPascal, | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.