Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: file-matcher #3137

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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