11import { parse as swcParse } from "@swc/core" ;
2- import type { ImportDeclaration , JSXAttrValue , Module } from "@swc/core" ;
2+ import type { Module } from "@swc/core" ;
33import { visit } from "esvisitor" ;
44
5- import type { Import , Primitive } from "../../types" ;
6- import type { Nodes , Plugin , PluginItemOutput } from "../plugin" ;
5+ import type { Import , Nodes , Primitive } from "../../types" ;
6+ import type { Plugin , PluginOutput } from "../plugin" ;
77
8- type Options = {
9- onAdd : ( item : PluginItemOutput ) => void ;
10- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11- plugins : Plugin < any > [ ] ;
8+ export type ParseOptions = {
9+ onAdd : ( item : PluginOutput ) => void ;
10+ /**
11+ * A list of plugins to enable.
12+ */
13+ plugins : Plugin [ ] ;
1214} ;
1315
14- export const parse = async ( code : string , { onAdd, plugins } : Options ) => {
16+ export const parse = async ( code : string , { onAdd, plugins } : ParseOptions ) => {
1517 const context = {
1618 imports : new Map < Import [ "alias" ] , Import > ( ) ,
1719 } ;
@@ -31,7 +33,7 @@ export const parse = async (code: string, { onAdd, plugins }: Options) => {
3133 if ( ast === null ) return ;
3234
3335 const visitor : {
34- [ Key in keyof SupportedNodes ] ?: VisitorFunction < Key > ;
36+ [ Key in keyof Nodes ] ?: VisitorFunction < Key > ;
3537 } = {
3638 ImportDeclaration ( node ) {
3739 const module = node . source . value ;
@@ -54,19 +56,26 @@ export const parse = async (code: string, { onAdd, plugins }: Options) => {
5456 for ( const plugin of plugins ) {
5557 const pluginOutput = plugin ( context , {
5658 getJSXAttributeValue,
57- } ) ;
59+ } ) as Record <
60+ keyof Nodes ,
61+ ( node : Nodes [ keyof Nodes ] ) => PluginOutput | undefined
62+ > ;
5863
59- const nodeKeys = Object . keys ( pluginOutput ) as ( keyof SupportedNodes ) [ ] ;
64+ const nodeKeys = Object . keys (
65+ pluginOutput ,
66+ ) as ( keyof typeof pluginOutput ) [ ] ;
6067
6168 for ( const nodeKey of nodeKeys ) {
62- const currentVisitorFn = visitor [ nodeKey ] as VisitorFunction ;
69+ const currentVisitorFn = visitor [ nodeKey ] as
70+ | VisitorFunction
71+ | undefined ;
6372
6473 visitor [ nodeKey ] = ( node ) => {
6574 if ( typeof currentVisitorFn === "function" ) {
6675 currentVisitorFn ( node ) ;
6776 }
6877
69- const output = pluginOutput [ nodeKey ] ?. ( node ) ;
78+ const output = pluginOutput [ nodeKey ] ( node ) ;
7079
7180 if ( output ) {
7281 onAdd ( output ) ;
@@ -75,17 +84,16 @@ export const parse = async (code: string, { onAdd, plugins }: Options) => {
7584 }
7685 }
7786
78- visit < SupportedNodes > ( ast , visitor ) ;
79- } ;
80-
81- type SupportedNodes = Nodes & {
82- ImportDeclaration : ImportDeclaration ;
87+ visit < Nodes > ( ast , visitor ) ;
8388} ;
8489
85- type VisitorFunction < Key extends keyof SupportedNodes = keyof SupportedNodes > =
86- ( node : SupportedNodes [ Key ] ) => void ;
90+ type VisitorFunction < Key extends keyof Nodes = keyof Nodes > = (
91+ node : Nodes [ Key ] ,
92+ ) => void ;
8793
88- const getJSXAttributeValue = ( node : JSXAttrValue | undefined ) : Primitive => {
94+ const getJSXAttributeValue = (
95+ node : Nodes [ "JSXAttrValue" ] | undefined ,
96+ ) : Primitive => {
8997 if ( ! node ) {
9098 return true ;
9199 }
@@ -105,7 +113,7 @@ const getJSXAttributeValue = (node: JSXAttrValue | undefined): Primitive => {
105113 }
106114
107115 if ( node . type === "JSXExpressionContainer" ) {
108- return getJSXAttributeValue ( node . expression as JSXAttrValue ) ;
116+ return getJSXAttributeValue ( node . expression as Nodes [ " JSXAttrValue" ] ) ;
109117 }
110118
111119 return createUnknownToken ( node . type ) ;
0 commit comments