MCPcopy
hub / github.com/nuxt/nuxt / processFactory

Function processFactory

packages/nuxt/src/compiler/plugins/keyed-function-factories.ts:132–223  ·  view source on GitHub ↗
(
    walkContext: ThisParameterType<NonNullable<Parameters<typeof walk>[1]['enter']>>, // TODO: export type from `oxc-walker`
    node: ExportNamedDeclaration | ExportDefaultDeclaration,
    handler: (ctx: { parseFactoryResult: ParsedKeyedFunctionFactory, factory: KeyedFunctionFactory }) => void,
  )

Source from the content-addressed store, hash-verified

130 }
131
132 function processFactory (
133 walkContext: ThisParameterType<NonNullable<Parameters<typeof walk>[1]['enter']>>, // TODO: export type from `oxc-walker`
134 node: ExportNamedDeclaration | ExportDefaultDeclaration,
135 handler: (ctx: { parseFactoryResult: ParsedKeyedFunctionFactory, factory: KeyedFunctionFactory }) => void,
136 ) {
137 const parsedFactoryCalls = parseKeyedFunctionFactory(node, LOCAL_FACTORY_NAMES_RE, scopeTracker)
138 if (!parsedFactoryCalls.length) { return }
139
140 for (const parsedFactoryCall of parsedFactoryCalls) {
141 const factoryMeta = getFactoryByLocalName(parsedFactoryCall.factoryName)
142 if (!factoryMeta) {
143 logger.error(`[nuxt:compiler] No factory function found for \`${parsedFactoryCall.functionName}\` in file \`${filePath}\`. This is a Nuxt bug.`)
144 continue
145 }
146
147 const scopeTrackerNode = scopeTracker.getDeclaration(!parsedFactoryCall.namespace ? parsedFactoryCall.factoryName : parsedFactoryCall.namespace)
148
149 function isFactoryImport (node: ScopeTrackerNode | null): node is ScopeTrackerNode & { type: 'Import' } {
150 return node?.type === 'Import' && node.importNode.importKind !== 'type'
151 }
152
153 // import source WITHOUT EXTENSION and with resolved alias
154 let importSourceResolved: string | undefined
155
156 if (isFactoryImport(scopeTrackerNode)) {
157 importSourceResolved = stripExtension(_resolvePath(scopeTrackerNode.importNode.source.value))
158 } else if (!scopeTrackerNode) {
159 const autoImportedSource = autoImportsToSources?.get(factoryMeta.name)
160 if (autoImportedSource) {
161 importSourceResolved = stripExtension(_resolvePath(autoImportedSource))
162 }
163 }
164
165 if (!importSourceResolved) {
166 continue
167 }
168
169 // resolved source WITHOUT an extension
170 const resolvedFactorySource = factoryMeta.source
171
172 // the factory is called directly
173 // `createUseFetch()`
174 if (
175 !parsedFactoryCall.namespace && (
176 // and the factory is imported directly
177 (
178 isFactoryImport(scopeTrackerNode) && (
179 (
180 // import { createUseFetch } from '...'
181 scopeTrackerNode.node.type === 'ImportSpecifier'
182 && scopeTrackerNode.node.importKind !== 'type'
183 )
184 // import createUseFetch from '...'
185 || (
186 scopeTrackerNode.node.type === 'ImportDefaultSpecifier'
187 && factoryMeta.name === 'default'
188 )
189 )

Callers 1

enterFunction · 0.85

Calls 7

stripExtensionFunction · 0.90
getFactoryByLocalNameFunction · 0.85
isFactoryImportFunction · 0.85
getMethod · 0.80
_resolvePathFunction · 0.70
handlerFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…