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

前端 未结 3 1143
误落风尘
误落风尘 2021-01-06 06:20

I put everything in parentheses but code below still throws error in jslint:

Problem at line 5 character 104: The \'&&\' subexpression should be wrap         


        
相关标签:
3条回答
  • 2021-01-06 06:56

    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.

    0 讨论(0)
  • 2021-01-06 07:12

    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 讨论(0)
  • 2021-01-06 07:13

    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.

    0 讨论(0)
提交回复
热议问题