0

I'm linting my JavaScript with JSHint, and have this option enabled

"asi": true,
"white": true

to avoid semicolons in my code.

But I have to begin my new line with a bracket, so I have to put a semicolon before the opening of that one

;(function () {

})

JSHint give me two errors:

  • Missing space after ';'
  • If I put a space after ';' I get: Expected '(' to have a different identation

I noticed that in this way JSHint is happy

;
(function () {

})

but I think is not a good solution.

Is there a way to solve this problem, without turning off JSHint or the white option?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Luke
  • 2,976
  • 6
  • 34
  • 44
  • 4
    Is it worth all this trouble to avoid having to type semicolons? – Pointy Jul 17 '12 at 13:35
  • 1
    There are good reasons why avoiding automatic semi-colon insertion is recommended – Quentin Jul 17 '12 at 13:36
  • The point of JSHint isn't just to validate that you are writing javascript that parses. You can just try running it for that. While there's certainly different philosophies on ASI, when it comes to writing one really ugly construct to avoid another one, is it really better? Putting a semicolon before a function def is a workaround in the first place, it seems legit that JSHint wouldn't validate it. – Jamie Treworgy Jul 17 '12 at 13:39
  • 3
    guys i really care and understand your point of view, but this isn't a debate about the semicolon insertion, it's about a jshint warning =) – Luke Jul 17 '12 at 13:39
  • Are you using any other JSHint options? Because I am not getting those errors when using jshint.com. – George Jul 17 '12 at 13:47
  • @GeorgeP my problem is related to the white: true option, i was wondering if there is a way to keep it true without getting the warning i was writing about – Luke Jul 17 '12 at 13:49
  • What does *"asi"* mean? E.g., is it an abbreviation for something? ASI: [automatic semicolon insertion](https://en.wikipedia.org/wiki/JavaScript_syntax#Whitespace_and_semicolons) – Peter Mortensen Oct 08 '20 at 20:34

1 Answers1

3

The legacy white: true option in JSHint is used to enforce the coding style promoted by Douglas Crockford in his original JSLint tool. Semicolon-less JavaScript code will not fit his coding style. If you don't want to be restricted to his style guidelines then don't use white: true.

This list of JSHint options doesn't show any parameters to customize the coding style they enforce.

To prove that there isn't an answer to this, I went and found the relevant check in the JSHint source:

function nonadjacent(left, right) {
    if (option.white) {
        left = left || token;
        right = right || nexttoken;
        if (left.line === right.line && left.character === right.from) {
            left.from += (left.character - left.from);
            warning("Missing space after '{a}'.",
                    left, left.value);
        }
    }
}

The only configuration option checked is option.white, so unfortunately there isn't any way to achieve your desired behavior. If you really wanted a tool that would do exactly what you want, you could easily fork JSHint, add another option, and check it in the nonadjacent function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
George
  • 4,147
  • 24
  • 33