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

风格不统一 提交于 2019-12-19 03:42:25

问题


I put everything in parentheses 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;
    }
}

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7570868/how-to-fix-jslint-the-subexpression-should-be-wrapped-in-parens-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!