Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add no-trailing-zeros rule #236

Merged
merged 10 commits into from May 7, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -225,6 +225,7 @@ The rules with the following star :star: are included in the config.
| [yml/key-spacing](https://ota-meshi.github.io/eslint-plugin-yml/rules/key-spacing.html) | enforce consistent spacing between keys and values in mapping pairs | :wrench: | | :star: |
| [yml/no-irregular-whitespace](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-irregular-whitespace.html) | disallow irregular whitespace | | :star: | :star: |
| [yml/no-multiple-empty-lines](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-multiple-empty-lines.html) | disallow multiple empty lines | :wrench: | | |
| [yml/no-trailing-zeros](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-trailing-zeros.html) | disallow trailing zeros for numbers. | | | :star: |
| [yml/spaced-comment](https://ota-meshi.github.io/eslint-plugin-yml/rules/spaced-comment.html) | enforce consistent spacing after the `#` in a comment | :wrench: | | :star: |

<!--RULES_TABLE_END-->
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -44,4 +44,5 @@ The rules with the following star :star: are included in the `plugin:yml/recomme
| [yml/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in mapping pairs | :wrench: | | :star: |
| [yml/no-irregular-whitespace](./no-irregular-whitespace.md) | disallow irregular whitespace | | :star: | :star: |
| [yml/no-multiple-empty-lines](./no-multiple-empty-lines.md) | disallow multiple empty lines | :wrench: | | |
| [yml/no-trailing-zeros](./no-trailing-zeros.md) | disallow trailing zeros for numbers. | | | :star: |
| [yml/spaced-comment](./spaced-comment.md) | enforce consistent spacing after the `#` in a comment | :wrench: | | :star: |
48 changes: 48 additions & 0 deletions docs/rules/no-trailing-zeros.md
@@ -0,0 +1,48 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "yml/no-trailing-zeros"
description: "disallow trailing zeros for numbers"
since: "v1.5.1"
sxyazi marked this conversation as resolved.
Show resolved Hide resolved
---

# yml/no-trailing-zeros

> disallow trailing zeros for numbers

- :gear: This rule is included in `"plugin:yml/standard"`.
- :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 numbers.
sxyazi marked this conversation as resolved.
Show resolved Hide resolved

<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.

## :rocket: Version

This rule was introduced in eslint-plugin-yml v1.5.1
sxyazi marked this conversation as resolved.
Show resolved Hide resolved

## :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)
1 change: 1 addition & 0 deletions src/configs/standard.ts
Expand Up @@ -21,6 +21,7 @@ export = {
"yml/no-empty-sequence-entry": "error",
"yml/no-irregular-whitespace": "error",
"yml/no-tab-indent": "error",
"yml/no-trailing-zeros": "error",
"yml/plain-scalar": "error",
"yml/quotes": "error",
"yml/spaced-comment": "error",
Expand Down
54 changes: 54 additions & 0 deletions src/rules/no-trailing-zeros.ts
@@ -0,0 +1,54 @@
import type { AST } from "yaml-eslint-parser";
import { createRule } from "../utils";

export default createRule("no-trailing-zeros", {
meta: {
docs: {
description: "disallow trailing zeros",
categories: ["standard"],
sxyazi marked this conversation as resolved.
Show resolved Hide resolved
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;
}

// https://github.com/stylelint/stylelint/blob/650f597806679a3d2eb57672c931b1a5e2acd0d6/lib/rules/number-no-trailing-zeros/index.js#LL67C25-L67C25
const match = /(\d*)\.(\d{0,100}?)(0+)(?:\D|$)/.exec(node.strValue);
sxyazi marked this conversation as resolved.
Show resolved Hide resolved
// match[2] is any numbers between the decimal and our trailing zero, could be empty
// match[3] is our trailing zero(s)
if (match === null || match[2] === null || match[3] === null) {
return;
}

const fixed = node.raw.replace(match[0], `${match[1]}.${match[2]}`);
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
context.report({
node,
messageId: "wrongZeros",
data: {
fixed,
},
fix(fixer) {
return fixer.replaceText(node, fixed);
},
});
},
};
},
});
2 changes: 2 additions & 0 deletions src/utils/rules.ts
Expand Up @@ -19,6 +19,7 @@ import noEmptySequenceEntry from "../rules/no-empty-sequence-entry";
import noIrregularWhitespace from "../rules/no-irregular-whitespace";
import noMultipleEmptyLines from "../rules/no-multiple-empty-lines";
import noTabIndent from "../rules/no-tab-indent";
import noTrailingZeros from "../rules/no-trailing-zeros";
import plainScalar from "../rules/plain-scalar";
import quotes from "../rules/quotes";
import requireStringKey from "../rules/require-string-key";
Expand Down Expand Up @@ -48,6 +49,7 @@ export const rules = [
noIrregularWhitespace,
noMultipleEmptyLines,
noTabIndent,
noTrailingZeros,
plainScalar,
quotes,
requireStringKey,
Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/rules/no-trailing-zeros/invalid/errors.json
@@ -0,0 +1,22 @@
[
{
"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.2`.",
"line": 10,
"column": 12
},
{
"message": "Trailing zeros are not allowed, fix to `.2`.",
"line": 12,
"column": 12
}
]
12 changes: 12 additions & 0 deletions tests/fixtures/rules/no-trailing-zeros/invalid/input.yml
@@ -0,0 +1,12 @@
# {"options": []}
- {
a: 1.23,
b: 1.20,
c: .23,
d: .20
}
-
- "a": 1.23
- "b": 1.20
- "c": .23
- "d": .20
sxyazi marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions tests/fixtures/rules/no-trailing-zeros/invalid/output.yml
@@ -0,0 +1,12 @@
# no-trailing-zeros/invalid/input.yml
- {
a: 1.23,
b: 1.2,
c: .23,
d: .2
}
-
- "a": 1.23
- "b": 1.2
- "c": .23
- "d": .2
12 changes: 12 additions & 0 deletions tests/fixtures/rules/no-trailing-zeros/valid/input.yml
@@ -0,0 +1,12 @@
# {"options": []}
- {
a: 1.23,
b: "1.20",
c: .23,
d: .2
}
-
- "a": 1.23
- "b": "1.20"
- "c": .23
- "d": .2
16 changes: 16 additions & 0 deletions tests/src/rules/no-trailing-zeros.ts
@@ -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")
);