Skip to content

Commit 92f3e6b

Browse files
authored
Add jsonc/no-undefined-value rule (#8)
1 parent 2be4854 commit 92f3e6b

File tree

10 files changed

+123
-0
lines changed

10 files changed

+123
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ The rules with the following star :star: are included in the config.
137137
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
138138
| [jsonc/no-comments](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-comments.html) | disallow comments | | :star: | | |
139139
| [jsonc/no-template-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-template-literals.html) | disallow template literals | :wrench: | :star: | :star: | :star: |
140+
| [jsonc/no-undefined-value](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-undefined-value.html) | disallow `undefined` | | :star: | :star: | :star: |
140141
| [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: | |
141142

142143
## Extension Rules

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
1515
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
1616
| [jsonc/no-comments](./no-comments.md) | disallow comments | | :star: | | |
1717
| [jsonc/no-template-literals](./no-template-literals.md) | disallow template literals | :wrench: | :star: | :star: | :star: |
18+
| [jsonc/no-undefined-value](./no-undefined-value.md) | disallow `undefined` | | :star: | :star: | :star: |
1819
| [jsonc/valid-json-number](./valid-json-number.md) | disallow invalid number for JSON | :wrench: | :star: | :star: | |
1920

2021
## Extension Rules

docs/rules/no-undefined-value.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "jsonc/no-undefined-value"
5+
description: "disallow `undefined`"
6+
---
7+
# jsonc/no-undefined-value
8+
9+
> disallow `undefined`
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 reports the use of `undefined`.
16+
17+
JSON, JSONC and JSON5 do not allow `undefined`.
18+
19+
<eslint-code-block>
20+
21+
```json5
22+
/* eslint jsonc/no-undefined-value: 'error' */
23+
{
24+
/* ✗ BAD */
25+
"BAD": undefined
26+
}
27+
```
28+
29+
</eslint-code-block>
30+
31+
## :wrench: Options
32+
33+
Nothing.
34+
35+
## Implementation
36+
37+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-undefined-value.ts)
38+
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-undefined-value.js)

lib/configs/recommended-with-json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export = {
1111
"jsonc/no-dupe-keys": "error",
1212
"jsonc/no-multi-str": "error",
1313
"jsonc/no-template-literals": "error",
14+
"jsonc/no-undefined-value": "error",
1415
"jsonc/no-useless-escape": "error",
1516
"jsonc/quote-props": "error",
1617
"jsonc/quotes": "error",

lib/configs/recommended-with-json5.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-template-literals": "error",
11+
"jsonc/no-undefined-value": "error",
1112
"jsonc/no-useless-escape": "error",
1213
},
1314
}

lib/configs/recommended-with-jsonc.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export = {
99
"jsonc/no-dupe-keys": "error",
1010
"jsonc/no-multi-str": "error",
1111
"jsonc/no-template-literals": "error",
12+
"jsonc/no-undefined-value": "error",
1213
"jsonc/no-useless-escape": "error",
1314
"jsonc/quote-props": "error",
1415
"jsonc/quotes": "error",

lib/rules/no-undefined-value.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { JSONIdentifier } from "../parser/ast"
2+
import { createRule } from "../utils"
3+
import { isUndefinedIdentifier } from "../utils/ast"
4+
5+
export default createRule("no-undefined-value", {
6+
meta: {
7+
docs: {
8+
description: "disallow `undefined`",
9+
recommended: ["json", "jsonc", "json5"],
10+
extensionRule: false,
11+
},
12+
schema: [],
13+
messages: {
14+
unexpected: "`undefined` is not allowed.",
15+
},
16+
type: "problem",
17+
},
18+
create(context) {
19+
return {
20+
JSONIdentifier(node: JSONIdentifier) {
21+
if (!isUndefinedIdentifier(node)) {
22+
return
23+
}
24+
context.report({
25+
loc: node.loc,
26+
messageId: "unexpected",
27+
})
28+
},
29+
}
30+
},
31+
})

lib/utils/ast.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ export function isNumberIdentifier(
5353
)
5454
}
5555

56+
/**
57+
* Checks if given node is JSONUndefinedIdentifier
58+
*/
59+
export function isUndefinedIdentifier(
60+
node: JSONIdentifier,
61+
): node is JSONUndefinedIdentifier {
62+
return isExpression(node) && node.name === "undefined"
63+
}
64+
5665
export type JSONValue =
5766
| string
5867
| number

lib/utils/rules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import noDupeKeys from "../rules/no-dupe-keys"
1111
import noMultiStr from "../rules/no-multi-str"
1212
import noOctalEscape from "../rules/no-octal-escape"
1313
import noTemplateLiterals from "../rules/no-template-literals"
14+
import noUndefinedValue from "../rules/no-undefined-value"
1415
import noUselessEscape from "../rules/no-useless-escape"
1516
import objectCurlyNewline from "../rules/object-curly-newline"
1617
import objectCurlySpacing from "../rules/object-curly-spacing"
@@ -33,6 +34,7 @@ export const rules = [
3334
noMultiStr,
3435
noOctalEscape,
3536
noTemplateLiterals,
37+
noUndefinedValue,
3638
noUselessEscape,
3739
objectCurlyNewline,
3840
objectCurlySpacing,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/no-undefined-value"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
6+
})
7+
8+
tester.run("no-undefined-value", rule as any, {
9+
valid: ["null", "[1,,2]", "{undefined:1}"],
10+
invalid: [
11+
{
12+
code: "undefined",
13+
errors: ["`undefined` is not allowed."],
14+
},
15+
{
16+
code: `[
17+
undefined,
18+
{
19+
undefined: undefined
20+
}
21+
]`,
22+
errors: [
23+
{
24+
message: "`undefined` is not allowed.",
25+
line: 2,
26+
column: 17,
27+
endColumn: 26,
28+
},
29+
{
30+
message: "`undefined` is not allowed.",
31+
line: 4,
32+
column: 32,
33+
endColumn: 41,
34+
},
35+
],
36+
},
37+
],
38+
})

0 commit comments

Comments
 (0)