Skip to content

Commit bacf10e

Browse files
committed
Add jsonc/no-sparse-arrays rule
1 parent 92f3e6b commit bacf10e

File tree

10 files changed

+101
-4
lines changed

10 files changed

+101
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ The rules with the following star :star: are included in the config.
154154
| [jsonc/no-dupe-keys](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-dupe-keys.html) | disallow duplicate keys in object literals | | :star: | :star: | :star: |
155155
| [jsonc/no-multi-str](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-multi-str.html) | disallow multiline strings | | :star: | :star: | |
156156
| [jsonc/no-octal-escape](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-octal-escape.html) | disallow octal escape sequences in string literals | | | | |
157+
| [jsonc/no-sparse-arrays](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-sparse-arrays.html) | disallow sparse arrays | | :star: | :star: | :star: |
157158
| [jsonc/no-useless-escape](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-useless-escape.html) | disallow unnecessary escape usage | | :star: | :star: | :star: |
158159
| [jsonc/object-curly-newline](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/object-curly-newline.html) | enforce consistent line breaks inside braces | :wrench: | | | |
159160
| [jsonc/object-curly-spacing](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/object-curly-spacing.html) | enforce consistent spacing inside braces | :wrench: | | | |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
3232
| [jsonc/no-dupe-keys](./no-dupe-keys.md) | disallow duplicate keys in object literals | | :star: | :star: | :star: |
3333
| [jsonc/no-multi-str](./no-multi-str.md) | disallow multiline strings | | :star: | :star: | |
3434
| [jsonc/no-octal-escape](./no-octal-escape.md) | disallow octal escape sequences in string literals | | | | |
35+
| [jsonc/no-sparse-arrays](./no-sparse-arrays.md) | disallow sparse arrays | | :star: | :star: | :star: |
3536
| [jsonc/no-useless-escape](./no-useless-escape.md) | disallow unnecessary escape usage | | :star: | :star: | :star: |
3637
| [jsonc/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks inside braces | :wrench: | | | |
3738
| [jsonc/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: | | | |

docs/rules/no-sparse-arrays.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "jsonc/no-sparse-arrays"
5+
description: "disallow sparse arrays"
6+
---
7+
# jsonc/no-sparse-arrays
8+
9+
> disallow sparse arrays
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+
13+
## :book: Rule Details
14+
15+
This rule disallows sparse array literals which have "holes" where commas are not preceded by elements. It does not apply to a trailing comma following the last element.
16+
17+
JSON, JSONC and JSON5 do not allow arrays contain empty slots.
18+
19+
<eslint-code-block>
20+
21+
```json5
22+
/* eslint jsonc/no-sparse-arrays: 'error' */
23+
{
24+
/* ✓ GOOD */
25+
"GOOD": [1, 2, 3, 4],
26+
"GOOD": [1, 2, 3, 4,],
27+
28+
/* ✗ BAD */
29+
"BAD": [1, , , 4],
30+
"BAD": [, 2, 3, 4]
31+
}
32+
```
33+
34+
</eslint-code-block>
35+
36+
## :wrench: Options
37+
38+
Nothing.
39+
40+
## :couple: Related rules
41+
42+
- [no-sparse-arrays]
43+
44+
[no-sparse-arrays]: https://eslint.org/docs/rules/no-sparse-arrays
45+
46+
## Implementation
47+
48+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-sparse-arrays.ts)
49+
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-sparse-arrays.js)
50+
51+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-sparse-arrays)</sup>

lib/configs/recommended-with-json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export = {
1010
"jsonc/no-comments": "error",
1111
"jsonc/no-dupe-keys": "error",
1212
"jsonc/no-multi-str": "error",
13+
"jsonc/no-sparse-arrays": "error",
1314
"jsonc/no-template-literals": "error",
1415
"jsonc/no-undefined-value": "error",
1516
"jsonc/no-useless-escape": "error",

lib/configs/recommended-with-json5.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export = {
77
rules: {
88
// eslint-plugin-jsonc rules
99
"jsonc/no-dupe-keys": "error",
10+
"jsonc/no-sparse-arrays": "error",
1011
"jsonc/no-template-literals": "error",
1112
"jsonc/no-undefined-value": "error",
1213
"jsonc/no-useless-escape": "error",

lib/configs/recommended-with-jsonc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export = {
88
// eslint-plugin-jsonc rules
99
"jsonc/no-dupe-keys": "error",
1010
"jsonc/no-multi-str": "error",
11+
"jsonc/no-sparse-arrays": "error",
1112
"jsonc/no-template-literals": "error",
1213
"jsonc/no-undefined-value": "error",
1314
"jsonc/no-useless-escape": "error",

lib/rules/no-sparse-arrays.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/no-sparse-arrays"
2+
import { createRule, defineWrapperListener } from "../utils"
3+
4+
export default createRule("no-sparse-arrays", {
5+
meta: {
6+
docs: {
7+
description: "disallow sparse arrays",
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, context.options)
18+
},
19+
})

lib/utils/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import noComments from "../rules/no-comments"
1010
import noDupeKeys from "../rules/no-dupe-keys"
1111
import noMultiStr from "../rules/no-multi-str"
1212
import noOctalEscape from "../rules/no-octal-escape"
13+
import noSparseArrays from "../rules/no-sparse-arrays"
1314
import noTemplateLiterals from "../rules/no-template-literals"
1415
import noUndefinedValue from "../rules/no-undefined-value"
1516
import noUselessEscape from "../rules/no-useless-escape"
@@ -33,6 +34,7 @@ export const rules = [
3334
noDupeKeys,
3435
noMultiStr,
3536
noOctalEscape,
37+
noSparseArrays,
3638
noTemplateLiterals,
3739
noUndefinedValue,
3840
noUselessEscape,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/no-sparse-arrays"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
6+
})
7+
8+
tester.run("no-sparse-arrays", rule as any, {
9+
valid: ["[1,2,3,4]", "[1,2,3,4,]"],
10+
invalid: [
11+
{
12+
code: "[1,,,4]",
13+
errors: ["Unexpected comma in middle of array."],
14+
},
15+
{
16+
code: "[,2,3,4]",
17+
errors: ["Unexpected comma in middle of array."],
18+
},
19+
],
20+
})

tools/new-rule.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export default createRule("${ruleId}", {
3232
description: "...",
3333
recommended: true,
3434
},
35-
fixable: coreRule.meta?.fixable,
36-
schema: coreRule.meta?.schema!,
37-
messages: coreRule.meta?.messages!,
38-
type: coreRule.meta?.type!,
35+
fixable: coreRule.meta!.fixable,
36+
schema: coreRule.meta!.schema!,
37+
messages: coreRule.meta!.messages!,
38+
type: coreRule.meta!.type!,
3939
},
4040
create(context) {
4141
return defineWrapperListener(coreRule, context, context.options)

0 commit comments

Comments
 (0)