You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Take the following abbreviated sample of an embedded plugin:
// This will only return the first element in the .mdconstprocessor=mdast().use(function(mdst,opt){functiontransformer(ast,file){ast.children=ast.children.slice(0,1);}returntransformer;});returnprocessor.process(data);
In this example, the transformer method is expected to mutate the incoming parameters, ast and file. This had me confused for quite a while as it is commonly considered a best-practice to keep parameters immutable. Due to expecting transformer to return the tranformed objects and not seeing it in any of your plugins, I was thrown a bit. The transformer doesn't actually do anything with its returned object.
A more optimum approach would be something like this:
// This will only return the first element in the .mdconstprocessor=mdast().use(function(mdst,opt){functiontransformer(ast,file){varmutatedAst=ast.children.slice(0,1);returnmutatedAst;}returntransformer;});returnprocessor.process(data);
While I understand two parameters are in play, they should probably be returned grouped together as an object. The point is that one should not expect the user to mutate incoming parameters and not even return a result, which is a basic in functional programming.
I know correcting this would probably break other plugins: perhaps you could schedule it in to the next major release?
The text was updated successfully, but these errors were encountered:
Well, yes. That would break every plugin out there :) In addition, though I like the concept of immutability, this is how ware (which is used internally), koa, and express also use.
Also, it is impossible with the current set-up, through the previously mentioned ware, to add this, because it doesn’t support return values (P.S. transformers can be async)
In addition, often, plug-ins only make small changes. Adding a few attributes, &c. Wouldn’t it be very expensive in performance to have to clone the AST each time?
Maybe just a doc update that clearly explains that's what is happening would suffice. The main point is predictability, so if its clear that shouldn't be a problem IMHO.
This additions add an example of how to accept options, and how to
transform the syntax tree. Both examples additionality include how
they could be `use`d by a user.
ClosesGH-44, GH-45.
Anyway, I think that 8664a03 closes this because it clearly shows that no AST is returned. Do you agree? If not, please re-open and let me know where you’d like this notice!
Take the following abbreviated sample of an embedded plugin:
In this example, the
transformer
method is expected to mutate the incoming parameters,ast
andfile
. This had me confused for quite a while as it is commonly considered a best-practice to keep parameters immutable. Due to expectingtransformer
toreturn
the tranformed objects and not seeing it in any of your plugins, I was thrown a bit. The transformer doesn't actually do anything with its returned object.A more optimum approach would be something like this:
While I understand two parameters are in play, they should probably be returned grouped together as an object. The point is that one should not expect the user to mutate incoming parameters and not even return a result, which is a basic in functional programming.
I know correcting this would probably break other plugins: perhaps you could schedule it in to the next major release?
The text was updated successfully, but these errors were encountered: