Merge pull request #4169 from adangel:issue-4165-InaccurateNumericLiteral
[javascript] InaccurateNumericLiteral - consider underscores #4169
This commit is contained in:
@ -55,6 +55,8 @@ The rule is part of the quickstart.xml ruleset.
|
||||
* java-errorprone
|
||||
* [#929](https://github.com/pmd/pmd/issues/929): \[java] Inconsistent results with TestClassWithoutTestCases
|
||||
* [#2636](https://github.com/pmd/pmd/issues/2636): \[java] TestClassWithoutTestCases false positive with JUnit5 ParameterizedTest
|
||||
* javascript
|
||||
* [#4165](https://github.com/pmd/pmd/issues/4165): \[javascript] InaccurateNumericLiteral underscore separator notation false positive
|
||||
|
||||
### API Changes
|
||||
|
||||
|
@ -23,7 +23,10 @@ public class ASTNumberLiteral extends AbstractEcmascriptNode<NumberLiteral> {
|
||||
|
||||
public String getNormalizedImage() {
|
||||
String image = getImage();
|
||||
image = image.replaceAll("_", "");
|
||||
image = normalizeHexIntegerLiteral(image);
|
||||
image = normalizeBinaryLiteral(image);
|
||||
image = normalizeOctalLiteral(image);
|
||||
image = image.replace('e', 'E');
|
||||
if (image.indexOf('.') == -1 && image.indexOf('E') == -1) {
|
||||
image = image + ".0";
|
||||
@ -38,6 +41,20 @@ public class ASTNumberLiteral extends AbstractEcmascriptNode<NumberLiteral> {
|
||||
return image;
|
||||
}
|
||||
|
||||
private String normalizeBinaryLiteral(String image) {
|
||||
if (image.startsWith("0b") || image.startsWith("0B")) {
|
||||
return String.valueOf(Integer.parseInt(image.substring(2), 2));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
private String normalizeOctalLiteral(String image) {
|
||||
if (image.startsWith("0o") || image.startsWith("0O")) {
|
||||
return String.valueOf(Integer.parseInt(image.substring(2), 8));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
public double getNumber() {
|
||||
return node.getNumber();
|
||||
}
|
||||
|
@ -58,4 +58,17 @@ var hex1 = 0x20;
|
||||
var hex2 = 0X20;
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>[javascript] InaccurateNumericLiteral underscore separator notation false positive #4165</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
const value1a = 1_000_000; // number
|
||||
const value1b = 1_000.12_34; // decimal
|
||||
const value2 = 0b1010_0001_1000_0101; // binary
|
||||
const value3 = 0xA0_B0; // hex
|
||||
const value4 = 9_223_372_036_854_775_807n; // big int
|
||||
const value5 = 0o1234_5670; // octal
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
Reference in New Issue
Block a user