ReviewOS

also looking at this

stacks/dtsx

chore(deps): update all non-major dependencies

#2997
Closed renovate[bot] wants to merge renovate/all-minor-patch into main
29 files +1047 -444
packages/dtsx/src/processor/index.tsmodified+100-99
Changes to packages/dtsx/src/processor/index.ts
@@ -3,7 +3,7 @@
33 */
44
55import type { Declaration, DeclarationKind, ProcessingContext } from '../types'
6import { extractTripleSlashDirectives } from '../extractor/helpers'
6import { extractTripleSlashDirectives } from '../extractor/directives'
77import { getCachedRegex } from './cache'
88import { formatComments } from './comments'
99import {
@@ -62,10 +62,15 @@ export function processDeclarations(
6262 const output: string[] = []
6363
6464 // Extract and add triple-slash directives at the top of the file
65 const tripleSlashDirectives = extractTripleSlashDirectives(context.sourceCode)
66 if (tripleSlashDirectives.length > 0) {
67 output.push(...tripleSlashDirectives)
68 output.push('') // Blank line after directives
65 // Fast check: only call extractTripleSlashDirectives if source starts with ///
66 const src = context.sourceCode
67 const firstNonWS = src.length > 0 ? src.trimStart() : ''
68 if (firstNonWS.startsWith('///')) {
69 const tripleSlashDirectives = extractTripleSlashDirectives(src)
70 if (tripleSlashDirectives.length > 0) {
71 output.push(...tripleSlashDirectives)
72 output.push('') // Blank line after directives
73 }
6974 }
7075
7176 // Group declarations by type for better organization (single pass)
@@ -99,8 +104,10 @@ export function processDeclarations(
99104
100105 // Parse all exports to understand what's being exported
101106 const exportedItems = new Set<string>()
102 const exportStatements: string[] = []
107 const typeExportStatements: string[] = []
108 const valueExportStatements: string[] = []
103109 const defaultExport: string[] = []
110 const seenExports = new Set<string>()
104111
105112 for (const decl of exports) {
106113 // Prepend comments if present
@@ -129,8 +136,15 @@ export function processDeclarations(
129136 }
130137
131138 const fullExportText = comments + exportText
132 if (!exportStatements.includes(fullExportText)) {
133 exportStatements.push(fullExportText)
139 if (!seenExports.has(fullExportText)) {
140 seenExports.add(fullExportText)
141 // Categorize into type vs value exports in one pass
142 if (fullExportText.includes('export type')) {
143 typeExportStatements.push(fullExportText)
144 }
145 else {
146 valueExportStatements.push(fullExportText)
147 }
134148 }
135149 }
136150 }
@@ -145,55 +159,47 @@ export function processDeclarations(
145159 }
146160 }
147161
148 // Get all unique imported item names for regex matching
149 const allImportedItemNames = Array.from(allImportedItemsMap.keys())
150
151162 // Filter imports to only include those that are used in exports or declarations
152163 const usedImportItems = new Set<string>()
153164
154 // Collect all declaration texts that need to be checked for imports (single pass)
155 const declarationTexts: Array<{ text: string, additionalTexts: string[] }> = []
165 // Build combined text for import usage detection (single allocation)
166 const allTexts: string[] = []
156167
157168 // Add exported functions
158169 for (const func of functions) {
159170 if (func.isExported) {
160 declarationTexts.push({ text: func.text, additionalTexts: [] })
171 allTexts.push(func.text)
161172 }
162173 }
163174
164175 // Add exported variables
165176 for (const variable of variables) {
166177 if (variable.isExported) {
167 const additionalTexts: string[] = []
178 allTexts.push(variable.text)
168179 if (variable.typeAnnotation) {
169 additionalTexts.push(variable.typeAnnotation)
180 allTexts.push(variable.typeAnnotation)
170181 }
171 declarationTexts.push({ text: variable.text, additionalTexts })
172182 }
173183 }
174184
175185 // Build reference check sets for interfaces (optimized: single pass over text sources)
176186 const interfaceReferences = new Set<string>()
177187 if (interfaces.length > 0) {
178 // Collect all texts to search in one array
179 const textsToSearch: string[] = []
188 // Build combined text from functions/classes/types for interface reference check
189 let refCheckText = ''
180190 for (const func of functions) {
181191 if (func.isExported)
182 textsToSearch.push(func.text)
192 refCheckText += func.text + '\n'
183193 }
184194 for (const cls of classes) {
185 textsToSearch.push(cls.text)
195 refCheckText += cls.text + '\n'
186196 }
187197 for (const type of types) {
188 textsToSearch.push(type.text)
198 refCheckText += type.text + '\n'
189199 }
190200
191 // Join all texts for a single search per interface (faster than N*M individual searches)
192 const combinedText = textsToSearch.join('\n')
193
194 // Single pass: check each interface name against combined text
195201 for (const iface of interfaces) {
196 if (combinedText.includes(iface.name)) {
202 if (refCheckText.includes(iface.name)) {
197203 interfaceReferences.add(iface.name)
198204 }
199205 }
@@ -202,43 +208,37 @@ export function processDeclarations(
202208 // Add interfaces (exported or referenced)
203209 for (const iface of interfaces) {
204210 if (iface.isExported || interfaceReferences.has(iface.name)) {
205 declarationTexts.push({ text: iface.text, additionalTexts: [] })
211 allTexts.push(iface.text)
206212 }
207213 }
208214
209 // Add all types, classes, enums, modules (they may be included in DTS)
215 // Add all types, classes, enums, modules
210216 for (const type of types) {
211 declarationTexts.push({ text: type.text, additionalTexts: [] })
217 allTexts.push(type.text)
212218 }
213219 for (const cls of classes) {
214 declarationTexts.push({ text: cls.text, additionalTexts: [] })
220 allTexts.push(cls.text)
215221 }
216222 for (const enumDecl of enums) {
217 declarationTexts.push({ text: enumDecl.text, additionalTexts: [] })
223 allTexts.push(enumDecl.text)
218224 }
219225 for (const mod of modules) {
220 declarationTexts.push({ text: mod.text, additionalTexts: [] })
226 allTexts.push(mod.text)
221227 }
222228
223229 // Add export statements
224230 for (const exp of exports) {
225 declarationTexts.push({ text: exp.text, additionalTexts: [] })
226 }
227
228 // Optimized: combine ALL declaration texts into one and extract identifiers in a single pass
229 const allTexts: string[] = []
230 for (const { text, additionalTexts } of declarationTexts) {
231 allTexts.push(text)
232 if (additionalTexts.length > 0) {
233 allTexts.push(...additionalTexts)
234 }
231 allTexts.push(exp.text)
235232 }
236 const combinedDeclarationText = allTexts.join('\n')
237233
238234 // Two-phase import detection: fast includes() rejection then regex word-boundary check
239 for (const item of allImportedItemNames) {
240 if (combinedDeclarationText.includes(item) && getCachedRegex(item).test(combinedDeclarationText)) {
241 usedImportItems.add(item)
235 // Check each text individually to avoid large join() allocation
236 for (const item of allImportedItemsMap.keys()) {
237 for (let t = 0; t < allTexts.length; t++) {
238 if (allTexts[t].includes(item) && getCachedRegex(item).test(allTexts[t])) {
239 usedImportItems.add(item)
240 break
241 }
242242 }
243243 }
244244
@@ -326,62 +326,63 @@ export function processDeclarations(
326326 output.push('')
327327
328328 // Process type exports first
329 const typeExports = exportStatements.filter(exp => exp.includes('export type'))
330 output.push(...typeExports)
331
332 // Process other declarations (functions, interfaces, etc.)
333 const otherDecls = [...functions, ...variables, ...interfaces, ...types, ...classes, ...enums, ...modules]
334
335 for (const decl of otherDecls) {
336 let processed = ''
337 const kind: DeclarationKind = decl.kind
338 switch (kind) {
339 case 'function':
340 processed = processFunctionDeclaration(decl, keepComments)
341 break
342 case 'variable':
343 processed = processVariableDeclaration(decl, keepComments)
344 break
345 case 'interface':
346 processed = processInterfaceDeclaration(decl, keepComments)
347 break
348 case 'type':
349 processed = processTypeDeclaration(decl, keepComments)
350 break
351 case 'class':
352 processed = processClassDeclaration(decl, keepComments)
353 break
354 case 'enum':
355 processed = processEnumDeclaration(decl, keepComments)
356 break
357 case 'module':
358 processed = processModuleDeclaration(decl, keepComments)
359 break
360 // import and export are handled separately above
361 case 'import':
362 case 'export':
363 break
364 case 'namespace':
365 processed = processModuleDeclaration(decl, keepComments)
366 break
367 case 'unknown':
368 // Skip unknown declarations
369 break
370 default:
371 assertNever(kind, `Unhandled declaration kind in processor: ${kind}`)
372 }
373
374 if (processed) {
375 output.push(processed)
329 for (let i = 0; i < typeExportStatements.length; i++) output.push(typeExportStatements[i])
330
331 // Process other declarations — iterate each group directly (no spread allocation)
332 const declGroups = [functions, variables, interfaces, types, classes, enums, modules]
333 for (let g = 0; g < declGroups.length; g++) {
334 const group = declGroups[g]
335 for (let d = 0; d < group.length; d++) {
336 const decl = group[d]
337 let processed = ''
338 const kind: DeclarationKind = decl.kind
339 switch (kind) {
340 case 'function':
341 processed = processFunctionDeclaration(decl, keepComments)
342 break
343 case 'variable':
344 processed = processVariableDeclaration(decl, keepComments)
345 break
346 case 'interface':
347 processed = processInterfaceDeclaration(decl, keepComments)
348 break
349 case 'type':
350 processed = processTypeDeclaration(decl, keepComments)
351 break
352 case 'class':
353 processed = processClassDeclaration(decl, keepComments)
354 break
355 case 'enum':
356 processed = processEnumDeclaration(decl, keepComments)
357 break
358 case 'module':
359 case 'namespace':
360 processed = processModuleDeclaration(decl, keepComments)
361 break
362 case 'import':
363 case 'export':
364 case 'unknown':
365 break
366 default:
367 assertNever(kind, `Unhandled declaration kind in processor: ${kind}`)
368 }
369 if (processed) output.push(processed)
376370 }
377371 }
378372
379373 // Process value exports
380 const valueExports = exportStatements.filter(exp => !exp.includes('export type'))
381 output.push(...valueExports)
374 for (let i = 0; i < valueExportStatements.length; i++) output.push(valueExportStatements[i])
382375
383376 // Process default export last
384 output.push(...defaultExport)
385
386 return output.filter(line => line !== '').join('\n')
377 for (let i = 0; i < defaultExport.length; i++) output.push(defaultExport[i])
378
379 // Build final output — skip empty strings inline instead of filter()
380 let result = ''
381 for (let i = 0; i < output.length; i++) {
382 if (output[i] !== '') {
383 if (result) result += '\n'
384 result += output[i]
385 }
386 }
387 return result
387388}