|
| 1 | +import luau from "@roblox-ts/luau-ast"; |
| 2 | +import { errors, warnings } from "Shared/diagnostics"; |
| 3 | +import { DiagnosticService } from "TSTransformer/classes/DiagnosticService"; |
| 4 | +import { MacroList, PropertyCallMacro } from "TSTransformer/macros/types"; |
| 5 | +import { isUnityObjectType } from "TSTransformer/util/airshipBehaviourUtils"; |
| 6 | +import { convertToIndexableExpression } from "TSTransformer/util/convertToIndexableExpression"; |
| 7 | +import { isAirshipBehaviourType, isAirshipBehaviourTypeNode } from "TSTransformer/util/extendsAirshipBehaviour"; |
| 8 | +import ts from "typescript"; |
| 9 | + |
| 10 | +const expectAirshipComponentGeneric = ( |
| 11 | + name: string, |
| 12 | + propertyCallMacro: PropertyCallMacro, |
| 13 | + index: 0 = 0, |
| 14 | +): PropertyCallMacro => { |
| 15 | + return (state, node, expression, args) => { |
| 16 | + if (node.typeArguments) { |
| 17 | + const typeNode = node.typeArguments[index]; |
| 18 | + if (!isAirshipBehaviourTypeNode(state, typeNode)) { |
| 19 | + DiagnosticService.addDiagnostic( |
| 20 | + errors.unityMacroExpectsAirshipComponentTypeArgument( |
| 21 | + node, |
| 22 | + state.typeChecker.typeToString(state.typeChecker.getTypeFromTypeNode(node.typeArguments[0])), |
| 23 | + getAirshipMacroAlternativeName(propertyCallMacro)!, |
| 24 | + isUnityObjectType(state, state.getType(typeNode)), |
| 25 | + ), |
| 26 | + ); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return propertyCallMacro(state, node, expression, args); |
| 31 | + }; |
| 32 | +}; |
| 33 | + |
| 34 | +const expectUnityComponentGeneric = ( |
| 35 | + name: string, |
| 36 | + propertyCallMacro: PropertyCallMacro, |
| 37 | + index: 0 = 0, |
| 38 | +): PropertyCallMacro => { |
| 39 | + return (state, node, expression, args) => { |
| 40 | + if (node.typeArguments) { |
| 41 | + const type = state.getType(node.typeArguments[index]); |
| 42 | + if (!isUnityObjectType(state, type)) { |
| 43 | + DiagnosticService.addDiagnostic( |
| 44 | + errors.unityMacroExpectsComponentTypeArgument( |
| 45 | + node, |
| 46 | + state.typeChecker.typeToString(state.typeChecker.getTypeFromTypeNode(node.typeArguments[0])), |
| 47 | + getAirshipMacroAlternativeName(propertyCallMacro)!, |
| 48 | + isAirshipBehaviourType(state, type, true), |
| 49 | + ), |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return propertyCallMacro(state, node, expression, args); |
| 55 | + }; |
| 56 | +}; |
| 57 | + |
| 58 | +const makeTypeArgumentAsStringMacro = |
| 59 | + (method: string, requiresArgument = true, defaultTypeName?: string): PropertyCallMacro => |
| 60 | + (state, node, expression, args) => { |
| 61 | + let type: ts.Type | undefined; |
| 62 | + |
| 63 | + if (node.typeArguments) { |
| 64 | + type = state.getType(node.typeArguments[0]); |
| 65 | + } else if (ts.isAsExpression(node.parent)) { |
| 66 | + type = state.getType(node.parent.type); |
| 67 | + DiagnosticService.addDiagnostic( |
| 68 | + warnings.unityMacroAsExpressionWarning(method, state.typeChecker.typeToString(type))(node.parent), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + if (requiresArgument && !defaultTypeName && !type && args.length === 0) { |
| 73 | + DiagnosticService.addSingleDiagnostic(errors.unityMacroTypeArgumentRequired(node, method)); |
| 74 | + } |
| 75 | + |
| 76 | + if (type) { |
| 77 | + args.unshift(luau.string(state.typeChecker.typeToString(type))); |
| 78 | + return luau.create(luau.SyntaxKind.MethodCallExpression, { |
| 79 | + expression: convertToIndexableExpression(expression), |
| 80 | + name: method, |
| 81 | + args: luau.list.make(...args), |
| 82 | + }); |
| 83 | + } else { |
| 84 | + if (defaultTypeName !== undefined) { |
| 85 | + args.unshift(luau.string(defaultTypeName)); |
| 86 | + } |
| 87 | + |
| 88 | + return luau.create(luau.SyntaxKind.MethodCallExpression, { |
| 89 | + expression: convertToIndexableExpression(expression), |
| 90 | + name: method, |
| 91 | + args: luau.list.make(...args), |
| 92 | + }); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | +const COMPONENT_MACROS = { |
| 97 | + GetComponent: makeTypeArgumentAsStringMacro("GetComponent"), |
| 98 | + GetComponents: makeTypeArgumentAsStringMacro("GetComponents"), |
| 99 | + AddComponent: makeTypeArgumentAsStringMacro("AddComponent"), |
| 100 | + GetComponentInChildren: makeTypeArgumentAsStringMacro("GetComponentInChildren"), |
| 101 | + GetComponentsInChildren: makeTypeArgumentAsStringMacro("GetComponentsInChildren"), |
| 102 | + GetComponentInParent: makeTypeArgumentAsStringMacro("GetComponentInParent"), |
| 103 | + GetComponentsInParent: makeTypeArgumentAsStringMacro("GetComponentsInParent"), |
| 104 | +} satisfies MacroList<PropertyCallMacro>; |
| 105 | + |
| 106 | +const AIRSHIP_COMPONENT_MACROS = { |
| 107 | + GetAirshipComponent: makeTypeArgumentAsStringMacro("GetAirshipComponent"), |
| 108 | + GetAirshipComponents: makeTypeArgumentAsStringMacro("GetAirshipComponents"), |
| 109 | + AddAirshipComponent: makeTypeArgumentAsStringMacro("AddAirshipComponent"), |
| 110 | + GetAirshipComponentsInChildren: makeTypeArgumentAsStringMacro("GetAirshipComponentsInChildren"), |
| 111 | + GetAirshipComponentInChildren: makeTypeArgumentAsStringMacro("GetAirshipComponentInChildren"), |
| 112 | + GetAirshipComponentInParent: makeTypeArgumentAsStringMacro("GetAirshipComponentInParent"), |
| 113 | + GetAirshipComponentsInParent: makeTypeArgumentAsStringMacro("GetAirshipComponentsInParent"), |
| 114 | +} satisfies MacroList<PropertyCallMacro>; |
| 115 | + |
| 116 | +const ALTERNATIVE_NAMES: ReadonlyArray< |
| 117 | + [airship: PropertyCallMacro, unityName: keyof typeof COMPONENT_MACROS | keyof typeof AIRSHIP_COMPONENT_MACROS] |
| 118 | +> = [ |
| 119 | + [COMPONENT_MACROS.AddComponent, "AddAirshipComponent"], |
| 120 | + [COMPONENT_MACROS.GetComponent, "GetAirshipComponent"], |
| 121 | + [COMPONENT_MACROS.GetComponentInChildren, "GetAirshipComponentInChildren"], |
| 122 | + [COMPONENT_MACROS.GetComponentInParent, "GetAirshipComponentInParent"], |
| 123 | + [COMPONENT_MACROS.GetComponents, "GetAirshipComponents"], |
| 124 | + [COMPONENT_MACROS.GetComponentsInChildren, "GetAirshipComponentsInChildren"], |
| 125 | + [COMPONENT_MACROS.GetComponentsInParent, "GetAirshipComponentsInParent"], |
| 126 | + |
| 127 | + [AIRSHIP_COMPONENT_MACROS.AddAirshipComponent, "AddComponent"], |
| 128 | + [AIRSHIP_COMPONENT_MACROS.GetAirshipComponent, "GetComponent"], |
| 129 | + [AIRSHIP_COMPONENT_MACROS.GetAirshipComponentInChildren, "GetComponentInChildren"], |
| 130 | + [AIRSHIP_COMPONENT_MACROS.GetAirshipComponentInParent, "GetComponentInParent"], |
| 131 | + [AIRSHIP_COMPONENT_MACROS.GetAirshipComponents, "GetComponents"], |
| 132 | + [AIRSHIP_COMPONENT_MACROS.GetAirshipComponentsInChildren, "GetComponentsInChildren"], |
| 133 | + [AIRSHIP_COMPONENT_MACROS.GetAirshipComponentsInParent, "GetComponentsInParent"], |
| 134 | +]; |
| 135 | + |
| 136 | +export const UNITY_GAMEOBJECT_METHODS: MacroList<PropertyCallMacro> = {}; |
| 137 | +for (const [macro, call] of Object.entries(AIRSHIP_COMPONENT_MACROS)) { |
| 138 | + UNITY_GAMEOBJECT_METHODS[macro] = expectAirshipComponentGeneric(macro, call); |
| 139 | +} |
| 140 | +for (const [macro, call] of Object.entries(COMPONENT_MACROS)) { |
| 141 | + UNITY_GAMEOBJECT_METHODS[macro] = expectUnityComponentGeneric(macro, call); |
| 142 | +} |
| 143 | + |
| 144 | +export const UNITY_STATIC_GAMEOBJECT_METHODS: MacroList<PropertyCallMacro> = { |
| 145 | + FindObjectOfType: makeTypeArgumentAsStringMacro("FindObjectOfType"), |
| 146 | + FindObjectsByType: makeTypeArgumentAsStringMacro("FindObjectsByType"), |
| 147 | +}; |
| 148 | +export const UNITY_COMPONENT_METHODS: MacroList<PropertyCallMacro> = { |
| 149 | + GetComponent: COMPONENT_MACROS.GetComponent, |
| 150 | + GetComponents: COMPONENT_MACROS.GetComponents, |
| 151 | +}; |
| 152 | +export const UNITY_OBJECT_METHODS: MacroList<PropertyCallMacro> = { |
| 153 | + IsA: makeTypeArgumentAsStringMacro("IsA"), |
| 154 | +}; |
| 155 | + |
| 156 | +function getAirshipMacroAlternativeName(propertyMacro: PropertyCallMacro): string | undefined { |
| 157 | + return ALTERNATIVE_NAMES.find(f => f[0] === propertyMacro)?.[1]; |
| 158 | +} |
0 commit comments