Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: serializable types and optional key for IDependencyInformation
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kloubert committed Jan 31, 2024
1 parent a7275ee commit dc61b65
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log (@egomobile/documentation)

## 0.3.0

- implement serializable types and `serializeDependencies()` utility function
- add `classOrInstance` prop to `IMethodDependencyItem`, `IParameterDependencyItem` and `IPropertyDependencyItem`
- add optional `key` to `IDependencyInformation`

## 0.2.0

- add `references` props to `IDependencyInformation`, `IDependencyInformationEntity` and `IDependencyInformationEntityAttribute`
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egomobile/documentation",
"version": "0.2.0",
"version": "0.3.0",
"description": "Tools for documenting (TypeScript) code.",
"main": "lib/index.js",
"engines": {
Expand Down
116 changes: 83 additions & 33 deletions src/decorators/DependsOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import type { ClassPropKey, Collection, Constructor, IArrayLike, Nilable, Optional, ReferenceValue } from "../types/internal";
import type { ClassOrInstance, ClassPropKey, Collection, Constructor, IArrayLike, Nilable, Optional, ReferenceValue } from "../types/internal";
import { isCollection } from "../utils/internal";

/**
Expand Down Expand Up @@ -101,6 +101,10 @@ export interface IDependencyInformation {
* Optional entities for this app.
*/
entities?: Nilable<DependencyInformationEntities>;
/**
* Optional unique key. Value should be handled case-insensitive.
*/
key?: Nilable<ReferenceValue>;
/**
* An optional list of references. Should be handled as URIs.
*/
Expand Down Expand Up @@ -209,6 +213,10 @@ export interface IDependencyItem {
* A `IDependencyItem` with information about a class method and its dependencies.
*/
export interface IMethodDependencyItem extends IDependencyItem {
/**
* The underlying class or constructor.
*/
classOrInstance: ClassOrInstance<any>;
/**
* The key / name.
*/
Expand All @@ -224,13 +232,17 @@ export interface IMethodDependencyItem extends IDependencyItem {
*/
export interface IParameterDependencyItem extends IDependencyItem {
/**
* The key / name, if available.
* The underlying class or constructor.
*/
key?: Optional<ClassPropKey>;
classOrInstance: ClassOrInstance<any>;
/**
* The zero-based index inside the method.
*/
index: number;
/**
* The key / name, if available.
*/
key?: Optional<ClassPropKey>;
/**
* The type.
*/
Expand All @@ -241,6 +253,10 @@ export interface IParameterDependencyItem extends IDependencyItem {
* A `IDependencyItem` with information about a class property and its dependencies.
*/
export interface IPropertyDependencyItem extends IDependencyItem {
/**
* The underlying class or constructor.
*/
classOrInstance: ClassOrInstance<any>;
/**
* The key / name.
*/
Expand Down Expand Up @@ -453,45 +469,79 @@ export function DependsOn(
return (...args: any[]) => {
const target: any = args[0];

if (typeof target === "function") {
const newClassItem: IClassDependencyItem = {
"constructor": target,
"type": "class"
const addAsMethod = () => {
const propertyKey: Optional<ClassPropKey> = args[1];

const newMethodItem: IMethodDependencyItem = {
"classOrInstance": target,
"key": propertyKey as ClassPropKey,
"type": "method"
};

addItem(newClassItem);
}
else {
addItem(newMethodItem);
};

const addAsParameter = () => {
const descriptorOrIndex: Optional<TypedPropertyDescriptor<any> | number> = args[2];
const propertyKey: Optional<ClassPropKey> = args[1];

const newParameterItem: IParameterDependencyItem = {
"classOrInstance": target,
"index": descriptorOrIndex as number,
"key": propertyKey,
"type": "parameter"
};

addItem(newParameterItem);
};

const addAsProperty = () => {
const propertyKey: Optional<ClassPropKey> = args[1];

const newPropertyItem: IPropertyDependencyItem = {
"classOrInstance": target,
"key": propertyKey as ClassPropKey,
"type": "property"
};

addItem(newPropertyItem);
};

const addWith3Args = () => {
const descriptorOrIndex: Optional<TypedPropertyDescriptor<any> | number> = args[2];

if (typeof descriptorOrIndex === "undefined") {
const newPropertyItem: IPropertyDependencyItem = {
"key": propertyKey as ClassPropKey,
"type": "property"
};

addItem(newPropertyItem);
addAsProperty();
}
else if (typeof descriptorOrIndex === "number") {
addAsParameter();
}
else {
if (typeof descriptorOrIndex === "number") {
const newParameterItem: IParameterDependencyItem = {
"index": descriptorOrIndex as number,
"key": propertyKey,
"type": "parameter"
};

addItem(newParameterItem);
}
else {
const newMethodItem: IMethodDependencyItem = {
"key": propertyKey as ClassPropKey,
"type": "method"
};

addItem(newMethodItem);
}
addAsMethod();
}
};

if (typeof target === "function") {
if (args.length === 3) {
// static member

addWith3Args();
}
else if (args.length === 1) {
// class

const newClassItem: IClassDependencyItem = {
"constructor": target,
"type": "class"
};

addItem(newClassItem);
}
}
else {
// instance member

addWith3Args();
}
};
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export * from "./decorators";
export * from "./types";
export * from "./utils";
116 changes: 116 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// This file is part of the @egomobile/documentation distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// @egomobile/documentation is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// @egomobile/documentation is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import type { IDependencyInformation } from "../decorators";
import type { Nullable } from "./internal";

/**
* A serizable version of an `IClassDependencyItem`.
*/
export interface ISerializableClassDependencyItemWithInfo extends ISerializableDependencyItemWithInfo {
/**
* The name of the class.
*/
name: string;
/**
* The type
*/
type: "class";
}

/**
* A serizable version of an `IDependencyItem`.
*/
export interface ISerializableDependencyItemWithInfo {
/**
* The underlying information.
*/
info: IDependencyInformation;
}

/**
* A serizable version of an `IMethodDependencyItem`.
*/
export interface ISerializableMethodDependencyItemWithInfo extends ISerializableDependencyItemWithInfo, IWithClassNameProp, IWithIsStaticProp {
/**
* The name of the method.
*/
name: string;
/**
* The type.
*/
type: "method";
}

/**
* A serizable version of an `IParameterDependencyItem`.
*/
export interface ISerializableParameterDependencyItemWithInfo extends ISerializableDependencyItemWithInfo, IWithClassNameProp, IWithIsStaticProp {
/**
* The zero-based index inside the method.
*/
index: number;
/**
* The name, if available.
*/
name: Nullable<string>;
/**
* The type.
*/
type: "parameter";
}

/**
* A serizable version of an `IPropertyDependencyItem`.
*/
export interface ISerializablePropertyDependencyItemWithInfo extends ISerializableDependencyItemWithInfo, IWithClassNameProp, IWithIsStaticProp {
/**
* The name of the property.
*/
name: string;
/**
* The type.
*/
type: "property";
}

/**
* An object with an `className` property.
*/
export interface IWithClassNameProp {
/**
* The name of the underlying class, if available.
*/
className: Nullable<string>;
}

/**
* An object with an `isStatic` property.
*/
export interface IWithIsStaticProp {
/**
* Is underlying type static or not.
*/
isStatic: boolean;
}

/**
* An serializable dependency info item.
*/
export type SerializableDependencyItem =
ISerializableClassDependencyItemWithInfo |
ISerializableMethodDependencyItemWithInfo |
ISerializableParameterDependencyItemWithInfo |
ISerializablePropertyDependencyItemWithInfo;
2 changes: 2 additions & 0 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export type ClassOrInstance<T extends any = Object> = T | Constructor<T>;

export type ClassPropKey = string | symbol;

export type Collection<T extends any = any> =
Expand Down
Loading

0 comments on commit dc61b65

Please sign in to comment.