Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Issue with array filled in a loop and returned at the end #594

Closed
gyandeeps opened this issue Jun 21, 2017 · 3 comments
Closed

Issue with array filled in a loop and returned at the end #594

gyandeeps opened this issue Jun 21, 2017 · 3 comments
Labels
bug Confirmed bug with es2015 The bug is caused when used with es2015-block-scoping plugin

Comments

@gyandeeps
Copy link

gyandeeps commented Jun 21, 2017

Choose one: is this a bug report or feature request?

This used to work with 0.0.12 but as i upgraded to latest 0.1.4 it broke. I am sorry but not sure which plugin might be causing this as i have no idea about the internals of babili.

Input Code

const getMinQueryNames = (name) => {
    const coll = [];

    for (let i = 0; i < queries.length; i++) {
        const queryName = queries[i].name;

        coll.push(queryName);
        if (queryName === name) {
            break;
        }
    }

    return coll;
};

Babel Configuration (.babelrc, package.json, cli command)

var webpack = require("webpack");
var path = require("path");

module.exports = {
    entry: "./src/index.js",
    output: { 
        filename: "./dist/bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                include: [
                    path.join(__dirname, "src"),
                ],
                use: [
                    {
                        loader: "babel-loader",
                        options: {
                            presets: [["es2015", {"modules": false}], ["babili"]]
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new webpack.optimize.ModuleConcatenationPlugin()
    ],
    stats: {
        // Nice colored output
        colors: true
    }
};

Expected Behavior

Output from 0.0.12 version and also the demo site gives this output as well.
Watch the return at end.

var getMinQueryNames=function(a){for(var d,b=[],c=0;c<queries.length&&(d=queries[c].name,b.push(d),d!==a);c++);return b};

Current Behavior

Watch the return at end. At runtime its and error because coll is undefined.

var getMinQueryNames=function(a){for(var b,c=[],d=0;d<queries.length&&(b=queries[d].name,c.push(b),b!==a);d++);return coll};

Your Environment

software version(s)
Babel 6.25
node 8.0
npm 5.0.3
Operating System Windows 7

UPDATE

I narrowed it down to mangle and mergeVars plugin. If you use them individually they work fine but if you use them together then they produce this error..

@vigneshshanmugam vigneshshanmugam added bug Confirmed bug with es2015 The bug is caused when used with es2015-block-scoping plugin labels Jun 21, 2017
@vigneshshanmugam
Copy link
Member

vigneshshanmugam commented Jun 21, 2017

The other option is to use babili as babili-webpack-plugin which operates on the entire bundle as single run instead of having as a preset along with es2015 preset.

There are couple of scoping issues when running babili along with es2015 https://github.com/babel/babili/issues?q=is%3Aissue+is%3Aopen+label%3A%22with+es2015%22

@piersroberts
Copy link

piersroberts commented Jul 13, 2017

curiously you can sticky plaster fix it like this;

const getMinQueryNames = (name) => {
    const coll = [];
    'bananas';
    for (let i = 0; i < queries.length; i++) {
        const queryName = queries[i].name;
        coll.push(queryName);
        if (queryName === name) {
            break;
        }
    }

    return coll;
};

@Mik13
Copy link

Mik13 commented Oct 25, 2017

I've got the same problem, and work around it by using var, where the var still will be pulled into the for loop initializer, but the name when returning it is correct.

I've tried the above solution aswell, which works too, and the var will be before the for loop, and the string will be in the initializer. Quite funny.

boopathi pushed a commit that referenced this issue Nov 12, 2017
…) (#713)

* Fix(merge-sibling-var): force recalc ref when concat for-loop var (#485)

Fix #594
Fix #430
Fix #412

* Add test cases of es2015 scoping issue (#713)

* Use babel path API instead of mutating node
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Confirmed bug with es2015 The bug is caused when used with es2015-block-scoping plugin
Projects
None yet
Development

No branches or pull requests

4 participants