I'm trying to build a VS Code extension and I need to find all global variables in a typescript file.
I've tried
function findGlobalVariables(editor: vscode.TextEditor) {
const text = editor.document.getText();
const globalVariables: string[] = [];
const sourceFile = ts.createSourceFile('temp.ts', text, ts.ScriptTarget.Latest, true);
const visit = (node: ts.Node) => {
if (ts.isVariableDeclaration(node)) {
const identifier = (node.name as ts.Identifier).text;
globalVariables.push(identifier);
}
ts.forEachChild(node, visit);
};
visit(sourceFile);
}
But that only returns the variables defined in functions using let, const etc...
Is there a way to return all the global variables, in any form and shape:
thisIsAGlobalVariable: any;
thisIsAnotherGlobalVariable;
thisIsYetAnotherGlobalVariable = "hello";
Also, is there a way to find all global identifiers in general, so both global variables and function names?