also looking at this
chore(deps): update all non-major dependencies
#2997
29 files
+1047
-444
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | */ |
| 4 | 4 | |
| 5 | 5 | import type { Declaration, DeclarationKind, ProcessingContext } from '../types' |
| 6 | import { extractTripleSlashDirectives } from '../extractor/helpers' | |
| 6 | import { extractTripleSlashDirectives } from '../extractor/directives' | |
| 7 | 7 | import { getCachedRegex } from './cache' |
| 8 | 8 | import { formatComments } from './comments' |
| 9 | 9 | import { |
| @@ -62,10 +62,15 @@ export function processDeclarations( | ||
| 62 | 62 | const output: string[] = [] |
| 63 | 63 | |
| 64 | 64 | // 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 | } | |
| 69 | 74 | } |
| 70 | 75 | |
| 71 | 76 | // Group declarations by type for better organization (single pass) |
| @@ -99,8 +104,10 @@ export function processDeclarations( | ||
| 99 | 104 | |
| 100 | 105 | // Parse all exports to understand what's being exported |
| 101 | 106 | const exportedItems = new Set<string>() |
| 102 | const exportStatements: string[] = [] | |
| 107 | const typeExportStatements: string[] = [] | |
| 108 | const valueExportStatements: string[] = [] | |
| 103 | 109 | const defaultExport: string[] = [] |
| 110 | const seenExports = new Set<string>() | |
| 104 | 111 | |
| 105 | 112 | for (const decl of exports) { |
| 106 | 113 | // Prepend comments if present |
| @@ -129,8 +136,15 @@ export function processDeclarations( | ||
| 129 | 136 | } |
| 130 | 137 | |
| 131 | 138 | 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 | } | |
| 134 | 148 | } |
| 135 | 149 | } |
| 136 | 150 | } |
| @@ -145,55 +159,47 @@ export function processDeclarations( | ||
| 145 | 159 | } |
| 146 | 160 | } |
| 147 | 161 | |
| 148 | // Get all unique imported item names for regex matching | |
| 149 | const allImportedItemNames = Array.from(allImportedItemsMap.keys()) | |
| 150 | ||
| 151 | 162 | // Filter imports to only include those that are used in exports or declarations |
| 152 | 163 | const usedImportItems = new Set<string>() |
| 153 | 164 | |
| 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[] = [] | |
| 156 | 167 | |
| 157 | 168 | // Add exported functions |
| 158 | 169 | for (const func of functions) { |
| 159 | 170 | if (func.isExported) { |
| 160 | declarationTexts.push({ text: func.text, additionalTexts: [] }) | |
| 171 | allTexts.push(func.text) | |
| 161 | 172 | } |
| 162 | 173 | } |
| 163 | 174 | |
| 164 | 175 | // Add exported variables |
| 165 | 176 | for (const variable of variables) { |
| 166 | 177 | if (variable.isExported) { |
| 167 | const additionalTexts: string[] = [] | |
| 178 | allTexts.push(variable.text) | |
| 168 | 179 | if (variable.typeAnnotation) { |
| 169 | additionalTexts.push(variable.typeAnnotation) | |
| 180 | allTexts.push(variable.typeAnnotation) | |
| 170 | 181 | } |
| 171 | declarationTexts.push({ text: variable.text, additionalTexts }) | |
| 172 | 182 | } |
| 173 | 183 | } |
| 174 | 184 | |
| 175 | 185 | // Build reference check sets for interfaces (optimized: single pass over text sources) |
| 176 | 186 | const interfaceReferences = new Set<string>() |
| 177 | 187 | 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 = '' | |
| 180 | 190 | for (const func of functions) { |
| 181 | 191 | if (func.isExported) |
| 182 | textsToSearch.push(func.text) | |
| 192 | refCheckText += func.text + '\n' | |
| 183 | 193 | } |
| 184 | 194 | for (const cls of classes) { |
| 185 | textsToSearch.push(cls.text) | |
| 195 | refCheckText += cls.text + '\n' | |
| 186 | 196 | } |
| 187 | 197 | for (const type of types) { |
| 188 | textsToSearch.push(type.text) | |
| 198 | refCheckText += type.text + '\n' | |
| 189 | 199 | } |
| 190 | 200 | |
| 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 | |
| 195 | 201 | for (const iface of interfaces) { |
| 196 | if (combinedText.includes(iface.name)) { | |
| 202 | if (refCheckText.includes(iface.name)) { | |
| 197 | 203 | interfaceReferences.add(iface.name) |
| 198 | 204 | } |
| 199 | 205 | } |
| @@ -202,43 +208,37 @@ export function processDeclarations( | ||
| 202 | 208 | // Add interfaces (exported or referenced) |
| 203 | 209 | for (const iface of interfaces) { |
| 204 | 210 | if (iface.isExported || interfaceReferences.has(iface.name)) { |
| 205 | declarationTexts.push({ text: iface.text, additionalTexts: [] }) | |
| 211 | allTexts.push(iface.text) | |
| 206 | 212 | } |
| 207 | 213 | } |
| 208 | 214 | |
| 209 | // Add all types, classes, enums, modules (they may be included in DTS) | |
| 215 | // Add all types, classes, enums, modules | |
| 210 | 216 | for (const type of types) { |
| 211 | declarationTexts.push({ text: type.text, additionalTexts: [] }) | |
| 217 | allTexts.push(type.text) | |
| 212 | 218 | } |
| 213 | 219 | for (const cls of classes) { |
| 214 | declarationTexts.push({ text: cls.text, additionalTexts: [] }) | |
| 220 | allTexts.push(cls.text) | |
| 215 | 221 | } |
| 216 | 222 | for (const enumDecl of enums) { |
| 217 | declarationTexts.push({ text: enumDecl.text, additionalTexts: [] }) | |
| 223 | allTexts.push(enumDecl.text) | |
| 218 | 224 | } |
| 219 | 225 | for (const mod of modules) { |
| 220 | declarationTexts.push({ text: mod.text, additionalTexts: [] }) | |
| 226 | allTexts.push(mod.text) | |
| 221 | 227 | } |
| 222 | 228 | |
| 223 | 229 | // Add export statements |
| 224 | 230 | 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) | |
| 235 | 232 | } |
| 236 | const combinedDeclarationText = allTexts.join('\n') | |
| 237 | 233 | |
| 238 | 234 | // 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 | } | |
| 242 | 242 | } |
| 243 | 243 | } |
| 244 | 244 | |
| @@ -326,62 +326,63 @@ export function processDeclarations( | ||
| 326 | 326 | output.push('') |
| 327 | 327 | |
| 328 | 328 | // 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) | |
| 376 | 370 | } |
| 377 | 371 | } |
| 378 | 372 | |
| 379 | 373 | // 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]) | |
| 382 | 375 | |
| 383 | 376 | // 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 | |
| 387 | 388 | } |