File tree Expand file tree Collapse file tree 12 files changed +248
-0
lines changed
fixtures/rules/no-trailing-zeros Expand file tree Collapse file tree 12 files changed +248
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " eslint-plugin-yml " : minor
3
+ ---
4
+
5
+ feat: add ` yml/no-trailing-zeros ` rule
Original file line number Diff line number Diff line change @@ -207,6 +207,7 @@ The rules with the following star :star: are included in the config.
207
207
| [ 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 : |
208
208
| [ 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 : |
209
209
| [ 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 : | | |
210
211
| [ yml/plain-scalar] ( https://ota-meshi.github.io/eslint-plugin-yml/rules/plain-scalar.html ) | require or disallow plain style scalar. | :wrench : | | :star : |
211
212
| [ 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 : |
212
213
| [ yml/require-string-key] ( https://ota-meshi.github.io/eslint-plugin-yml/rules/require-string-key.html ) | disallow mapping keys other than strings | | | |
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ The rules with the following star :star: are included in the `plugin:yml/recomme
26
26
| [ yml/no-empty-mapping-value] ( ./no-empty-mapping-value.md ) | disallow empty mapping values | | :star : | :star : |
27
27
| [ yml/no-empty-sequence-entry] ( ./no-empty-sequence-entry.md ) | disallow empty sequence entries | | :star : | :star : |
28
28
| [ 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 : | | |
29
30
| [ yml/plain-scalar] ( ./plain-scalar.md ) | require or disallow plain style scalar. | :wrench : | | :star : |
30
31
| [ yml/quotes] ( ./quotes.md ) | enforce the consistent use of either double, or single quotes | :wrench : | | :star : |
31
32
| [ yml/require-string-key] ( ./require-string-key.md ) | disallow mapping keys other than strings | | | |
Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ export = {
15
15
"yml/indent" : "off" ,
16
16
"yml/key-spacing" : "off" ,
17
17
"yml/no-multiple-empty-lines" : "off" ,
18
+ "yml/no-trailing-zeros" : "off" ,
18
19
"yml/quotes" : "off" ,
19
20
} ,
20
21
} ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import noEmptySequenceEntry from "../rules/no-empty-sequence-entry";
19
19
import noIrregularWhitespace from "../rules/no-irregular-whitespace" ;
20
20
import noMultipleEmptyLines from "../rules/no-multiple-empty-lines" ;
21
21
import noTabIndent from "../rules/no-tab-indent" ;
22
+ import noTrailingZeros from "../rules/no-trailing-zeros" ;
22
23
import plainScalar from "../rules/plain-scalar" ;
23
24
import quotes from "../rules/quotes" ;
24
25
import requireStringKey from "../rules/require-string-key" ;
@@ -48,6 +49,7 @@ export const rules = [
48
49
noIrregularWhitespace ,
49
50
noMultipleEmptyLines ,
50
51
noTabIndent ,
52
+ noTrailingZeros ,
51
53
plainScalar ,
52
54
quotes ,
53
55
requireStringKey ,
Original file line number Diff line number Diff line change
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
+ ]
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments