Skip to content

Commit a732b8e

Browse files
authored
Add jsonc/no-regexp-literals rule (#11)
* Add `jsonc/no-regexp-literals` rule * Fix message
1 parent bc12417 commit a732b8e

File tree

9 files changed

+97
-0
lines changed

9 files changed

+97
-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-number-props](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-number-props.html) | disallow number property keys | :wrench: | :star: | :star: | :star: |
140+
| [jsonc/no-regexp-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-regexp-literals.html) | disallow RegExp literals | | :star: | :star: | :star: |
140141
| [jsonc/no-template-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-template-literals.html) | disallow template literals | :wrench: | :star: | :star: | :star: |
141142
| [jsonc/no-undefined-value](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-undefined-value.html) | disallow `undefined` | | :star: | :star: | :star: |
142143
| [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: | |

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-number-props](./no-number-props.md) | disallow number property keys | :wrench: | :star: | :star: | :star: |
18+
| [jsonc/no-regexp-literals](./no-regexp-literals.md) | disallow RegExp literals | | :star: | :star: | :star: |
1819
| [jsonc/no-template-literals](./no-template-literals.md) | disallow template literals | :wrench: | :star: | :star: | :star: |
1920
| [jsonc/no-undefined-value](./no-undefined-value.md) | disallow `undefined` | | :star: | :star: | :star: |
2021
| [jsonc/valid-json-number](./valid-json-number.md) | disallow invalid number for JSON | :wrench: | :star: | :star: | |

docs/rules/no-regexp-literals.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-regexp-literals"
5+
description: "disallow RegExp literals"
6+
---
7+
# jsonc/no-regexp-literals
8+
9+
> disallow RegExp literals
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 RegExp literals.
16+
17+
JSON, JSONC and JSON5 do not allow RegExp literals.
18+
19+
<eslint-code-block>
20+
21+
```json5
22+
/* eslint jsonc/no-regexp-literals: 'error' */
23+
{
24+
/* ✗ BAD */
25+
"BAD": /foo/
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-regexp-literals.ts)
38+
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-regexp-literals.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-number-props": "error",
14+
"jsonc/no-regexp-literals": "error",
1415
"jsonc/no-sparse-arrays": "error",
1516
"jsonc/no-template-literals": "error",
1617
"jsonc/no-undefined-value": "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-number-props": "error",
11+
"jsonc/no-regexp-literals": "error",
1112
"jsonc/no-sparse-arrays": "error",
1213
"jsonc/no-template-literals": "error",
1314
"jsonc/no-undefined-value": "error",

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-number-props": "error",
12+
"jsonc/no-regexp-literals": "error",
1213
"jsonc/no-sparse-arrays": "error",
1314
"jsonc/no-template-literals": "error",
1415
"jsonc/no-undefined-value": "error",

lib/rules/no-regexp-literals.ts

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

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 noNumberProps from "../rules/no-number-props"
1313
import noOctalEscape from "../rules/no-octal-escape"
14+
import noRegexpLiterals from "../rules/no-regexp-literals"
1415
import noSparseArrays from "../rules/no-sparse-arrays"
1516
import noTemplateLiterals from "../rules/no-template-literals"
1617
import noUndefinedValue from "../rules/no-undefined-value"
@@ -37,6 +38,7 @@ export const rules = [
3738
noMultiStr,
3839
noNumberProps,
3940
noOctalEscape,
41+
noRegexpLiterals,
4042
noSparseArrays,
4143
noTemplateLiterals,
4244
noUndefinedValue,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/no-regexp-literals"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
6+
})
7+
8+
tester.run("no-regexp-literals", rule as any, {
9+
valid: ['{"key": "value"}', '"string"', '["element"]'],
10+
invalid: [
11+
{
12+
code: "/reg/",
13+
errors: ["RegExp literals are not allowed."],
14+
},
15+
{
16+
code: "[/reg/, {'/val/': /reg/}]",
17+
errors: [
18+
"RegExp literals are not allowed.",
19+
"RegExp literals are not allowed.",
20+
],
21+
},
22+
],
23+
})

0 commit comments

Comments
 (0)