[javascript] InaccurateNumericLiteral - consider underscores
Fixes #4165
This commit is contained in:
@ -33,6 +33,8 @@ The rule is part of the quickstart.xml ruleset.
|
|||||||
* [#4163](https://github.com/pmd/pmd/issues/4163): \[doc] Broken links on page "Architecture Decisions"
|
* [#4163](https://github.com/pmd/pmd/issues/4163): \[doc] Broken links on page "Architecture Decisions"
|
||||||
* java-documentation
|
* java-documentation
|
||||||
* [#4141](https://github.com/pmd/pmd/issues/4141): \[java] UncommentedEmptyConstructor FP when constructor annotated with @<!-- -->Autowired
|
* [#4141](https://github.com/pmd/pmd/issues/4141): \[java] UncommentedEmptyConstructor FP when constructor annotated with @<!-- -->Autowired
|
||||||
|
* javascript
|
||||||
|
* [#4165](https://github.com/pmd/pmd/issues/4165): \[javascript] InaccurateNumericLiteral underscore separator notation false positive
|
||||||
|
|
||||||
### API Changes
|
### API Changes
|
||||||
|
|
||||||
|
@ -23,7 +23,10 @@ public class ASTNumberLiteral extends AbstractEcmascriptNode<NumberLiteral> {
|
|||||||
|
|
||||||
public String getNormalizedImage() {
|
public String getNormalizedImage() {
|
||||||
String image = getImage();
|
String image = getImage();
|
||||||
|
image = image.replaceAll("_", "");
|
||||||
image = normalizeHexIntegerLiteral(image);
|
image = normalizeHexIntegerLiteral(image);
|
||||||
|
image = normalizeBinaryLiteral(image);
|
||||||
|
image = normalizeOctalLiteral(image);
|
||||||
image = image.replace('e', 'E');
|
image = image.replace('e', 'E');
|
||||||
if (image.indexOf('.') == -1 && image.indexOf('E') == -1) {
|
if (image.indexOf('.') == -1 && image.indexOf('E') == -1) {
|
||||||
image = image + ".0";
|
image = image + ".0";
|
||||||
@ -38,6 +41,20 @@ public class ASTNumberLiteral extends AbstractEcmascriptNode<NumberLiteral> {
|
|||||||
return 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() {
|
public double getNumber() {
|
||||||
return node.getNumber();
|
return node.getNumber();
|
||||||
}
|
}
|
||||||
|
@ -58,4 +58,17 @@ var hex1 = 0x20;
|
|||||||
var hex2 = 0X20;
|
var hex2 = 0X20;
|
||||||
]]></code>
|
]]></code>
|
||||||
</test-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>
|
</test-data>
|
||||||
|
Reference in New Issue
Block a user