Skip to content

Commit c76bdeb

Browse files
authored
Add jsonc/space-unary-ops rule (#7)
* Add `jsonc/space-unary-ops` rule * update
1 parent d4903bb commit c76bdeb

File tree

9 files changed

+118
-0
lines changed

9 files changed

+118
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ The rules with the following star :star: are included in the config.
162162
| [jsonc/quote-props](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/quote-props.html) | require quotes around object literal property names | :wrench: | :star: | :star: | |
163163
| [jsonc/quotes](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/quotes.html) | enforce use of double or single quotes | :wrench: | :star: | :star: | |
164164
| [jsonc/sort-keys](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/sort-keys.html) | require object keys to be sorted | | | | |
165+
| [jsonc/space-unary-ops](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/space-unary-ops.html) | disallow spaces after unary operators | :wrench: | :star: | :star: | :star: |
165166

166167
<!--RULES_TABLE_END-->
167168
<!--RULES_SECTION_END-->

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
4040
| [jsonc/quote-props](./quote-props.md) | require quotes around object literal property names | :wrench: | :star: | :star: | |
4141
| [jsonc/quotes](./quotes.md) | enforce use of double or single quotes | :wrench: | :star: | :star: | |
4242
| [jsonc/sort-keys](./sort-keys.md) | require object keys to be sorted | | | | |
43+
| [jsonc/space-unary-ops](./space-unary-ops.md) | disallow spaces after unary operators | :wrench: | :star: | :star: | :star: |

docs/rules/space-unary-ops.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "jsonc/space-unary-ops"
5+
description: "disallow spaces after unary operators"
6+
---
7+
# jsonc/space-unary-ops
8+
9+
> disallow spaces after unary operators
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+
- :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.
13+
14+
## :book: Rule Details
15+
16+
This rule reports the space after the sign operator.
17+
18+
JSON, JSONC and JSON5 do not allow spaces after the sign.
19+
20+
<eslint-code-block fix>
21+
22+
```json5
23+
/* eslint jsonc/space-unary-ops: 'error' */
24+
{
25+
/* ✓ GOOD */
26+
"GOOD": -42,
27+
28+
/* ✗ BAD */
29+
"BAD": - 42
30+
}
31+
```
32+
33+
</eslint-code-block>
34+
35+
## :wrench: Options
36+
37+
Nothing.
38+
39+
## :couple: Related rules
40+
41+
- [space-unary-ops]
42+
43+
[space-unary-ops]: https://eslint.org/docs/rules/space-unary-ops
44+
45+
## Implementation
46+
47+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/space-unary-ops.ts)
48+
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/space-unary-ops.js)
49+
50+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/space-unary-ops)</sup>

lib/configs/recommended-with-json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export = {
1616
"jsonc/no-useless-escape": "error",
1717
"jsonc/quote-props": "error",
1818
"jsonc/quotes": "error",
19+
"jsonc/space-unary-ops": "error",
1920
"jsonc/valid-json-number": "error",
2021
},
2122
}

lib/configs/recommended-with-json5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export = {
1111
"jsonc/no-template-literals": "error",
1212
"jsonc/no-undefined-value": "error",
1313
"jsonc/no-useless-escape": "error",
14+
"jsonc/space-unary-ops": "error",
1415
},
1516
}

lib/configs/recommended-with-jsonc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export = {
1414
"jsonc/no-useless-escape": "error",
1515
"jsonc/quote-props": "error",
1616
"jsonc/quotes": "error",
17+
"jsonc/space-unary-ops": "error",
1718
"jsonc/valid-json-number": "error",
1819
},
1920
}

lib/rules/space-unary-ops.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import coreRule from "eslint/lib/rules/space-unary-ops"
2+
import { createRule, defineWrapperListener } from "../utils"
3+
4+
export default createRule("space-unary-ops", {
5+
meta: {
6+
docs: {
7+
description: "disallow spaces after unary operators",
8+
recommended: ["json", "jsonc", "json5"],
9+
extensionRule: true,
10+
},
11+
fixable: coreRule.meta!.fixable,
12+
schema: coreRule.meta!.schema!,
13+
messages: coreRule.meta!.messages!,
14+
type: coreRule.meta!.type!,
15+
},
16+
create(context) {
17+
return defineWrapperListener(coreRule, context, [{ nonwords: false }])
18+
},
19+
})

lib/utils/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import objectPropertyNewline from "../rules/object-property-newline"
2020
import quoteProps from "../rules/quote-props"
2121
import quotes from "../rules/quotes"
2222
import sortKeys from "../rules/sort-keys"
23+
import spaceUnaryOps from "../rules/space-unary-ops"
2324
import validJsonNumber from "../rules/valid-json-number"
2425

2526
export const rules = [
@@ -44,5 +45,6 @@ export const rules = [
4445
quoteProps,
4546
quotes,
4647
sortKeys,
48+
spaceUnaryOps,
4749
validJsonNumber,
4850
] as RuleModule[]

tests/lib/rules/space-unary-ops.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/space-unary-ops"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
6+
})
7+
8+
tester.run("space-unary-ops", rule as any, {
9+
valid: ["-1", "+1", "-0", "+0"],
10+
invalid: [
11+
{
12+
code: "[- 1, + 1, - 0, + 0]",
13+
output: "[-1, +1, -0, +0]",
14+
errors: [
15+
{
16+
message: "Unexpected space after unary operator '-'.",
17+
line: 1,
18+
column: 2,
19+
endColumn: 5,
20+
},
21+
{
22+
message: "Unexpected space after unary operator '+'.",
23+
line: 1,
24+
column: 7,
25+
endColumn: 11,
26+
},
27+
{
28+
message: "Unexpected space after unary operator '-'.",
29+
line: 1,
30+
column: 13,
31+
endColumn: 17,
32+
},
33+
{
34+
message: "Unexpected space after unary operator '+'.",
35+
line: 1,
36+
column: 19,
37+
endColumn: 23,
38+
},
39+
],
40+
},
41+
],
42+
})

0 commit comments

Comments
 (0)