-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
jsonc/no-irregular-whitespace
rule (#199)
* feat: add `jsonc/no-irregular-whitespace` rule * Create shiny-dodos-relax.md
- Loading branch information
Showing
9 changed files
with
343 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-jsonc": minor | ||
--- | ||
|
||
feat: add `jsonc/no-irregular-whitespace` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "jsonc/no-irregular-whitespace" | ||
description: "disallow irregular whitespace" | ||
--- | ||
|
||
# jsonc/no-irregular-whitespace | ||
|
||
> disallow irregular whitespace | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule is aimed at catching invalid whitespace that is not a normal tab and space. | ||
|
||
<eslint-code-block> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```json | ||
/* eslint jsonc/no-irregular-whitespace: 'error' */ | ||
{ | ||
/* ✓ GOOD */ | ||
"GOOD": "", | ||
|
||
/* ✗ BAD */ | ||
"BAD": "foo", | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
ESLint core [no-irregular-whitespace] rule don't work well in JSON. Turn off that rule in JSON files and use `jsonc/no-irregular-whitespace` rule. | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
```json | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": ["*.json", "*.json5"], | ||
"rules": { | ||
"no-irregular-whitespace": "off", | ||
"jsonc/no-irregular-whitespace": [ | ||
"error", | ||
{ | ||
"skipStrings": true, | ||
"skipComments": false, | ||
"skipRegExps": false, | ||
"skipTemplates": false | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Same as [no-irregular-whitespace] rule option. See [here](https://eslint.org/docs/rules/no-irregular-whitespace#options) for details. | ||
|
||
## :couple: Related rules | ||
|
||
- [no-irregular-whitespace] | ||
|
||
[no-irregular-whitespace]: https://eslint.org/docs/rules/no-irregular-whitespace | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-irregular-whitespace.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/tests/lib/rules/no-irregular-whitespace.ts) | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-irregular-whitespace)</sup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createRule, defineWrapperListener, getCoreRule } from "../utils"; | ||
const coreRule = getCoreRule("no-irregular-whitespace"); | ||
|
||
export default createRule("no-irregular-whitespace", { | ||
meta: { | ||
docs: { | ||
description: "disallow irregular whitespace", | ||
// TODO: We will switch this in the next major version. | ||
recommended: null, | ||
// recommended: ["json", "jsonc", "json5"], // TODO: We need to turn off core `no-irregular-whitespace` rule in shared config. | ||
extensionRule: true, | ||
layout: false, | ||
}, | ||
fixable: coreRule.meta?.fixable, | ||
hasSuggestions: (coreRule.meta as any)?.hasSuggestions, | ||
schema: coreRule.meta!.schema!, | ||
messages: coreRule.meta!.messages!, | ||
type: coreRule.meta!.type!, | ||
}, | ||
create(context) { | ||
return defineWrapperListener(coreRule, context, context.options); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
import { RuleTester } from "eslint"; | ||
import rule from "../../../lib/rules/no-irregular-whitespace"; | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve("jsonc-eslint-parser"), | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
}, | ||
}); | ||
|
||
tester.run("no-irregular-whitespace", rule as any, { | ||
valid: [ | ||
`"\u0020"`, | ||
`"\u0009"`, | ||
`"\\u000B"`, | ||
`["\u0009"]`, | ||
`["\\u000B"]`, | ||
`{"\u0009": "\u0009"}`, | ||
`{"\\u000B": "\\u000B"}`, | ||
|
||
// String | ||
`"\u000B"`, | ||
`["\u000B"]`, | ||
`{"\u000B": "\u000B"}`, | ||
{ | ||
code: `"\u000B"`, | ||
options: [{ skipStrings: true }], | ||
}, | ||
{ | ||
code: `["\u000B"]`, | ||
options: [{ skipStrings: true }], | ||
}, | ||
{ | ||
code: `{"\u000B": "\u000B"}`, | ||
options: [{ skipStrings: true }], | ||
}, | ||
// Templates | ||
{ | ||
code: `\`\u000B\``, | ||
options: [{ skipTemplates: true }], | ||
}, | ||
{ | ||
code: `[\`\u000B\`]`, | ||
options: [{ skipTemplates: true }], | ||
}, | ||
{ | ||
code: `{"\u000B": \`\u000B\`}`, | ||
options: [{ skipTemplates: true }], | ||
}, | ||
// RegExps | ||
{ | ||
code: `/\u000B/`, | ||
options: [{ skipRegExps: true }], | ||
}, | ||
{ | ||
code: `[/\u000B/]`, | ||
options: [{ skipRegExps: true }], | ||
}, | ||
{ | ||
code: `{"\u000B": /\u000B/}`, | ||
options: [{ skipRegExps: true }], | ||
}, | ||
// Comments | ||
{ | ||
code: `{} // \u000B`, | ||
options: [{ skipComments: true }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: `{"\u000B": [\`\u000B\`, /\u000B/]} \u000B // \u000B`, | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 9, | ||
}, | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 14, | ||
}, | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 19, | ||
}, | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 24, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `{"\u000B": [\`\u000B\`, /\u000B/]} \u000B // \u000B`, | ||
options: [ | ||
{ | ||
skipStrings: true, | ||
skipComments: true, | ||
skipRegExps: true, | ||
skipTemplates: true, | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 19, | ||
}, | ||
], | ||
}, | ||
// String | ||
{ | ||
code: `{"\u000B": "\u000B"}`, | ||
options: [{ skipStrings: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 3, | ||
}, | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 8, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `["\u000B"]`, | ||
options: [{ skipStrings: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `"\u000B"`, | ||
options: [{ skipStrings: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 2, | ||
}, | ||
], | ||
}, | ||
// Templates | ||
{ | ||
code: `\`\u000B\``, | ||
options: [{ skipTemplates: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 2, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `[\`\u000B\`]`, | ||
options: [{ skipTemplates: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `{"\u000B": \`\u000B\`}`, | ||
options: [{ skipTemplates: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 8, | ||
}, | ||
], | ||
}, | ||
// RegExps | ||
{ | ||
code: `/\u000B/`, | ||
options: [{ skipRegExps: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 2, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `[/\u000B/]`, | ||
options: [{ skipRegExps: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `{"\u000B": /\u000B/}`, | ||
options: [{ skipRegExps: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 8, | ||
}, | ||
], | ||
}, | ||
// Comments | ||
{ | ||
code: `{} // \u000B`, | ||
options: [{ skipComments: false }], | ||
errors: [ | ||
{ | ||
message: "Irregular whitespace not allowed.", | ||
line: 1, | ||
column: 7, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |