@@ -4,7 +4,9 @@ import { assert } from "Shared/util/assert";
44import { DiagnosticService } from "TSTransformer/classes/DiagnosticService" ;
55import { TransformState } from "TSTransformer/classes/TransformState" ;
66import { MacroList , PropertyCallMacro } from "TSTransformer/macros/types" ;
7+ import { isUnityObjectType } from "TSTransformer/util/airshipBehaviourUtils" ;
78import { convertToIndexableExpression } from "TSTransformer/util/convertToIndexableExpression" ;
9+ import { isAirshipBehaviourType , isAirshipBehaviourTypeNode } from "TSTransformer/util/extendsAirshipBehaviour" ;
810import { isUsedAsStatement } from "TSTransformer/util/isUsedAsStatement" ;
911import { offset } from "TSTransformer/util/offset" ;
1012import { isDefinitelyType , isNumberType , isStringType } from "TSTransformer/util/types" ;
@@ -933,6 +935,44 @@ const PROMISE_METHODS: MacroList<PropertyCallMacro> = {
933935 } ) ,
934936} ;
935937
938+ const expectAirshipComponentGeneric = ( propertyCallMacro : PropertyCallMacro , index : 0 = 0 ) : PropertyCallMacro => {
939+ return ( state , node , expression , args ) => {
940+ if ( node . typeArguments ) {
941+ const typeNode = node . typeArguments [ index ] ;
942+ if ( ! isAirshipBehaviourTypeNode ( state , typeNode ) ) {
943+ DiagnosticService . addDiagnostic (
944+ errors . unityMacroExpectsAirshipComponentTypeArgument (
945+ node ,
946+ state . typeChecker . typeToString ( state . typeChecker . getTypeFromTypeNode ( node . typeArguments [ 0 ] ) ) ,
947+ isUnityObjectType ( state , state . getType ( typeNode ) ) ,
948+ ) ,
949+ ) ;
950+ }
951+ }
952+
953+ return propertyCallMacro ( state , node , expression , args ) ;
954+ } ;
955+ } ;
956+
957+ const expectUnityComponentGeneric = ( propertyCallMacro : PropertyCallMacro , index : 0 = 0 ) : PropertyCallMacro => {
958+ return ( state , node , expression , args ) => {
959+ if ( node . typeArguments ) {
960+ const type = state . getType ( node . typeArguments [ index ] ) ;
961+ if ( ! isUnityObjectType ( state , type ) ) {
962+ DiagnosticService . addDiagnostic (
963+ errors . unityMacroExpectsComponentTypeArgument (
964+ node ,
965+ state . typeChecker . typeToString ( state . typeChecker . getTypeFromTypeNode ( node . typeArguments [ 0 ] ) ) ,
966+ isAirshipBehaviourType ( state , type , true ) ,
967+ ) ,
968+ ) ;
969+ }
970+ }
971+
972+ return propertyCallMacro ( state , node , expression , args ) ;
973+ } ;
974+ } ;
975+
936976const makeTypeArgumentAsStringMacro =
937977 ( method : string , requiresArgument = true , defaultTypeName ?: string ) : PropertyCallMacro =>
938978 ( state , node , expression , args ) => {
@@ -971,21 +1011,34 @@ const makeTypeArgumentAsStringMacro =
9711011 }
9721012 } ;
9731013
974- const UNITY_GAMEOBJECT_METHODS : MacroList < PropertyCallMacro > = {
1014+ const COMPONENT_MACROS : MacroList < PropertyCallMacro > = {
9751015 GetComponent : makeTypeArgumentAsStringMacro ( "GetComponent" ) ,
976- GetAirshipComponent : makeTypeArgumentAsStringMacro ( "GetAirshipComponent" ) ,
9771016 GetComponents : makeTypeArgumentAsStringMacro ( "GetComponents" ) ,
978- GetAirshipComponents : makeTypeArgumentAsStringMacro ( "GetAirshipComponents" ) ,
9791017 AddComponent : makeTypeArgumentAsStringMacro ( "AddComponent" ) ,
1018+ GetComponentInChildren : makeTypeArgumentAsStringMacro ( "GetComponentInChildren" ) ,
1019+ GetComponentsInChildren : makeTypeArgumentAsStringMacro ( "GetComponentsInChildren" ) ,
9801020 GetComponentInParent : makeTypeArgumentAsStringMacro ( "GetComponentInParent" ) ,
1021+ } ;
1022+
1023+ const AIRSHIP_COMPONENT_MACROS : MacroList < PropertyCallMacro > = {
1024+ GetAirshipComponent : makeTypeArgumentAsStringMacro ( "GetAirshipComponent" ) ,
1025+ GetAirshipComponents : makeTypeArgumentAsStringMacro ( "GetAirshipComponents" ) ,
9811026 AddAirshipComponent : makeTypeArgumentAsStringMacro ( "AddAirshipComponent" ) ,
982- GetComponentsInChildren : makeTypeArgumentAsStringMacro ( "GetComponentsInChildren" ) ,
9831027 GetAirshipComponentsInChildren : makeTypeArgumentAsStringMacro ( "GetAirshipComponentsInChildren" ) ,
984- GetComponentInChildren : makeTypeArgumentAsStringMacro ( "GetComponentInChildren" ) ,
9851028 GetAirshipComponentInChildren : makeTypeArgumentAsStringMacro ( "GetAirshipComponentInChildren" ) ,
9861029 GetAirshipComponentInParent : makeTypeArgumentAsStringMacro ( "GetAirshipComponentInParent" ) ,
9871030 GetAirshipComponentsInParent : makeTypeArgumentAsStringMacro ( "GetAirshipComponentsInParent" ) ,
9881031} ;
1032+
1033+ const UNITY_GAMEOBJECT_METHODS : MacroList < PropertyCallMacro > = { } ;
1034+
1035+ for ( const [ macro , call ] of Object . entries ( AIRSHIP_COMPONENT_MACROS ) ) {
1036+ UNITY_GAMEOBJECT_METHODS [ macro ] = expectAirshipComponentGeneric ( call ) ;
1037+ }
1038+ for ( const [ macro , call ] of Object . entries ( COMPONENT_MACROS ) ) {
1039+ UNITY_GAMEOBJECT_METHODS [ macro ] = expectUnityComponentGeneric ( call ) ;
1040+ }
1041+
9891042const UNITY_STATIC_GAMEOBJECT_METHODS : MacroList < PropertyCallMacro > = {
9901043 FindObjectOfType : makeTypeArgumentAsStringMacro ( "FindObjectOfType" ) ,
9911044 FindObjectsByType : makeTypeArgumentAsStringMacro ( "FindObjectsByType" ) ,
0 commit comments