(context: ParserOptions)
| 23 | } |
| 24 | |
| 25 | export function createParser(context: ParserOptions) { |
| 26 | const { jsx, imports, recipes, config } = context |
| 27 | const syntax = config.syntax |
| 28 | |
| 29 | return function parse( |
| 30 | sourceFile: SourceFile | undefined, |
| 31 | encoder?: Generator['encoder'], |
| 32 | options?: ParserResultConfigureOptions & Partial<JsxFactoryResultTransform>, |
| 33 | ) { |
| 34 | if (!sourceFile) return |
| 35 | |
| 36 | const importDeclarations: ImportResult[] = getImportDeclarations(context, sourceFile) |
| 37 | |
| 38 | const file = imports.file(importDeclarations) |
| 39 | |
| 40 | const filePath = sourceFile.getFilePath() |
| 41 | |
| 42 | logger.debug( |
| 43 | 'ast:import', |
| 44 | !file.isEmpty() ? `Found import { ${file.toString()} } in ${filePath}` : `No import found in ${filePath}`, |
| 45 | ) |
| 46 | |
| 47 | const parserResult = new ParserResult(context, encoder) |
| 48 | |
| 49 | if (file.isEmpty() && !jsx.isEnabled) { |
| 50 | return parserResult |
| 51 | } |
| 52 | |
| 53 | const extractResultByName = extract({ |
| 54 | ast: sourceFile, |
| 55 | tokens: context.tokens |
| 56 | ? { |
| 57 | view: context.tokens.view, |
| 58 | isTokenFn: (fnName) => file.isTokenAlias(fnName), |
| 59 | } |
| 60 | : undefined, |
| 61 | components: jsx.isEnabled |
| 62 | ? { |
| 63 | matchTag: (prop) => { |
| 64 | if (options?.matchTag) { |
| 65 | // If the user has a custom matchTag function, |
| 66 | // we're not going to match every uppercased tag |
| 67 | const isPandaComponent = file.isPandaComponent(prop.tagName) |
| 68 | const matchTagMode = options.matchTagMode ?? 'extend' |
| 69 | const isCustomMatch = options.matchTag(prop.tagName, isPandaComponent) |
| 70 | |
| 71 | if (matchTagMode === 'override') { |
| 72 | return isCustomMatch |
| 73 | } |
| 74 | |
| 75 | return isPandaComponent || isCustomMatch |
| 76 | } |
| 77 | |
| 78 | return !!file.matchTag(prop.tagName) |
| 79 | }, |
| 80 | matchProp: (prop) => { |
| 81 | const isPandaProp = file.matchTagProp(prop.tagName, prop.propName) |
| 82 |
no test coverage detected