@@ -6,6 +6,22 @@ import { getExtendsNode } from "TSTransformer/util/getExtendsNode";
66import { getOriginalSymbolOfNode } from "TSTransformer/util/getOriginalSymbolOfNode" ;
77import ts from "typescript" ;
88
9+ export function isRootAirshipBehaviourClassNoState (
10+ singletonSymbol : ts . Symbol ,
11+ typeChecker : ts . TypeChecker ,
12+ node : ts . ClassLikeDeclaration ,
13+ ) {
14+ const extendsNode = getExtendsNode ( node ) ;
15+ if ( extendsNode ) {
16+ const symbol = getOriginalSymbolOfNode ( typeChecker , extendsNode . expression ) ;
17+ if ( symbol === singletonSymbol ) {
18+ return true ;
19+ }
20+ }
21+
22+ return false ;
23+ }
24+
925export function isRootAirshipBehaviourClass ( state : TransformState , node : ts . ClassLikeDeclaration ) {
1026 const extendsNode = getExtendsNode ( node ) ;
1127 if ( extendsNode ) {
@@ -25,10 +41,11 @@ export function isRootAirshipSingletonClass(state: TransformState, node: ts.Clas
2541 if ( extendsNode ) {
2642 const airshipSingletonSymbol = state . services . airshipSymbolManager . getAirshipSingletonSymbolOrThrow ( ) ;
2743
28- const symbol = getOriginalSymbolOfNode ( state . typeChecker , extendsNode . expression ) ;
29- if ( symbol === airshipSingletonSymbol ) {
30- return true ;
31- }
44+ return isRootAirshipBehaviourClassNoState ( airshipSingletonSymbol , state . typeChecker , node ) ;
45+ // const symbol = getOriginalSymbolOfNode(state.typeChecker, extendsNode.expression);
46+ // if (symbol === airshipSingletonSymbol) {
47+ // return true;
48+ // }
3249 }
3350
3451 return false ;
@@ -86,7 +103,7 @@ export function isAirshipSingletonClassNoState(
86103 }
87104
88105 // Get the root inheriting symbol (Should match AirshipBehaviour for this to be "extending" AirshipBehaviour)
89- const baseTypeDeclaration = inheritance [ inheritance . length - 1 ] ;
106+ const baseTypeDeclaration = inheritance [ inheritance . length - 2 ] ?? inheritance [ inheritance . length - 1 ] ;
90107 if ( baseTypeDeclaration !== undefined ) {
91108 return baseTypeDeclaration === airshipBehaviourSymbol ;
92109 }
@@ -102,6 +119,7 @@ export function isAirshipSingletonClass(state: TransformState, node: ts.ClassLik
102119
103120 // check if the immediate extends is AirshipBehaviour
104121 let type = state . typeChecker . getTypeAtLocation ( node ) ;
122+
105123 if ( type . isNullableType ( ) ) {
106124 type = type . getNonNullableType ( ) ;
107125 }
@@ -111,17 +129,11 @@ export function isAirshipSingletonClass(state: TransformState, node: ts.ClassLik
111129 return true ;
112130 }
113131
114- // Get the inheritance tree, otherwise
115- const inheritance = getAncestorTypeSymbols ( type , state . typeChecker ) ;
116- if ( inheritance . length === 0 ) {
117- return false ;
118- }
132+ const extendsClasses = getTypesOfClasses ( state . typeChecker , getExtendsClasses ( state . typeChecker , node ) ) ;
133+ if ( extendsClasses . length === 0 ) return false ;
119134
120- // Get the root inheriting symbol (Should match AirshipBehaviour for this to be "extending" AirshipBehaviour)
121- const baseTypeDeclaration = inheritance [ inheritance . length - 1 ] ;
122- if ( baseTypeDeclaration !== undefined ) {
123- return baseTypeDeclaration === airshipBehaviourSymbol ;
124- }
135+ const baseClass = extendsClasses [ extendsClasses . length - 2 ] ?? extendsClasses [ extendsClasses . length - 1 ] ;
136+ return baseClass . symbol === airshipBehaviourSymbol ;
125137 }
126138
127139 return false ;
@@ -167,6 +179,23 @@ export function isAirshipBehaviourType(state: TransformState, type: ts.Type) {
167179 }
168180}
169181
182+ export const enum SingletonQueryType {
183+ IsRootSingleton ,
184+ IsAnySingleton ,
185+ }
186+
187+ export function isClassInheritingSymbol ( state : TransformState , node : ts . ClassLikeDeclaration , symbol : ts . Symbol ) {
188+ const type = state . typeChecker . getTypeAtLocation ( node ) ;
189+
190+ // Get the inheritance tree, otherwise
191+ const inheritance = getAncestorTypeSymbols ( type , state . typeChecker ) ;
192+ if ( inheritance . length === 0 ) {
193+ return false ;
194+ }
195+
196+ return inheritance . some ( value => value === symbol ) ;
197+ }
198+
170199export function isAirshipSingletonType ( state : TransformState , type : ts . Type ) {
171200 const airshipBehaviourSymbol = state . services . airshipSymbolManager . getAirshipSingletonSymbolOrThrow ( ) ;
172201
0 commit comments