Question: if I wanted to take a function declaration and return it with an expression before it, how would I do that?

Asked By
wagonofjustice
Asked At
2017-09-19 23:18:16

Found 15 possible answers.

User Answered At Possible Answer
zoe 2017-09-19 23:19:17 I assume that is not related to my question.
wagonofjustice 2017-09-19 23:19:57 another way to ask this question is how do I return a new node with multiple parts without using sequenceExpression, since a function declaration obviously is not a sequenceExpression ? can you give more examples about yours pudility?
loganfsmyth 2017-09-19 23:20:41 @zoe Do you have examples as what qualifies as "before"? @wagonofjustice What's the runtime behavior you're hoping to achieve? function declarations and hoisted, so they don't really have a "before", they exist immediately at the start of their container
zoe 2017-09-19 23:22:53 sorry, What I mean is if I am trying to set a name constant, like this: Identifier(path) { const name = path.node.name;} and then access it here: ExportDefaultDeclaration: function(path){/* do something with name */} because name does not become defined until after ExportDefaultDeclaration is run. how do I do that?
loganfsmyth 2017-09-19 23:24:39 ordering can be kind of hard to manage in Babel, usually it's easiest to have your visitor kind of focus on some "main" node that you care about the most, and then do your processing all inside the visitor for that specific node
wagonofjustice 2017-09-19 23:25:08 so, I'm trying to take this function, modify its body, and assign it to the global object. also, thanks for your help, this is edifying
loganfsmyth 2017-09-19 23:25:16 so you could look for ExportDefaultDeclaration and then process the file with a separate traversal inside there maybe?
}
  });
    Identifier(idPath){},
  path.parentPath.traverse({
ExportDefaultDeclaration: function(path){
zoe 2017-09-19 23:26:06 ah, that seems like it may work.
wagonofjustice 2017-09-19 23:26:08 so I have function thing () { } and I want it to wind up looking like global.thing = thing; function thing() { **modifications inside** }
loganfsmyth 2017-09-19 23:27:48 Maybe
FunctionDeclaration(path){
? }, path.insertBefore(assign); const assign = t.assignmentExpression(t.memberExpression(t.identifier('global'), t.clone(path.node.id)), t.clone(path.node.id));
zoe 2017-09-19 23:28:50 @loganfsmyth that sort of worked, Thanks! The only issue is that I am trying to access multiple Identifiers. Any ideas on that?
loganfsmyth 2017-09-19 23:30:20 the Identifier traversal should get called for each one
wagonofjustice 2017-09-19 23:30:52 hmm Icic that's not a bad one what if I wanted to place another function call afterwards, it seems like path.insertAfter(functionexpressionhere) would do the trick
loganfsmyth 2017-09-19 23:32:18 yup, though keep in mind for a function declaration there's no programmatic different between putting something before or after it
zoe 2017-09-19 23:32:35 @loganfsmyth , It does not get called for each one: Identifier baz Identifier baz Identifier Error idPath Error

Related Questions