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

Add jsonc/no-bigint-literals rule #12

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ The rules with the following star :star: are included in the config.

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

Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom

| Rule ID | Description | Fixable | JSON | JSONC | JSON5 |
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
| [jsonc/no-bigint-literals](./no-bigint-literals.md) | disallow BigInt literals | | :star: | :star: | :star: |
| [jsonc/no-comments](./no-comments.md) | disallow comments | | :star: | | |
| [jsonc/valid-json-number](./valid-json-number.md) | disallow invalid number for JSON | :wrench: | :star: | :star: | |

Expand Down
41 changes: 41 additions & 0 deletions docs/rules/no-bigint-literals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-bigint-literals"
description: "disallow BigInt literals"
---
# jsonc/no-bigint-literals

> disallow BigInt literals

- :gear: This rule is included in all of `"plugin:jsonc/recommended-with-json"`, `"plugin:jsonc/recommended-with-json5"` and `"plugin:jsonc/recommended-with-jsonc"`.

## :book: Rule Details

This rule reports the use of BigInt literals.

JSON, JSONC and JSON5 do not allow BigInt literals.

<eslint-code-block>

```json5
/* eslint jsonc/no-bigint-literals: 'error' */
{
/* ✓ GOOD */
"GOOD": 42,

/* ✗ BAD */
"BAD": 42n
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-bigint-literals.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-bigint-literals.js)
1 change: 1 addition & 0 deletions lib/configs/recommended-with-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export = {
rules: {
// eslint-plugin-jsonc rules
"jsonc/comma-dangle": "error",
"jsonc/no-bigint-literals": "error",
"jsonc/no-comments": "error",
"jsonc/no-dupe-keys": "error",
"jsonc/no-multi-str": "error",
Expand Down
1 change: 1 addition & 0 deletions lib/configs/recommended-with-json5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export = {
extends: [baseExtend],
rules: {
// eslint-plugin-jsonc rules
"jsonc/no-bigint-literals": "error",
"jsonc/no-dupe-keys": "error",
"jsonc/no-useless-escape": "error",
},
Expand Down
1 change: 1 addition & 0 deletions lib/configs/recommended-with-jsonc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export = {
extends: [baseExtend],
rules: {
// eslint-plugin-jsonc rules
"jsonc/no-bigint-literals": "error",
"jsonc/no-dupe-keys": "error",
"jsonc/no-multi-str": "error",
"jsonc/no-useless-escape": "error",
Expand Down
29 changes: 29 additions & 0 deletions lib/rules/no-bigint-literals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { JSONLiteral } from "../parser/ast"
import { createRule } from "../utils"

export default createRule("no-bigint-literals", {
meta: {
docs: {
description: "disallow BigInt literals",
recommended: ["json", "jsonc", "json5"],
extensionRule: false,
},
schema: [],
messages: {
unexpected: "BigInt literals are not allowed.",
},
type: "problem",
},
create(context) {
return {
JSONLiteral(node: JSONLiteral) {
if (node.bigint != null) {
context.report({
loc: node.loc,
messageId: "unexpected",
})
}
},
}
},
})
2 changes: 2 additions & 0 deletions lib/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import commaDangle from "../rules/comma-dangle"
import commaStyle from "../rules/comma-style"
import indent from "../rules/indent"
import keySpacing from "../rules/key-spacing"
import noBigintLiterals from "../rules/no-bigint-literals"
import noComments from "../rules/no-comments"
import noDupeKeys from "../rules/no-dupe-keys"
import noMultiStr from "../rules/no-multi-str"
Expand All @@ -27,6 +28,7 @@ export const rules = [
commaStyle,
indent,
keySpacing,
noBigintLiterals,
noComments,
noDupeKeys,
noMultiStr,
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/no-bigint-literals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RuleTester } from "eslint"
import rule from "../../../lib/rules/no-bigint-literals"

const tester = new RuleTester({
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
parserOptions: {
ecmaVersion: 2020,
},
})

tester.run("no-bigint-literals", rule as any, {
valid: ['{"key": "value"}', '"string"', '["element"]'],
invalid: [
{
code: "42n",
errors: ["BigInt literals are not allowed."],
},
{
code: "[1n, {'2n': 3n}]",
errors: [
"BigInt literals are not allowed.",
"BigInt literals are not allowed.",
],
},
],
})