Skip to content

Commit

Permalink
Try catch syncs
Browse files Browse the repository at this point in the history
  • Loading branch information
anubra266 committed Jun 13, 2024
1 parent 5d8b415 commit 4e6f95d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-terms-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pandacss/eslint-plugin": patch
---

Try catch syncs
2 changes: 2 additions & 0 deletions plugin/src/rules/no-invalid-token-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const rule: Rule = createRule({
const sendReport = (node: any, _value?: string) => {
const value = _value ?? node.value?.toString()
const tokens = getInvalidTokens(value, context)
if (!tokens) return

if (tokens.length > 0) {
tokens.forEach((token) => {
Expand Down Expand Up @@ -78,6 +79,7 @@ const rule: Rule = createRule({
const quasis = node.quasi.quasis[0]
const styles = quasis.value.raw
const tokens = getInvalidTokens(styles, context)
if (!tokens) return

tokens.forEach((token, i, arr) => {
// Avoid duplicate reports on the same token
Expand Down
23 changes: 2 additions & 21 deletions plugin/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ const _getImports = (context: RuleContext<any, any>) => {

const getImports = (context: RuleContext<any, any>) => {
const imports = _getImports(context)
try {
return imports.filter((imp) => syncAction('matchImports', getSyncOpts(context), imp))
} catch (error) {
return []
}
return imports.filter((imp) => syncAction('matchImports', getSyncOpts(context), imp))
}

const isValidStyledProp = <T extends Node | string>(node: T, context: RuleContext<any, any>) => {
Expand Down Expand Up @@ -156,7 +152,7 @@ export const isPandaProp = (node: TSESTree.JSXAttribute, context: RuleContext<an
return true
}

export const isStyledNode = (node: TSESTree.Property, context: RuleContext<any, any>, calleeName?: string) => {
export const isStyledProperty = (node: TSESTree.Property, context: RuleContext<any, any>, calleeName?: string) => {
if (!isIdentifier(node.key) && !isLiteral(node.key) && !isTemplateLiteral(node.key)) return

if (isIdentifier(node.key) && !isValidProperty(node.key.name, context, calleeName)) return
Expand All @@ -167,20 +163,9 @@ export const isStyledNode = (node: TSESTree.Property, context: RuleContext<any,
)
return
if (isTemplateLiteral(node.key) && !isValidProperty(node.key.quasis[0].value.raw, context, calleeName)) return

return true
}

export const isStyledProperty = (node: TSESTree.Property, context: RuleContext<any, any>, calleeName?: string) => {
const ancestor = node.parent.parent

const isValidFuncAncestor =
isCallExpression(ancestor) && isIdentifier(ancestor.callee) && isPandaIsh(ancestor.callee.name, context)
if (isValidFuncAncestor) return isStyledNode(node, context, calleeName)

return isStyledNode(ancestor as any, context, calleeName) && isStyledNode(node, context, calleeName)
}

export const isInPandaFunction = (node: TSESTree.Property, context: RuleContext<any, any>) => {
const callAncestor = getAncestor(isCallExpression, node)
if (!callAncestor) return
Expand Down Expand Up @@ -271,10 +256,6 @@ export const getInvalidTokens = (value: string, context: RuleContext<any, any>)
return syncAction('filterInvalidTokens', getSyncOpts(context), tokens)
}

export const getExtendWarnings = (context: RuleContext<any, any>) => {
return syncAction('getExtendWarnings', getSyncOpts(context))
}

export const getTokenImport = (context: RuleContext<any, any>) => {
const imports = _getImports(context)
return imports.find((imp) => imp.name === 'token')
Expand Down
11 changes: 10 additions & 1 deletion plugin/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export type Rule<A extends readonly unknown[] = any, B extends string = any> = R
const isBase = process.env.NODE_ENV !== 'test' || import.meta.url.endsWith('dist/index.js')
export const distDir = fileURLToPath(new URL(isBase ? './' : '../../dist', import.meta.url))

export const syncAction = createSyncFn(join(distDir, 'utils/worker.mjs')) as typeof run
export const _syncAction = createSyncFn(join(distDir, 'utils/worker.mjs'))
// export const _syncAction = createSyncFn(join(distDir, 'utils/worker.mjs')) as typeof run

export const syncAction = ((...args: any) => {
try {
return _syncAction(...args)
} catch (error) {
return
}
}) as typeof run | ((...args: any) => undefined)

export interface ImportResult {
name: string
Expand Down
30 changes: 0 additions & 30 deletions plugin/src/utils/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,6 @@ export async function getContext(opts: Opts) {
}
}

async function getExtendWarnings(): Promise<string[]> {
if (!configPath) return []

const cwd = path.dirname(configPath)
const { config } = await bundleConfig({ cwd, file: configPath! })

if (!config.presets || config.presets.length === 0) return []
if (config.eject) return []

const warnings = new Set<string>()

if (config.theme && !config.theme.extend) {
warnings.add('theme')
}

if (config.conditions && !config.conditions.extend) {
warnings.add('conditions')
}

if (config.patterns && !config.patterns.extend) {
warnings.add('patterns')
}

return Array.from(warnings)
}

async function filterInvalidTokens(ctx: PandaContext, paths: string[]): Promise<string[]> {
return paths.filter((path) => !ctx.utility.tokens.view.get(path))
}
Expand Down Expand Up @@ -137,7 +111,6 @@ type Opts = {
configPath?: string
}

export function runAsync(action: 'getExtendWarnings', opts: Opts): Promise<string[]>
export function runAsync(action: 'filterInvalidTokens', opts: Opts, paths: string[]): Promise<string[]>
export function runAsync(action: 'isColorToken', opts: Opts, value: string): Promise<boolean>
export function runAsync(action: 'isColorAttribute', opts: Opts, attr: string): Promise<boolean>
Expand Down Expand Up @@ -180,12 +153,9 @@ export async function runAsync(action: string, opts: Opts, ...args: any): Promis
case 'filterInvalidTokens':
// @ts-expect-error cast
return filterInvalidTokens(ctx, ...args)
case 'getExtendWarnings':
return getExtendWarnings()
}
}

export function run(action: 'getExtendWarnings', opts: Opts): string[]
export function run(action: 'filterInvalidTokens', opts: Opts, paths: string[]): string[]
export function run(action: 'isColorToken', opts: Opts, value: string): boolean
export function run(action: 'isColorAttribute', opts: Opts, attr: string): boolean
Expand Down

0 comments on commit 4e6f95d

Please sign in to comment.