Rules which enforce a specific coding style.
Table of Contents

AssignmentInOperand

Since: PMD 5.0

Priority: Medium High (2)

Avoid assignments in operands; this can make code more complicated and harder to read. This is sometime indicative of the bug where the assignment operator ‘=’ was used instead of the equality operator ‘==’.

This rule is defined by the following XPath expression:

//IfStatement[$allowIf = false()]/child::node()[1]/descendant-or-self::node()[self::Assignment or self::UpdateExpression[$allowIncrementDecrement = false() and @Operator = ('--', '++')]]
|
    //WhileLoop[$allowWhile = false()]/child::node()[1]/descendant-or-self::node()[self::Assignment or self::UpdateExpression[$allowIncrementDecrement = false() and @Operator = ('--', '++')]]
|
    //DoLoop[$allowWhile = false()]/child::node()[2]/descendant-or-self::node()[self::Assignment or self::UpdateExpression[$allowIncrementDecrement = false() and @Operator = ('--', '++')]]
|
    //ForLoop[$allowFor = false()]/child::node()[2]/descendant-or-self::node()[self::Assignment or self::UpdateExpression[$allowIncrementDecrement = false() and @Operator = ('--', '++')]]
|
   //ConditionalExpression[$allowTernary = false()]/child::node()[1]/descendant-or-self::node()[self::Assignment or self::UpdateExpression[$allowIncrementDecrement = false() and @Operator = ('--', '++')]]
|
   //ConditionalExpression[$allowTernaryResults = false()]/child::node()[position() = 2 or position() = 3]/descendant-or-self::node()[self::Assignment or self::UpdateExpression[$allowIncrementDecrement = false() and @Operator = ('--', '++')]]

Example(s):

var x = 2;
// Bad
if ((x = getX()) == 3) {
    alert('3!');
}

function getX() {
    return 3;
}

This rule has the following properties:

Name Default Value Description
allowIf false Allow assignment within the conditional expression of an if statement
allowFor false Allow assignment within the conditional expression of a for statement
allowWhile false Allow assignment within the conditional expression of a while statement
allowTernary false Allow assignment within the conditional expression of a ternary operator
allowTernaryResults false Allow assignment within the result expressions of a ternary operator
allowIncrementDecrement false Allow increment or decrement operators within the conditional expression of an if, for, or while statement

Use this rule with the default properties by just referencing it:

<rule ref="category/ecmascript/codestyle.xml/AssignmentInOperand" />

Use this rule and customize it:

<rule ref="category/ecmascript/codestyle.xml/AssignmentInOperand">
    <properties>
        <property name="allowIf" value="false" />
        <property name="allowFor" value="false" />
        <property name="allowWhile" value="false" />
        <property name="allowTernary" value="false" />
        <property name="allowTernaryResults" value="false" />
        <property name="allowIncrementDecrement" value="false" />
    </properties>
</rule>

ForLoopsMustUseBraces

Since: PMD 5.0

Priority: Medium (3)

Avoid using ‘for’ statements without using curly braces.

This rule is defined by the following XPath expression:

//ForLoop[not(child::Scope)]
|
//ForInLoop[not(child::Scope)]

Example(s):

// Ok
for (var i = 0; i < 42; i++) {
    foo();
}

// Bad
for (var i = 0; i < 42; i++)
    foo();

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/ForLoopsMustUseBraces" />

IfElseStmtsMustUseBraces

Since: PMD 5.0

Priority: Medium (3)

Avoid using if..else statements without using curly braces.

This rule is defined by the following XPath expression:

//ExpressionStatement[parent::IfStatement[@Else = true()]]
   [not(child::Scope)]
   [not(child::IfStatement)]

Example(s):

// Ok
if (foo) {
    x++;
} else {
    y++;
}

// Bad
if (foo)
    x++;
else
    y++;

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/IfElseStmtsMustUseBraces" />

IfStmtsMustUseBraces

Since: PMD 5.0

Priority: Medium (3)

Avoid using if statements without using curly braces.

This rule is defined by the following XPath expression:

//IfStatement[@Else = false() and not(child::Scope)]

Example(s):

// Ok
if (foo) {
    x++;
}

// Bad
if (foo)
    x++;

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/IfStmtsMustUseBraces" />

NoElseReturn

Since: PMD 5.5.0

Priority: Medium (3)

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

This rule is defined by the following XPath expression:

//IfStatement[@Else=true()][Scope[1]/ReturnStatement]

Example(s):

// Bad:
if (x) {
    return y;
} else {
    return z;
}

// Good:
if (x) {
    return y;
}
return z;

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/NoElseReturn" />

UnnecessaryBlock

Since: PMD 5.0

Priority: Medium (3)

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.

This rule is defined by the following XPath expression:

/AstRoot/Scope[not(preceding::EmptyStatement)]
                    | //SwitchCase[Scope]
                    | //(Scope|Block)[Scope|Block][count(*) = 1]

Example(s):

if (foo) {
    // Ok
}
if (bar) {
    {
        // Bad
    }
}

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/UnnecessaryBlock" />

UnnecessaryParentheses

Since: PMD 5.0

Priority: Medium Low (4)

Unnecessary parentheses should be removed.

This rule is defined by the following XPath expression:

//ParenthesizedExpression/ParenthesizedExpression

Example(s):

var x = 1; // Ok
var y = (1 + 1); // Ok
var z = ((1 + 1)); // Bad

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/UnnecessaryParentheses" />

UnreachableCode

Since: PMD 5.0

Priority: High (1)

A ‘return’, ‘break’, ‘continue’, or ‘throw’ statement should be the last in a block. Statements after these will never execute. This is a bug, or extremely poor style.

This rule is defined by the following XPath expression:

//ReturnStatement[following-sibling::node()]
|
    //ContinueStatement[following-sibling::node()]
|
    //BreakStatement[following-sibling::node()]
|
    //ThrowStatement[following-sibling::node()]

Example(s):

// Ok
function foo() {
   return 1;
}
// Bad
function bar() {
   var x = 1;
   return x;
   x = 2;
}

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/UnreachableCode" />

WhileLoopsMustUseBraces

Since: PMD 5.0

Priority: Medium (3)

Avoid using ‘while’ statements without using curly braces.

This rule is defined by the following XPath expression:

//WhileLoop[not(child::Scope)]

Example(s):

// Ok
while (true) {
    x++;
}

// Bad
while (true)
    x++;

Use this rule by referencing it:

<rule ref="category/ecmascript/codestyle.xml/WhileLoopsMustUseBraces" />