category.ecmascript.errorprone.xml Maven / Gradle / Ivy
<?xml version="1.0" encoding="UTF-8"?> <ruleset name="Error Prone" 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> Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors. </description> <rule name="AvoidTrailingComma" message="Avoid trailing commas in object or array literals" language="ecmascript" since="5.1" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_ecmascript_errorprone.html#avoidtrailingcomma"> <description> This rule helps improve code portability due to differences in browser treatment of trailing commas in object or array literals. </description> <priority>1</priority> <properties> <property name="xpath"> <value> <![CDATA[ //ObjectLiteral[$allowObjectLiteral = false() and @TrailingComma = true()] | //ArrayLiteral[$allowArrayLiteral = false() and @TrailingComma = true()] ]]> </value> </property> <property name="allowObjectLiteral" type="Boolean" value="false" description="Allow a trailing comma within an object literal" /> <property name="allowArrayLiteral" type="Boolean" value="false" description="Allow a trailing comma within an array literal" /> </properties> <example> <![CDATA[ function(arg) { var obj1 = { a : 1 }; // Ok var arr1 = [ 1, 2 ]; // Ok var obj2 = { a : 1, }; // Syntax error in some browsers! var arr2 = [ 1, 2, ]; // Length 2 or 3 depending on the browser! } ]]> </example> </rule> <rule name="EqualComparison" language="ecmascript" since="5.0" message="Use '==='/'!==' to compare with true/false or Numbers" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_ecmascript_errorprone.html#equalcomparison"> <description> Using == in condition may lead to unexpected results, as the variables are automatically casted to be of the same type. The === operator avoids the casting. </description> <priority>3</priority> <properties> <property name="xpath"> <value> <![CDATA[ //InfixExpression[ (@Operator = '==' or @Operator = '!=') and (KeywordLiteral[@Literal = 'true' or @Literal = 'false'] or NumberLiteral) ] ]]> </value> </property> </properties> <example> <![CDATA[ // Ok if (someVar === true) { ... } // Ok if (someVar !== 3) { ... } // Bad if (someVar == true) { ... } // Bad if (someVar != 3) { ... } ]]> </example> </rule> <rule name="InaccurateNumericLiteral" language="ecmascript" since="5.0" message="The numeric literal ''{0}'' will have at different value at runtime." class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_ecmascript_errorprone.html#inaccuratenumericliteral"> <description> The numeric literal will have a different value at runtime, which can happen if you provide too much precision in a floating point number. This may result in numeric calculations being in error. [Numbers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) in JavaScript are represented by 64bit double-precision floating point numbers internally and that's why there are some limits to the available precision of the number. See [Number.isSafeInteger()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger) and [Number.EPSILON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON). Note: This rule was named InnaccurateNumericLiteral before PMD 7.4.0. </description> <priority>2</priority> <properties> <property name="xpath"> <value> <![CDATA[ //NumberLiteral[@Inaccurate = true()] ]]> </value> </property> </properties> <example> <![CDATA[ var a = 9; // Ok var b = 999999999999999; // Ok var c = 999999999999999999999; // Not good var w = 1.12e-4; // Ok var x = 1.12; // Ok var y = 1.1234567890123; // Ok var z = 1.12345678901234567; // Not good ]]> </example> </rule> <rule name="InnaccurateNumericLiteral" ref="InaccurateNumericLiteral" deprecated="true"/> </ruleset>