Skip to content

Commit

Permalink
wip: file-matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
cgatian committed Feb 11, 2025
1 parent 046521b commit 4a608aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 13 additions & 6 deletions packages/core/__tests__/file-matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('file matcher', () => {
expect(file.isValidPattern('stack')).toMatchInlineSnapshot('true')

expect(file.isValidPattern('__vstack')).toMatchInlineSnapshot('true')
expect(file.isValidPattern('vstack')).toMatchInlineSnapshot('true')
expect(file.isValidPattern('vstack')).toMatchInlineSnapshot('false') // This shouldnt match vstack is aliased as __vstack
})

test('is valid recipe', () => {
Expand All @@ -126,28 +126,35 @@ describe('file matcher', () => {
])

expect(file.isValidRecipe('randxxx')).toMatchInlineSnapshot('false')
expect(file.isValidRecipe('button')).toMatchInlineSnapshot('true')
expect(file.isValidRecipe('button')).toMatchInlineSnapshot('false') // this shouldnt match button is aliased as buttonStyle

expect(file.isValidRecipe('buttonStyle')).toMatchInlineSnapshot('true')
expect(file.isValidRecipe('button')).toMatchInlineSnapshot('true')
expect(file.isValidRecipe('button')).toMatchInlineSnapshot('false') // Redundant test?
expect(file.isValidRecipe('xxxbutton')).toMatchInlineSnapshot('false')
})

test('is raw fn', () => {
const ctx = createContext()

const file = ctx.imports.file([
{ mod: 'other-system/css', name: 'css', alias: 'css' },
{ mod: 'styled-system/css', name: 'css', alias: 'xcss' },
{ mod: 'styled-system/css', name: 'cva', alias: 'cva' },
{ mod: 'styled-system/patterns', name: 'stack', alias: 'stack' },
{ mod: 'styled-system/patterns', name: 'grid', alias: 'aliasedGrid' },
])

expect(file.isRawFn('css')).toMatchInlineSnapshot('true')
expect(file.isRawFn('xcss')).toMatchInlineSnapshot('false')
expect(file.isRawFn('css')).toMatchInlineSnapshot('false')
expect(file.isRawFn('xcss')).toMatchInlineSnapshot('true')

expect(file.isRawFn('css.raw')).toMatchInlineSnapshot('false')
expect(file.isRawFn('xcss.raw')).toMatchInlineSnapshot('true')

expect(file.isRawFn('css.raw')).toMatchInlineSnapshot('true')
expect(file.isRawFn('stack.raw')).toMatchInlineSnapshot('true')

expect(file.isRawFn('card.raw')).toMatchInlineSnapshot('false')
expect(file.isRawFn('aliasedGrid.raw')).toMatchInlineSnapshot('true')

expect(file.isRawFn('cva.raw')).toMatchInlineSnapshot('false')
})

Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/file-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ export class FileMatcher {
if (mod.kind === 'namespace') {
return keys.includes(id.replace(`${mod.alias}.`, ''))
}

return mod.alias === id || mod.name === id
// prefer alias
if (mod.alias) {
return mod.alias === id
}
return mod.name === id
})
})
}
Expand Down Expand Up @@ -177,9 +180,16 @@ export class FileMatcher {
return this._recipesMatcher(id)
}

private _cssMatcher: ReturnType<typeof this.createMatch> | undefined

isValidCss = (id: string) => {
this._cssMatcher ||= this.createMatch(this.importMap.css, ['css'])
return this._cssMatcher(id)
}

isRawFn = (fnName: string) => {
const name = fnName.split('.raw')[0] ?? ''
return name === 'css' || this.isValidPattern(name) || this.isValidRecipe(name)
return this.isValidCss(name) || this.isValidPattern(name) || this.isValidRecipe(name)
}

isNamespaced = (fnName: string) => {
Expand Down

0 comments on commit 4a608aa

Please sign in to comment.