This project is read-only.

'this' not being output when required for lambda function

Topics: General
Jan 20, 2014 at 11:21 PM
Edited Jan 20, 2014 at 11:37 PM
I've developed a site that sorta acts as a fiddler for typescript. One of the bugs I've run into when using the js version of the script is that 'var _this = this;' is not being output when appropriate.

Here is a gist of the compiler

I haven't been able to find any settings that would allow me to force using _this. Is there something else I'm missing? From what I can understand reading the source is that the script itself defines that it should be capture 'this'. I wonder if maybe PullElementFlags.MustCaptureThis is not defined properly on my end.

Here's a sample prototype that fails - the code is copied from the playground for the raytracer.

And here's another bare-bones sample
Jan 21, 2014 at 12:35 AM
At a minimum you need to be calling getSemanticDiagnostics - this is what does all the typechecking that internally sets the flags so the emitter knows whether or not to emit var _this = this;. I think doing that should be enough to get it fixed. You definitely shouldn't be trying to set PullElementFlags yourself.
Jan 21, 2014 at 12:42 AM
I should add that you can refer to the 'HarnessCompiler' class in src\harness\harness.ts (see near line 1050 in the develop branch) as a reference for how to invoke the compiler. It's not pretty but it is what we use internally to compile all the test baselines, so it's a good enough implementation for this sort of webpage where you don't need to host file I/O.

You can also check 'BatchCompiler' in src\compiler\tsc.ts which is a complete implementation (and guaranteed to match the commandline behavior, since that's the actual backing class for tsc.exe), but it's more code to wade through.
Jan 21, 2014 at 1:55 AM
Ah!!!! I see what you mean about getSemanticDiagnostics. It makes sense now. Added that and it works perfectly now. So in essence that was a sort of preprocessor to mark each node and it's type information.

Thanks a ton for your help. It's been bugging me so much. I've been scouring the source for typescript and didn't realize the connection there.