开发者

how to fix jslint The '&&' subexpression should be wrapped in parens error

开发者 https://www.devze.com 2023-04-08 13:28 出处:网络
I put everything in parenth开发者_开发百科eses but code below still throws error in jslint: Problem at line 5 character 104: The \'&&\' subexpression should be wrapped in parens.

I put everything in parenth开发者_开发百科eses but code below still throws error in jslint:

Problem at line 5 character 104: The '&&' subexpression should be wrapped in parens.

if ((typeof (c1) === 'string') && (typeof (c2) === 'string') && (c1 !== n...

How to fix ?

"use strict";

function t() {
    var c1, c2;
    if (((typeof (c1)) === 'string') && ((typeof (c2)) === 'string') && (c1 !== null) && (c2 !== null) && ((c1.trim()) === '') || ((c2.trim()) !== '')) {
        return;
    }
}


It's complaining about the form if(a && b && c || d) because (I suppose) it's not immediately obvious whether && or || will take precedence. Fix it to look like if(a && b && (c || d)) and it will stop complaining.


I think it wants this:

    if (((typeof (c1) === 'string') && (typeof (c2) === 'string') && (c1 !== null) && (c2 !== null)) && ((c1.trim()) === '') || ((c2.trim()) !== '')) {

wrap the 4 anded expressions on the left of the && at 100.


I'm fairly certain you want the following:

function t() {
    var c1, c2;
    if (typeof c1 === 'string' && typeof c2 === 'string' && c1 !== null && c2 !== null && (c1.trim() === '' || c2.trim() !== '')) {
        return;
    }
}

Not everyone knows the precedence for boolean logic, so they want you to wrap the c1.trim() || c2.trim() statements in parenthesis so it's clear how they get operated.

As a side note, I think it's ridiculous that jslint wants spaces between my operators and my operands. I think it's much more clear when there is NOT a space.

0

精彩评论

暂无评论...
验证码 换一张
取 消