[javascriptlint-commit] SF.net SVN: javascriptlint:[382] trunk
Status: Beta
Brought to you by:
matthiasmiller
From: <mat...@us...> - 2018-06-10 20:14:05
|
Revision: 382 http://sourceforge.net/p/javascriptlint/code/382 Author: matthiasmiller Date: 2018-06-10 20:14:03 +0000 (Sun, 10 Jun 2018) Log Message: ----------- Split into two separate warnings. Modified Paths: -------------- trunk/javascriptlint/lintwarnings.py trunk/tests/warnings/for_in_missing_identifier.js Added Paths: ----------- trunk/tests/warnings/unexpected_not_comparison.js Removed Paths: ------------- trunk/tests/warnings/unexpected_not.js Modified: trunk/javascriptlint/lintwarnings.py =================================================================== --- trunk/javascriptlint/lintwarnings.py 2018-06-10 05:55:22 UTC (rev 381) +++ trunk/javascriptlint/lintwarnings.py 2018-06-10 20:14:03 UTC (rev 382) @@ -107,7 +107,8 @@ 'e4x_deprecated': 'e4x is deprecated', 'ambiguous_numeric_prop': 'numeric property should be normalized; use {normalized}', 'duplicate_property': 'duplicate property in object initializer', - 'unexpected_not': 'the ! operator is unexpected; use clarifying parentheses' + 'unexpected_not_for_in': 'the ! operator is unexpected; add clarifying parentheses', + 'unexpected_not_comparison': 'the ! operator is unexpected; add clarifying parentheses or compare against !!', } errors = { @@ -688,15 +689,34 @@ raise LintWarning(node) @lookfor((tok.UNARYOP, op.NOT)) -def unexpected_not(node): +def unexpected_not_for_in(node): # Avoid for(!s in o) if node.parent and node.parent.kind == tok.IN: raise LintWarning(node) +@lookfor((tok.UNARYOP, op.NOT)) +def unexpected_not_comparison(node): # Avoid use in comparisons. - if node.parent and node.parent.kind in (tok.EQOP, tok.RELOP): + if node.parent and node.parent.kind == tok.RELOP: raise LintWarning(node) + if node.parent and node.parent.kind == tok.EQOP: + # Allow !! + kid, = node.kids + if kid.kind == tok.UNARYOP and kid.opcode == op.NOT: + return + + # Allow when compared against ! + for i, kid in enumerate(node.parent.kids): + if i == node.node_index: + continue + if kid.kind != tok.UNARYOP or kid.opcode != op.NOT: + break + else: + return + + raise LintWarning(node) + def _get_function_property_name(node): # Ignore function statements. if node.opcode in (None, op.CLOSURE): Modified: trunk/tests/warnings/for_in_missing_identifier.js =================================================================== --- trunk/tests/warnings/for_in_missing_identifier.js 2018-06-10 05:55:22 UTC (rev 381) +++ trunk/tests/warnings/for_in_missing_identifier.js 2018-06-10 20:14:03 UTC (rev 382) @@ -7,6 +7,6 @@ for (var prop2 in o) o[prop2]++; - for (!prop in o) /*warning:for_in_missing_identifier*/ /*warning:unexpected_not*/ + for (!prop in o) /*warning:for_in_missing_identifier*/ /*warning:unexpected_not_for_in*/ o[prop]++; } Deleted: trunk/tests/warnings/unexpected_not.js =================================================================== --- trunk/tests/warnings/unexpected_not.js 2018-06-10 05:55:22 UTC (rev 381) +++ trunk/tests/warnings/unexpected_not.js 2018-06-10 20:14:03 UTC (rev 382) @@ -1,26 +0,0 @@ -function unexpected_not() { - var i, j; - var f, s, o; - - if (!i == -1) { /*warning:unexpected_not*/ - return false; - } - - if (!f() < -1) { /*warning:unexpected_not*/ - return false; - } - - if (!i != -1) { /*warning:unexpected_not*/ - return false; - } - - if (i != -1 || !j == -1) { /*warning:unexpected_not*/ - return false; - } - - if (!s in o) { /*warning:unexpected_not*/ - return false; - } - - return true; -} Copied: trunk/tests/warnings/unexpected_not_comparison.js (from rev 381, trunk/tests/warnings/unexpected_not.js) =================================================================== --- trunk/tests/warnings/unexpected_not_comparison.js (rev 0) +++ trunk/tests/warnings/unexpected_not_comparison.js 2018-06-10 20:14:03 UTC (rev 382) @@ -0,0 +1,51 @@ +function unexpected_not_comparison() { + var i, j; + var b, f, s, o; + + if (!i == -1) { /*warning:unexpected_not_comparison*/ + return false; + } + + if (!f() < -1) { /*warning:unexpected_not_comparison*/ + return false; + } + + if (!i != -1) { /*warning:unexpected_not_comparison*/ + return false; + } + + if (i != -1 || !j == -1) { /*warning:unexpected_not_comparison*/ + return false; + } + + if (!s in o) { /*warning:unexpected_not_for_in*/ + return false; + } + + // Allow ! and !! + if (!!i == b) { + return false; + } + if (b == !!i) { + return false; + } + if (b === !i) { /*warning:unexpected_not_comparison*/ + return false; + } + if (!i === b) { /*warning:unexpected_not_comparison*/ + return false; + } + if (!!b === !i) { + return false; + } + if (!i === !!b) { + return false; + } + + // Do not allow !! for relative comparison + if (!!i <= b) { /*warning:unexpected_not_comparison*/ + return false; + } + + return true; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |