Skip to content

Commit

Permalink
Add new rules
Browse files Browse the repository at this point in the history
- Add `jsonc/no-octal` rule that disallow legacy octal literals.
- Add `jsonc/no-binary-numeric-literals` rule that disallow binary numeric literals.
- Add `jsonc/no-octal-numeric-literals` rule that disallow octal numeric literals.
- Add `jsonc/no-hexadecimal-numeric-literals` rule that disallow hexadecimal numeric literals.
- Add `jsonc/no-unicode-codepoint-escapes` rule that disallow Unicode code point escape sequences.
- Add `jsonc/no-escape-sequence-in-identifier` rule that disallow escape sequences in identifiers.
  • Loading branch information
ota-meshi committed Feb 26, 2021
1 parent e779da2 commit c465dc9
Show file tree
Hide file tree
Showing 22 changed files with 775 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,17 @@ The rules with the following star :star: are included in the config.
| [jsonc/auto](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/auto.html) | apply jsonc rules similar to your configured ESLint core rules | :wrench: | | | |
| [jsonc/key-name-casing](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/key-name-casing.html) | enforce naming convention to property key names | | | | |
| [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-binary-numeric-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-binary-numeric-literals.html) | disallow binary numeric literals | :wrench: | | | |
| [jsonc/no-comments](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-comments.html) | disallow comments | | :star: | | |
| [jsonc/no-escape-sequence-in-identifier](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-escape-sequence-in-identifier.html) | disallow escape sequences in identifiers. | :wrench: | | | |
| [jsonc/no-hexadecimal-numeric-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-hexadecimal-numeric-literals.html) | disallow hexadecimal numeric literals | :wrench: | | | |
| [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: |
| [jsonc/no-numeric-separators](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-numeric-separators.html) | disallow numeric separators | :wrench: | :star: | :star: | :star: |
| [jsonc/no-octal-numeric-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-octal-numeric-literals.html) | disallow octal numeric literals | :wrench: | | | |
| [jsonc/no-regexp-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-regexp-literals.html) | disallow RegExp literals | | :star: | :star: | :star: |
| [jsonc/no-template-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-template-literals.html) | disallow template literals | :wrench: | :star: | :star: | :star: |
| [jsonc/no-undefined-value](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-undefined-value.html) | disallow `undefined` | | :star: | :star: | :star: |
| [jsonc/no-unicode-codepoint-escapes](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-unicode-codepoint-escapes.html) | disallow Unicode code point escape sequences. | :wrench: | | | |
| [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: | |
| [jsonc/vue-custom-block/no-parsing-error](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/vue-custom-block/no-parsing-error.html) | disallow parsing errors in Vue custom blocks | | :star: | :star: | :star: |

Expand All @@ -193,6 +198,7 @@ The rules with the following star :star: are included in the config.
| [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: | |
| [jsonc/no-multi-str](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-multi-str.html) | disallow multiline strings | | :star: | :star: | |
| [jsonc/no-octal-escape](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-octal-escape.html) | disallow octal escape sequences in string literals | | | | |
| [jsonc/no-octal](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-octal.html) | disallow legacy octal literals | | | | |
| [jsonc/no-sparse-arrays](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-sparse-arrays.html) | disallow sparse arrays | | :star: | :star: | :star: |
| [jsonc/no-useless-escape](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-useless-escape.html) | disallow unnecessary escape usage | | :star: | :star: | :star: |
| [jsonc/object-curly-newline](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/object-curly-newline.html) | enforce consistent line breaks inside braces | :wrench: | | | |
Expand Down
6 changes: 6 additions & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
| [jsonc/auto](./auto.md) | apply jsonc rules similar to your configured ESLint core rules | :wrench: | | | |
| [jsonc/key-name-casing](./key-name-casing.md) | enforce naming convention to property key names | | | | |
| [jsonc/no-bigint-literals](./no-bigint-literals.md) | disallow BigInt literals | | :star: | :star: | :star: |
| [jsonc/no-binary-numeric-literals](./no-binary-numeric-literals.md) | disallow binary numeric literals | :wrench: | | | |
| [jsonc/no-comments](./no-comments.md) | disallow comments | | :star: | | |
| [jsonc/no-escape-sequence-in-identifier](./no-escape-sequence-in-identifier.md) | disallow escape sequences in identifiers. | :wrench: | | | |
| [jsonc/no-hexadecimal-numeric-literals](./no-hexadecimal-numeric-literals.md) | disallow hexadecimal numeric literals | :wrench: | | | |
| [jsonc/no-number-props](./no-number-props.md) | disallow number property keys | :wrench: | :star: | :star: | :star: |
| [jsonc/no-numeric-separators](./no-numeric-separators.md) | disallow numeric separators | :wrench: | :star: | :star: | :star: |
| [jsonc/no-octal-numeric-literals](./no-octal-numeric-literals.md) | disallow octal numeric literals | :wrench: | | | |
| [jsonc/no-regexp-literals](./no-regexp-literals.md) | disallow RegExp literals | | :star: | :star: | :star: |
| [jsonc/no-template-literals](./no-template-literals.md) | disallow template literals | :wrench: | :star: | :star: | :star: |
| [jsonc/no-undefined-value](./no-undefined-value.md) | disallow `undefined` | | :star: | :star: | :star: |
| [jsonc/no-unicode-codepoint-escapes](./no-unicode-codepoint-escapes.md) | disallow Unicode code point escape sequences. | :wrench: | | | |
| [jsonc/valid-json-number](./valid-json-number.md) | disallow invalid number for JSON | :wrench: | :star: | :star: | |
| [jsonc/vue-custom-block/no-parsing-error](./vue-custom-block/no-parsing-error.md) | disallow parsing errors in Vue custom blocks | | :star: | :star: | :star: |

Expand All @@ -40,6 +45,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
| [jsonc/no-floating-decimal](./no-floating-decimal.md) | disallow leading or trailing decimal points in numeric literals | :wrench: | :star: | :star: | |
| [jsonc/no-multi-str](./no-multi-str.md) | disallow multiline strings | | :star: | :star: | |
| [jsonc/no-octal-escape](./no-octal-escape.md) | disallow octal escape sequences in string literals | | | | |
| [jsonc/no-octal](./no-octal.md) | disallow legacy octal literals | | | | |
| [jsonc/no-sparse-arrays](./no-sparse-arrays.md) | disallow sparse arrays | | :star: | :star: | :star: |
| [jsonc/no-useless-escape](./no-useless-escape.md) | disallow unnecessary escape usage | | :star: | :star: | :star: |
| [jsonc/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks inside braces | :wrench: | | | |
Expand Down
42 changes: 42 additions & 0 deletions docs/rules/no-binary-numeric-literals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-binary-numeric-literals"
description: "disallow binary numeric literals"
---
# jsonc/no-binary-numeric-literals

> disallow binary numeric literals
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :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 disallow binary numeric literals

<eslint-code-block fix>

<!-- eslint-skip -->

```json5
/* eslint jsonc/no-binary-numeric-literals: 'error' */
{
/* ✓ GOOD */
"GOOD": 10,

/* ✗ BAD */
"BAD": 0b1010
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-binary-numeric-literals.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-binary-numeric-literals.js)
42 changes: 42 additions & 0 deletions docs/rules/no-escape-sequence-in-identifier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-escape-sequence-in-identifier"
description: "disallow escape sequences in identifiers."
---
# jsonc/no-escape-sequence-in-identifier

> disallow escape sequences in identifiers.
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :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 reports disallow escape sequences in identifiers.

<eslint-code-block fix>

<!-- eslint-skip -->

```json5
/* eslint jsonc/no-escape-sequence-in-identifier: 'error' */
{
/* ✓ GOOD */
GOOD: "GOOD",

/* ✗ BAD */
\u0042\u{41}\u{44}: "BAD"
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-escape-sequence-in-identifier.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-escape-sequence-in-identifier.js)
42 changes: 42 additions & 0 deletions docs/rules/no-hexadecimal-numeric-literals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-hexadecimal-numeric-literals"
description: "disallow hexadecimal numeric literals"
---
# jsonc/no-hexadecimal-numeric-literals

> disallow hexadecimal numeric literals
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :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 disallow hexadecimal numeric literals.

<eslint-code-block fix>

<!-- eslint-skip -->

```json5
/* eslint jsonc/no-hexadecimal-numeric-literals: 'error' */
{
/* ✓ GOOD */
"GOOD": 65535,

/* ✗ BAD */
"BAD": 0xFFFF
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-hexadecimal-numeric-literals.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-hexadecimal-numeric-literals.js)
42 changes: 42 additions & 0 deletions docs/rules/no-octal-numeric-literals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-octal-numeric-literals"
description: "disallow octal numeric literals"
---
# jsonc/no-octal-numeric-literals

> disallow octal numeric literals
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :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 disallow octal numeric literals.

<eslint-code-block fix>

<!-- eslint-skip -->

```json5
/* eslint jsonc/no-octal-numeric-literals: 'error' */
{
/* ✓ GOOD */
"GOOD": 511,

/* ✗ BAD */
"BAD": 0o777
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-octal-numeric-literals.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-octal-numeric-literals.js)
49 changes: 49 additions & 0 deletions docs/rules/no-octal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-octal"
description: "disallow legacy octal literals"
---
# jsonc/no-octal

> disallow legacy octal literals
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Rule Details

The rule disallows octal literals.

<eslint-code-block>

<!-- eslint-skip -->

```json5
/* eslint jsonc/no-octal: 'error' */
{
/* ✓ GOOD */
"GOOD": 777,

/* ✗ BAD */
"BAD": 0777
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :couple: Related rules

- [no-octal]

[no-octal]: https://eslint.org/docs/rules/no-octal

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-octal.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-octal.js)

<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-octal)</sup>
42 changes: 42 additions & 0 deletions docs/rules/no-unicode-codepoint-escapes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-unicode-codepoint-escapes"
description: "disallow Unicode code point escape sequences."
---
# jsonc/no-unicode-codepoint-escapes

> disallow Unicode code point escape sequences.
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :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 disallow Unicode code point escape sequences.

<eslint-code-block fix>

<!-- eslint-skip -->

```json5
/* eslint jsonc/no-unicode-codepoint-escapes: 'error' */
{
/* ✓ GOOD */
"GOOD": "\u0041",

/* ✗ BAD */
"BAD": "\u{41}"
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-unicode-codepoint-escapes.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-unicode-codepoint-escapes.js)
43 changes: 43 additions & 0 deletions lib/rules/no-binary-numeric-literals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { AST } from "jsonc-eslint-parser"
import { createRule } from "../utils"

const binaryNumericLiteralPattern = /^0[bB]/u

export default createRule("no-binary-numeric-literals", {
meta: {
docs: {
description: "disallow binary numeric literals",
// TODO major version recommended: ["json","jsonc","json5"],
recommended: null,
extensionRule: false,
layout: false,
},
fixable: "code",
messages: {
disallow: "Binary numeric literals should not be used.",
},
schema: [],
type: "problem",
},
create(context) {
return {
JSONLiteral(node: AST.JSONLiteral) {
if (
typeof node.value === "number" &&
binaryNumericLiteralPattern.test(node.raw)
) {
context.report({
loc: node.loc,
messageId: "disallow",
fix: (fixer) => {
return fixer.replaceTextRange(
node.range,
`${node.value}`,
)
},
})
}
},
}
},
})
Loading

0 comments on commit c465dc9

Please sign in to comment.