Closes #1486 Add rule for no-else-return - New Javascript rule
This commit is contained in:
@ -27,5 +27,6 @@ This ruleset contains links to rules that are new in PMD v5.5.0
|
|||||||
<rule ref="rulesets/apex/style.xml/AvoidLogicInTrigger" />
|
<rule ref="rulesets/apex/style.xml/AvoidLogicInTrigger" />
|
||||||
<rule ref="rulesets/apex/style.xml/AvoidGlobalModifier" />
|
<rule ref="rulesets/apex/style.xml/AvoidGlobalModifier" />
|
||||||
|
|
||||||
|
<rule ref="rulesets/ecmascript/unnecessary.xml/NoElseReturn" />
|
||||||
</ruleset>
|
</ruleset>
|
||||||
|
|
||||||
|
@ -71,4 +71,37 @@ if (bar) {
|
|||||||
</example>
|
</example>
|
||||||
</rule>
|
</rule>
|
||||||
|
|
||||||
|
<rule name="NoElseReturn"
|
||||||
|
language="ecmascript"
|
||||||
|
since="5.5.0"
|
||||||
|
message="The else block is unnecessary"
|
||||||
|
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||||
|
externalInfoUrl="${pmd.website.baseurl}/rules/ecmascript/unnecessary.html#NoElseReturn">
|
||||||
|
<description>The else block in a if-else-construct is unnecessary if the `if` block contains a return.
|
||||||
|
Then the content of the else block can be put outside.
|
||||||
|
|
||||||
|
See also: http://eslint.org/docs/rules/no-else-return
|
||||||
|
</description>
|
||||||
|
<priority>3</priority>
|
||||||
|
<properties>
|
||||||
|
<property name="xpath"><value><![CDATA[
|
||||||
|
//IfStatement[@Else="true"][Scope[1]/ReturnStatement]
|
||||||
|
]]></value></property>
|
||||||
|
</properties>
|
||||||
|
<example><![CDATA[
|
||||||
|
// Bad:
|
||||||
|
if (x) {
|
||||||
|
return y;
|
||||||
|
} else {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Good:
|
||||||
|
if (x) {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
return z;
|
||||||
|
]]></example>
|
||||||
|
</rule>
|
||||||
|
|
||||||
</ruleset>
|
</ruleset>
|
@ -13,5 +13,6 @@ public class UnnecessaryRulesTest extends SimpleAggregatorTst {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
addRule(RULESET, "UnnecessaryBlock");
|
addRule(RULESET, "UnnecessaryBlock");
|
||||||
addRule(RULESET, "UnnecessaryParentheses");
|
addRule(RULESET, "UnnecessaryParentheses");
|
||||||
|
addRule(RULESET, "NoElseReturn");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<test-data>
|
||||||
|
<test-code>
|
||||||
|
<description>Simple violation</description>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
function foo() {
|
||||||
|
if (x) {
|
||||||
|
return y;
|
||||||
|
} else {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Simple fix</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
function foo() {
|
||||||
|
if (x) {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>No return in if</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
function foo() {
|
||||||
|
if (x) {
|
||||||
|
x++;
|
||||||
|
} else {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
</test-data>
|
@ -19,6 +19,7 @@ you'll need a java8 runtime environment.
|
|||||||
* CPD: New command line parameter `--ignore-usings`: Ignore using directives in C# when comparing text.
|
* CPD: New command line parameter `--ignore-usings`: Ignore using directives in C# when comparing text.
|
||||||
* A JSON-renderer for PMD which is compatible with CodeClimate. See [PR#83](https://github.com/pmd/pmd/pull/83).
|
* A JSON-renderer for PMD which is compatible with CodeClimate. See [PR#83](https://github.com/pmd/pmd/pull/83).
|
||||||
* [#1360](https://sourceforge.net/p/pmd/bugs/1360/): Provide backwards compatibility for PMD configuration file
|
* [#1360](https://sourceforge.net/p/pmd/bugs/1360/): Provide backwards compatibility for PMD configuration file
|
||||||
|
* [#1486](https://sourceforge.net/p/pmd/bugs/1486/): Add rule for no-else-return
|
||||||
|
|
||||||
**New/Modified/Deprecated Rules:**
|
**New/Modified/Deprecated Rules:**
|
||||||
|
|
||||||
@ -38,6 +39,11 @@ you'll need a java8 runtime environment.
|
|||||||
* apex-style: VariableNamingConventions, MethodNamingConventions, ClassNamingConventions,
|
* apex-style: VariableNamingConventions, MethodNamingConventions, ClassNamingConventions,
|
||||||
MethodWithSameNameAsEnclosingClass, AvoidLogicInTrigger, AvoidGlobalModifier
|
MethodWithSameNameAsEnclosingClass, AvoidLogicInTrigger, AvoidGlobalModifier
|
||||||
|
|
||||||
|
* Javascript
|
||||||
|
* New Rule: ecmascript-unnecessary/NoElseReturn: The else block in a if-else-construct is
|
||||||
|
unnecessary if the `if` block contains a return. Then the content of the else block can be
|
||||||
|
put outside.
|
||||||
|
|
||||||
**Pull Requests:**
|
**Pull Requests:**
|
||||||
|
|
||||||
* [#25](https://github.com/adangel/pmd/pull/25): Added option to exclude C# using directives from CPD analysis
|
* [#25](https://github.com/adangel/pmd/pull/25): Added option to exclude C# using directives from CPD analysis
|
||||||
|
Reference in New Issue
Block a user