Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line!! function!! and file!! #5

Open
rrichardson opened this issue Oct 6, 2021 · 1 comment
Open

line!! function!! and file!! #5

rrichardson opened this issue Oct 6, 2021 · 1 comment

Comments

@rrichardson
Copy link

At the moment I am mostly interested in macros for more perform ant logging/tracing in typescript. So conditional execution that can be decided by the environment is crucial.

In addition to that, the line, function and file directives have been missing from the typescript spec. I would like to get them back.

Would it be possible to add these?

@blainehansen
Copy link
Owner

It certainly seems possible. Are you just saying you want access to that ambient information in the body of macros so you can use it when emitting logging/tracing statements? Such as this?

export const macros = {
  my_macro: FunctionMacro((ctx, args) => {
    console.log(ctx.filename, ctx.line, ctx.column)
    ... use them here ...
  }),
}

It seems very doable to add that kind of information to MacroContext, since the functions that expand macros (attemptBlockMacro, attemptFunctionMacro, attemptDecoratorMacros, attemptImportMacro in lib/transformer.ts) are already passed:

  • CompileContext which contains information about the current file.
  • And the actual ts.TextRange/ts.Node objects the macro is transforming, which can be used to figure out the line/column.

I'm not sure function would be very easy, the various functions that walk the AST would all have to be made aware of the nearest parent function they were in, and honestly it doesn't even seem that necessary when you already have line/column.

I don't really have the bandwidth to work on macro-ts right now, but I'm happy to accept pull requests and give pointers :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants