Skip to content

Commit 8027edd

Browse files
feat: bring in ESLint-oriented types, with plugin docs (#186)
* feat: bring in ESLint-oriented types, with plugin docs * Added changeset
1 parent a1cab04 commit 8027edd

File tree

8 files changed

+68
-15
lines changed

8 files changed

+68
-15
lines changed

.changeset/eight-bananas-burn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"jsonc-eslint-parser": minor
3+
---
4+
5+
Added ESLint-oriented types, with plugin docs

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ If not specified, all syntaxes that express static values ​​are accepted. Fo
6969
## Usage for Custom Rules / Plugins
7070

7171
- [AST.md](./docs/AST.md) is AST specification.
72+
- [Plugins.md](./docs/Plugins.md) describes using this in an ESLint plugin.
7273
- [no-template-literals.ts](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/master/lib/rules/no-template-literals.ts) is an example.
7374
- You can see the AST on the [Online DEMO](https://ota-meshi.github.io/jsonc-eslint-parser/).
7475

docs/Plugins.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Plugins
2+
3+
Users of plugins that rely on `jsonc-eslint-parser` need to explicitly configure the parser for files linted with that plugin.
4+
5+
Consider including snippets like the following in the plugin's documentation:
6+
7+
```shell
8+
npm install eslint eslint-plugin-your-name-here jsonc-eslint-parser --save-dev
9+
```
10+
11+
```js
12+
module.exports = {
13+
// ...
14+
overrides: [
15+
{
16+
files: ["*.json", "*.json5"],
17+
extends: ["plugin:your-name-here/recommended"],
18+
parser: "jsonc-eslint-parser",
19+
plugins: ["your-name-here"],
20+
},
21+
],
22+
};
23+
```
24+
25+
See [`eslint-plugin-jsonc`](https://github.com/ota-meshi/eslint-plugin-jsonc) for an example package.
26+
27+
## TypeScript
28+
29+
`jsonc-eslint-parser` exports types that replace the following built-in ESLint types:
30+
31+
- `RuleFunction`: Sets the `node` parameter to be an `AST.JSONNode` or `never`
32+
- `RuleListener`: Replaces built-in rule listeners with JSON node types
33+
- For example, `JSONLiteral(node) {` sets type `AST.JSONLiteral` for `node`
34+
- It also sets the equivalent `:exit` types, such as `'JSONLiteral:exit(node) {`
35+
36+
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.

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type * as AST from "./parser/ast";
1111
import { getVisitorKeys } from "./parser/visitor-keys";
1212
export * as meta from "./meta";
1313
export { name } from "./meta";
14+
export type * from "./types";
1415

1516
// parser
1617
export { parseForESLint };

src/parser/ast.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Token, Comment } from "../types";
1+
import type { AST as ESLintAST } from "eslint";
2+
import type { Comment } from "estree";
23

34
export interface Locations {
45
loc: SourceLocation;
@@ -33,7 +34,7 @@ export interface JSONProgram extends BaseJSONNode {
3334
type: "Program";
3435
body: [JSONExpressionStatement];
3536
comments: Comment[];
36-
tokens: Token[];
37+
tokens: ESLintAST.Token[];
3738
parent: null;
3839
}
3940

src/parser/errors.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Node } from "estree";
1+
import type { Comment, Node } from "estree";
22
import type { TokenStore, MaybeNodeOrToken } from "./token-store";
3-
import type { Comment } from "../types";
43
import type { JSONNode } from "./ast";
54
import { isRegExpLiteral } from "./utils";
65

src/parser/extend-parser.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { TokenStore } from "./token-store";
22
import { validateNode } from "./validate";
33
import type { Parser, Options, Node } from "acorn";
4-
import type { Comment } from "../types";
5-
import type { Node as ESTreeNode } from "estree";
4+
import type { Comment, Node as ESTreeNode } from "estree";
65
import { getAcorn } from "./modules/acorn";
76
import {
87
ParseError,

src/types.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import type { AST } from "eslint";
2-
import type { Comment as ESTreeComment } from "estree";
3-
export interface RuleListener {
4-
[key: string]: (node: never) => void;
5-
}
6-
7-
export type Token = AST.Token;
8-
export type Comment = ESTreeComment;
1+
import type { AST } from "jsonc-eslint-parser";
92

103
export type JSONSyntax = "JSON" | "JSONC" | "JSON5" | null;
114

12-
export interface ParserOptions {
5+
export interface JSONParserOptions {
136
jsonSyntax?: JSONSyntax;
147
}
8+
9+
export type RuleFunction<Node extends AST.JSONNode = never> = (
10+
node: Node,
11+
) => void;
12+
13+
export type BuiltInRuleListeners = {
14+
[Node in AST.JSONNode as Node["type"]]?: RuleFunction<Node>;
15+
};
16+
17+
export type BuiltInRuleListenerExits = {
18+
[Node in AST.JSONNode as `${Node["type"]}:exit`]?: RuleFunction<Node>;
19+
};
20+
21+
export interface RuleListener
22+
extends BuiltInRuleListeners,
23+
BuiltInRuleListenerExits {
24+
[key: string]: RuleFunction | undefined;
25+
}

0 commit comments

Comments
 (0)