Skip to content

Commit 7e34b77

Browse files
sxyaziota-meshi
andauthored
feat: add no-trailing-zeros rule (#236)
* feat: add `no-trailing-zeros` rule * docs: add `no-trailing-zeros` doc * chore: lint * chore: remove unused schema * fix: test * docs: fix * fix: revert to fix tests temporarily * Revert "fix: revert to fix tests temporarily" This reverts commit 9582fd1. * Create sixty-cougars-ring.md * add more tests & update docs --------- Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
1 parent dc9edb1 commit 7e34b77

File tree

12 files changed

+248
-0
lines changed

12 files changed

+248
-0
lines changed

.changeset/sixty-cougars-ring.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-yml": minor
3+
---
4+
5+
feat: add `yml/no-trailing-zeros` rule

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ The rules with the following star :star: are included in the config.
207207
| [yml/no-empty-mapping-value](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-empty-mapping-value.html) | disallow empty mapping values | | :star: | :star: |
208208
| [yml/no-empty-sequence-entry](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-empty-sequence-entry.html) | disallow empty sequence entries | | :star: | :star: |
209209
| [yml/no-tab-indent](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-tab-indent.html) | disallow tabs for indentation. | | :star: | :star: |
210+
| [yml/no-trailing-zeros](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-trailing-zeros.html) | disallow trailing zeros for floats | :wrench: | | |
210211
| [yml/plain-scalar](https://ota-meshi.github.io/eslint-plugin-yml/rules/plain-scalar.html) | require or disallow plain style scalar. | :wrench: | | :star: |
211212
| [yml/quotes](https://ota-meshi.github.io/eslint-plugin-yml/rules/quotes.html) | enforce the consistent use of either double, or single quotes | :wrench: | | :star: |
212213
| [yml/require-string-key](https://ota-meshi.github.io/eslint-plugin-yml/rules/require-string-key.html) | disallow mapping keys other than strings | | | |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The rules with the following star :star: are included in the `plugin:yml/recomme
2626
| [yml/no-empty-mapping-value](./no-empty-mapping-value.md) | disallow empty mapping values | | :star: | :star: |
2727
| [yml/no-empty-sequence-entry](./no-empty-sequence-entry.md) | disallow empty sequence entries | | :star: | :star: |
2828
| [yml/no-tab-indent](./no-tab-indent.md) | disallow tabs for indentation. | | :star: | :star: |
29+
| [yml/no-trailing-zeros](./no-trailing-zeros.md) | disallow trailing zeros for floats | :wrench: | | |
2930
| [yml/plain-scalar](./plain-scalar.md) | require or disallow plain style scalar. | :wrench: | | :star: |
3031
| [yml/quotes](./quotes.md) | enforce the consistent use of either double, or single quotes | :wrench: | | :star: |
3132
| [yml/require-string-key](./require-string-key.md) | disallow mapping keys other than strings | | | |

docs/rules/no-trailing-zeros.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "yml/no-trailing-zeros"
5+
description: "disallow trailing zeros for floats"
6+
---
7+
8+
# yml/no-trailing-zeros
9+
10+
> disallow trailing zeros for floats
11+
12+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
13+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
14+
15+
## :book: Rule Details
16+
17+
This rule enforces the removal of unnecessary trailing zeros from floats.
18+
19+
<eslint-code-block fix>
20+
21+
<!-- eslint-skip -->
22+
23+
```yaml
24+
# eslint yml/no-trailing-zeros: 'error'
25+
26+
# ✓ GOOD
27+
"GOOD": 1.2
28+
29+
# ✗ BAD
30+
'BAD': 1.20
31+
```
32+
33+
</eslint-code-block>
34+
35+
## :wrench: Options
36+
37+
Nothing.
38+
39+
## :mag: Implementation
40+
41+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-yml/blob/master/src/rules/no-trailing-zeros.ts)
42+
- [Test source](https://github.com/ota-meshi/eslint-plugin-yml/blob/master/tests/src/rules/no-trailing-zeros.ts)
43+
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-yml/tree/master/tests/fixtures/rules/no-trailing-zeros)

src/configs/prettier.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export = {
1515
"yml/indent": "off",
1616
"yml/key-spacing": "off",
1717
"yml/no-multiple-empty-lines": "off",
18+
"yml/no-trailing-zeros": "off",
1819
"yml/quotes": "off",
1920
},
2021
};

src/rules/no-trailing-zeros.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { AST } from "yaml-eslint-parser";
2+
import { createRule } from "../utils";
3+
4+
export default createRule("no-trailing-zeros", {
5+
meta: {
6+
docs: {
7+
description: "disallow trailing zeros for floats",
8+
categories: null,
9+
extensionRule: false,
10+
layout: true,
11+
},
12+
fixable: "code",
13+
schema: [],
14+
messages: {
15+
wrongZeros: "Trailing zeros are not allowed, fix to `{{fixed}}`.",
16+
},
17+
type: "layout",
18+
},
19+
create(context) {
20+
if (!context.parserServices.isYAML) {
21+
return {};
22+
}
23+
24+
return {
25+
YAMLScalar(node: AST.YAMLScalar) {
26+
if (node.style !== "plain") {
27+
return;
28+
} else if (typeof node.value !== "number") {
29+
return;
30+
} else if (!node.strValue.endsWith("0")) {
31+
return;
32+
}
33+
34+
const parts = node.strValue.split(".");
35+
if (parts.length !== 2) {
36+
return;
37+
}
38+
39+
while (parts[1].endsWith("0")) {
40+
parts[1] = parts[1].slice(0, -1);
41+
}
42+
const fixed = parts[1] ? parts.join(".") : parts[0] || "0";
43+
44+
context.report({
45+
node,
46+
messageId: "wrongZeros",
47+
data: {
48+
fixed,
49+
},
50+
fix(fixer) {
51+
return fixer.replaceText(node, fixed);
52+
},
53+
});
54+
},
55+
};
56+
},
57+
});

src/utils/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import noEmptySequenceEntry from "../rules/no-empty-sequence-entry";
1919
import noIrregularWhitespace from "../rules/no-irregular-whitespace";
2020
import noMultipleEmptyLines from "../rules/no-multiple-empty-lines";
2121
import noTabIndent from "../rules/no-tab-indent";
22+
import noTrailingZeros from "../rules/no-trailing-zeros";
2223
import plainScalar from "../rules/plain-scalar";
2324
import quotes from "../rules/quotes";
2425
import requireStringKey from "../rules/require-string-key";
@@ -48,6 +49,7 @@ export const rules = [
4849
noIrregularWhitespace,
4950
noMultipleEmptyLines,
5051
noTabIndent,
52+
noTrailingZeros,
5153
plainScalar,
5254
quotes,
5355
requireStringKey,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"message": "Trailing zeros are not allowed, fix to `1.2`.",
4+
"line": 4,
5+
"column": 10
6+
},
7+
{
8+
"message": "Trailing zeros are not allowed, fix to `.2`.",
9+
"line": 6,
10+
"column": 10
11+
},
12+
{
13+
"message": "Trailing zeros are not allowed, fix to `1`.",
14+
"line": 7,
15+
"column": 10
16+
},
17+
{
18+
"message": "Trailing zeros are not allowed, fix to `0`.",
19+
"line": 8,
20+
"column": 10
21+
},
22+
{
23+
"message": "Trailing zeros are not allowed, fix to `+1`.",
24+
"line": 9,
25+
"column": 10
26+
},
27+
{
28+
"message": "Trailing zeros are not allowed, fix to `-1`.",
29+
"line": 10,
30+
"column": 10
31+
},
32+
{
33+
"message": "Trailing zeros are not allowed, fix to `1.2`.",
34+
"line": 14,
35+
"column": 12
36+
},
37+
{
38+
"message": "Trailing zeros are not allowed, fix to `.2`.",
39+
"line": 16,
40+
"column": 12
41+
},
42+
{
43+
"message": "Trailing zeros are not allowed, fix to `1`.",
44+
"line": 17,
45+
"column": 12
46+
},
47+
{
48+
"message": "Trailing zeros are not allowed, fix to `0`.",
49+
"line": 18,
50+
"column": 12
51+
},
52+
{
53+
"message": "Trailing zeros are not allowed, fix to `+1`.",
54+
"line": 19,
55+
"column": 12
56+
},
57+
{
58+
"message": "Trailing zeros are not allowed, fix to `-1`.",
59+
"line": 20,
60+
"column": 12
61+
}
62+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# {"options": []}
2+
- {
3+
a: 1.23,
4+
b: 1.20,
5+
c: .23,
6+
d: .20,
7+
e: 1.0,
8+
f: .0,
9+
g: +1.0,
10+
h: -1.0
11+
}
12+
-
13+
- "a": 1.23
14+
- "b": 1.20
15+
- "c": .23
16+
- "d": .20
17+
- "e": 1.0
18+
- "f": .0
19+
- "g": +1.0
20+
- "h": -1.0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# no-trailing-zeros/invalid/input.yml
2+
- {
3+
a: 1.23,
4+
b: 1.2,
5+
c: .23,
6+
d: .2,
7+
e: 1,
8+
f: 0,
9+
g: +1,
10+
h: -1
11+
}
12+
-
13+
- "a": 1.23
14+
- "b": 1.2
15+
- "c": .23
16+
- "d": .2
17+
- "e": 1
18+
- "f": 0
19+
- "g": +1
20+
- "h": -1

0 commit comments

Comments
 (0)