Skip to content

Commit

Permalink
chore: update snapshots & handle undefined filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer committed Feb 29, 2024
1 parent 5e3d5cb commit 28398cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('JSX style props', () => {

expect(await run(code)).toMatchInlineSnapshot(`
"import { NotPanda } from "not-panda";
<NotPanda m="1" fontSize="md" px="2" py={2}>
<NotPanda m="1" py={2} px="2" fontSize="md">
Hello
</NotPanda>;
"
Expand Down
28 changes: 27 additions & 1 deletion packages/prettier-plugin-panda/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import typescriptParser from 'prettier/plugins/typescript'
import { BuilderResolver } from './builder-resolver'
import type { PluginOptions } from './options'
import { PrettyPanda } from './pretty-panda'
import { Builder } from '@pandacss/node'

export { options } from './options'
export type { PluginOptions }

// Programatic usage
const builder = new Builder()
let configPath: string | undefined

// CLI/VSCode usage
const resolver = new BuilderResolver()

export const parsers: Plugin['parsers'] = {
Expand All @@ -16,14 +22,34 @@ export const parsers: Plugin['parsers'] = {
const ast = typescriptParser.parsers.typescript.parse(text, options)
const filePath = options.filepath

// Programatic usage
if (!options.filepath) {
if (options.pandaConfigPath) {
configPath = options.pandaConfigPath
}

const setupOptions = { configPath: options.pandaConfigPath, cwd: options.pandaCwd }
const ctx = (await builder.setup(setupOptions)) || builder.context
if (!ctx) return ast

// cache the config path
if (!configPath) configPath = ctx.conf.path

const pretty = new PrettyPanda(ast, ctx, options as any)
return pretty.format(text)
}

// CLI/VSCode usage
try {
const ctx = await resolver.getOrCreate(filePath)
if (!ctx) return ast
if (options.pandaOnlyIncluded && !resolver.isIncluded(filePath)) return ast

const pretty = new PrettyPanda(ast, ctx, options as any)
return pretty.format(text)
} catch {
} catch (err) {
console.log('prettier-plugin-panda error')
console.error(err)
return ast
}
},
Expand Down

0 comments on commit 28398cf

Please sign in to comment.