Skip to content

API walk* proxies don't work, and here’s how we fix them #53

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

Closed
4 tasks done
jonathantneal opened this issue May 2, 2018 · 2 comments · Fixed by #54
Closed
4 tasks done

API walk* proxies don't work, and here’s how we fix them #53

jonathantneal opened this issue May 2, 2018 · 2 comments · Fixed by #54

Comments

@jonathantneal
Copy link
Collaborator

jonathantneal commented May 2, 2018

Hey, I’ve been a long-time user and advocate of postcss-values-parser. I’d like to think I’ve helped push it as the defacto value parser in PostCSS.


I’ve also been avoiding a significant bug in the API, and I’m here to resolve it with you. The walk proxies don’t work. Here’s why:

Container.registerWalker creates new proxy methods that walk nodes by a specific type. This means that something like Container.registerWalker(FunctionNode) becomes walkType(type, callback), where type is FunctionNode. So far, so good.

But within the walkType function, we normalize the type:

type = type.name && type.prototype ? type.name : type;

So now our type would be the following string; FunctionNode. And do you remember how we verify the node type?

if (node.type === type) { // node.type will be "func" and type will be "FunctionNode"

How do we fix this?

The fastest solution would be to update the walkType function like so:

walkType (type, callback) {
  if (!type || !callback) {
    throw new Error('Parameters {type} and {callback} are required.');
  }

  const isTypeCallable = typeof type === 'function'

  return this.walk((node, index) => {
    if (isTypeCallable && node instanceof type || !isTypeCallable && node.type === type) {
      return callback.call(this, node, index);
    }
  });
}

Would this be an acceptable solution? If so, I would gladly write up the PR.

I also want to acknowledge that @corysimmons and @nuintun tried to tell us in #34, #50, and #52. I just avoided the bug for a long time.


  • Node Version: v10.0.0
  • NPM Version: 6.0.0
  • postcss-values-parser Version: 1.5.0

This issue is regarding a problem with:

  • Standard CSS
  • LESS
  • SCSS
  • SASS

Expected Behavior

The walk proxies walk nodes by their respective type

Actual Behavior

The walk proxies do not walk nodes by their respective type

How can we reproduce the behavior?

By testing the walk proxies

@shellscape
Copy link
Owner

Fantastic write up, thanks for contributing and for your advocacy 🎉. I'm totally open to.this PR. I've been admittedly short on time with a new marriage and a littany if additional open source work on webpack.

@corysimmons
Copy link
Contributor

@shellscape Congrats on the marriage! :D

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

Successfully merging a pull request may close this issue.

3 participants