ricardofbarros / linter-js-standard

Atom linter plugin for JavaScript, using JavaScript Standard Style
https://atom.io/packages/linter-js-standard
MIT License
99 stars 48 forks source link

Problems with function-expressions. #169

Closed DAGapps closed 7 years ago

DAGapps commented 7 years ago

When a self-invoking function-expression is on the bottom of a script, the linter throws a warning: 'Unexpected newline between function and ( of function call.' Because the linter does not understand that a expression bracket is standalone and assigned to the following wrapped function. Not the function or object in front.

Example:

var foo = {
  'value': 42
}

console.log(foo.value)

(function () {
  return
})()

js-standard:

Warning
Unexpected space between function name and paren.at line 5 col 1
Warning
Unexpected newline between function and ( of function call.at line 7 col 1

You can fix this by a workaround by adding a semicolon before expression function. But this is not solution. Because extra semicolons:

var foo = {
  'value': 42
}

console.log('Hello');
console.log(foo.value);

(function () {
  return
})()

js-standard:

Warning
Extra semicolon.at line 5 col 1

I'm sure, this is a bug. Or what's wrong?

DAGapps commented 7 years ago

Ok, I see now that a semicolon in front of the expression is wanted and the correct way. But in my opinion, this looks really ugly. But, ok. This is not a bug. See here: http://standardjs.com/rules.html (no-unexpected-multiline)

var foo = {
  'value': 42
}

console.log(foo.value)

;(function () {
  return
})()

It would be better if the linter warns with: Never start a line with (, [, or ` (brackets or quotes). Start with semicolon if required.

Why do I use anonymous self-invoking expression functions?

  1. Local scope without creating a new variable.
  2. Return from code.