Domanda

I'm using JSLint to make my (working) code a bit cleaner.

It's a useful tool, but it doesn't like at all this:

//...
switch (process.argv.length) {
    default:

    case 9:
        LOG = process.argv[8];
    case 8:
        EMERGENCY = process.argv[7];
    //...
        break;
    case 1:
    case 0:
        emergency('Error');
        break;
}

JSLint say:

Missing 'case'.
Expected '}' to match 'switch' and instead saw 'case'
Expected '9' at column 5, not column 10.
Expected ';' and instead saw ':'
Expected ':' at column 5, not column 11
Unexpected ':'
Stopping.

All on the 'case 9:' line

I've putted default as first to don t block the app if the user put too much arguments.

I could put it at the end and copy paste all the pass-throught in it, but I don t like the idea, or make all of this in nested if, but it would be unreadable.

Is there a way to make JSLint accept this?

EDIT:

Here the code that was handling the role of the switch case in a older version (which accepted less arguments:

if (process.argv.length>=3){
    DOWNLOAD_DIR=PATH+process.argv[2];
    if(process.argv.length>=4){
        DOWNLOAD_ADD=process.argv[3];
        if (process.argv.length>=5)
        {
            SMIL_ADD=process.argv[4];
            if (process.argv.length>=6)
            {
                UPDATE_H=parseInt(process.argv[5],10);
                if (process.argv.length>=7)
                {
                    UPDATE_M=parseInt(process.argv[6],10);
                }
            }
        }
    }
}

As you can see, the switch is a lot more readable than thoses nested if, even if it make me use default in a unusual way.

È stato utile?

Soluzione

You need to have a break; in your default case too.

Edit:

Sorry, I didn't quite understand your question. First of all, in your edited version, there's no need to nest each if statement. Also, what do those variables (for example, DOWNLOAD_DIR) equal if these conditions aren't met? Can't you do something on the lines of:

DOWNLOAD_DIR = (process.argv[2] !== undefined) ? PATH + process.argv[2] : '';
DOWNLOAD_ADD = (process.argv[3] !== undefined) ? process.argv[3] : '';
// etc, etc

Then you can add at the end:

LOG = (process.argv.length >= 9) ? process.argv[8] : '';

In your case, it sounds like you'd want to set those empty strings as the default value, meaning the whole thing could be done in a very small amount of code, for example:

var DOWNLOAD_DIR = (process.argv[2] !== undefined) ? PATH + process.argv[2] : '/downloads';
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top