Files
pmd/pmd-javascript/src/main/resources/rulesets/ecmascript/unnecessary.xml

107 lines
3.1 KiB
XML

<?xml version="1.0"?>
<ruleset name="Unnecessary"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
The Unnecessary Ruleset contains a collection of rules for unnecessary code.
</description>
<rule name="UnnecessaryParentheses"
language="ecmascript"
since="5.0"
message="Unnecessary parentheses."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/ecmascript/unnecessary.html#UnnecessaryParentheses">
<description>Unnecessary parentheses should be removed.</description>
<priority>4</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ParenthesizedExpression/ParenthesizedExpression
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
var x = 1; // Ok
var y = (1 + 1); // Ok
var z = ((1 + 1)); // Bad
]]>
</example>
</rule>
<rule name="UnnecessaryBlock"
language="ecmascript"
since="5.0"
message="Unnecessary block."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/ecmascript/unnecessary.html#UnnecessaryBlock">
<description>An unnecessary Block is present. Such Blocks are often used in other languages to
introduce a new variable scope. Blocks do not behave like this in ECMAScipt, and using them can
be misleading. Considering removing this unnecessary Block.</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Block[not(parent::FunctionNode or parent::IfStatement or parent::ForLoop or parent::ForInLoop
or parent::WhileLoop or parent::DoLoop or parent::TryStatement or parent::CatchClause)]
|
//Scope[not(parent::FunctionNode or parent::IfStatement or parent::ForLoop or parent::ForInLoop
or parent::WhileLoop or parent::DoLoop or parent::TryStatement or parent::CatchClause)]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
if (foo) {
// Ok
}
if (bar) {
{
// Bad
}
}
]]>
</example>
</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>