-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: bring in ESLint-oriented types, with plugin docs (#186)
* feat: bring in ESLint-oriented types, with plugin docs * Added changeset
- Loading branch information
1 parent
a1cab04
commit 8027edd
Showing
8 changed files
with
68 additions
and
15 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 @@ | ||
--- | ||
"jsonc-eslint-parser": minor | ||
--- | ||
|
||
Added ESLint-oriented types, with plugin docs |
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,36 @@ | ||
# Plugins | ||
|
||
Users of plugins that rely on `jsonc-eslint-parser` need to explicitly configure the parser for files linted with that plugin. | ||
|
||
Consider including snippets like the following in the plugin's documentation: | ||
|
||
```shell | ||
npm install eslint eslint-plugin-your-name-here jsonc-eslint-parser --save-dev | ||
``` | ||
|
||
```js | ||
module.exports = { | ||
// ... | ||
overrides: [ | ||
{ | ||
files: ["*.json", "*.json5"], | ||
extends: ["plugin:your-name-here/recommended"], | ||
parser: "jsonc-eslint-parser", | ||
plugins: ["your-name-here"], | ||
}, | ||
], | ||
}; | ||
``` | ||
|
||
See [`eslint-plugin-jsonc`](https://github.com/ota-meshi/eslint-plugin-jsonc) for an example package. | ||
|
||
## TypeScript | ||
|
||
`jsonc-eslint-parser` exports types that replace the following built-in ESLint types: | ||
|
||
- `RuleFunction`: Sets the `node` parameter to be an `AST.JSONNode` or `never` | ||
- `RuleListener`: Replaces built-in rule listeners with JSON node types | ||
- For example, `JSONLiteral(node) {` sets type `AST.JSONLiteral` for `node` | ||
- It also sets the equivalent `:exit` types, such as `'JSONLiteral:exit(node) {` | ||
|
||
See [`eslint-plugin-jsonc`](https://github.com/ota-meshi/eslint-plugin-jsonc)'s [`lib/types.ts`](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/types.ts) for example usage of this parser's TypeScript types. |
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
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 |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import type { AST } from "eslint"; | ||
import type { Comment as ESTreeComment } from "estree"; | ||
export interface RuleListener { | ||
[key: string]: (node: never) => void; | ||
} | ||
|
||
export type Token = AST.Token; | ||
export type Comment = ESTreeComment; | ||
import type { AST } from "jsonc-eslint-parser"; | ||
|
||
export type JSONSyntax = "JSON" | "JSONC" | "JSON5" | null; | ||
|
||
export interface ParserOptions { | ||
export interface JSONParserOptions { | ||
jsonSyntax?: JSONSyntax; | ||
} | ||
|
||
export type RuleFunction<Node extends AST.JSONNode = never> = ( | ||
node: Node, | ||
) => void; | ||
|
||
export type BuiltInRuleListeners = { | ||
[Node in AST.JSONNode as Node["type"]]?: RuleFunction<Node>; | ||
}; | ||
|
||
export type BuiltInRuleListenerExits = { | ||
[Node in AST.JSONNode as `${Node["type"]}:exit`]?: RuleFunction<Node>; | ||
}; | ||
|
||
export interface RuleListener | ||
extends BuiltInRuleListeners, | ||
BuiltInRuleListenerExits { | ||
[key: string]: RuleFunction | undefined; | ||
} |