Typescript compiler, how to get an exported symbol by name? - typescript-compiler-api

What is the best way to get the symbol for an export by name?
Bellow is functional code. However, it does seems a bit fragile as I can't get the 'symbol' from 'sourceFile' without ignoring the type system.
const sourceFile = tsprogram.getSourceFile('foo_file.ts');
const fileSymbol = (sourceFile as any).symbol as ts.Symbol; // anything better her?
const export = fileSymbol.exports.get('FooComponent');

Use the type checker:
const fileSymbol = tsprogram.getTypeChecker().getSymbolAtLocation(sourceFile);
const fooComponentSymbol = fileSymbol?.exports.get('FooComponent');
Note that fileSymbol will be undefined when there are no file exports.

Related

Get apparent type of NewExpression using ts-morph

I am currently trying to get the type of such declarations:
const x = new BehaviourSubject<string>(); // Should be of type BehaviourSubject<string>
Using ts-morph i tried:
const declX = sourceFile.getVariableDeclarationOrThrow("x");
declX.getType().getText();
declX.getApparentType().getText();
but in both cases i just get any as type. I suspect that i need to evaluate the NewExpression in some form using the ts-typechecker but can not find any references on how to do this.
Any input would be appreciated.

What type is a points vector in objective C

I have the following declaration for a function and I don't know what I am doing wrong with regards to the type declaration:
//function is defined in the OpenCVWrapper.mm file .....no errors
+ (NSArray *)analysePoints:(std::vector<cv::Point> )pointsVector{
.......
}
//error is in the OpenCVWrapper.h file
#interface OpenCVWrapper : NSObject
+ (NSArray *)analysePoints:(NSMutableArray *)mutableArray:(std::vector<cv::Point>)pointsArray;
//red marker under the std
#end
I am getting the error "expecting type" for the vector. What am I doing wrong here?
Actually, I found the solution through user11118321 input to look at the bigger picture. I am using this set up in a swift app that uses openCV through a bridging header. It is actually not possible to import or use a vector in swift.

Update a field within a NamedTuple (from typing)

I'm looking for a more pythonic way to update a field within a NamedTuple (from typing).
I get field name and value during runtime from a textfile und therefore used exec, but I believe, there must be a better way:
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
from typing import NamedTuple
class Template (NamedTuple):
number : int = 0
name : str = "^NAME^"
oneInstance = Template()
print(oneInstance)
# If I know the variable name during development, I can do this:
oneInstance = oneInstance._replace(number = 77)
# I get this from a file during runtime:
para = {'name' : 'Jones'}
mykey = 'name'
# Therefore, I used exec:
ExpToEval = "oneInstance = oneInstance._replace(" + mykey + " = para[mykey])"
exec(ExpToEval) # How can I do this in a more pythonic (and secure) way?
print(oneInstance)
I guess, from 3.7 on, I could solve this issue with dataclasses, but I need it for 3.6
Using _replace on namedtuples can not be made "pythonic" whatsoever. Namedtuples are meant to be immutable. If you use a namedtuple other developers will expect that you do not intent to alter your data.
A pythonic approach is indeed the dataclass. You can use dataclasses in Python3.6 as well. Just use the dataclasses backport from PyPi.
Then the whole thing gets really readable and you can use getattrand setattr to address properties by name easily:
from dataclasses import dataclass
#dataclass
class Template:
number: int = 0
name: str = "^Name^"
t = Template()
# use setattr and getattr to access a property by a runtime-defined name
setattr(t, "name", "foo")
print(getattr(t, "name"))
This will result in
foo

Qt error - probably wrongly used pointer issue

Can someone help with resolving this error?
QString *description = new QString;
description = *dialog.descriptionEdit->toPlainText();
error: no match for 'operator*' in '*QTextEdit::toPlainText() const()'
and when I try:
QString *description = new QString;
description = dialog.descriptionEdit->toPlainText();
error: cannot convert 'QString' to 'QString*' in assignment
You need this
*description = dialog.descriptionEdit->toPlainText();
otherwise you are assigning to the pointer not the object it points to. description is a pointer to QString, *description is a QString.

UDK "Error, Unrecognized member 'OpenMenu' in class 'GameUISceneClient'"

Upon compiling, I am getting the following error:
C:\UDK\UDK-2010-03\Development\Src\FixIt\Classes\ZInteraction.uc(41) : Error, Unrecognized member 'OpenMenu' in class 'GameUISceneClient'
Line 41 is the following:
GetSceneClient().OpenMenu("ZInterface.ZNLGWindow");
But when I search for OpenMenu, I find that it is indeed defined in GameUISceneClient.uc of the UDK:
Line 1507: exec function OpenMenu( string MenuPath, optional int PlayerIndex=INDEX_NONE )
It looks like I have everything correct. So what's wrong? Why can't it find the OpenMenu function?
From the wiki page on Legacy:Exec Function:
Exec Functions are functions that a player or user can execute by typing its name in the console. Basically, they provide a way to define new console commands in UnrealScript code.
Okay, so OpenMenu has been converted to a console command. Great. But still, how do I execute it in code? The page doesn't say!
More searching revealed this odd documentation page, which contains the answer:
Now then, there is also a function
within class Console called 'bool
ConsoleCommand(coerce string s)'. to
call your exec'd function,
'myFunction' from code, you type:
* bool isFunctionThere; //optional
isFunctionThere = ConsoleCommand("myFunction myArgument");
So, I replaced my line with the following:
GetSceneClient().ConsoleCommand("OpenMenu ZInterface.ZNLGWindow");
Now this causes another error which I covered in my other question+answer a few minutes ago. But that's it!
Not sure if this is your intent, but if you are trying to create a UIScene based on an Archetype that has been created in the UI Editor, you want to do something like this:
UIScene openedScene;
UIScene mySceneArchetype;
mySceneArchetype = UIScene'Package.Scene';
GameSceneClient = class'UIRoot'.static.GetSceneClient();
//Open the Scene
if( GameSceneClient != none && MySceneArchetype != none )
{
GameSceneClient.OpenScene(mySceneArchetype,LocalPlayer(PlayerOwner.Player), openedScene);
}

Resources