hughsk / envify

:wrench: Selectively replace Node-style environment variables with plain strings.
902 stars 57 forks source link

Replace computed MemberExpressions w/ string literal as key #21

Closed monsanto closed 9 years ago

monsanto commented 9 years ago

Cross-ref https://github.com/hughsk/envify/issues/20, ccing @yoshuawuyts

The first commit adds the requested feature. Unfortunately it causes a test to fail. The test however is broken: it is supposed to test whether envify ignores assignments to environment variables, but it only passed before because the test happened to use computed MemberExpressions. The second commit adds additional tests that show that envify's previous behavior was broken.

I don't have time at the moment to fix the assignment bug, perhaps someone else can pitch in.

yoshuawuyts commented 9 years ago

Guess most of the code looks ok. Haven't tested it. Only thing that needs to happen is make the tests pass (don't have time to do that either now.)

zertosh commented 9 years ago

@monsanto You're right about the assignment bug. All you have to do is check that if the parent node is an AssignmentExpression, that we are not on the left side of that. Adding this && !(path[0] && path[0].type === Syntax.AssignmentExpression && path[0].left === node) to the already gross visitor test fixes it. So:

    return (
      node.type === Syntax.MemberExpression
      && !(path[0] && path[0].type === Syntax.AssignmentExpression && path[0].left === node)
      && (!node.computed && node.property.type === Syntax.Identifier
          || node.computed && node.property.type === Syntax.Literal
                           && typeof node.property.value === 'string')
      && node.object.type === Syntax.MemberExpression
      && node.object.object.type === Syntax.Identifier
      && node.object.object.name === 'process'
      && node.object.property.type === Syntax.Identifier
      && node.object.property.name === 'env'
    )

Side note: The visitor test doesn't have to be that complicated. In fact, we could probably speed up the traversal a bit by only checking for node.type === Syntax.MemberExpression, then performing the rest of the checks in visitProcessEnv. That way, we can return false; if the criteria doesn't match and avoid re-traversing that branch.

zertosh commented 9 years ago

@monsanto The fix for the assignment bug is in master. If you rebase your changes, I'd be more than happy to review and merge this :smile:

monsanto commented 9 years ago

@zertosh awesome. i rebased, all tests pass for me

zertosh commented 9 years ago

@monsanto Thanks for the fast turnaround. While I was testing your code, I noticed yet another bug https://github.com/hughsk/envify/pull/24. But here are some points on your PR:

monsanto commented 9 years ago

@zertosh OK, those are in.

zertosh commented 9 years ago

@monsanto Sorry for the delay :disappointed:

zertosh commented 9 years ago

Published as envify@3.4.0