2

I know there is an old post about this functionality in javascript: Getting All Variables In Scope

But I am desperately looking and thought I'd just ask in case there might be transpile magic in typescript (or a way we can extend it) that may acheve this..(e.g.)

.ts

() => {
  const a = 123;
  console.log(scope)
} 

.js transpile

var scope = {};
() => { 
  scope.a = 123;
  console.log(scope);
}

The reason I'm asking is that I'm looking for a node backend solution to be able to log a function's scope state and save it in a debug database for review. (So any time an error occurs the state is never lost but recorded instead)


@k0pernikus

Getting specific on my issue, I'm trying to get more context on what went wrong with my handler for a firebase event. functions.firestore.document('users/{uid}').onUpdate

// ====== Pretend code I wish would work =====
const logScopeSomewhere = (anonymousScope) => (err) => {
  console.log(anonymousScope) // { pie: 'pie', apple: 'apple' }
  // or write error to database..
  // Main goal is that all variables in the function that errored are logged with the Type Error..
}
const handleUpdate = (change: Change<QueryDocumentSnapshot>, context: EventContext) => {
  let anonymousScope;
  return (async () => {
    anonymousScope = scope; // special reserved - // Possible JS transpile manipulation can have a closure on top..
    const pie = 'pie'; // anonymousScope.pie
    const apple = 'apple'; // anonymousScope.apple
    // apple.core.seed - // TypeError (will go to catch..)
  })().catch(logScopeSomewhere(anonymousScope))
}
functions.firestore.document('users/{uid}').onUpdate(handleUpdate)

Trying to be more clear, I want to have an object that has a snapshot of the state of the executing function when the error occurred. I plan to catch it and will use it as logging information.

Jonathan002
  • 9,639
  • 8
  • 37
  • 58
  • The underlying Javascript language (which TypeScript compiles to and is what is running at runtime) does not have any publicly accessible way to get all variables in scope. Debuggers have this capability, but not code written from the language itself. I would think your best chance would be somehow hooking into a debugger somehow. – jfriend00 Mar 04 '21 at 07:52
  • @jfriend00 Yes, I know it's difficult through javascript. But I think we bend the rules through transpliing.. (e.g. Typescript has class modifyers which at a time javascript could not do). Do you know if their are libraries or tools that allow us to extend typescript this way? (Or As for the debugger, do you have a link to one that can be used in the backend?) – Jonathan002 Mar 05 '21 at 06:40
  • I'm still a bit fuzzy about what you want to log on error. Can you elaborate your use case? – k0pernikus Mar 06 '21 at 19:54
  • @k0pernikus I have updated my answer with code. I hope this is more understandable. – Jonathan002 Mar 07 '21 at 05:05
  • 1
    I'd willing to bet that you should use some builtins of nodejs or even V8 engine. Try to add v8 engine tag to your question. I believe @jmrk can help you – captain-yossarian from Ukraine Mar 08 '21 at 12:57

1 Answers1

1

I don't think V8 exposes scope information in any other way than through DevTools.

You can parse your own source code, which lets you do any analysis you want. See this answer to the old question you linked for an example.

The TypeScript compiler must be doing the same analysis already internally, so I'm pretty sure it could be extended to dump that information. For example, it should be possible to extend the TypeScript language with a scope keyword (or whatever) that gets compiled to a JS object containing all in-scope variables and their values. That said, I have no idea whether something like that already exists, or how one would go about adding it.

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • @Jonathan002 here https://davidgomes.com/using-the-typescript-api-to-find-issues-in-your-code/?utm_source=typescript-weekly.com&utm_campaign=typescript_weekly_160&utm_medium=email you can find some info how to work with compiler API – captain-yossarian from Ukraine Mar 10 '21 at 11:13