From ab2a4580d62f2686a1e7523dc6ed7ca43e176f5f Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 5 Nov 2021 13:48:18 +0900 Subject: [PATCH] Add `jsonc/no-binary-expression` rule --- README.md | 1 + docs/rules/README.md | 1 + docs/rules/no-binary-expression.md | 42 +++++++++++++++++++++++ docs/rules/no-parenthesized.md | 6 ---- lib/rules/no-binary-expression.ts | 44 +++++++++++++++++++++++++ lib/utils/rules.ts | 2 ++ package.json | 2 +- tests/lib/rules/no-binary-expression.ts | 44 +++++++++++++++++++++++++ 8 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 docs/rules/no-binary-expression.md create mode 100644 lib/rules/no-binary-expression.ts create mode 100644 tests/lib/rules/no-binary-expression.ts diff --git a/README.md b/README.md index 59008f71..d19e6697 100644 --- a/README.md +++ b/README.md @@ -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: | | | | diff --git a/docs/rules/README.md b/docs/rules/README.md index a6a9cc0d..4fa8c0a7 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -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: | | | | diff --git a/docs/rules/no-binary-expression.md b/docs/rules/no-binary-expression.md new file mode 100644 index 00000000..0158f7b9 --- /dev/null +++ b/docs/rules/no-binary-expression.md @@ -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: ***This rule has not been released yet.*** +- :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. + + + + + +```json5 +/* eslint jsonc/no-binary-expression: 'error' */ +{ + /* ✓ GOOD */ + "GOOD": 86400, + + /* ✗ BAD */ + "BAD": 60 * 60 * 24 +} +``` + + + +## :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) diff --git a/docs/rules/no-parenthesized.md b/docs/rules/no-parenthesized.md index 989abab8..b2e2f4fd 100644 --- a/docs/rules/no-parenthesized.md +++ b/docs/rules/no-parenthesized.md @@ -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) diff --git a/lib/rules/no-binary-expression.ts b/lib/rules/no-binary-expression.ts new file mode 100644 index 00000000..14391526 --- /dev/null +++ b/lib/rules/no-binary-expression.ts @@ -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), + ) + }, + }) + }, + } + }, +}) diff --git a/lib/utils/rules.ts b/lib/utils/rules.ts index c410d403..23500c7a 100644 --- a/lib/utils/rules.ts +++ b/lib/utils/rules.ts @@ -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" @@ -52,6 +53,7 @@ export const rules = [ keyNameCasing, keySpacing, noBigintLiterals, + noBinaryExpression, noBinaryNumericLiterals, noComments, noDupeKeys, diff --git a/package.json b/package.json index e0b361e4..c0783758 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/tests/lib/rules/no-binary-expression.ts b/tests/lib/rules/no-binary-expression.ts new file mode 100644 index 00000000..8e9bc762 --- /dev/null +++ b/tests/lib/rules/no-binary-expression.ts @@ -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.", + ], + }, + ], +})