Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Auto-indent error when passing Javascript object constructed from variables. #552

Open
rsese opened this issue Feb 12, 2018 · 0 comments
Open

Comments

@rsese
Copy link

rsese commented Feb 12, 2018

From @wbt on February 12, 2018 18:52

I am using Atom 1.23.2 x64 on the latest Windows 10, writing in Javascript in a file with a .js extension.
The code below, written as an illustrative example, is presented as Atom auto-indents it, with four spaces per indent for visual clarity:

function workaround1(param1, message, extra, beyond) {
    var starter = doSomeFormatting(param1,
        {message: "hardcoded",
        information: extra,
        more: beyond}
    );
    var a = 4;
    var b = 5;
    return add(a,b);
}

function workaround2(param1, message, extra, beyond) {
    var tempObject = {
        message: "hardcoded",
        information: extra,
        more: beyond,
    };
    
    var starter = doSomeFormatting(param1, tempObject);
    var a = 4;
    var b = 5;
    return add(a,b);
}

function workaround3(param1, message, extra, beyond) {
    var tempObject = {};
    tempObject.message =  message,
    tempObject.information = extra,
    tempObject.more: beyond,
    
    var starter = doSomeFormatting(param1, tempObject);
    var a = 4;
    var b = 5;
    return add(a,b);
}

function unexpected1(param1, message, extra, beyond) {
    var starter = doSomeFormatting(param1,
        {information: extra,
            message: "hardcoded",        
            more: beyond}
        );
        var a = 4;
        var b = 5;
        return add(a,b);
    }
    
    function unexpected2(param1, message, extra, beyond) {
        var starter = doSomeFormatting(param1,
            {message: message,
                information: extra,
                more: "beyond"}
            );
            var a = 4;
            var b = 5;
            return add(a,b);
        }
        
        function unexpected3(param1, message, extra, beyond) {
            var starter = doSomeFormatting(param1,
                {message: message,
                    information: extra,
                    more: beyond}
                );
                var a = 4;
                var b = 5;
                return add(a,b);
            }
            
            function add(first, second) {
                return first + second;
            }
            
            function subtract(first, second) {
                return first - second;
            }
            
            //Any further code starts three levels indented.
            //The indentation errors accumulate quickly.
    

The functions named workaround# show the expected formatting.
The difference between workaround1 and unexpected1 which triggers an indentation error in the latter is that the first element of the object being passed in as the second parameter is a variable, not a hard-coded string.
Unfortunately, the workaround of reordering the fields in the object does not work for the case of unexpected3, in which all of the fields are variables. That's the case which led me to find this bug.

Auto-indentation should not change behavior when a variable is used in place of a hard-coded string for the value of the first field in a JavaScript object passed as a parameter to a function.

Copied from original issue: atom/atom#16720

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant