11import { Tokenizer , TokenKind } from 'liquidjs'
2+ import type { TopLevelToken , TagToken } from 'liquidjs'
23
34import { deprecated } from '@/versions/lib/enterprise-server-releases'
45
5- // Using `any` for the cache because TopLevelToken is a complex union type from liquidjs
6- // that includes TagToken, OutputToken, and HTMLToken with different properties.
7- // The cache is private to this module and we control all access to it.
8- const liquidTokenCache = new Map < string , any > ( )
6+ // Cache for liquid tokens to improve performance
7+ const liquidTokenCache = new Map < string , TopLevelToken [ ] > ( )
98
10- // Returns `any[]` instead of `TopLevelToken[]` because TopLevelToken is a union type
11- // (TagToken | OutputToken | HTMLToken) and consumers of this function access properties
12- // like `name` and `args` that only exist on TagToken. Using `any` here avoids complex
13- // type narrowing throughout the codebase.
9+ // Returns TopLevelToken array from liquidjs which is a union of TagToken, OutputToken, and HTMLToken
1410export function getLiquidTokens (
1511 content : string ,
1612 { noCache = false } : { noCache ?: boolean } = { } ,
17- ) : any [ ] {
13+ ) : TopLevelToken [ ] {
1814 if ( ! content ) return [ ]
1915
2016 if ( noCache ) {
@@ -23,13 +19,13 @@ export function getLiquidTokens(
2319 }
2420
2521 if ( liquidTokenCache . has ( content ) ) {
26- return liquidTokenCache . get ( content )
22+ return liquidTokenCache . get ( content ) !
2723 }
2824
2925 const tokenizer = new Tokenizer ( content )
3026 const tokens = tokenizer . readTopLevelTokens ( )
3127 liquidTokenCache . set ( content , tokens )
32- return liquidTokenCache . get ( content )
28+ return liquidTokenCache . get ( content ) !
3329}
3430
3531export const OUTPUT_OPEN = '{%'
@@ -40,10 +36,9 @@ export const TAG_CLOSE = '}}'
4036export const conditionalTags = [ 'if' , 'elseif' , 'unless' , 'case' , 'ifversion' ]
4137const CONDITIONAL_TAG_NAMES = [ 'if' , 'ifversion' , 'elsif' , 'else' , 'endif' ]
4238
43- // Token is `any` because it's used with different token types from liquidjs
44- // that all have `begin` and `end` properties but are part of complex union types.
39+ // Token parameter uses TopLevelToken which has begin and end properties
4540export function getPositionData (
46- token : any ,
41+ token : TopLevelToken ,
4742 lines : string [ ] ,
4843) : { lineNumber : number ; column : number ; length : number } {
4944 // Liquid indexes are 0-based, but we want to
@@ -77,9 +72,9 @@ export function getPositionData(
7772 * by Markdownlint:
7873 * [ { lineNumber: 1, column: 1, deleteCount: 3, }]
7974 */
80- // Token is `any` because it's used with different token types from liquidjs.
75+ // Token parameter uses TopLevelToken from liquidjs
8176export function getContentDeleteData (
82- token : any ,
77+ token : TopLevelToken ,
8378 tokenEnd : number ,
8479 lines : string [ ] ,
8580) : Array < { lineNumber : number ; column : number ; deleteCount : number } > {
@@ -123,15 +118,14 @@ export function getContentDeleteData(
123118// related elsif, else, and endif tags).
124119// Docs doesn't use the standard `if` tag for versioning, instead the
125120// `ifversion` tag is used.
126- // Returns `any[]` because the tokens need to be accessed as TagToken with `name` and `args` properties,
127- // but TopLevelToken union type would require complex type narrowing.
128- export function getLiquidIfVersionTokens ( content : string ) : any [ ] {
121+ // Returns TagToken array since we filter to only Tag tokens
122+ export function getLiquidIfVersionTokens ( content : string ) : TagToken [ ] {
129123 const tokens = getLiquidTokens ( content )
130- . filter ( ( token ) => token . kind === TokenKind . Tag )
124+ . filter ( ( token ) : token is TagToken => token . kind === TokenKind . Tag )
131125 . filter ( ( token ) => CONDITIONAL_TAG_NAMES . includes ( token . name ) )
132126
133127 let inIfStatement = false
134- const ifVersionTokens : any [ ] = [ ]
128+ const ifVersionTokens : TagToken [ ] = [ ]
135129 for ( const token of tokens ) {
136130 if ( token . name === 'if' ) {
137131 inIfStatement = true
0 commit comments