Skip to content

Commit 9fe5581

Browse files
authored
Add jsonc/no-bigint-literals rule (#12)
1 parent a732b8e commit 9fe5581

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
@@ -135,6 +135,7 @@ The rules with the following star :star: are included in the config.
135135

136136
| Rule ID | Description | Fixable | JSON | JSONC | JSON5 |
137137
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
138+
| [jsonc/no-bigint-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-bigint-literals.html) | disallow BigInt literals | | :star: | :star: | :star: |
138139
| [jsonc/no-comments](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-comments.html) | disallow comments | | :star: | | |
139140
| [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: |
140141
| [jsonc/no-regexp-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-regexp-literals.html) | disallow RegExp literals | | :star: | :star: | :star: |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
1313

1414
| Rule ID | Description | Fixable | JSON | JSONC | JSON5 |
1515
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
16+
| [jsonc/no-bigint-literals](./no-bigint-literals.md) | disallow BigInt literals | | :star: | :star: | :star: |
1617
| [jsonc/no-comments](./no-comments.md) | disallow comments | | :star: | | |
1718
| [jsonc/no-number-props](./no-number-props.md) | disallow number property keys | :wrench: | :star: | :star: | :star: |
1819
| [jsonc/no-regexp-literals](./no-regexp-literals.md) | disallow RegExp literals | | :star: | :star: | :star: |

docs/rules/no-bigint-literals.md

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

lib/configs/recommended-with-json.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/comma-dangle": "error",
10+
"jsonc/no-bigint-literals": "error",
1011
"jsonc/no-comments": "error",
1112
"jsonc/no-dupe-keys": "error",
1213
"jsonc/no-multi-str": "error",

lib/configs/recommended-with-json5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export = {
66
extends: [baseExtend],
77
rules: {
88
// eslint-plugin-jsonc rules
9+
"jsonc/no-bigint-literals": "error",
910
"jsonc/no-dupe-keys": "error",
1011
"jsonc/no-number-props": "error",
1112
"jsonc/no-regexp-literals": "error",

lib/configs/recommended-with-jsonc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export = {
66
extends: [baseExtend],
77
rules: {
88
// eslint-plugin-jsonc rules
9+
"jsonc/no-bigint-literals": "error",
910
"jsonc/no-dupe-keys": "error",
1011
"jsonc/no-multi-str": "error",
1112
"jsonc/no-number-props": "error",

lib/rules/no-bigint-literals.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { JSONLiteral } from "../parser/ast"
2+
import { createRule } from "../utils"
3+
4+
export default createRule("no-bigint-literals", {
5+
meta: {
6+
docs: {
7+
description: "disallow BigInt literals",
8+
recommended: ["json", "jsonc", "json5"],
9+
extensionRule: false,
10+
},
11+
schema: [],
12+
messages: {
13+
unexpected: "BigInt literals are not allowed.",
14+
},
15+
type: "problem",
16+
},
17+
create(context) {
18+
return {
19+
JSONLiteral(node: JSONLiteral) {
20+
if (node.bigint != null) {
21+
context.report({
22+
loc: node.loc,
23+
messageId: "unexpected",
24+
})
25+
}
26+
},
27+
}
28+
},
29+
})

lib/utils/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import commaDangle from "../rules/comma-dangle"
66
import commaStyle from "../rules/comma-style"
77
import indent from "../rules/indent"
88
import keySpacing from "../rules/key-spacing"
9+
import noBigintLiterals from "../rules/no-bigint-literals"
910
import noComments from "../rules/no-comments"
1011
import noDupeKeys from "../rules/no-dupe-keys"
1112
import noMultiStr from "../rules/no-multi-str"
@@ -33,6 +34,7 @@ export const rules = [
3334
commaStyle,
3435
indent,
3536
keySpacing,
37+
noBigintLiterals,
3638
noComments,
3739
noDupeKeys,
3840
noMultiStr,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/no-bigint-literals"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
6+
parserOptions: {
7+
ecmaVersion: 2020,
8+
},
9+
})
10+
11+
tester.run("no-bigint-literals", rule as any, {
12+
valid: ['{"key": "value"}', '"string"', '["element"]'],
13+
invalid: [
14+
{
15+
code: "42n",
16+
errors: ["BigInt literals are not allowed."],
17+
},
18+
{
19+
code: "[1n, {'2n': 3n}]",
20+
errors: [
21+
"BigInt literals are not allowed.",
22+
"BigInt literals are not allowed.",
23+
],
24+
},
25+
],
26+
})

0 commit comments

Comments
 (0)