Movatterモバイル変換


[0]ホーム

URL:


 
» Code Style Edit on GitHub

Code Style

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 sometimeindicative 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::Assignmentorself::UpdateExpression[$allowIncrementDecrement=false()and@Operator=('--','++')]]|//WhileLoop[$allowWhile=false()]/child::node()[1]/descendant-or-self::node()[self::Assignmentorself::UpdateExpression[$allowIncrementDecrement=false()and@Operator=('--','++')]]|//DoLoop[$allowWhile=false()]/child::node()[2]/descendant-or-self::node()[self::Assignmentorself::UpdateExpression[$allowIncrementDecrement=false()and@Operator=('--','++')]]|//ForLoop[$allowFor=false()]/child::node()[2]/descendant-or-self::node()[self::Assignmentorself::UpdateExpression[$allowIncrementDecrement=false()and@Operator=('--','++')]]|//ConditionalExpression[$allowTernary=false()]/child::node()[1]/descendant-or-self::node()[self::Assignmentorself::UpdateExpression[$allowIncrementDecrement=false()and@Operator=('--','++')]]|//ConditionalExpression[$allowTernaryResults=false()]/child::node()[position()=2orposition()=3]/descendant-or-self::node()[self::Assignmentorself::UpdateExpression[$allowIncrementDecrement=false()and@Operator=('--','++')]]

Example(s):

varx=2;// Badif((x=getX())==3){alert('3!');}functiongetX(){return3;}

This rule has the following properties:

NameDefault ValueDescription
allowIffalseAllow assignment within the conditional expression of an if statement
allowForfalseAllow assignment within the conditional expression of a for statement
allowWhilefalseAllow assignment within the conditional expression of a while statement
allowTernaryfalseAllow assignment within the conditional expression of a ternary operator
allowTernaryResultsfalseAllow assignment within the result expressions of a ternary operator
allowIncrementDecrementfalseAllow 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:

<ruleref="category/ecmascript/codestyle.xml/AssignmentInOperand"/>

Use this rule and customize it:

<ruleref="category/ecmascript/codestyle.xml/AssignmentInOperand"><properties><propertyname="allowIf"value="false"/><propertyname="allowFor"value="false"/><propertyname="allowWhile"value="false"/><propertyname="allowTernary"value="false"/><propertyname="allowTernaryResults"value="false"/><propertyname="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):

// Okfor(vari=0;i<42;i++){foo();}// Badfor(vari=0;i<42;i++)foo();

Use this rule by referencing it:

<ruleref="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):

// Okif(foo){x++;}else{y++;}// Badif(foo)x++;elsey++;

Use this rule by referencing it:

<ruleref="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()andnot(child::Scope)]

Example(s):

// Okif(foo){x++;}// Badif(foo)x++;

Use this rule by referencing it:

<ruleref="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 theif 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){returny;}else{returnz;}// Good:if(x){returny;}returnz;

Use this rule by referencing it:

<ruleref="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 tointroduce a new variable scope. Blocks do not behave like this in ECMAScipt, and using them canbe 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:

<ruleref="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):

varx=1;// Okvary=(1+1);// Okvarz=((1+1));// Bad

Use this rule by referencing it:

<ruleref="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 thesewill 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):

// Okfunctionfoo(){return1;}// Badfunctionbar(){varx=1;returnx;x=2;}

Use this rule by referencing it:

<ruleref="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):

// Okwhile(true){x++;}// Badwhile(true)x++;

Use this rule by referencing it:

<ruleref="category/ecmascript/codestyle.xml/WhileLoopsMustUseBraces"/>

This documentation is written in markdown.
If there is something missing or can be improved, edit this page on github and create a PR: Edit on GitHub

©2025 PMD Open Source Project. All rights reserved.
Site last generated: Nov 28, 2025

PMD                logo


[8]ページ先頭

©2009-2025 Movatter.jp