Skip to content

Commit

Permalink
test: add more testcases for no-trailing-zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed May 7, 2023
1 parent 7e34b77 commit 829d9cf
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"debug": "^4.3.2",
"lodash": "^4.17.21",
"natural-compare": "^1.4.0",
"yaml-eslint-parser": "^1.1.0"
"yaml-eslint-parser": "^1.2.1"
},
"peerDependencies": {
"eslint": ">=6.0.0"
Expand Down
57 changes: 49 additions & 8 deletions src/rules/no-trailing-zeros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,28 @@ export default createRule("no-trailing-zeros", {
return;
} else if (typeof node.value !== "number") {
return;
} else if (!node.strValue.endsWith("0")) {
return;
}

const parts = node.strValue.split(".");
if (parts.length !== 2) {
const floating = parseFloatingPoint(node.strValue);
if (!floating) {
return;
}

while (parts[1].endsWith("0")) {
parts[1] = parts[1].slice(0, -1);
let { decimalPart } = floating;
while (decimalPart.endsWith("_")) {
decimalPart = decimalPart.slice(0, -1);
}
if (!decimalPart.endsWith("0")) {
return;
}
const fixed = parts[1] ? parts.join(".") : parts[0] || "0";
while (decimalPart.endsWith("0")) {
decimalPart = decimalPart.slice(0, -1);
while (decimalPart.endsWith("_")) {
decimalPart = decimalPart.slice(0, -1);
}
}
const fixed = decimalPart
? `${floating.sign}${floating.intPart}.${decimalPart}${floating.expPart}`
: `${floating.sign}${floating.intPart || "0"}${floating.expPart}`;

context.report({
node,
Expand All @@ -55,3 +64,35 @@ export default createRule("no-trailing-zeros", {
};
},
});

/** Parse floating point number string */
function parseFloatingPoint(str: string) {
const parts = str.split(".");
if (parts.length !== 2) {
// No floating point present.
return null;
}
let decimalPart: string, expPart: string, intPart: string, sign: string;
const expIndex = parts[1].search(/e/iu);
if (expIndex >= 0) {
decimalPart = parts[1].slice(0, expIndex);
expPart = parts[1].slice(expIndex);
} else {
decimalPart = parts[1];
expPart = "";
}
if (parts[0].startsWith("-") || parts[0].startsWith("+")) {
sign = parts[0][0];
intPart = parts[0].slice(1);
} else {
sign = "";
intPart = parts[0];
}

return {
sign,
intPart,
decimalPart,
expPart,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,15 @@
"message": "Trailing zeros are not allowed, fix to `-1`.",
"line": 20,
"column": 12
},
{
"message": "Trailing zeros are not allowed, fix to `+0`.",
"line": 21,
"column": 12
},
{
"message": "Trailing zeros are not allowed, fix to `-0`.",
"line": 22,
"column": 12
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
- "f": .0
- "g": +1.0
- "h": -1.0
- "i": +.0
- "j": -.0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# no-trailing-zeros/invalid/input.yml
# no-trailing-zeros/invalid/basic-test-input.yml
- {
a: 1.23,
b: 1.2,
Expand All @@ -18,3 +18,5 @@
- "f": 0
- "g": +1
- "h": -1
- "i": +0
- "j": -0
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[
{
"message": "Trailing zeros are not allowed, fix to `1.2e20`.",
"line": 3,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `.2e20`.",
"line": 5,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `1e20`.",
"line": 6,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `0e20`.",
"line": 7,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `+1e20`.",
"line": 8,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `-1e20`.",
"line": 9,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `+0e20`.",
"line": 10,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `-0e20`.",
"line": 11,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `1.2E20`.",
"line": 13,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `.2E20`.",
"line": 15,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `1E20`.",
"line": 16,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `0E20`.",
"line": 17,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `+1E20`.",
"line": 18,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `-1E20`.",
"line": 19,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `+0E20`.",
"line": 20,
"column": 3
},
{
"message": "Trailing zeros are not allowed, fix to `-0E20`.",
"line": 21,
"column": 3
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# {"options": []}
- 1.23e20
- 1.20e20
- .23e20
- .20e20
- 1.0e20
- .0e20
- +1.0e20
- -1.0e20
- +.0e20
- -.0e20
- 1.23E20
- 1.20E20
- .23E20
- .20E20
- 1.0E20
- .0E20
- +1.0E20
- -1.0E20
- +.0E20
- -.0E20
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# no-trailing-zeros/invalid/exponential-test-input.yml
- 1.23e20
- 1.2e20
- .23e20
- .2e20
- 1e20
- 0e20
- +1e20
- -1e20
- +0e20
- -0e20
- 1.23E20
- 1.2E20
- .23E20
- .2E20
- 1E20
- 0E20
- +1E20
- -1E20
- +0E20
- -0E20
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"message": "Trailing zeros are not allowed, fix to `1_2.3`.",
"line": 4,
"column": 4
},
{
"message": "Trailing zeros are not allowed, fix to `1_2.3`.",
"line": 5,
"column": 4
},
{
"message": "Trailing zeros are not allowed, fix to `1_2.3_4e30`.",
"line": 6,
"column": 4
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# {"options": []}
%YAML 1.1
---
a: 1_2.3_0
b: 1_2.3_0_0
c: 1_2.3_4_0__0_e30
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# no-trailing-zeros/invalid/yaml1_1-test-input.yml
%YAML 1.1
---
a: 1_2.3
b: 1_2.3
c: 1_2.3_4e30
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
- "f": ".0"
- "g": "+1.0"
- "h": "-1.0"
- "i": "+.0"
- "j": "-.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# {"options": []}
- 1.23e20
- "1.20e20"
- .23e20
- .2e20
- "1.0e20"
- ".0e20"
- "+1.0e20"
- "-1.0e20"
- "+.0e20"
- "-.0e20"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# {"options": []}
%YAML 1.1
---
a: 1_2.3_4
b: 1_2.3_4e30

0 comments on commit 829d9cf

Please sign in to comment.