Skip to content

πŸ›  Tooling: Find a way to avoid 'as unknown as ESTree.Node' assertionsΒ #125

@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: toolingManaging the repository's maintenance πŸ› οΈstatus: accepting prsPlease, send a pull request to resolve this! πŸ™

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions