Add support for checking condition in the ternary operator to the AssignmentInOperand rule for ECMAScript. Optionally can check for use of Assignment inside of one of the results of the ternary too (ternary is semi-complex operator, argues for KISS in usage).

The Java version of this Rule does not seem to have ternary support, we should consider adding it.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6730 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Ryan Gustafson committed 2008-12-01 17:47:47 +00:00
1 parent 5d4ec1b5cd
commit f8788ae248
2 files changed
+43 -2

No files matched your search

@@ -14,6 +14,9 @@ do {
} while(x);
for (var i = 0; i < 10; i++) {
}
x ? true : false;
x ? true : false;
x ? true : false;
]]></code>
<source-type>ecmascript 3</source-type>
</test-code>
@@ -21,7 +24,7 @@ for (var i = 0; i < 10; i++) {
<description><![CDATA[
Bad, assignment, all cases
]]></description>
<expected-problems>4</expected-problems>
<expected-problems>7</expected-problems>
<code><![CDATA[
if (x = true) {
}
@@ -31,6 +34,9 @@ do {
} while(x = true);
for (var i = 0; x = true; i++) {
}
(x = true) ? true : false;
x ? x = true : false;
x ? true : x = false;
]]></code>
<source-type>ecmascript 3</source-type>
</test-code>
@@ -84,9 +90,32 @@ for (var i = 0; x = true; i++) {
</test-code>
<test-code>
<description><![CDATA[
Ok, allow assignment, ternary
]]></description>
<expected-problems>0</expected-problems>
<rule-property name="allowTernary">true</rule-property>
<code><![CDATA[
(x = true) ? true : false;
]]></code>
<source-type>ecmascript 3</source-type>
</test-code>
<test-code>
<description><![CDATA[
Ok, allow assignment, ternary results
]]></description>
<expected-problems>0</expected-problems>
<rule-property name="allowTernaryResults">true</rule-property>
<code><![CDATA[
x ? x = true : false;
x ? true : x = false;
]]></code>
<source-type>ecmascript 3</source-type>
</test-code>
<test-code>
<description><![CDATA[
Bad, increment/decrement, all cases
]]></description>
<expected-problems>16</expected-problems>
<expected-problems>28</expected-problems>
<code><![CDATA[
if (++x || --x || x++ || x--) {
}
@@ -96,6 +125,9 @@ while (++x || --x || x++ || x--) {
}
do {
} while (++x || --x || x++ || x--);
(++x || --x || x++ || x--) ? true : false;
x ? (++x || --x || x++ || x--) : false;
x ? true : (++x || --x || x++ || x--);
]]></code>
<source-type>ecmascript 3</source-type>
</test-code>
@@ -114,6 +146,9 @@ while (++x || --x || x++ || x--) {
}
do {
} while (++x || --x || x++ || x--);
(++x || --x || x++ || x--) ? true : false;
x ? (++x || --x || x++ || x--) : false;
x ? true : (++x || --x || x++ || x--);
]]></code>
<source-type>ecmascript 3</source-type>
</test-code>
+6
View File
@@ -30,12 +30,18 @@ indicative of the bug where the assignment operator '=' was used instead of the
//DoLoop[$allowWhile = "false"]/child::node()[2]/descendant-or-self::node()[self::Assignment or self::UnaryExpression[$allowIncrementDecrement = "false" and (@Image = "--" or @Image = "++")]]
|
//ForLoop[$allowFor = "false"]/child::node()[2]/descendant-or-self::node()[self::Assignment or self::UnaryExpression[$allowIncrementDecrement = "false" and (@Image = "--" or @Image = "++")]]
|
//ConditionalExpression[$allowTernary = "false"]/child::node()[1]/descendant-or-self::node()[self::Assignment or self::UnaryExpression[$allowIncrementDecrement = "false" and (@Image = "--" or @Image = "++")]]
|
//ConditionalExpression[$allowTernaryResults = "false"]/child::node()[position() = 2 or position() = 3]/descendant-or-self::node()[self::Assignment or self::UnaryExpression[$allowIncrementDecrement = "false" and (@Image = "--" or @Image = "++")]]
]]>
</value>
</property>
<property name="allowIf" type="Boolean" value="false" description="Allow assignment within the conditional expression of an if statement" />
<property name="allowFor" type="Boolean" value="false" description="Allow assignment within the conditional expression of a for statement" />
<property name="allowWhile" type="Boolean" value="false" description="Allow assignment within the conditional expression of a while statement" />
<property name="allowTernary" type="Boolean" value="false" description="Allow assignment within the conditional expression of a ternary operator" />
<property name="allowTernaryResults" type="Boolean" value="false" description="Allow assignment within the result expressions of a ternary operator" />
<property name="allowIncrementDecrement" type="Boolean" value="false" description="Allow increment or decrement operators within the conditional expression of an if, for, or while statement" />
</properties>
<example>