11import {
2+ isBooleanishNode ,
23 isReferenceNode ,
34 isRegularNode ,
45 RegularNode ,
@@ -9,10 +10,8 @@ import {
910import { Box } from '@stoplight/mosaic' ;
1011import * as React from 'react' ;
1112
12- import { COMMON_JSON_SCHEMA_AND_OAS_FORMATS } from '../../consts' ;
13- import { isPrimitiveArray } from '../../tree' ;
1413import { printName } from '../../utils' ;
15- import { Format } from './Format ' ;
14+ import { getApplicableFormats } from '../../utils/getApplicableFormats ' ;
1615
1716function shouldRenderName ( type : SchemaNodeKind | SchemaCombinerName | '$ref' ) : boolean {
1817 return type === SchemaNodeKind . Array || type === SchemaNodeKind . Object || type === '$ref' ;
@@ -32,32 +31,6 @@ function getTypes(schemaNode: RegularNode): Array<SchemaNodeKind | SchemaCombine
3231 ) ;
3332}
3433
35- function getFormats ( schemaNode : RegularNode ) : Partial < Record < SchemaNodeKind , string > > {
36- const formats : Partial < Record < SchemaNodeKind , string > > = { } ;
37-
38- if ( isPrimitiveArray ( schemaNode ) && schemaNode . children [ 0 ] . format !== null ) {
39- formats . array = schemaNode . children [ 0 ] . format ;
40- }
41-
42- if ( schemaNode . format === null ) {
43- return formats ;
44- }
45-
46- const types = getTypes ( schemaNode ) ;
47-
48- for ( const type of types ) {
49- if ( ! ( type in COMMON_JSON_SCHEMA_AND_OAS_FORMATS ) ) continue ;
50-
51- if ( COMMON_JSON_SCHEMA_AND_OAS_FORMATS [ type ] . includes ( schemaNode . format ) ) {
52- formats [ type ] = schemaNode . format ;
53- return formats ;
54- }
55- }
56-
57- formats . string = schemaNode . format ;
58- return formats ;
59- }
60-
6134export const Types : React . FunctionComponent < { schemaNode : SchemaNode } > = ( { schemaNode } ) => {
6235 if ( isReferenceNode ( schemaNode ) ) {
6336 return (
@@ -67,32 +40,51 @@ export const Types: React.FunctionComponent<{ schemaNode: SchemaNode }> = ({ sch
6740 ) ;
6841 }
6942
43+ if ( isBooleanishNode ( schemaNode ) ) {
44+ return (
45+ < Box as = "span" textOverflow = "truncate" color = "muted" data-test = "property-type" >
46+ { schemaNode . fragment ? 'any' : 'never' }
47+ </ Box >
48+ ) ;
49+ }
50+
7051 if ( ! isRegularNode ( schemaNode ) ) {
7152 return null ;
7253 }
7354
55+ const formats = getApplicableFormats ( schemaNode ) ;
7456 const types = getTypes ( schemaNode ) ;
75- const formats = getFormats ( schemaNode ) ;
7657
7758 if ( types . length === 0 ) {
78- return formats . string !== void 0 ? < Format format = { formats . string } /> : null ;
79- }
80-
81- const rendered = types . map ( ( type , i , { length } ) => (
82- < React . Fragment key = { type } >
59+ return (
8360 < Box as = "span" textOverflow = "truncate" color = "muted" data-test = "property-type" >
84- { shouldRenderName ( type ) ? printName ( schemaNode ) ?? type : type }
61+ { formats === null ? 'any' : `< ${ formats [ 1 ] } >` }
8562 </ Box >
63+ ) ;
64+ }
65+
66+ const rendered = types . map ( ( type , i , { length } ) => {
67+ let printedName ;
68+ if ( shouldRenderName ( type ) ) {
69+ printedName = printName ( schemaNode ) ;
70+ }
8671
87- { type in formats ? < Format format = { formats [ type ] } /> : null }
72+ printedName ??= type + ( formats === null || formats [ 0 ] !== type ? '' : `< ${ formats [ 1 ] } >` ) ;
8873
89- { i < length - 1 && (
90- < Box as = "span" key = { `${ i } -sep` } color = "muted" >
91- { ' or ' }
74+ return (
75+ < React . Fragment key = { type } >
76+ < Box as = "span" textOverflow = "truncate" color = "muted" data-test = "property-type" >
77+ { printedName }
9278 </ Box >
93- ) }
94- </ React . Fragment >
95- ) ) ;
79+
80+ { i < length - 1 && (
81+ < Box as = "span" key = { `${ i } -sep` } color = "muted" >
82+ { ' or ' }
83+ </ Box >
84+ ) }
85+ </ React . Fragment >
86+ ) ;
87+ } ) ;
9688
9789 return rendered . length > 1 ? < Box textOverflow = "truncate" > { rendered } </ Box > : < > { rendered } </ > ;
9890} ;
0 commit comments