@@ -3,11 +3,6 @@ import { walk } from "estree-walker";
3
3
import prettier from "@prettier/sync" ;
4
4
import { codeBlockPrettierConfig } from "../other/code-block-prettier.js" ;
5
5
6
- // eslint-disable-next-line ts/no-explicit-any
7
- type Attribute = any ;
8
- // eslint-disable-next-line ts/no-explicit-any
9
- type TemplateNode = any ;
10
-
11
6
type Chunk = {
12
7
name : string ;
13
8
dependencies : string [ ] ;
@@ -18,38 +13,38 @@ type Chunk = {
18
13
container : { className : string } ;
19
14
} ;
20
15
export function getChunks ( source : string , filename : string ) {
21
- const ast = parse ( source , { filename } ) ;
16
+ type TemplateNode = typeof ast [ "fragment" ] [ "nodes" ] extends Array < infer T > ? T : typeof ast [ "fragment" ] [ "nodes" ] ;
17
+ const ast = parse ( source , { filename, modern : true } ) ;
22
18
const chunks : Chunk [ ] = [ ] ;
23
19
24
- // eslint-disable-next-line ts/no-explicit-any
25
- walk ( ast as any , {
26
- enter ( n ) {
20
+ // @ts -expect-error yea, stfu
21
+ walk ( ast , {
22
+ enter ( n : TemplateNode ) {
27
23
const chunkNode = n as TemplateNode ;
28
- if ( chunkNode . type !== "Element " && chunkNode . type !== "InlineComponent " ) return ;
24
+ if ( chunkNode . type !== "RegularElement " && chunkNode . type !== "Component " ) return ;
29
25
30
- const attrs : Attribute [ ] = chunkNode . attributes ;
26
+ const attrs = chunkNode . attributes . filter ( a => a . type === "Attribute" ) ;
31
27
const nameNode = attrs . find ( ( a ) => a . name === "data-x-chunk-name" ) ;
32
28
const descriptionNode = attrs . find ( ( a ) => a . name === "data-x-chunk-description" ) ;
33
29
if ( descriptionNode === undefined || nameNode === undefined ) return ;
34
30
35
31
const containerNode = attrs . find ( ( a ) => a . name === "data-x-chunk-container" ) ;
36
32
37
- const name : string = nameNode . value [ 0 ] . data ;
38
- const description : string = descriptionNode . value [ 0 ] . data ;
39
- const containerClassName : string = containerNode ?. value [ 0 ] . data ?? "" ;
33
+ const name = extractAttributeValue ( nameNode ) ! ;
34
+ const description = extractAttributeValue ( descriptionNode ) ! ;
35
+ const containerClassName = containerNode ? extractAttributeValue ( containerNode ) ! : "" ;
40
36
const dependencies = new Set < string > ( ) ;
41
37
42
38
// discard any prop members
43
- const [ componentName ] = chunkNode . name . split ( "." ) as string [ ] ;
39
+ const [ componentName ] = chunkNode . name . split ( "." ) ;
44
40
dependencies . add ( componentName ) ;
45
41
46
42
// walk the chunk to acquire all component dependencies
47
- // eslint-disable-next-line ts/no-explicit-any
48
- walk ( chunkNode as any , {
49
- enter ( n ) {
50
- const node = n as TemplateNode ;
51
- if ( node . type === "InlineComponent" ) {
52
- const [ componentName ] = node . name . split ( "." ) as string [ ] ;
43
+ // @ts -expect-error stfu
44
+ walk ( chunkNode . fragment , {
45
+ enter ( node : TemplateNode ) {
46
+ if ( node . type === "Component" ) {
47
+ const [ componentName ] = node . name . split ( "." ) ;
53
48
dependencies . add ( componentName ) ;
54
49
}
55
50
} ,
@@ -75,6 +70,13 @@ export function getChunks(source: string, filename: string) {
75
70
return chunks ;
76
71
}
77
72
73
+ // eslint-disable-next-line ts/no-explicit-any
74
+ function extractAttributeValue ( attribute : any ) : string | undefined {
75
+ if ( Array . isArray ( attribute . value ) && attribute . value [ 0 ] . type === "Text" ) {
76
+ return attribute . value [ 0 ] . data ;
77
+ }
78
+ }
79
+
78
80
export function transformChunk ( source : string , chunk : Chunk ) : string {
79
81
const html = source . substring ( chunk . start , chunk . end ) ;
80
82
const lines = source . split ( "\n" ) ;
@@ -83,7 +85,7 @@ export function transformChunk(source: string, chunk: Chunk): string {
83
85
// we only want to look at the script tag...
84
86
. slice ( 0 , scriptEndIdx )
85
87
// spaced on the edges to prevent false positives (e.g. `CreditCard` could be falsely triggered by `Card`)
86
- . filter ( ( line ) => chunk . dependencies . some ( ( dep ) => line . includes ( ` ${ dep } ` ) ) ) ;
88
+ . filter ( ( line ) => chunk . dependencies . some ( ( dep ) => line . includes ( ` ${ dep } ` ) || line . includes ( ` ${ dep } ,` ) ) ) ;
87
89
88
90
let template = `<script lang="ts">\n` ;
89
91
template += imports . join ( "\n" ) ;
0 commit comments