Skip to content

Commit 48ac2d2

Browse files
committed
Include inherited class decorators
1 parent 52bdea7 commit 48ac2d2

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Shared/types.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ProjectType } from "Shared/constants";
2-
import ts from "typescript";
2+
import ts, { convertToBase64 } from "typescript";
33

44
export interface PackageJson {
55
name: string;
@@ -305,6 +305,25 @@ export interface AirshipBehaviourClassDecorator {
305305
*/
306306
readonly parameters: ReadonlyArray<AirshipBehaviourFieldDecoratorParameter> | undefined;
307307
}
308+
export const AirshipBehaviourClassDecorator = {
309+
getId(this: void, value: AirshipBehaviourClassDecorator) {
310+
if (value.parameters) {
311+
return convertToBase64(
312+
`${value.name}(${value.parameters
313+
.map(v => {
314+
if (v.type === "string") {
315+
return `"${v.value}"`;
316+
} else {
317+
return v.value;
318+
}
319+
})
320+
.join(", ")})`,
321+
);
322+
} else {
323+
return `${value.name}`;
324+
}
325+
},
326+
};
308327

309328
export interface AirshipBehaviourInfo {
310329
readonly filePath: string;

src/TSTransformer/nodes/class/transformClassLikeDeclaration.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,26 @@ export function getClassDecorators(state: TransformState, classNode: ts.ClassLik
727727
return items;
728728
}
729729

730+
const onlyOneOf = ["AirshipComponentMenu", "AirshipComponentIcon"];
731+
function pushOrReplaceDecorator(
732+
decorators: Array<AirshipBehaviourClassDecorator>,
733+
decorator: AirshipBehaviourClassDecorator,
734+
) {
735+
if (onlyOneOf.includes(decorator.name)) {
736+
const existing = decorators.findIndex(f => f.name === decorator.name);
737+
if (existing != -1) {
738+
decorators[existing] = decorator;
739+
} else {
740+
decorators.push(decorator);
741+
}
742+
} else {
743+
const decoratorId = AirshipBehaviourClassDecorator.getId(decorator);
744+
if (!decorators.some(value => decoratorId === AirshipBehaviourClassDecorator.getId(value))) {
745+
decorators.push(decorator);
746+
}
747+
}
748+
}
749+
730750
function getPropertyDecorators(
731751
state: TransformState,
732752
propertyNode: ts.PropertyDeclaration,
@@ -840,6 +860,8 @@ function generateMetaForAirshipBehaviour(state: TransformState, node: ts.ClassLi
840860

841861
// Inheritance
842862
const inheritance = getAncestorTypeSymbols(classType, state.typeChecker);
863+
const classDecorators = new Array<AirshipBehaviourClassDecorator>();
864+
843865
for (const inherited of inheritance.reverse()) {
844866
const valueDeclaration = inherited.valueDeclaration;
845867
if (!valueDeclaration) continue;
@@ -858,11 +880,20 @@ function generateMetaForAirshipBehaviour(state: TransformState, node: ts.ClassLi
858880
}
859881

860882
inheritedBehaviourIds.push(name);
883+
884+
const inheritedClassDecorators = getClassDecorators(state, valueDeclaration);
885+
for (const decorator of inheritedClassDecorators) {
886+
pushOrReplaceDecorator(classDecorators, decorator);
887+
}
888+
}
889+
890+
for (const decorator of getClassDecorators(state, node)) {
891+
pushOrReplaceDecorator(classDecorators, decorator);
861892
}
862893

863894
pushPropertyMetadataForAirshipBehaviour(state, node, metadata);
864895

865-
const classDecorators = getClassDecorators(state, node);
896+
// const classDecorators = getClassDecorators(state, node);
866897
if (classDecorators.length > 0 && !state.data.isPublishing) metadata.decorators = classDecorators;
867898

868899
const sha1 = crypto.createHash("sha1");

0 commit comments

Comments
 (0)