(files: ReadonlyArray<JSDocModelFile>)
| 2532 | } |
| 2533 | |
| 2534 | function createExampleImportPolicy(files: ReadonlyArray<JSDocModelFile>): ExampleImportPolicy { |
| 2535 | const allowedNamed = new Map<string, Set<string>>() |
| 2536 | const namespaceModuleBySource = new Map<string, { readonly barrelModule: string; readonly name: string }>() |
| 2537 | const flatModuleBySource = new Map<string, { readonly barrelModule: string; readonly names: ReadonlySet<string> }>() |
| 2538 | const namedModuleBySource = new Map<string, ReadonlySet<string>>() |
| 2539 | const addAllowed = (source: string, name: string) => { |
| 2540 | const names = allowedNamed.get(source) ?? new Set<string>() |
| 2541 | names.add(name) |
| 2542 | allowedNamed.set(source, names) |
| 2543 | } |
| 2544 | for (const file of files) { |
| 2545 | if (file.imports === undefined) continue |
| 2546 | const rootValues = file.declarations.filter((declaration) => |
| 2547 | declaration.bucket === "value" && isIdentifierName(declaration.name) |
| 2548 | ).map((declaration) => declaration.name) |
| 2549 | if (file.imports.barrel?.type === "namespace") { |
| 2550 | addAllowed(file.imports.barrel.module, file.imports.barrel.name) |
| 2551 | for (const name of file.imports.flatNames) addAllowed(file.imports.barrel.module, name) |
| 2552 | namespaceModuleBySource.set(file.imports.module, { |
| 2553 | barrelModule: file.imports.barrel.module, |
| 2554 | name: file.imports.barrel.name |
| 2555 | }) |
| 2556 | continue |
| 2557 | } |
| 2558 | if (file.imports.barrel?.type === "flat") { |
| 2559 | for (const name of new Set([...rootValues, ...file.imports.flatNames])) { |
| 2560 | addAllowed(file.imports.barrel.module, name) |
| 2561 | } |
| 2562 | flatModuleBySource.set(file.imports.module, { |
| 2563 | barrelModule: file.imports.barrel.module, |
| 2564 | names: new Set([...rootValues, ...file.imports.flatNames]) |
| 2565 | }) |
| 2566 | continue |
| 2567 | } |
| 2568 | const names = new Set([...rootValues, ...file.imports.flatNames]) |
| 2569 | for (const name of names) addAllowed(file.imports.module, name) |
| 2570 | namedModuleBySource.set(file.imports.module, names) |
| 2571 | } |
| 2572 | return { allowedNamedBySource: allowedNamed, namespaceModuleBySource, flatModuleBySource, namedModuleBySource } |
| 2573 | } |
| 2574 | |
| 2575 | function importSpecifierName(specifier: ts.ImportSpecifier): string { |
| 2576 | return specifier.propertyName?.text ?? specifier.name.text |
no test coverage detected