Skip to content

Commit 6bd563a

Browse files
committed
feat(generator): support instanceOf
1 parent ba90e22 commit 6bd563a

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/generator.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export function generate(node: t.Node | t.PropTypeNode[], options: GenerateOptio
134134
return `${importedName}.${node.elementType}`;
135135
}
136136

137+
if (t.isInstanceOfNode(node)) {
138+
return `${importedName}.instanceOf(${node.instance})`;
139+
}
140+
137141
if (t.isArrayNode(node)) {
138142
if (t.isAnyNode(node.arrayType)) {
139143
return `${importedName}.array`;
@@ -145,10 +149,14 @@ export function generate(node: t.Node | t.PropTypeNode[], options: GenerateOptio
145149
if (t.isUnionNode(node)) {
146150
let [literals, rest] = _.partition(node.types, t.isLiteralNode);
147151
literals = _.uniqBy(literals, x => x.value);
148-
rest = _.uniqBy(rest, x => x.type);
152+
rest = _.uniqBy(rest, x => (t.isInstanceOfNode(x) ? `${x.type}.${x.instance}` : x.type));
149153

150154
literals = literals.sort((a, b) => a.value.localeCompare(b.value));
151-
rest = rest.sort((a, b) => a.type.localeCompare(b.type));
155+
rest = rest.sort((a, b) =>
156+
(t.isInstanceOfNode(a) ? `${a.type}.${a.instance}` : a.type).localeCompare(
157+
t.isInstanceOfNode(b) ? `${b.type}.${b.instance}` : b.type,
158+
),
159+
);
152160

153161
if (literals.find(x => x.value === 'true') && literals.find(x => x.value === 'false')) {
154162
rest.push(t.booleanNode());

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from './props/any';
1515
export * from './props/object';
1616
export * from './props/array';
1717
export * from './props/element';
18+
export * from './props/instanceOf';

src/types/props/instanceOf.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Node } from '../nodes/baseNodes';
2+
3+
const typeString = 'InstanceOfNode';
4+
5+
export interface InstanceOfNode extends Node {
6+
instance: string;
7+
}
8+
9+
export function instanceOfNode(instance: string): InstanceOfNode {
10+
return {
11+
type: typeString,
12+
instance,
13+
};
14+
}
15+
16+
export function isInstanceOfNode(node: Node): node is InstanceOfNode {
17+
return node.type === typeString;
18+
}

0 commit comments

Comments
 (0)