Skip to content

Commit bc12417

Browse files
authored
Add jsonc/no-number-props rule (#10)
* Add `jsonc/no-number-props` rule * add testcase
1 parent c76bdeb commit bc12417

File tree

9 files changed

+110
-0
lines changed

9 files changed

+110
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ The rules with the following star :star: are included in the config.
136136
| Rule ID | Description | Fixable | JSON | JSONC | JSON5 |
137137
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
138138
| [jsonc/no-comments](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-comments.html) | disallow comments | | :star: | | |
139+
| [jsonc/no-number-props](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-number-props.html) | disallow number property keys | :wrench: | :star: | :star: | :star: |
139140
| [jsonc/no-template-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-template-literals.html) | disallow template literals | :wrench: | :star: | :star: | :star: |
140141
| [jsonc/no-undefined-value](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-undefined-value.html) | disallow `undefined` | | :star: | :star: | :star: |
141142
| [jsonc/valid-json-number](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/valid-json-number.html) | disallow invalid number for JSON | :wrench: | :star: | :star: | |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
1414
| Rule ID | Description | Fixable | JSON | JSONC | JSON5 |
1515
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
1616
| [jsonc/no-comments](./no-comments.md) | disallow comments | | :star: | | |
17+
| [jsonc/no-number-props](./no-number-props.md) | disallow number property keys | :wrench: | :star: | :star: | :star: |
1718
| [jsonc/no-template-literals](./no-template-literals.md) | disallow template literals | :wrench: | :star: | :star: | :star: |
1819
| [jsonc/no-undefined-value](./no-undefined-value.md) | disallow `undefined` | | :star: | :star: | :star: |
1920
| [jsonc/valid-json-number](./valid-json-number.md) | disallow invalid number for JSON | :wrench: | :star: | :star: | |

docs/rules/no-number-props.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "jsonc/no-number-props"
5+
description: "disallow number property keys"
6+
---
7+
# jsonc/no-number-props
8+
9+
> disallow number property keys
10+
11+
- :gear: This rule is included in all of `"plugin:jsonc/recommended-with-json"`, `"plugin:jsonc/recommended-with-json5"` 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 reports the use of number property keys.
17+
18+
JSON, JSONC and JSON5 do not allow number property keys.
19+
20+
<eslint-code-block fix>
21+
22+
```json5
23+
/* eslint jsonc/no-number-props: 'error' */
24+
{
25+
/* ✓ GOOD */
26+
"GOOD": {
27+
"42": "foo"
28+
},
29+
30+
/* ✗ BAD */
31+
"BAD": {
32+
42: "foo"
33+
}
34+
}
35+
```
36+
37+
</eslint-code-block>
38+
39+
## :wrench: Options
40+
41+
Nothing.
42+
43+
## Implementation
44+
45+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-number-props.ts)
46+
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-number-props.js)

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-comments": "error",
1111
"jsonc/no-dupe-keys": "error",
1212
"jsonc/no-multi-str": "error",
13+
"jsonc/no-number-props": "error",
1314
"jsonc/no-sparse-arrays": "error",
1415
"jsonc/no-template-literals": "error",
1516
"jsonc/no-undefined-value": "error",

lib/configs/recommended-with-json5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export = {
77
rules: {
88
// eslint-plugin-jsonc rules
99
"jsonc/no-dupe-keys": "error",
10+
"jsonc/no-number-props": "error",
1011
"jsonc/no-sparse-arrays": "error",
1112
"jsonc/no-template-literals": "error",
1213
"jsonc/no-undefined-value": "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-dupe-keys": "error",
1010
"jsonc/no-multi-str": "error",
11+
"jsonc/no-number-props": "error",
1112
"jsonc/no-sparse-arrays": "error",
1213
"jsonc/no-template-literals": "error",
1314
"jsonc/no-undefined-value": "error",

lib/rules/no-number-props.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { JSONProperty } from "../parser/ast"
2+
import { createRule } from "../utils"
3+
4+
export default createRule("no-number-props", {
5+
meta: {
6+
docs: {
7+
description: "disallow number property keys",
8+
recommended: ["json", "jsonc", "json5"],
9+
extensionRule: false,
10+
},
11+
fixable: "code",
12+
schema: [],
13+
messages: {
14+
unexpected: "The number property keys are not allowed.",
15+
},
16+
type: "problem",
17+
},
18+
create(context) {
19+
return {
20+
JSONProperty(node: JSONProperty) {
21+
if (node.key.type !== "JSONLiteral") {
22+
return
23+
}
24+
if (typeof node.key.value === "number") {
25+
const raw = node.key.raw
26+
context.report({
27+
loc: node.key.loc,
28+
messageId: "unexpected",
29+
fix(fixer) {
30+
return fixer.replaceTextRange(
31+
node.key.range,
32+
`"${raw}"`,
33+
)
34+
},
35+
})
36+
}
37+
},
38+
}
39+
},
40+
})

lib/utils/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import keySpacing from "../rules/key-spacing"
99
import noComments from "../rules/no-comments"
1010
import noDupeKeys from "../rules/no-dupe-keys"
1111
import noMultiStr from "../rules/no-multi-str"
12+
import noNumberProps from "../rules/no-number-props"
1213
import noOctalEscape from "../rules/no-octal-escape"
1314
import noSparseArrays from "../rules/no-sparse-arrays"
1415
import noTemplateLiterals from "../rules/no-template-literals"
@@ -34,6 +35,7 @@ export const rules = [
3435
noComments,
3536
noDupeKeys,
3637
noMultiStr,
38+
noNumberProps,
3739
noOctalEscape,
3840
noSparseArrays,
3941
noTemplateLiterals,

tests/lib/rules/no-number-props.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/no-number-props"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
6+
})
7+
8+
tester.run("no-number-props", rule as any, {
9+
valid: ['{"key": 123}', "{key: 123}", "123", "[123]"],
10+
invalid: [
11+
{
12+
code: "{123: 123}",
13+
output: '{"123": 123}',
14+
errors: ["The number property keys are not allowed."],
15+
},
16+
],
17+
})

0 commit comments

Comments
 (0)