Please expose Abstract Syntax Tree from Language service for TypeScript. It will be of great help to analyze code model rather then reparsing it again (as there is no way to access internal model), I want to access imported modules.
Can you add a method that goes inside worker and can access the model and return the result?
function nodeVisitor(root) {
// .. ast related code without any closure...
return // ... some result...
}
// source of nodeVisitor is passed to
// typescript worker
const result = await worker.processAst( nodeVisitor.toString() );
The underlying code simply passed this function as source code, which is executed in worker's context and result is returned either as string or or some JSON.
The AST can't be passed through the web worker as it can only send JSON
It would be nice to add some API (like plugins or dynamic imports) to the workers, so we can expand the visitors and access their ASTs. I currently have to get the editor content and then parse the AST via JScodeshift (recast).
@orta Can you expose a method to access AST within the worker context? as shown in updated description of this issue?
A worker is async background code which you don't really have access to in a general sense from your user-land code.
@luminaxster's idea of being able to add your own dynamic import is probably the best option here - perhaps a new function in here: https://github.com/microsoft/monaco-typescript/blob/master/src/tsWorker.ts#L235 which calls importScripts with your own URL which has a default export as a function which receives a copy of ts and and the TypeScriptWorker this instance.
@orta Yes I know what is background worker, this is the reason the function is passed as a string, which is evaluated in worker and executed. Your recommendation is little longer way to do same thing, and there is no much documentation and sample on how to do it exactly, how to access AST and when and what to do?
https://github.com/microsoft/monaco-typescript/pull/65 would allow you to create your own webworker which can do AST stuff I the background
Most helpful comment
A worker is async background code which you don't really have access to in a general sense from your user-land code.
@luminaxster's idea of being able to add your own dynamic import is probably the best option here - perhaps a new function in here: https://github.com/microsoft/monaco-typescript/blob/master/src/tsWorker.ts#L235 which calls
importScriptswith your own URL which has a default export as a function which receives a copy oftsand and theTypeScriptWorkerthisinstance.