ota-meshi/jsonc-eslint-parser

Types: Allow passing JsonAST nodes directly to ESLint context APIs

JoshuaKGoldberg opened this issue · 3 comments

Description

Following #184 -> #186: I'm still finding that I need to do some massaging of TypeScript types. Also filed in JoshuaKGoldberg/eslint-plugin-package-json#125.

The current state of types for AST nodes in eslint-plugin-package-json is that we've completely swapped out the built-in ESLint ones. Nodes are instead typed using import type { AST as JsonAST } from "jsonc-eslint-parser"; -> node: JsonAST.JSONProperty and the like.

Unfortunately, ESLint's APIs still expect ESTree nodes. So we end up with assertions any time we need to pass the nodes to ESLint's APIs:

context.report({ 
  messageId: "mismatched", 
  node: node as unknown as ESTree.Node, 
});

This isn't ideal. One workaround done by eslint-plugin-jsonc is to [instead pass loc: node.loc](https://github.com/ota-meshi/eslint-plugin-jsonc/blob/bb0cc408c928ef12952d140add87f3c2b8a2eb40/lib/rules/no-bigint-literals.ts#L26-L27). But I'd ideally like to be able to just pass the node` directly.

Is this something we can solve at the parser level in jsonc-eslint-parser? Slash, maybe we should modify @types/eslint in some way to make the context generic (per JoshuaKGoldberg/eslint-plugin-package-json#125 additional info)?

Thank you for posting this issue.

Hmm... In my opinion, the best option is to change @types/eslint to accept ESTree.Node | {type: string, loc: SourceLocation} as a node.
What do you think?

Oh! I should have thought of that 🤦. Yes, that makes sense. +1 😄