tamzinblake / js3-mode

A chimeric fork of js2-mode and js-mode
GNU General Public License v3.0
181 stars 13 forks source link

Indentation of anonymous function expression hard to read #95

Open JacksonGariety opened 10 years ago

JacksonGariety commented 10 years ago

Lets say I want to define a variable, export it from a CommonJS module, and set it to a function. js3-mode will auto-indent this as follows:

var foo = module.exports = function foo() {
                             // logic here
                           }

Wouldn't it make more sense to indent it to the variable name?

var foo = module.exports = function foo() {
      // logic here
    }

This is the opposite issue of #94.

ghost commented 7 years ago

I completely agree to this. I write code in angularJs, and to the controllers, i pass anonymous functions. Right now with js3-mode, it looks like,

angular.controller('firstController', ["$scope", "$state",
                                         function($scope, $state){
                                             console.log("this is a function argument");
                                         }
                                      ]);

Web-mode got it right as,

angular.controller('firstController', ["$scope", "$state",
    function($scope, $state){
        console.log("this is a function argument");
    }
]);

Web-mode's indentation is more readable & unnecessary code wraps.

horusscope commented 7 years ago

The parser also seems to lack respect for all closures,

function tagListener( ) {
  this.search = (function( ) {
                   var value = ""
                     , result = []
                   function exec(resolve, reject) {
                     function f(resultSet) {
                       result = resultSet
                       resolve(result)
                     }
                     API.do('Search', 'user-search'
    , {
      resource: 'user-search'
          , params: { search: value }
    })
                     .then(f).catch(reject)
                   }
                   return function(term) {
                        if(term == value) return Promise.resolve(result)
                        value = term
                     return new Promise(exec)
                   }
                 })( )
}

So I must fight with it, for example to produce this readable indentation:

function tagListener( ) {
  this.search = (function( ) {
    var value = ""
    , result = []
    function exec(resolve, reject) {
      function f(resultSet) {
        result = resultSet
        resolve(result)
      }
      API.do('Search', 'user-search'
        , {
          resource: 'user-search'
          , params: { search: value }
        })
      .then(f).catch(reject)
    }
    return function(term) {
      if(term == value) return Promise.resolve(result)
      value = term
      return new Promise(exec)
    }
  })( )
}