Skip to content

Conversation

@vigneshshanmugam
Copy link
Member

@vigneshshanmugam vigneshshanmugam commented Jun 21, 2017

this is required for only var bindings.

The other way we could fix this is to hoist all the var bindings before we run simplify & deadcode.

let replacementPath = binding.path;
let isReferencedBefore = false;

if (binding.referencePaths.length > 1) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary check.


const replacements = [];
const { path: bindingPath } =
path.scope.getBinding(test.node.name) || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use || {} ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (binding) check is clearer, like it's used everywhere else in babili

const source = unpad(`
function foo(v) {
if (v) var w = 10;
if (w) console.log("hello", v);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also

while (v) { var w = 10; }
if (w) console.log(w);

@vigneshshanmugam
Copy link
Member Author

We need to handle this in simplify as well for the same case.

Started to think, hoisting that var would be a better option than monkey patching at all places. Thoughts? @boopathi

@boopathi
Copy link
Member

var hoisting was considered earlier. But I didn't try measuring its effect on gzip yet. #48 (comment)

@vigneshshanmugam
Copy link
Member Author

Hmm alright, the problem itself is with babel's path.evaluate() being truthy even for bindings present in different scope.

It becomes tricky to do the scope check on every pattern matching step in simplify.

@vigneshshanmugam vigneshshanmugam force-pushed the scope-fix branch 2 times, most recently from d7f8de5 to 20c2f81 Compare August 7, 2017 12:23
@boopathi boopathi merged commit 19720cb into master Aug 7, 2017
@boopathi boopathi deleted the scope-fix branch August 7, 2017 19:46
@boopathi boopathi added the Tag: Bug Fix Pull Request fixes a bug label Aug 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Tag: Bug Fix Pull Request fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

if statement wrongly removed when var declaration inside conditional block

3 participants