Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛠 Tooling: Find a way to avoid 'as unknown as ESTree.Node' assertions #125

Open
3 tasks done
JoshuaKGoldberg opened this issue Jan 20, 2024 · 0 comments
Open
3 tasks done
Labels
area: tooling Managing the repository's maintenance status: blocked Waiting for something else to be resolved

Comments

@JoshuaKGoldberg
Copy link
Owner

JoshuaKGoldberg commented Jan 20, 2024

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Overview

Following #40 & #59 & ota-meshi/jsonc-eslint-parser#184: the current state of types for AST nodes in this plugin 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: directoryProperty.value as unknown as ESTree.Node,

This isn't ideal. One workaround done by eslint-plugin-jsonc is to instead pass loc: node.loc. But I'd ideally like to be able to just pass the node directly.

Additional Info

I don't know a straightforward way to make this better. context.report is typed in @types/eslint as:

report(descriptor: ReportDescriptor): void;

...with:

type ReportDescriptor = ReportDescriptorMessage & ReportDescriptorLocation & ReportDescriptorOptions;
type ReportDescriptorMessage = { message: string } | { messageId: string };
type ReportDescriptorLocation =
    | { node: ESTree.Node }
    | { loc: AST.SourceLocation | { line: number; column: number } };

I suppose @types/eslint could mark the context with a type parameter like <Node = ESTree.Node> 😬... Eww.

Filed ota-meshi/jsonc-eslint-parser#198. Marking as blocked pending discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: tooling Managing the repository's maintenance status: blocked Waiting for something else to be resolved
Projects
None yet
Development

No branches or pull requests

1 participant