(files: ReadonlyArray<JSDocModelFile>)
| 2479 | } |
| 2480 | |
| 2481 | function createExampleImportPolicy(files: ReadonlyArray<JSDocModelFile>): ExampleImportPolicy { |
| 2482 | const allowedNamed = new Map<string, Set<string>>() |
| 2483 | const namespaceModuleBySource = new Map<string, { readonly barrelModule: string; readonly name: string }>() |
| 2484 | const flatModuleBySource = new Map<string, { readonly barrelModule: string; readonly names: ReadonlySet<string> }>() |
| 2485 | const namedModuleBySource = new Map<string, ReadonlySet<string>>() |
| 2486 | const addAllowed = (source: string, name: string) => { |
| 2487 | const names = allowedNamed.get(source) ?? new Set<string>() |
| 2488 | names.add(name) |
| 2489 | allowedNamed.set(source, names) |
| 2490 | } |
| 2491 | for (const file of files) { |
| 2492 | if (file.imports === undefined) continue |
| 2493 | const rootValues = file.declarations.filter((declaration) => |
| 2494 | declaration.bucket === "value" && isIdentifierName(declaration.name) |
| 2495 | ).map((declaration) => declaration.name) |
| 2496 | if (file.imports.barrel?.type === "namespace") { |
| 2497 | addAllowed(file.imports.barrel.module, file.imports.barrel.name) |
| 2498 | for (const name of file.imports.flatNames) addAllowed(file.imports.barrel.module, name) |
| 2499 | namespaceModuleBySource.set(file.imports.module, { |
| 2500 | barrelModule: file.imports.barrel.module, |
| 2501 | name: file.imports.barrel.name |
| 2502 | }) |
| 2503 | continue |
| 2504 | } |
| 2505 | if (file.imports.barrel?.type === "flat") { |
| 2506 | for (const name of new Set([...rootValues, ...file.imports.flatNames])) { |
| 2507 | addAllowed(file.imports.barrel.module, name) |
| 2508 | } |
| 2509 | flatModuleBySource.set(file.imports.module, { |
| 2510 | barrelModule: file.imports.barrel.module, |
| 2511 | names: new Set([...rootValues, ...file.imports.flatNames]) |
| 2512 | }) |
| 2513 | continue |
| 2514 | } |
| 2515 | for (const name of rootValues) addAllowed(file.imports.module, name) |
| 2516 | namedModuleBySource.set(file.imports.module, new Set(rootValues)) |
| 2517 | } |
| 2518 | return { allowedNamedBySource: allowedNamed, namespaceModuleBySource, flatModuleBySource, namedModuleBySource } |
| 2519 | } |
| 2520 | |
| 2521 | function importSpecifierName(specifier: ts.ImportSpecifier): string { |
| 2522 | return specifier.propertyName?.text ?? specifier.name.text |
no test coverage detected