Skip to content

Commit

Permalink
Merge pull request #4169 from adangel:issue-4165-InaccurateNumericLit…
Browse files Browse the repository at this point in the history
…eral

[javascript] InaccurateNumericLiteral - consider underscores #4169
  • Loading branch information
adangel committed Oct 28, 2022
2 parents e59d25b + c7acac7 commit 66d67c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Expand Up @@ -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

Expand Down
Expand Up @@ -23,7 +23,10 @@ public Object jjtAccept(EcmascriptParserVisitor visitor, Object data) {

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";
Expand All @@ -38,6 +41,20 @@ private String normalizeHexIntegerLiteral(String image) {
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();
}
Expand Down
Expand Up @@ -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>

0 comments on commit 66d67c7

Please sign in to comment.