Skip to content

PMD Stock JavaScript Rules

The stock rules file for JavaScript of Appendix F.1, in full. A project repository keeps it as config/pmd/pmd-stock-rules-javascript.xml (The PMD Folder).

pmd-stock-rules-javascript.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ruleset
    name="Stock Rules"
    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 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
    <description>
        The stock PMD rules this reference adopts for JavaScript, each with an
        explicit priority. The stock PMD rules for the other languages are in
        pmd-stock-rules.xml, and the custom rules are in
        pmd-custom-rules-apex.xml, pmd-custom-rules-markup.xml,
        pmd-custom-rules-metadata.xml, pmd-custom-rules-flow.xml, and, for
        JavaScript, pmd-custom-rules-javascript.xml; all seven files are listed
        under engines.pmd.custom_rulesets in code-analyzer.yml.
    </description>

    <!--
        How to read this file.
        - It follows pmd-stock-rules.xml, and the comment at the top of that file
          explains the priorities, the properties, which are all listed, and the comments
          that name a PMD default.
        - Every JavaScript rule in PMD 7.26.0, the version Code Analyzer 5.16.0 bundles, is here.
        - The JavaScript rules have a file of their own because PMD ships the four brace
          rules (ForLoopsMustUseBraces, IfElseStmtsMustUseBraces, IfStmtsMustUseBraces, and
          WhileLoopsMustUseBraces) under one name for Apex and for JavaScript. Code Analyzer
          runs only the first of two rules that share a name in one ruleset file, so in one
          file the JavaScript four would never run. PMD by itself runs both.
        - The ruleset has the name of the one in pmd-stock-rules.xml, so Code Analyzer gives
          every stock PMD rule the one tag, StockRules.
    -->

    <exclude-pattern>.*/.sfdx/.*</exclude-pattern>
    <exclude-pattern>.*/.sf/.*</exclude-pattern>
    <exclude-pattern>.*/node_modules/.*</exclude-pattern>

    <!-- ============================================================ -->
    <!-- JAVASCRIPT: BEST PRACTICES                                   -->
    <!-- ============================================================ -->

    <rule ref="category/ecmascript/bestpractices.xml/AvoidWithStatement">
        <!-- PMD default: 1 -->
        <priority>2</priority>
    </rule>

    <rule ref="category/ecmascript/bestpractices.xml/ConsistentReturn">
        <priority>2</priority>
    </rule>

    <!--
        ESLint's no-undef, in the sample ESLint configuration, reports the same
        missing declaration, and Lightning Web Security keeps each component's
        globals to itself.
    -->
    <!-- <rule ref="category/ecmascript/bestpractices.xml/GlobalVariable" /> -->

    <!--
        A for...in variable with no declaration is the same missing declaration
        that ESLint's no-undef reports.
    -->
    <!-- <rule ref="category/ecmascript/bestpractices.xml/ScopeForInVariable" /> -->

    <rule ref="category/ecmascript/bestpractices.xml/UseBaseWithParseInt">
        <!-- PMD default: 1 -->
        <priority>2</priority>
    </rule>

    <!-- ============================================================ -->
    <!-- JAVASCRIPT: CODE STYLE                                       -->
    <!-- ============================================================ -->

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

    <rule ref="category/ecmascript/codestyle.xml/ForLoopsMustUseBraces">
        <!-- PMD default: 3 -->
        <priority>2</priority>
    </rule>

    <rule ref="category/ecmascript/codestyle.xml/IfElseStmtsMustUseBraces">
        <!-- PMD default: 3 -->
        <priority>2</priority>
    </rule>

    <rule ref="category/ecmascript/codestyle.xml/IfStmtsMustUseBraces">
        <!-- PMD default: 3 -->
        <priority>2</priority>
    </rule>

    <rule ref="category/ecmascript/codestyle.xml/NoElseReturn">
        <!-- PMD default: 3 -->
        <priority>2</priority>
    </rule>

    <!--
        The rule was written before JavaScript had block scope, and it also
        reports braces that scope a let or const, such as the braces of a case
        that declares a const of its own. ESLint's no-lone-blocks reports only
        the blocks that do nothing.
    -->
    <!-- <rule ref="category/ecmascript/codestyle.xml/UnnecessaryBlock" /> -->

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

    <rule ref="category/ecmascript/codestyle.xml/UnreachableCode">
        <!-- PMD default: 1 -->
        <priority>2</priority>
    </rule>

    <rule ref="category/ecmascript/codestyle.xml/WhileLoopsMustUseBraces">
        <!-- PMD default: 3 -->
        <priority>2</priority>
    </rule>

    <!-- ============================================================ -->
    <!-- JAVASCRIPT: ERROR PRONE                                      -->
    <!-- ============================================================ -->

    <rule ref="category/ecmascript/errorprone.xml/AvoidTrailingComma">
        <!-- PMD default: 1 -->
        <priority>2</priority>
        <properties>
            <property name="allowArrayLiteral" value="false" />
            <property name="allowObjectLiteral" value="false" />
        </properties>
    </rule>

    <rule ref="category/ecmascript/errorprone.xml/EqualComparison">
        <!-- PMD default: 3 -->
        <priority>2</priority>
    </rule>

    <!--
        A number literal beyond the precision of a JavaScript number is a rare
        mistake in component code, and ESLint's no-loss-of-precision also
        reports it.
    -->
    <!-- <rule ref="category/ecmascript/errorprone.xml/InaccurateNumericLiteral" /> -->

    <!-- ============================================================ -->
    <!-- JAVASCRIPT: PERFORMANCE                                      -->
    <!-- ============================================================ -->

    <rule ref="category/ecmascript/performance.xml/AvoidConsoleStatements">
        <!-- PMD default: 3 -->
        <priority>2</priority>
    </rule>
</ruleset>