Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Ternary operator and self-executing function #226

Closed
Zenwolf opened this issue Aug 3, 2011 · 9 comments · Fixed by #414
Closed

Ternary operator and self-executing function #226

Zenwolf opened this issue Aug 3, 2011 · 9 comments · Fixed by #414

Comments

@Zenwolf
Copy link

Zenwolf commented Aug 3, 2011

This issue demonstrates two problems:

  1. A case where a self-executing function is not considered a function call when used in a ternary operator.
  2. A semicolon is considered unnecessary when it is defensive in nature.

PROBLEM 1: When I add a self-executing function as part of a ternary operator expression, I get an error.

Error received from jshint:
Expected an assignment or function call and instead saw an expression. "} ())"

A self-executing function is a function call: it calls itself, and it should be treated as a function call.

EXAMPLE:

var foo;

; (foo)
? foo.bar = {}
: (function () {    
    foo = {};
    foo.bar = {};
} ())
;

PROBLEM 2: Semicolon before expression parentheses.

The initial semicolon above is reported as unnecessary, but can help prevent errors from code that may have been included that did not include appropriate semicolons.

Thanks!

@Zenwolf
Copy link
Author

Zenwolf commented Aug 3, 2011

Actually, this function error still happens when I break the function call out into a standard function:

var foo;

function myFunc() {
    foo = {};
    foo.bar = {};
}

; (foo)
? foo.bar = {}
: myFunc()
;

@Zenwolf Zenwolf closed this as completed Aug 3, 2011
@Zenwolf Zenwolf reopened this Aug 3, 2011
@Zenwolf
Copy link
Author

Zenwolf commented Aug 3, 2011

closed by mistake.

@ryanfitzer
Copy link

This also exists when using other operators to create an immediately invoked function expression:

!function() {

}();

@goatslacker
Copy link
Member

@ryanfitzer you can turn that on by setting expr flag to true.

@ryanfitzer
Copy link

That was it! Thanks soo much.

Any clue on the "Missing space after 'function'." warning? This happens if there is no space between the word function and ()?

!function() {}();

I assumed it was the white option, but that wasn't the case. I figure it is checking it against the same rules that apply to if/while/for type expressions, but I can't find the option (if it even exists).

Thanks again.

@goatslacker
Copy link
Member

It's supposed to be white.

This works for me:

/*jshint expr: true, white: false */

!function() {
}();

@ryanfitzer
Copy link

Just tried yours and it worked. Not sure why, but indent: 4 is causing the issue. When removed, it works?

Not sure why it would conflict. Bug?

@WolfgangKluge
Copy link
Contributor

indent implies white: true... Hope we can remove it someday (or output a warning at least)

goatslacker added a commit to goatslacker/jshint that referenced this issue Jan 9, 2012
valueof added a commit that referenced this issue Jan 9, 2012
@valueof
Copy link
Member

valueof commented Jan 9, 2012

This was fixed in GH-414.

@valueof valueof closed this as completed Jan 9, 2012
jugglinmike pushed a commit to jugglinmike/jshint that referenced this issue Oct 21, 2014
jugglinmike pushed a commit to jugglinmike/jshint that referenced this issue Oct 21, 2014
jugglinmike pushed a commit to jugglinmike/jshint that referenced this issue Oct 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants