Skip to content

Commit 8167911

Browse files
committed
fix: typings
1 parent 77fa111 commit 8167911

File tree

8 files changed

+22
-21
lines changed

8 files changed

+22
-21
lines changed

src/components/SchemaRow.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IRowRendererOptions, Tree } from '@stoplight/tree-list';
22
import cn from 'classnames';
33
import * as React from 'react';
44

5-
import { MetadataStore } from '../tree/metadata';
5+
import { metadataStore } from '../tree/metadata';
66
import { GoToRefHandler, SchemaTreeListNode } from '../types';
77
import { Caret, Description, Divider, Property, Validations } from './shared';
88

@@ -18,15 +18,15 @@ const ICON_DIMENSION = 20;
1818
const ROW_OFFSET = 7;
1919

2020
export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({ className, node, rowOptions, onGoToRef }) => {
21-
const metadata = MetadataStore[node.id];
21+
const metadata = metadataStore[node.id];
2222
if (!metadata) {
2323
throw new Error('Missing metadata');
2424
}
2525

2626
const { path, schema: schemaNode } = metadata;
2727

2828
const parentSchemaNode =
29-
node.parent === null || !(node.parent.id in MetadataStore) ? null : MetadataStore[node.parent.id].schema;
29+
node.parent === null || !(node.parent.id in metadataStore) ? null : metadataStore[node.parent.id].schema;
3030
const description = 'annotations' in schemaNode ? schemaNode.annotations.description : null;
3131

3232
return (

src/components/shared/Property.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface IProperty {
1414

1515
export const Property: React.FunctionComponent<IProperty> = ({ node, path, onGoToRef }) => {
1616
const type = isRefNode(node) ? '$ref' : isCombinerNode(node) ? node.combiner : node.type;
17-
const subtype = isArrayNodeWithItems(node) ? inferType(node.items) : undefined;
17+
const subtype = isArrayNodeWithItems(node) ? inferType(node.items) : void 0;
1818

1919
const childrenCount = React.useMemo<number | null>(() => {
2020
if (type === SchemaKind.Object) {

src/components/shared/Types.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Dictionary } from '@stoplight/types';
1+
import { Dictionary, Optional } from '@stoplight/types';
22
import cn from 'classnames';
33
import { JSONSchema4TypeName } from 'json-schema';
44
import * as React from 'react';
@@ -9,8 +9,8 @@ import { JSONSchema4CombinerName } from '../../types';
99
* TYPE
1010
*/
1111
export interface IType {
12-
type: JSONSchema4TypeName | JSONSchema4CombinerName | 'binary' | '$ref';
13-
subtype?: JSONSchema4TypeName;
12+
type: JSONSchema4TypeName | JSONSchema4CombinerName | '$ref';
13+
subtype: Optional<JSONSchema4TypeName | JSONSchema4TypeName[]>;
1414
className?: string;
1515
}
1616

@@ -30,12 +30,12 @@ Type.displayName = 'JsonSchemaViewer.Type';
3030
*/
3131
interface ITypes {
3232
className?: string;
33-
type?: JSONSchema4TypeName | JSONSchema4TypeName[] | JSONSchema4CombinerName | '$ref';
34-
subtype?: JSONSchema4TypeName;
33+
type: Optional<JSONSchema4TypeName | JSONSchema4TypeName[] | JSONSchema4CombinerName | '$ref'>;
34+
subtype: Optional<JSONSchema4TypeName | JSONSchema4TypeName[]>;
3535
}
3636

3737
export const Types: React.FunctionComponent<ITypes> = ({ className, type, subtype, children }) => {
38-
if (!type) return null;
38+
if (type === void 0) return null;
3939

4040
if (!Array.isArray(type)) {
4141
return <Type className={className} type={type} subtype={subtype} children={children} />;
@@ -75,7 +75,7 @@ export const PropertyTypeColors: Dictionary<string, IType['type']> = {
7575
integer: 'text-red-7 dark:text-red-6',
7676
number: 'text-red-7 dark:text-red-6',
7777
boolean: 'text-red-4',
78-
binary: 'text-green-4',
78+
// binary: 'text-green-4',
7979
string: 'text-green-7 dark:text-green-5',
8080
$ref: 'text-purple-6 dark:text-purple-4',
8181
};

src/tree/metadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Dictionary, JsonPath } from '@stoplight/types';
2-
import { SchemaNode } from '../types';
1+
import { JsonPath } from '@stoplight/types';
2+
import { SchemaNode, SchemaTreeListNode } from '../types';
33

44
export interface ITreeNodeMeta {
55
path: JsonPath;
66
schema: SchemaNode;
77
}
88

9-
export const MetadataStore: Dictionary<ITreeNodeMeta, string> = {}; // todo: can be a weakmap I guess since no stringyfing is needed
9+
export const metadataStore = new WeakMap<SchemaTreeListNode, ITreeNodeMeta>();

src/tree/populateTree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { IArrayNode, IObjectNode, SchemaKind, SchemaNode, SchemaTreeListNode } f
66
import { getPrimaryType } from '../utils/getPrimaryType';
77
import { isCombinerNode, isRefNode } from '../utils/guards';
88
import { isCombiner } from '../utils/isCombiner';
9-
import { MetadataStore } from './metadata';
9+
import { metadataStore } from './metadata';
1010
import { walk } from './walk';
1111

1212
export type WalkingOptions = {
@@ -34,7 +34,7 @@ export const populateTree: Walker = (schema, parent, level, path, options) => {
3434
};
3535

3636
parent.children.push(treeNode);
37-
MetadataStore[treeNode.id] = {
37+
metadataStore[treeNode.id] = {
3838
schema: node,
3939
path,
4040
};

src/tree/tree.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { JSONSchema4 } from 'json-schema';
55
import { get } from 'lodash-es';
66
import { SchemaNode } from '../types';
77
import { isRefNode } from '../utils/guards';
8-
import { MetadataStore } from './metadata';
8+
import { metadataStore } from './metadata';
99
import { populateTree } from './populateTree';
1010

1111
export class SchemaTree extends Tree {
@@ -23,7 +23,7 @@ export class SchemaTree extends Tree {
2323
expanded[node.id] = false;
2424
}
2525

26-
if (MetadataStore[parentTreeNode.id] && isRefNode(MetadataStore[parentTreeNode.id].schema)) return false;
26+
if (metadataStore[parentTreeNode.id] && isRefNode(metadataStore[parentTreeNode.id].schema)) return false;
2727
return level <= this.defaultExpandedDepth + 1;
2828
},
2929
});
@@ -45,7 +45,7 @@ export class SchemaTree extends Tree {
4545
return super.unwrap(node);
4646
}
4747

48-
const { path, schema } = MetadataStore[node.id];
48+
const { path, schema } = metadataStore[node.id];
4949
if (isRefNode(schema)) {
5050
this.populateTreeFragment(node, pointerToPath(schema.$ref)); // DO NOTE THAT NODES PLACED UNDER THE REF WON'T HAVE CORREC PATHS
5151
} else {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type SchemaNode = ICombinerNode | IBaseNode | IArrayNode | IObjectNode |
5555

5656
export type SchemaTreeListNode = TreeListNode;
5757

58-
export type GoToRefHandler = (path: string, node: SchemaTreeListNode) => void;
58+
export type GoToRefHandler = (path: string, node: IRefNode) => void;
5959

6060
export type RowRenderer = (
6161
node: TreeListNode,

src/utils/inferType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { Optional } from '@stoplight/types';
12
import { JSONSchema4, JSONSchema4TypeName } from 'json-schema';
23
import { SchemaKind } from '../types';
34

4-
export function inferType(node: JSONSchema4): JSONSchema4TypeName | JSONSchema4TypeName[] | undefined {
5+
export function inferType(node: JSONSchema4): Optional<JSONSchema4TypeName | JSONSchema4TypeName[]> {
56
if ('type' in node) {
67
return node.type;
78
}

0 commit comments

Comments
 (0)