Skip to content

Commit

Permalink
feat: add class generic type param in manifest (#3292)
Browse files Browse the repository at this point in the history
The datepicker and the calendar components use generic type parameter to
allow custom implementation of Date, eg.

```
export class SbbDatepickerElement<T = Date> {
    private _min?: T | null;
    // and so on
}
```

This needs to be taken in account in Angular classes generation, so a
new param has been added to the manifest during its generation, eg.

```
{
    "kind": "javascript-module",
    "path": "datepicker/datepicker.js",
    "declarations": [
      {
          "kind": "class",
          "description": "Combined with a native input, it displays the input's value as a formatted date.",
          "name": "SbbDatepickerElement",
          "members": [],
          "events": [],
          "attributes": [],
          "superclass": {},
          "classGenerics": "T = Date",        <--------------------------------- new property
          "tagName": "sbb-datepicker",
          "customElement": true
        }
    ],
    exports: []
```
  • Loading branch information
DavideMininni-Fincons authored and github-actions committed Dec 12, 2024
1 parent 1d64853 commit 3d872c1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions tools/manifest/custom-elements-manifest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const overrideTypeKey = 'overrideType';
const classGenericsTypeKey = 'classGenerics';

/**
* Docs: https://custom-elements-manifest.open-wc.org/analyzer/getting-started/
Expand Down Expand Up @@ -96,6 +97,19 @@ export function createManifestConfig(library = '') {
}

if (ts.isClassDeclaration(node)) {
const classDeclaration = moduleDoc.declarations.find(
(declaration) => declaration.name === node.name.getText(),
);

/**
* If the class uses a generic type parameter, add it to the class declaration.
* It will be used in the Angular wrapper to correctly generate classes.
* Mainly used for datepicker and calendar components.
*/
if (node.typeParameters && node.typeParameters.length > 0) {
classDeclaration[classGenericsTypeKey] = node.typeParameters[0].getText();
}

/**
* When a generic T type is used in a superclass declaration, it overrides the type defined in derived class
* during the doc generation (as the `value` property in the `SbbFormAssociatedMixinType`).
Expand All @@ -109,9 +123,6 @@ export function createManifestConfig(library = '') {
// eslint-disable-next-line lyne/local-name-rule
if (tag.tagName.getText() === overrideTypeKey) {
const [memberName, memberOverrideType] = tag.comment.split(' - ');
const classDeclaration = moduleDoc.declarations.find(
(declaration) => declaration.name === node.name.getText(),
);
if (!classDeclaration[overrideTypeKey]) {
classDeclaration[overrideTypeKey] = [{ memberName, memberOverrideType }];
} else {
Expand Down

0 comments on commit 3d872c1

Please sign in to comment.