Skip to content

Commit 2be4854

Browse files
authored
Add jsonc/no-template-literals rule (#9)
* Add ``jsonc/no-template-literals` rule * Add parallel-finished * update
1 parent a93f47d commit 2be4854

File tree

11 files changed

+124
-0
lines changed

11 files changed

+124
-0
lines changed

.github/workflows/NodeCI.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ jobs:
5252
steps:
5353
- uses: actions/checkout@v1
5454
- uses: actions/setup-node@v1
55+
with:
56+
node-version: 10.x
5557
- name: Install Packages
5658
run: npm install
5759
- name: Test

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ The rules with the following star :star: are included in the config.
136136
| Rule ID | Description | Fixable | JSON | JSONC | JSON5 |
137137
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
138138
| [jsonc/no-comments](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-comments.html) | disallow comments | | :star: | | |
139+
| [jsonc/no-template-literals](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/no-template-literals.html) | disallow template literals | :wrench: | :star: | :star: | :star: |
139140
| [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: | |
140141

141142
## Extension Rules

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The rules with the following star :star: are included in the `plugin:jsonc/recom
1414
| Rule ID | Description | Fixable | JSON | JSONC | JSON5 |
1515
|:--------|:------------|:-------:|:----:|:-----:|:-----:|
1616
| [jsonc/no-comments](./no-comments.md) | disallow comments | | :star: | | |
17+
| [jsonc/no-template-literals](./no-template-literals.md) | disallow template literals | :wrench: | :star: | :star: | :star: |
1718
| [jsonc/valid-json-number](./valid-json-number.md) | disallow invalid number for JSON | :wrench: | :star: | :star: | |
1819

1920
## Extension Rules

docs/rules/no-template-literals.md

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

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-template-literals": "error",
1314
"jsonc/no-useless-escape": "error",
1415
"jsonc/quote-props": "error",
1516
"jsonc/quotes": "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-template-literals": "error",
1011
"jsonc/no-useless-escape": "error",
1112
},
1213
}

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-template-literals": "error",
1112
"jsonc/no-useless-escape": "error",
1213
"jsonc/quote-props": "error",
1314
"jsonc/quotes": "error",

lib/rules/no-template-literals.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { JSONTemplateLiteral } from "../parser/ast"
2+
import { createRule } from "../utils"
3+
4+
export default createRule("no-template-literals", {
5+
meta: {
6+
docs: {
7+
description: "disallow template literals",
8+
recommended: ["json", "jsonc", "json5"],
9+
extensionRule: false,
10+
},
11+
fixable: "code",
12+
schema: [],
13+
messages: {
14+
unexpected: "The template literals are not allowed.",
15+
},
16+
type: "problem",
17+
},
18+
create(context) {
19+
return {
20+
JSONTemplateLiteral(node: JSONTemplateLiteral) {
21+
context.report({
22+
loc: node.loc,
23+
messageId: "unexpected",
24+
fix(fixer) {
25+
const s = node.quasis[0].value.cooked
26+
return fixer.replaceTextRange(
27+
node.range,
28+
JSON.stringify(s),
29+
)
30+
},
31+
})
32+
},
33+
}
34+
},
35+
})

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 noTemplateLiterals from "../rules/no-template-literals"
1314
import noUselessEscape from "../rules/no-useless-escape"
1415
import objectCurlyNewline from "../rules/object-curly-newline"
1516
import objectCurlySpacing from "../rules/object-curly-spacing"
@@ -31,6 +32,7 @@ export const rules = [
3132
noDupeKeys,
3233
noMultiStr,
3334
noOctalEscape,
35+
noTemplateLiterals,
3436
noUselessEscape,
3537
objectCurlyNewline,
3638
objectCurlySpacing,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { RuleTester } from "eslint"
2+
import rule from "../../../lib/rules/no-template-literals"
3+
4+
const tester = new RuleTester({
5+
parser: require.resolve("../../../lib/parser/json-eslint-parser"),
6+
parserOptions: {
7+
ecmaVersion: 2020,
8+
},
9+
})
10+
11+
tester.run("no-template-literals", rule as any, {
12+
valid: ['{"key": "value"}', '"string"', '["element"]'],
13+
invalid: [
14+
{
15+
code: "`template`",
16+
output: '"template"',
17+
errors: ["The template literals are not allowed."],
18+
},
19+
{
20+
code: "[`template`]",
21+
output: '["template"]',
22+
errors: ["The template literals are not allowed."],
23+
},
24+
{
25+
code: '{"foo":`template`}',
26+
output: '{"foo":"template"}',
27+
errors: ["The template literals are not allowed."],
28+
},
29+
{
30+
code: "`temp\n\nlate`",
31+
output: '"temp\\n\\nlate"',
32+
errors: ["The template literals are not allowed."],
33+
},
34+
],
35+
})

0 commit comments

Comments
 (0)