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

Allow defensive semicolon #487

Closed
tauren opened this Issue Mar 20, 2012 · 10 comments

Comments

Projects
None yet
6 participants
@tauren
Copy link

tauren commented Mar 20, 2012

I'm not finding any option that will allows a defensive semicolon. For example:

;var myNamespace = (typeof process !== 'undefined' && 
    typeof process.title !== 'undefined') ? exports : myNamespace || {};

!function(exports) {
  exports.a = 5;
  exports.fn = function() {};
}( myNamespace );

Here's a real-life example:
https://github.com/flatiron/plates/blob/master/lib/plates.js

Does an option exist to handle this?

@tauren

This comment has been minimized.

Copy link

tauren commented Mar 20, 2012

BTW, the following issue is similar and has been resolved. But it only solves the problem for semicolons that precede an open parenthesis.
#218

The changelog indicates this was added on January 08, 2012, release r04:
http://www.jshint.com/changelog/

@valueof

This comment has been minimized.

Copy link
Member

valueof commented Mar 22, 2012

What case does a semicolon before var solve?

@tauren

This comment has been minimized.

Copy link

tauren commented Mar 22, 2012

@antonkovalyov That's a very good question, I'm not aware of a specific case for var. I'm using the plates.js 3rd party file which includes it.

The problem is my company's build process needs to successfully lint all code before it can go to production, including 3rd party code. Yes, I could just remove the semicolon from plates.js and it would pass. But I was wondering if there was a jshint option that would simply allow it.

By the way, the solution in #218 doesn't solve this:

var x = 0
[x, x+1, x+2].forEach(console.log)

It seems like an option to allow defensive semicolons at the beginning of any line should be added, and that it shouldn't be specific to what the next symbol on the line is like the solution in #218 is (must have an open parenthesis as next symbol).

@valueof

This comment has been minimized.

Copy link
Member

valueof commented Mar 23, 2012

JSHint is not simply a JavaScript parser—it is a tool to advise you on the best practices. So it makes sense for us to allow defensive semicolons only in cases where otherwise it might break but not everywhere. I agree (with precautions) about [ but we won't be adding support for meaningless constructions such as ;var.

@tauren

This comment has been minimized.

Copy link

tauren commented Mar 25, 2012

Sure, that makes sense. I will just remove the ; from my copy of plates.js then.

It is unlikely that real world code would have a line of code starting with a [, but it is possible. I'm glad you agree that allowing a defensive semicolon there would be good to do.

@dscape

This comment has been minimized.

Copy link

dscape commented Mar 25, 2012

What does not having comma first solve?

@domenic

This comment has been minimized.

Copy link
Contributor

domenic commented Mar 25, 2012

@dscape The same thing not having ;;;;;"a",5;;;(function () { })(); as the first line of your file solves.

@mikeal

This comment has been minimized.

Copy link

mikeal commented Mar 25, 2012

i can't figure out what ;var does either.

if you're not doing semicolons at the ends of lines it's common to start certain lines with a semicolon, most importantly ;(function (){}).

the npm coding-style guide notes two cases.

;(x || y).doSomething()
;[a, b, c].forEach(doSomething)

http://npmjs.org/doc/coding-style.html

none of this, btw, is a comma-first thing, it's a no-semicolon thing.

@scottgonzalez

This comment has been minimized.

Copy link

scottgonzalez commented Apr 2, 2012

Defensive semicolons are also used as a safe-guard against concatenation with another file that doesn't end with a semicolon. In such cases, it doesn't matter what your script starts with, even if it's a var statement.

Say you have a file which ends with:

var foo = bar

And another file that starts with:

var whatever = "...";

Concatenating the two files together could result in:

var foo = barvar whatever = "...";

This type of defensive semicolon is different from the npm-style, in that it will always be the first token of a file (ignoring comments and whitespace).

@scottgonzalez

This comment has been minimized.

Copy link

scottgonzalez commented Apr 2, 2012

I personally think that just handling ( and [ is fine if that's what you want to limit support to. It's pretty rare to want that type of defensive semicolon and not have, or be able to add, a paren at the beginning of the file.

@valueof valueof closed this in a47843f Dec 6, 2012

jugglinmike added a commit to jugglinmike/jshint that referenced this issue Oct 21, 2014

Added support for a defensive semicolon before [
Example: ;[a, b, c].forEach(doSomething)

Closes jshintGH-487
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment