Skip to content

Commit

Permalink
feat: support default export as @main tag fallback
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
elyukai committed Nov 14, 2023
1 parent f48ea67 commit 77ca2b1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/parser/resolvetypeargs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,15 @@ const resolveTypeArgumentsForNode = <T extends ContentNode>(
}
}
case NodeKind.ExportAssignment:
return undefined
return {
...node,
expression: resolveTypeArgumentsForNode(
node.expression,
typesInScope,
file,
files
),
}
default:
return assertExhaustive(node)
}
Expand Down Expand Up @@ -461,7 +469,7 @@ const rootTypesInScope = (
acc[child.name] = { node: child, kind: ScopeTypeKind.Default }
break
case NodeKind.ExportAssignment:
acc[child.name] = { node: child, kind: ScopeTypeKind.Default }
// ignore name
break
case NodeKind.Group:
acc[child.name] = { node: child, kind: ScopeTypeKind.Default }
Expand Down
40 changes: 36 additions & 4 deletions src/renderers/jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
ChildNode,
Doc,
DocTagTypes,
ExportAssignmentNode,
NodeKind,
RootNode,
StatementNode,
TokenKind,
isReferenceNode,
} from "../ast.js"
import { AstTransformer, Renderer } from "../main.js"
import { assertExhaustive } from "../utils/assertExhaustive.js"
Expand Down Expand Up @@ -421,15 +423,15 @@ const statementToDefinition = (
file: RootNode,
options: Required<JsonSchemaRendererOptions>,
shallowOptions: { isReadOnly?: boolean } = {}
): Definition => {
): Definition | undefined => {
const { isReadOnly } = shallowOptions

switch (node.kind) {
case NodeKind.TypeDefinition: {
return nodeToDefinition(node.definition, file, options)
}
case NodeKind.ExportAssignment: {
return nodeToDefinition(node.expression, file, options)
return undefined
}
case NodeKind.Enumeration: {
return {
Expand All @@ -455,16 +457,46 @@ const statementToDefinition = (
const toForwardSlashAbsolutePath = (path: string) =>
"/" + path.split(sep).join("/")

const getMainRef = (
file: RootNode,
spec: JsonSchemaSpec
): string | undefined => {
if (file.jsDoc?.tags.main !== undefined) {
return `#/${defsKey(spec)}/${file.jsDoc.tags.main}`
}

const defaultExport = file.children.find(
(node) => node.name === "default" && node.kind === NodeKind.ExportAssignment
) as ExportAssignmentNode | undefined

if (
defaultExport !== undefined &&
isReferenceNode(defaultExport.expression)
) {
const externalFilePath = getRelativeExternalPath(
defaultExport.expression,
file,
".schema.json"
)

const qualifiedName = getFullyQualifiedNameAsPath(
defaultExport.expression,
file
)

return `${externalFilePath}#/${defsKey(spec)}/${qualifiedName}`
}
}

const astToJsonSchema =
(options: Required<JsonSchemaRendererOptions>): AstTransformer =>
(file, { relativePath }): string => {
const { spec } = options
const mainType = file.jsDoc?.tags.main

const jsonSchema = {
$schema: schemaUri(spec),
$id: toForwardSlashAbsolutePath(relativePath),
$ref: mainType ? `#/${defsKey(spec)}/${mainType}` : mainType,
$ref: getMainRef(file, spec),
[defsKey(spec)]: Object.fromEntries(
file.children.map((node) => [
node.name,
Expand Down

0 comments on commit 77ca2b1

Please sign in to comment.