Skip to content

Commit 70ee0a2

Browse files
authored
Add jsonc/no-floating-decimal rule (#57)
1 parent b706b3e commit 70ee0a2

File tree

9 files changed

+103
-0
lines changed

9 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ The rules with the following star :star: are included in the config.
190190
| [jsonc/indent](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/indent.html) | enforce consistent indentation | :wrench: | | | |
191191
| [jsonc/key-spacing](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/key-spacing.html) | enforce consistent spacing between keys and values in object literal properties | :wrench: | | | |
192192
| [jsonc/no-dupe-keys](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-dupe-keys.html) | disallow duplicate keys in object literals | | :star: | :star: | :star: |
193+
| [jsonc/no-floating-decimal](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-floating-decimal.html) | disallow leading or trailing decimal points in numeric literals | :wrench: | :star: | :star: | |
193194
| [jsonc/no-multi-str](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-multi-str.html) | disallow multiline strings | | :star: | :star: | |
194195
| [jsonc/no-octal-escape](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-octal-escape.html) | disallow octal escape sequences in string literals | | | | |
195196
| [jsonc/no-sparse-arrays](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-sparse-arrays.html) | disallow sparse arrays | | :star: | :star: | :star: |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
3737
| [jsonc/indent](./indent.md) | enforce consistent indentation | :wrench: | | | |
3838
| [jsonc/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: | | | |
3939
| [jsonc/no-dupe-keys](./no-dupe-keys.md) | disallow duplicate keys in object literals | | :star: | :star: | :star: |
40+
| [jsonc/no-floating-decimal](./no-floating-decimal.md) | disallow leading or trailing decimal points in numeric literals | :wrench: | :star: | :star: | |
4041
| [jsonc/no-multi-str](./no-multi-str.md) | disallow multiline strings | | :star: | :star: | |
4142
| [jsonc/no-octal-escape](./no-octal-escape.md) | disallow octal escape sequences in string literals | | | | |
4243
| [jsonc/no-sparse-arrays](./no-sparse-arrays.md) | disallow sparse arrays | | :star: | :star: | :star: |

docs/rules/no-floating-decimal.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "jsonc/no-floating-decimal"
5+
description: "disallow leading or trailing decimal points in numeric literals"
6+
---
7+
# jsonc/no-floating-decimal
8+
9+
> disallow leading or trailing decimal points in numeric literals
10+
11+
- :gear: This rule is included in `"plugin:jsonc/recommended-with-json"` and `"plugin:jsonc/recommended-with-jsonc"`.
12+
- :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.
13+
14+
## :book: Rule Details
15+
16+
This rule is aimed at eliminating floating decimal points and will warn whenever a numeric value has a decimal point but is missing a number either before or after it.
17+
18+
<eslint-code-block fix>
19+
20+
<!-- eslint-skip -->
21+
22+
```json5
23+
/* eslint jsonc/no-floating-decimal: 'error' */
24+
{
25+
/* ✓ GOOD */
26+
"GOOD": 42.0,
27+
28+
/* ✗ BAD */
29+
"BAD": 42.
30+
}
31+
```
32+
33+
</eslint-code-block>
34+
35+
## :wrench: Options
36+
37+
Nothing.
38+
39+
## :couple: Related rules
40+
41+
- [no-floating-decimal]
42+
43+
[no-floating-decimal]: https://eslint.org/docs/rules/no-floating-decimal
44+
45+
## Implementation
46+
47+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-floating-decimal.ts)
48+
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-floating-decimal.js)
49+
50+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-floating-decimal)</sup>

lib/configs/prettier.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export = {
1313
"jsonc/comma-style": "off",
1414
"jsonc/indent": "off",
1515
"jsonc/key-spacing": "off",
16+
"jsonc/no-floating-decimal": "off",
1617
"jsonc/object-curly-newline": "off",
1718
"jsonc/object-curly-spacing": "off",
1819
"jsonc/object-property-newline": "off",

lib/configs/recommended-with-json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export = {
1010
"jsonc/no-bigint-literals": "error",
1111
"jsonc/no-comments": "error",
1212
"jsonc/no-dupe-keys": "error",
13+
"jsonc/no-floating-decimal": "error",
1314
"jsonc/no-multi-str": "error",
1415
"jsonc/no-number-props": "error",
1516
"jsonc/no-numeric-separators": "error",

lib/configs/recommended-with-jsonc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export = {
88
// eslint-plugin-jsonc rules
99
"jsonc/no-bigint-literals": "error",
1010
"jsonc/no-dupe-keys": "error",
11+
"jsonc/no-floating-decimal": "error",
1112
"jsonc/no-multi-str": "error",
1213
"jsonc/no-number-props": "error",
1314
"jsonc/no-numeric-separators": "error",

lib/rules/no-floating-decimal.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createRule, defineWrapperListener, getCoreRule } from "../utils"
2+
const coreRule = getCoreRule("no-floating-decimal")
3+
4+
export default createRule("no-floating-decimal", {
5+
meta: {
6+
docs: {
7+
description:
8+
"disallow leading or trailing decimal points in numeric literals",
9+
recommended: ["json", "jsonc"],
10+
extensionRule: true,
11+
layout: true,
12+
},
13+
fixable: coreRule.meta!.fixable,
14+
schema: coreRule.meta!.schema!,
15+
messages: coreRule.meta!.messages!,
16+
type: coreRule.meta!.type!,
17+
},
18+
create(context) {
19+
return defineWrapperListener(coreRule, context, context.options)
20+
},
21+
})

lib/utils/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import keySpacing from "../rules/key-spacing"
1111
import noBigintLiterals from "../rules/no-bigint-literals"
1212
import noComments from "../rules/no-comments"
1313
import noDupeKeys from "../rules/no-dupe-keys"
14+
import noFloatingDecimal from "../rules/no-floating-decimal"
1415
import noMultiStr from "../rules/no-multi-str"
1516
import noNumberProps from "../rules/no-number-props"
1617
import noNumericSeparators from "../rules/no-numeric-separators"
@@ -43,6 +44,7 @@ export const rules = [
4344
noBigintLiterals,
4445
noComments,
4546
noDupeKeys,
47+
noFloatingDecimal,
4648
noMultiStr,
4749
noNumberProps,
4850
noNumericSeparators,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/no-floating-decimal"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("jsonc-eslint-parser"),
6+
parserOptions: {
7+
ecmaVersion: 2020,
8+
},
9+
})
10+
11+
tester.run("no-floating-decimal", rule as any, {
12+
valid: ["42", "42.0", "0.42"],
13+
invalid: [
14+
{
15+
code: `.42`,
16+
output: `0.42`,
17+
errors: ["A leading decimal point can be confused with a dot."],
18+
},
19+
{
20+
code: `42.`,
21+
output: `42.0`,
22+
errors: ["A trailing decimal point can be confused with a dot."],
23+
},
24+
],
25+
})

0 commit comments

Comments
 (0)