-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Expose a prefix that plugins can use to annotate labelled statements and directives so they will be stripped out. #7544
Conversation
…and directives so they will be stripped out.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7193/ |
1 similar comment
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7193/ |
This might solve the precendence issue we're seeing in #7154; we could use this to label |
Good question. I haven't looked closely at that discussion before. You can't label expressions, so I don't know that it would be as simple as your description, but it might still be helpful. Hard to say. |
Would this work? co(function* foo () {
"BABEL_PREFIX_await_transformed"
const bar = yield baz;
}) |
That'd be stripped out, but I'm not sure it would be enough information for that other plugin to work. It needs to know specifically which My preference would be something like #7154 (comment) for that issue. |
@loganfsmyth In the above, my assumption was that statement would be injected above every transpiled instance. So it wouldn't apply to the whole block, just the next line, but maybe that's not the intention of this PR? Plugin ordering seemed like a much stickier problem than using something like this to flag individual lines, and I was hoping to push along that PR. |
This PR only removes directives, which are strings at the start of a function, so it would not apply to general standalone strings between statements.
Flagging individual lines is too granular unfortunately. You could certainly see someone doing
in the same statement.
No argument there, but it's already a problem we have to deal with, and at least in this specific case it seems like we can pretty reasonably throw a clear error to let users know when they get the ordering wrong. |
Is that legal syntax? I can't get it to parse in the repl. I get unexpected token errors when turning on
I'm assuming you mean not granular enough :). If flagging individual lines is enough, are you open to stripping directives throughout a block to provide this as a potential solution to that problem or is that just a no-go for you? |
Ah woops, it needs parens:
I'd rather not add more complexity to work around plugin ordering issues, unless there's literally no way to achieve what we want, and I'm not convinced that is the case here. |
Should ping some people that have had to do this before |
I'm gonna close this for now. It's not clear that it's the right approach, and it's a performance hit we may or may not want to take. |
Posting for thoughts. This idea popped into my head a while back and I'm curious what people think about it.
We have this problem where sometimes plugins will flag nodes with either random properties, or Symbols, or store them in WeakSets. I think that's a great approach when you are confident that the node won't be duplicated later, like if you are in direct control of a subtraversal and no other plugins can get in your way. On the other hand, it's entirely possible for unrelated plugins to copy or replace a node for one reason or another, and often this will clear random flags on the node.
This PR makes Babel automatically strip out directives like:
and whole labeled statements like
I'm proposing this PR because it seems like it could be kind of handy for us. It would be relatively straightforward to inject directives that contain metadata about a given block. The labeled statements I could also see, though they are probably harder to manage. On the other hand since they allow actually non-string content, they can interact with other transforms.
Essentially it exposes a
prefix
field on the plugin API object that can be used inside directive literals or inside the labels of labeled statements to make them be deleted at the end of Babel's transformation phase. We could for instance use this approach to mark each of Babel's helper functions with a directive, so they are easier to pick out when transforming a given file. It could also be useful as a way of injecting metadata about a function's bindings or something.Thoughts?