import ts from 'typescript' ; import type { TypeNode } from 'typescript' ; const { factory , SyntaxKind , ModifierFlags } = ts ; import { Context } from './context.js' ; import { arrayOf } from './util/arrayof.js' ; import { withComments } from './util/comments.js' ; import { typeUnion } from './util/union.js' ; function IdPropertyNode ( ) { return withComments ( 'IRI identifying the canonical address of this object.' , factory . createPropertySignature ( [ ] , factory . createStringLiteral ( '@id' ) , undefined , factory . createTypeReferenceNode ( 'string' , [ ] ) ) ) ; } function WithContextType ( context ) { return withComments ( 'Used at the top-level node to indicate the context for the JSON-LD ' + 'objects used. The context provided in this type is compatible ' + 'with the keys and URLs in the rest of this generated file.' , factory . createTypeAliasDeclaration ( [ ] , factory . createModifiersFromModifierFlags ( ModifierFlags . Export ) , 'WithContext' , [ factory . createTypeParameterDeclaration ( 'T' , factory . createTypeReferenceNode ( 'Thing' , undefined ) ) , ] , factory . createIntersectionTypeNode ( [ factory . createTypeReferenceNode ( 'T' , undefined ) , factory . createTypeLiteralNode ( [ context . contextProperty ( ) ] ) , ] ) ) ) ; } function GraphType ( context ) { return withComments ( '' , factory . createInterfaceDeclaration ( [ ] , factory . createModifiersFromModifierFlags ( ModifierFlags . Export ) , GraphTypeName , undefined , undefined , [ context . contextProperty ( ) , factory . createPropertySignature ( [ ] , factory . createStringLiteral ( '@graph' ) , undefined , factory . createTypeOperatorNode ( SyntaxKind . ReadonlyKeyword , factory . createArrayTypeNode ( factory . createTypeReferenceNode ( 'Thing' , undefined ) ) ) ) , ] ) ) ; } const SchemaValueName = 'SchemaValue' ; export const IdReferenceName = 'IdReference' ; export const GraphTypeName = 'Graph' ; export function SchemaValueReference ( { hasRole } , makeScalarType , propertyName ) { return factory . createTypeReferenceNode ( SchemaValueName , arrayOf ( makeScalarType ( ) , hasRole && factory . createLiteralTypeNode ( factory . createStringLiteral ( propertyName ) ) ) ) ; } export function HelperTypes ( context , { hasRole } ) { return [ WithContextType ( context ) , GraphType ( context ) , factory . createTypeAliasDeclaration ( [ ] , [ ] , SchemaValueName , arrayOf ( factory . createTypeParameterDeclaration ( 'T' ) , hasRole && factory . createTypeParameterDeclaration ( 'TProperty' , factory . createTypeReferenceNode ( 'string' ) ) ) , factory . createUnionTypeNode ( arrayOf < TypeNode > ( factory . createTypeReferenceNode ( 'T' , [ ] ) , hasRole && factory . createTypeReferenceNode ( 'Role' , [ factory . createTypeReferenceNode ( 'T' ) , factory . createTypeReferenceNode ( 'TProperty' ) , ] ) , factory . createTypeOperatorNode ( SyntaxKind . ReadonlyKeyword , factory . createArrayTypeNode ( typeUnion ( factory . createTypeReferenceNode ( 'T' , [ ] ) , hasRole && factory . createTypeReferenceNode ( 'Role' , [ factory . createTypeReferenceNode ( 'T' ) , factory . createTypeReferenceNode ( 'TProperty' ) , ] ) ) ) ) ) ) ) , factory . createTypeAliasDeclaration ( [ ] , [ ] , IdReferenceName , [ ] , factory . createTypeLiteralNode ( [ IdPropertyNode ( ) ] ) ) , ] ; }