-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
12 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-yml": minor | ||
--- | ||
|
||
feat: add `yml/no-trailing-zeros` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "yml/no-trailing-zeros" | ||
description: "disallow trailing zeros for floats" | ||
--- | ||
|
||
# yml/no-trailing-zeros | ||
|
||
> disallow trailing zeros for floats | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
- :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. | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces the removal of unnecessary trailing zeros from floats. | ||
|
||
<eslint-code-block fix> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```yaml | ||
# eslint yml/no-trailing-zeros: 'error' | ||
|
||
# ✓ GOOD | ||
"GOOD": 1.2 | ||
|
||
# ✗ BAD | ||
'BAD': 1.20 | ||
``` | ||
</eslint-code-block> | ||
## :wrench: Options | ||
Nothing. | ||
## :mag: Implementation | ||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-yml/blob/master/src/rules/no-trailing-zeros.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-yml/blob/master/tests/src/rules/no-trailing-zeros.ts) | ||
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-yml/tree/master/tests/fixtures/rules/no-trailing-zeros) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { AST } from "yaml-eslint-parser"; | ||
import { createRule } from "../utils"; | ||
|
||
export default createRule("no-trailing-zeros", { | ||
meta: { | ||
docs: { | ||
description: "disallow trailing zeros for floats", | ||
categories: null, | ||
extensionRule: false, | ||
layout: true, | ||
}, | ||
fixable: "code", | ||
schema: [], | ||
messages: { | ||
wrongZeros: "Trailing zeros are not allowed, fix to `{{fixed}}`.", | ||
}, | ||
type: "layout", | ||
}, | ||
create(context) { | ||
if (!context.parserServices.isYAML) { | ||
return {}; | ||
} | ||
|
||
return { | ||
YAMLScalar(node: AST.YAMLScalar) { | ||
if (node.style !== "plain") { | ||
return; | ||
} else if (typeof node.value !== "number") { | ||
return; | ||
} else if (!node.strValue.endsWith("0")) { | ||
return; | ||
} | ||
|
||
const parts = node.strValue.split("."); | ||
if (parts.length !== 2) { | ||
return; | ||
} | ||
|
||
while (parts[1].endsWith("0")) { | ||
parts[1] = parts[1].slice(0, -1); | ||
} | ||
const fixed = parts[1] ? parts.join(".") : parts[0] || "0"; | ||
|
||
context.report({ | ||
node, | ||
messageId: "wrongZeros", | ||
data: { | ||
fixed, | ||
}, | ||
fix(fixer) { | ||
return fixer.replaceText(node, fixed); | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/fixtures/rules/no-trailing-zeros/invalid/errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
[ | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `1.2`.", | ||
"line": 4, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `.2`.", | ||
"line": 6, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `1`.", | ||
"line": 7, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `0`.", | ||
"line": 8, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `+1`.", | ||
"line": 9, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `-1`.", | ||
"line": 10, | ||
"column": 10 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `1.2`.", | ||
"line": 14, | ||
"column": 12 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `.2`.", | ||
"line": 16, | ||
"column": 12 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `1`.", | ||
"line": 17, | ||
"column": 12 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `0`.", | ||
"line": 18, | ||
"column": 12 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `+1`.", | ||
"line": 19, | ||
"column": 12 | ||
}, | ||
{ | ||
"message": "Trailing zeros are not allowed, fix to `-1`.", | ||
"line": 20, | ||
"column": 12 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# {"options": []} | ||
- { | ||
a: 1.23, | ||
b: 1.20, | ||
c: .23, | ||
d: .20, | ||
e: 1.0, | ||
f: .0, | ||
g: +1.0, | ||
h: -1.0 | ||
} | ||
- | ||
- "a": 1.23 | ||
- "b": 1.20 | ||
- "c": .23 | ||
- "d": .20 | ||
- "e": 1.0 | ||
- "f": .0 | ||
- "g": +1.0 | ||
- "h": -1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# no-trailing-zeros/invalid/input.yml | ||
- { | ||
a: 1.23, | ||
b: 1.2, | ||
c: .23, | ||
d: .2, | ||
e: 1, | ||
f: 0, | ||
g: +1, | ||
h: -1 | ||
} | ||
- | ||
- "a": 1.23 | ||
- "b": 1.2 | ||
- "c": .23 | ||
- "d": .2 | ||
- "e": 1 | ||
- "f": 0 | ||
- "g": +1 | ||
- "h": -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# {"options": []} | ||
- { | ||
a: 1.23, | ||
b: "1.20", | ||
c: .23, | ||
d: .2, | ||
e: "1.0", | ||
f: ".0", | ||
g: "+1.0", | ||
h: "-1.0" | ||
} | ||
- | ||
- "a": 1.23 | ||
- "b": "1.20" | ||
- "c": .23 | ||
- "d": .2 | ||
- "e": "1.0" | ||
- "f": ".0" | ||
- "g": "+1.0" | ||
- "h": "-1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { RuleTester } from "eslint"; | ||
import rule from "../../../src/rules/no-trailing-zeros"; | ||
import { loadTestCases } from "../../utils/utils"; | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve("yaml-eslint-parser"), | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
}, | ||
}); | ||
|
||
tester.run( | ||
"no-trailing-zeros", | ||
rule as any, | ||
loadTestCases("no-trailing-zeros") | ||
); |