MCPcopy Create free account
hub / github.com/Effect-TS/effect / validateExampleImports

Function validateExampleImports

packages/tools/jsdocs/src/Jsdocs.ts:2579–2636  ·  view source on GitHub ↗
(
  code: string,
  policy: ExampleImportPolicy
)

Source from the content-addressed store, hash-verified

2577}
2578
2579function validateExampleImports(
2580 code: string,
2581 policy: ExampleImportPolicy
2582): ReadonlyArray<JSDocDiagnostic> {
2583 const diagnostics: Array<JSDocDiagnostic> = []
2584 const sourceFile = ts.createSourceFile("example.ts", code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
2585 for (const statement of sourceFile.statements) {
2586 if (!ts.isImportDeclaration(statement) || !ts.isStringLiteralLike(statement.moduleSpecifier)) continue
2587 if (statement.importClause?.isTypeOnly) continue
2588 const source = statement.moduleSpecifier.text
2589 const namedAllowed = policy.allowedNamedBySource.get(source)
2590 const namespaceModule = policy.namespaceModuleBySource.get(source)
2591 const flatModule = policy.flatModuleBySource.get(source)
2592 const namedModule = policy.namedModuleBySource.get(source)
2593 const clause = statement.importClause
2594 if (clause === undefined) continue
2595 if (namespaceModule !== undefined) {
2596 diagnostics.push(diagnostic(
2597 "example-import-style",
2598 `JSDoc examples should use \`import { ${namespaceModule.name} } from "${namespaceModule.barrelModule}"\` instead of importing from "${source}"`
2599 ))
2600 continue
2601 }
2602 if (flatModule !== undefined) {
2603 diagnostics.push(diagnostic(
2604 "example-import-style",
2605 `JSDoc examples should import ${
2606 Array.from(flatModule.names).map((name) => `\`${name}\``).join(", ")
2607 } from "${flatModule.barrelModule}" instead of "${source}"`
2608 ))
2609 continue
2610 }
2611 if (clause.namedBindings !== undefined && ts.isNamespaceImport(clause.namedBindings)) {
2612 if (namedAllowed !== undefined || namedModule !== undefined) {
2613 diagnostics.push(diagnostic("example-import-style", `JSDoc examples should use named imports from "${source}"`))
2614 }
2615 continue
2616 }
2617 if (clause.name !== undefined && (namedAllowed !== undefined || namedModule !== undefined)) {
2618 diagnostics.push(
2619 diagnostic("example-import-style", `JSDoc examples should not use default imports from "${source}"`)
2620 )
2621 }
2622 if (clause.namedBindings !== undefined && ts.isNamedImports(clause.namedBindings) && namedAllowed !== undefined) {
2623 for (const specifier of clause.namedBindings.elements) {
2624 if (specifier.isTypeOnly) continue
2625 const imported = importSpecifierName(specifier)
2626 if (!namedAllowed.has(imported)) {
2627 diagnostics.push(diagnostic(
2628 "example-import-style",
2629 `JSDoc example import \`${imported}\` from "${source}" is not a canonical public API import`
2630 ))
2631 }
2632 }
2633 }
2634 }
2635 return diagnostics
2636}

Callers 1

checkExamplesFunction · 0.85

Calls 8

diagnosticFunction · 0.85
importSpecifierNameFunction · 0.85
fromMethod · 0.80
getMethod · 0.65
pushMethod · 0.65
hasMethod · 0.65
joinMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected