From cf43badd32ca9478f62c0616ad5723d850de815c Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 5 May 2023 21:42:11 +0800 Subject: [PATCH 01/10] feat: add `no-trailing-zeros` rule --- src/rules/no-trailing-zeros.ts | 62 +++++++++++++++++++ src/utils/rules.ts | 2 + .../no-trailing-zeros/invalid/errors.json | 22 +++++++ .../rules/no-trailing-zeros/invalid/input.yml | 12 ++++ .../no-trailing-zeros/invalid/output.yml | 12 ++++ .../rules/no-trailing-zeros/valid/input.yml | 12 ++++ tests/src/rules/no-trailing-zeros.ts | 12 ++++ 7 files changed, 134 insertions(+) create mode 100644 src/rules/no-trailing-zeros.ts create mode 100644 tests/fixtures/rules/no-trailing-zeros/invalid/errors.json create mode 100644 tests/fixtures/rules/no-trailing-zeros/invalid/input.yml create mode 100644 tests/fixtures/rules/no-trailing-zeros/invalid/output.yml create mode 100644 tests/fixtures/rules/no-trailing-zeros/valid/input.yml create mode 100644 tests/src/rules/no-trailing-zeros.ts diff --git a/src/rules/no-trailing-zeros.ts b/src/rules/no-trailing-zeros.ts new file mode 100644 index 00000000..208e64a4 --- /dev/null +++ b/src/rules/no-trailing-zeros.ts @@ -0,0 +1,62 @@ +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"], + extensionRule: false, + layout: true, + }, + fixable: "code", + schema: [ + { + type: "object", + properties: { + prefer: { enum: ["number", "double-quotes", "single-quotes"] }, + }, + additionalProperties: false, + }, + ], + 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); + // 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]}`); + context.report({ + node, + messageId: "wrongZeros", + data: { + fixed, + }, + fix(fixer) { + return fixer.replaceText(node, fixed); + }, + }); + }, + }; + }, +}); diff --git a/src/utils/rules.ts b/src/utils/rules.ts index a5ef2728..167a4c10 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -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"; @@ -48,6 +49,7 @@ export const rules = [ noIrregularWhitespace, noMultipleEmptyLines, noTabIndent, + noTrailingZeros, plainScalar, quotes, requireStringKey, diff --git a/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json b/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json new file mode 100644 index 00000000..c2edaa04 --- /dev/null +++ b/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 + } +] diff --git a/tests/fixtures/rules/no-trailing-zeros/invalid/input.yml b/tests/fixtures/rules/no-trailing-zeros/invalid/input.yml new file mode 100644 index 00000000..b9d321e9 --- /dev/null +++ b/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 diff --git a/tests/fixtures/rules/no-trailing-zeros/invalid/output.yml b/tests/fixtures/rules/no-trailing-zeros/invalid/output.yml new file mode 100644 index 00000000..b03f296e --- /dev/null +++ b/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 diff --git a/tests/fixtures/rules/no-trailing-zeros/valid/input.yml b/tests/fixtures/rules/no-trailing-zeros/valid/input.yml new file mode 100644 index 00000000..a47e1f5b --- /dev/null +++ b/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 diff --git a/tests/src/rules/no-trailing-zeros.ts b/tests/src/rules/no-trailing-zeros.ts new file mode 100644 index 00000000..e3f2e205 --- /dev/null +++ b/tests/src/rules/no-trailing-zeros.ts @@ -0,0 +1,12 @@ +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")); From 3ad9259a422f083cb3d0d64b370399968edf06f4 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 5 May 2023 21:44:51 +0800 Subject: [PATCH 02/10] docs: add `no-trailing-zeros` doc --- docs/rules/README.md | 1 + docs/rules/no-trailing-zeros.md | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 docs/rules/no-trailing-zeros.md diff --git a/docs/rules/README.md b/docs/rules/README.md index 22a48bee..2990d507 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -26,6 +26,7 @@ The rules with the following star :star: are included in the `plugin:yml/recomme | [yml/no-empty-mapping-value](./no-empty-mapping-value.md) | disallow empty mapping values | | :star: | :star: | | [yml/no-empty-sequence-entry](./no-empty-sequence-entry.md) | disallow empty sequence entries | | :star: | :star: | | [yml/no-tab-indent](./no-tab-indent.md) | disallow tabs for indentation. | | :star: | :star: | +| [yml/no-trailing-zeros](./no-trailing-zeros.md) | disallow trailing zeros for numbers. | | | :star: | | [yml/plain-scalar](./plain-scalar.md) | require or disallow plain style scalar. | :wrench: | | :star: | | [yml/quotes](./quotes.md) | enforce the consistent use of either double, or single quotes | :wrench: | | :star: | | [yml/require-string-key](./require-string-key.md) | disallow mapping keys other than strings | | | | diff --git a/docs/rules/no-trailing-zeros.md b/docs/rules/no-trailing-zeros.md new file mode 100644 index 00000000..39cf6a7d --- /dev/null +++ b/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" +--- + +# 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. + + + + + +```yaml +# eslint yml/no-trailing-zeros: 'error' + +# ✓ GOOD +"GOOD": 1.2 + +# ✗ BAD +'BAD': 1.20 +``` + + + +## :wrench: Options + +Nothing. + +## :rocket: Version + +This rule was introduced in eslint-plugin-yml v1.5.1 + +## :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) From d0c0a0db11ff473e58e8b588e57360c47bbcb653 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 5 May 2023 22:13:28 +0800 Subject: [PATCH 03/10] chore: lint --- tests/src/rules/no-trailing-zeros.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/src/rules/no-trailing-zeros.ts b/tests/src/rules/no-trailing-zeros.ts index e3f2e205..31789409 100644 --- a/tests/src/rules/no-trailing-zeros.ts +++ b/tests/src/rules/no-trailing-zeros.ts @@ -9,4 +9,8 @@ const tester = new RuleTester({ }, }); -tester.run("no-trailing-zeros", rule as any, loadTestCases("no-trailing-zeros")); +tester.run( + "no-trailing-zeros", + rule as any, + loadTestCases("no-trailing-zeros") +); From 044021f4110aff6eecd7e8e495d3fd25a9836d9a Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 5 May 2023 23:03:28 +0800 Subject: [PATCH 04/10] chore: remove unused schema --- src/rules/no-trailing-zeros.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/rules/no-trailing-zeros.ts b/src/rules/no-trailing-zeros.ts index 208e64a4..e50efbe4 100644 --- a/src/rules/no-trailing-zeros.ts +++ b/src/rules/no-trailing-zeros.ts @@ -10,17 +10,9 @@ export default createRule("no-trailing-zeros", { layout: true, }, fixable: "code", - schema: [ - { - type: "object", - properties: { - prefer: { enum: ["number", "double-quotes", "single-quotes"] }, - }, - additionalProperties: false, - }, - ], + schema: [], messages: { - wrongZeros: "Trailing zeros are not allowed, fix to: {{fixed}}", + wrongZeros: "Trailing zeros are not allowed, fix to `{{fixed}}`", }, type: "layout", }, From 8cecae4375aa0764a453e71e1cff9675fc3b7927 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 5 May 2023 23:09:46 +0800 Subject: [PATCH 05/10] fix: test --- src/rules/no-trailing-zeros.ts | 2 +- .../fixtures/rules/no-trailing-zeros/invalid/errors.json | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/rules/no-trailing-zeros.ts b/src/rules/no-trailing-zeros.ts index e50efbe4..dd66278d 100644 --- a/src/rules/no-trailing-zeros.ts +++ b/src/rules/no-trailing-zeros.ts @@ -12,7 +12,7 @@ export default createRule("no-trailing-zeros", { fixable: "code", schema: [], messages: { - wrongZeros: "Trailing zeros are not allowed, fix to `{{fixed}}`", + wrongZeros: "Trailing zeros are not allowed, fix to `{{fixed}}`.", }, type: "layout", }, diff --git a/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json b/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json index c2edaa04..b001f78c 100644 --- a/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json +++ b/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json @@ -1,21 +1,21 @@ [ { - "message": "Trailing zeros are not allowed, fix to: 1.2", + "message": "Trailing zeros are not allowed, fix to `1.2`.", "line": 4, "column": 10 }, { - "message": "Trailing zeros are not allowed, fix to: .2", + "message": "Trailing zeros are not allowed, fix to `.2`.", "line": 6, "column": 10 }, { - "message": "Trailing zeros are not allowed, fix to: 1.2", + "message": "Trailing zeros are not allowed, fix to `1.2`.", "line": 10, "column": 12 }, { - "message": "Trailing zeros are not allowed, fix to: .2", + "message": "Trailing zeros are not allowed, fix to `.2`.", "line": 12, "column": 12 } From 6e390d04d676b13ac4646171c251cf16fbdc3391 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 6 May 2023 15:39:53 +0800 Subject: [PATCH 06/10] docs: fix --- README.md | 1 + docs/rules/README.md | 2 +- src/configs/standard.ts | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce7a2e0e..4dad2cd9 100644 --- a/README.md +++ b/README.md @@ -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: | diff --git a/docs/rules/README.md b/docs/rules/README.md index 2990d507..67cdad93 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -26,7 +26,6 @@ The rules with the following star :star: are included in the `plugin:yml/recomme | [yml/no-empty-mapping-value](./no-empty-mapping-value.md) | disallow empty mapping values | | :star: | :star: | | [yml/no-empty-sequence-entry](./no-empty-sequence-entry.md) | disallow empty sequence entries | | :star: | :star: | | [yml/no-tab-indent](./no-tab-indent.md) | disallow tabs for indentation. | | :star: | :star: | -| [yml/no-trailing-zeros](./no-trailing-zeros.md) | disallow trailing zeros for numbers. | | | :star: | | [yml/plain-scalar](./plain-scalar.md) | require or disallow plain style scalar. | :wrench: | | :star: | | [yml/quotes](./quotes.md) | enforce the consistent use of either double, or single quotes | :wrench: | | :star: | | [yml/require-string-key](./require-string-key.md) | disallow mapping keys other than strings | | | | @@ -45,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: | diff --git a/src/configs/standard.ts b/src/configs/standard.ts index 25f667a6..fc6916b7 100644 --- a/src/configs/standard.ts +++ b/src/configs/standard.ts @@ -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", From 9582fd17d140d29d8f953d30bde3624125f3b1e6 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 6 May 2023 15:50:46 +0800 Subject: [PATCH 07/10] fix: revert to fix tests temporarily --- src/configs/standard.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/configs/standard.ts b/src/configs/standard.ts index fc6916b7..25f667a6 100644 --- a/src/configs/standard.ts +++ b/src/configs/standard.ts @@ -21,7 +21,6 @@ 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", From 481d481029ebde6e86e9a8458eb4ff810b0bfe92 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 6 May 2023 15:55:25 +0800 Subject: [PATCH 08/10] Revert "fix: revert to fix tests temporarily" This reverts commit 9582fd17d140d29d8f953d30bde3624125f3b1e6. --- src/configs/standard.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/configs/standard.ts b/src/configs/standard.ts index 25f667a6..fc6916b7 100644 --- a/src/configs/standard.ts +++ b/src/configs/standard.ts @@ -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", From 5dd7a9bcfaa32f997de70b0fc2c4e7357feffcc9 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sun, 7 May 2023 12:06:10 +0900 Subject: [PATCH 09/10] Create sixty-cougars-ring.md --- .changeset/sixty-cougars-ring.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sixty-cougars-ring.md diff --git a/.changeset/sixty-cougars-ring.md b/.changeset/sixty-cougars-ring.md new file mode 100644 index 00000000..a45bbedb --- /dev/null +++ b/.changeset/sixty-cougars-ring.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-yml": minor +--- + +feat: add `yml/no-trailing-zeros` rule From d0059bc7fb4cffe6d8481683aa0c33fe07a17661 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 7 May 2023 22:13:28 +0800 Subject: [PATCH 10/10] add more tests & update docs --- README.md | 2 +- docs/rules/README.md | 2 +- docs/rules/no-trailing-zeros.md | 13 ++---- src/configs/prettier.ts | 1 + src/configs/standard.ts | 1 - src/rules/no-trailing-zeros.ts | 19 ++++---- .../no-trailing-zeros/invalid/errors.json | 44 ++++++++++++++++++- .../rules/no-trailing-zeros/invalid/input.yml | 10 ++++- .../no-trailing-zeros/invalid/output.yml | 10 ++++- .../rules/no-trailing-zeros/valid/input.yml | 10 ++++- 10 files changed, 87 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4dad2cd9..0212b8ac 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,7 @@ The rules with the following star :star: are included in the config. | [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: | | [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: | | [yml/no-tab-indent](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-tab-indent.html) | disallow tabs for indentation. | | :star: | :star: | +| [yml/no-trailing-zeros](https://ota-meshi.github.io/eslint-plugin-yml/rules/no-trailing-zeros.html) | disallow trailing zeros for floats | :wrench: | | | | [yml/plain-scalar](https://ota-meshi.github.io/eslint-plugin-yml/rules/plain-scalar.html) | require or disallow plain style scalar. | :wrench: | | :star: | | [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: | | [yml/require-string-key](https://ota-meshi.github.io/eslint-plugin-yml/rules/require-string-key.html) | disallow mapping keys other than strings | | | | @@ -225,7 +226,6 @@ 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: | diff --git a/docs/rules/README.md b/docs/rules/README.md index 67cdad93..800eabfc 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -26,6 +26,7 @@ The rules with the following star :star: are included in the `plugin:yml/recomme | [yml/no-empty-mapping-value](./no-empty-mapping-value.md) | disallow empty mapping values | | :star: | :star: | | [yml/no-empty-sequence-entry](./no-empty-sequence-entry.md) | disallow empty sequence entries | | :star: | :star: | | [yml/no-tab-indent](./no-tab-indent.md) | disallow tabs for indentation. | | :star: | :star: | +| [yml/no-trailing-zeros](./no-trailing-zeros.md) | disallow trailing zeros for floats | :wrench: | | | | [yml/plain-scalar](./plain-scalar.md) | require or disallow plain style scalar. | :wrench: | | :star: | | [yml/quotes](./quotes.md) | enforce the consistent use of either double, or single quotes | :wrench: | | :star: | | [yml/require-string-key](./require-string-key.md) | disallow mapping keys other than strings | | | | @@ -44,5 +45,4 @@ 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: | diff --git a/docs/rules/no-trailing-zeros.md b/docs/rules/no-trailing-zeros.md index 39cf6a7d..3159823f 100644 --- a/docs/rules/no-trailing-zeros.md +++ b/docs/rules/no-trailing-zeros.md @@ -2,20 +2,19 @@ pageClass: "rule-details" sidebarDepth: 0 title: "yml/no-trailing-zeros" -description: "disallow trailing zeros for numbers" -since: "v1.5.1" +description: "disallow trailing zeros for floats" --- # yml/no-trailing-zeros -> disallow trailing zeros for numbers +> disallow trailing zeros for floats -- :gear: This rule is included in `"plugin:yml/standard"`. +- :exclamation: **_This rule has not been released yet._** - :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. +This rule enforces the removal of unnecessary trailing zeros from floats. @@ -37,10 +36,6 @@ This rule enforces the removal of unnecessary trailing zeros from numbers. Nothing. -## :rocket: Version - -This rule was introduced in eslint-plugin-yml v1.5.1 - ## :mag: Implementation - [Rule source](https://github.com/ota-meshi/eslint-plugin-yml/blob/master/src/rules/no-trailing-zeros.ts) diff --git a/src/configs/prettier.ts b/src/configs/prettier.ts index 2267e337..349f7948 100644 --- a/src/configs/prettier.ts +++ b/src/configs/prettier.ts @@ -15,6 +15,7 @@ export = { "yml/indent": "off", "yml/key-spacing": "off", "yml/no-multiple-empty-lines": "off", + "yml/no-trailing-zeros": "off", "yml/quotes": "off", }, }; diff --git a/src/configs/standard.ts b/src/configs/standard.ts index fc6916b7..25f667a6 100644 --- a/src/configs/standard.ts +++ b/src/configs/standard.ts @@ -21,7 +21,6 @@ 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", diff --git a/src/rules/no-trailing-zeros.ts b/src/rules/no-trailing-zeros.ts index dd66278d..6e2d86b9 100644 --- a/src/rules/no-trailing-zeros.ts +++ b/src/rules/no-trailing-zeros.ts @@ -4,8 +4,8 @@ import { createRule } from "../utils"; export default createRule("no-trailing-zeros", { meta: { docs: { - description: "disallow trailing zeros", - categories: ["standard"], + description: "disallow trailing zeros for floats", + categories: null, extensionRule: false, layout: true, }, @@ -27,17 +27,20 @@ export default createRule("no-trailing-zeros", { return; } else if (typeof node.value !== "number") { return; + } else if (!node.strValue.endsWith("0")) { + 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); - // 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) { + const parts = node.strValue.split("."); + if (parts.length !== 2) { return; } - const fixed = node.raw.replace(match[0], `${match[1]}.${match[2]}`); + 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", diff --git a/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json b/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json index b001f78c..962796ff 100644 --- a/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json +++ b/tests/fixtures/rules/no-trailing-zeros/invalid/errors.json @@ -10,13 +10,53 @@ "column": 10 }, { - "message": "Trailing zeros are not allowed, fix to `1.2`.", + "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": 12, + "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 } ] diff --git a/tests/fixtures/rules/no-trailing-zeros/invalid/input.yml b/tests/fixtures/rules/no-trailing-zeros/invalid/input.yml index b9d321e9..79b7b6f6 100644 --- a/tests/fixtures/rules/no-trailing-zeros/invalid/input.yml +++ b/tests/fixtures/rules/no-trailing-zeros/invalid/input.yml @@ -3,10 +3,18 @@ a: 1.23, b: 1.20, c: .23, - d: .20 + 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 diff --git a/tests/fixtures/rules/no-trailing-zeros/invalid/output.yml b/tests/fixtures/rules/no-trailing-zeros/invalid/output.yml index b03f296e..2d6a8be0 100644 --- a/tests/fixtures/rules/no-trailing-zeros/invalid/output.yml +++ b/tests/fixtures/rules/no-trailing-zeros/invalid/output.yml @@ -3,10 +3,18 @@ a: 1.23, b: 1.2, c: .23, - d: .2 + 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 diff --git a/tests/fixtures/rules/no-trailing-zeros/valid/input.yml b/tests/fixtures/rules/no-trailing-zeros/valid/input.yml index a47e1f5b..539feec0 100644 --- a/tests/fixtures/rules/no-trailing-zeros/valid/input.yml +++ b/tests/fixtures/rules/no-trailing-zeros/valid/input.yml @@ -3,10 +3,18 @@ a: 1.23, b: "1.20", c: .23, - d: .2 + 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"