@@ -6,7 +6,10 @@ import { SINGLETON_FILE_IMPORT } from "TSTransformer/classes/TransformState";
66import { PROPERTY_SETTERS } from "TSTransformer/macros/propertyMacros" ;
77import { MacroList , PropertyCallMacro } from "TSTransformer/macros/types" ;
88import { skipUpwards } from "TSTransformer/util/traversal" ;
9+ import { findAncestorNode } from "TSTransformer/util/visitParentNodes" ;
910import ts from "typescript" ;
11+ import { DiagnosticService } from "./DiagnosticService" ;
12+ import { warnings } from "Shared/diagnostics" ;
1013
1114function getType ( typeChecker : ts . TypeChecker , node : ts . Node ) {
1215 return typeChecker . getTypeAtLocation ( skipUpwards ( node ) ) ;
@@ -39,11 +42,29 @@ const AIRSHIP_SERIALIZE_TYPES = {
3942 AnimationCurve : "AnimationCurve" ,
4043} as const ;
4144
45+ function isValidSingletonFetchContext ( value : ts . Node ) {
46+ // Essentially anything that isn't ran during script setup
47+ return (
48+ ts . isMethodDeclaration ( value ) ||
49+ ts . isFunctionDeclaration ( value ) ||
50+ ts . isConstructorDeclaration ( value ) ||
51+ ts . isFunctionExpression ( value ) ||
52+ ts . isArrowFunction ( value ) ||
53+ ts . isPropertyDeclaration ( value )
54+ ) ;
55+ }
56+
4257export const AIRSHIP_SINGLETON_MACROS = {
4358 Get : ( state , node ) => {
4459 const importId = state . getOrAddFileImport ( SINGLETON_FILE_IMPORT , "SingletonRegistry" ) ;
4560 const Singletons_Resolve = luau . property ( importId , "Resolve" ) ;
4661
62+ const ancestor = findAncestorNode ( node , isValidSingletonFetchContext ) ;
63+
64+ if ( ancestor === undefined ) {
65+ DiagnosticService . addDiagnostic ( warnings . singletonGetPossibleYield ( node , node . getText ( ) ) ) ;
66+ }
67+
4768 const functionType = state . typeChecker . getTypeAtLocation ( node ) ;
4869 if ( functionType !== undefined ) {
4970 return luau . call ( Singletons_Resolve , [ luau . string ( state . typeChecker . typeToString ( functionType ) ) ] ) ;
0 commit comments