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-binary-expression rule #134

Merged
merged 1 commit into from
Nov 5, 2021
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 @@ -186,6 +186,7 @@ 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-expression](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-binary-expression.html) | disallow binary expression | :wrench: | | | |
| [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: | | | |
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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-expression](./no-binary-expression.md) | disallow binary expression | :wrench: | | | |
| [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: | | | |
Expand Down
42 changes: 42 additions & 0 deletions docs/rules/no-binary-expression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "jsonc/no-binary-expression"
description: "disallow binary expression"
---
# jsonc/no-binary-expression

> disallow binary expression

- :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 expressions.

<eslint-code-block fix>

<!-- eslint-skip -->

```json5
/* eslint jsonc/no-binary-expression: 'error' */
{
/* ✓ GOOD */
"GOOD": 86400,

/* ✗ BAD */
"BAD": 60 * 60 * 24
}
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-binary-expression.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-binary-expression.ts)
6 changes: 0 additions & 6 deletions docs/rules/no-parenthesized.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ This rule always disallow parentheses around the expression.

Nothing.

## :couple: Related rules

- [no-parenthesized]

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

## :mag: Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-parenthesized.ts)
Expand Down
44 changes: 44 additions & 0 deletions lib/rules/no-binary-expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { AST } from "jsonc-eslint-parser"
import { getStaticJSONValue } from "jsonc-eslint-parser"
import { createRule } from "../utils"

export default createRule("no-binary-expression", {
meta: {
docs: {
description: "disallow binary expression",
// TODO major version recommended: ["json","jsonc","json5"],
recommended: null,
// recommended: ["json", "jsonc", "json5"],
extensionRule: false,
layout: false,
},
fixable: "code",
hasSuggestions: false,
schema: [],
messages: {
disallow: "The binary expressions are not allowed.",
},
type: "problem",
},
create(context) {
if (!context.parserServices.isJSON) {
return {}
}

return {
JSONBinaryExpression(node: AST.JSONBinaryExpression) {
context.report({
loc: node.loc,
messageId: "disallow",
fix(fixer) {
const value = getStaticJSONValue(node)
return fixer.replaceTextRange(
node.range,
JSON.stringify(value),
)
},
})
},
}
},
})
2 changes: 2 additions & 0 deletions lib/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import indent from "../rules/indent"
import keyNameCasing from "../rules/key-name-casing"
import keySpacing from "../rules/key-spacing"
import noBigintLiterals from "../rules/no-bigint-literals"
import noBinaryExpression from "../rules/no-binary-expression"
import noBinaryNumericLiterals from "../rules/no-binary-numeric-literals"
import noComments from "../rules/no-comments"
import noDupeKeys from "../rules/no-dupe-keys"
Expand Down Expand Up @@ -52,6 +53,7 @@ export const rules = [
keyNameCasing,
keySpacing,
noBigintLiterals,
noBinaryExpression,
noBinaryNumericLiterals,
noComments,
noDupeKeys,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"homepage": "https://ota-meshi.github.io/eslint-plugin-jsonc/",
"dependencies": {
"eslint-utils": "^3.0.0",
"jsonc-eslint-parser": "^2.0.0",
"jsonc-eslint-parser": "^2.0.1",
"natural-compare": "^1.4.0"
},
"peerDependencies": {
Expand Down
44 changes: 44 additions & 0 deletions tests/lib/rules/no-binary-expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { RuleTester } from "eslint"
import rule from "../../../lib/rules/no-binary-expression"

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

tester.run("no-binary-expression", rule as any, {
valid: ['{"42": 42}', "42", "[42]"],
invalid: [
{
code: `[42-1, {"key": 42-1}]`,
output: `[41, {"key": 41}]`,
errors: [
{
message: "The binary expressions are not allowed.",
line: 1,
column: 2,
},
{
message: "The binary expressions are not allowed.",
line: 1,
column: 16,
},
],
},
{
code: `42-1`,
output: `41`,
errors: ["The binary expressions are not allowed."],
},
{
code: `[42 * (42 - 3)]`,
output: `[1638]`,
errors: [
"The binary expressions are not allowed.",
"The binary expressions are not allowed.",
],
},
],
})