@prisma/ts-builders 6.6.0-dev.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +5 -0
- package/dist/AnyDeclarationBuilder.d.ts +6 -0
- package/dist/AnyDeclarationBuilder.js +16 -0
- package/dist/AnyDeclarationBuilder.mjs +0 -0
- package/dist/ArraySpread.d.ts +8 -0
- package/dist/ArraySpread.js +42 -0
- package/dist/ArraySpread.mjs +17 -0
- package/dist/ArrayType.d.ts +8 -0
- package/dist/ArrayType.js +43 -0
- package/dist/ArrayType.mjs +18 -0
- package/dist/ArrayType.test.d.ts +1 -0
- package/dist/ArrayType.test.js +24 -0
- package/dist/ArrayType.test.mjs +23 -0
- package/dist/BasicBuilder.d.ts +4 -0
- package/dist/BasicBuilder.js +16 -0
- package/dist/BasicBuilder.mjs +0 -0
- package/dist/Class.d.ts +22 -0
- package/dist/Class.js +79 -0
- package/dist/Class.mjs +54 -0
- package/dist/Class.test.d.ts +1 -0
- package/dist/Class.test.js +56 -0
- package/dist/Class.test.mjs +55 -0
- package/dist/ConstDeclaration.d.ts +13 -0
- package/dist/ConstDeclaration.js +49 -0
- package/dist/ConstDeclaration.mjs +24 -0
- package/dist/ConstDeclaration.test.d.ts +1 -0
- package/dist/ConstDeclaration.test.js +19 -0
- package/dist/ConstDeclaration.test.mjs +18 -0
- package/dist/DocComment.d.ts +11 -0
- package/dist/DocComment.js +101 -0
- package/dist/DocComment.mjs +76 -0
- package/dist/DocComment.test.d.ts +1 -0
- package/dist/DocComment.test.js +80 -0
- package/dist/DocComment.test.mjs +79 -0
- package/dist/Export.d.ts +12 -0
- package/dist/Export.js +48 -0
- package/dist/Export.mjs +23 -0
- package/dist/ExportFrom.d.ts +32 -0
- package/dist/ExportFrom.js +98 -0
- package/dist/ExportFrom.mjs +69 -0
- package/dist/ExportFrom.test.d.ts +1 -0
- package/dist/ExportFrom.test.js +25 -0
- package/dist/ExportFrom.test.mjs +24 -0
- package/dist/File.d.ts +15 -0
- package/dist/File.js +57 -0
- package/dist/File.mjs +32 -0
- package/dist/FunctionType.d.ts +17 -0
- package/dist/FunctionType.js +60 -0
- package/dist/FunctionType.mjs +35 -0
- package/dist/FunctionType.test.d.ts +1 -0
- package/dist/FunctionType.test.js +32 -0
- package/dist/FunctionType.test.mjs +31 -0
- package/dist/GenericParameter.d.ts +15 -0
- package/dist/GenericParameter.js +60 -0
- package/dist/GenericParameter.mjs +35 -0
- package/dist/GenericParameter.test.d.ts +1 -0
- package/dist/GenericParameter.test.js +19 -0
- package/dist/GenericParameter.test.mjs +18 -0
- package/dist/Import.d.ts +36 -0
- package/dist/Import.js +119 -0
- package/dist/Import.mjs +90 -0
- package/dist/Import.test.d.ts +1 -0
- package/dist/Import.test.js +33 -0
- package/dist/Import.test.mjs +32 -0
- package/dist/Interface.d.ts +22 -0
- package/dist/Interface.js +79 -0
- package/dist/Interface.mjs +54 -0
- package/dist/Interface.test.d.ts +1 -0
- package/dist/Interface.test.js +56 -0
- package/dist/Interface.test.mjs +55 -0
- package/dist/KeyType.d.ts +9 -0
- package/dist/KeyType.js +44 -0
- package/dist/KeyType.mjs +19 -0
- package/dist/KeyType.test.d.ts +1 -0
- package/dist/KeyType.test.js +28 -0
- package/dist/KeyType.test.mjs +27 -0
- package/dist/KeyofType.d.ts +8 -0
- package/dist/KeyofType.js +47 -0
- package/dist/KeyofType.mjs +22 -0
- package/dist/KeyofType.test.d.ts +1 -0
- package/dist/KeyofType.test.js +28 -0
- package/dist/KeyofType.test.mjs +27 -0
- package/dist/Method.d.ts +20 -0
- package/dist/Method.js +75 -0
- package/dist/Method.mjs +50 -0
- package/dist/Method.test.d.ts +1 -0
- package/dist/Method.test.js +43 -0
- package/dist/Method.test.mjs +42 -0
- package/dist/NamedType.d.ts +10 -0
- package/dist/NamedType.js +50 -0
- package/dist/NamedType.mjs +25 -0
- package/dist/NamedType.test.d.ts +1 -0
- package/dist/NamedType.test.js +15 -0
- package/dist/NamedType.test.mjs +14 -0
- package/dist/NamespaceDeclaration.d.ts +13 -0
- package/dist/NamespaceDeclaration.js +48 -0
- package/dist/NamespaceDeclaration.mjs +23 -0
- package/dist/ObjectType.d.ts +18 -0
- package/dist/ObjectType.js +71 -0
- package/dist/ObjectType.mjs +46 -0
- package/dist/ObjectType.test.d.ts +1 -0
- package/dist/ObjectType.test.js +32 -0
- package/dist/ObjectType.test.mjs +31 -0
- package/dist/Parameter.d.ts +12 -0
- package/dist/Parameter.js +50 -0
- package/dist/Parameter.mjs +25 -0
- package/dist/Parameter.test.d.ts +1 -0
- package/dist/Parameter.test.js +14 -0
- package/dist/Parameter.test.mjs +13 -0
- package/dist/PrimitiveType.d.ts +18 -0
- package/dist/PrimitiveType.js +70 -0
- package/dist/PrimitiveType.mjs +35 -0
- package/dist/Property.d.ts +18 -0
- package/dist/Property.js +75 -0
- package/dist/Property.mjs +50 -0
- package/dist/Property.test.d.ts +1 -0
- package/dist/Property.test.js +37 -0
- package/dist/Property.test.mjs +36 -0
- package/dist/StringLiteralType.d.ts +8 -0
- package/dist/StringLiteralType.js +42 -0
- package/dist/StringLiteralType.mjs +17 -0
- package/dist/TupleType.d.ts +17 -0
- package/dist/TupleType.js +69 -0
- package/dist/TupleType.mjs +42 -0
- package/dist/TupleType.test.d.ts +1 -0
- package/dist/TupleType.test.js +21 -0
- package/dist/TupleType.test.mjs +20 -0
- package/dist/TypeBuilder.d.ts +11 -0
- package/dist/TypeBuilder.js +47 -0
- package/dist/TypeBuilder.mjs +23 -0
- package/dist/TypeDeclaration.d.ts +17 -0
- package/dist/TypeDeclaration.js +62 -0
- package/dist/TypeDeclaration.mjs +37 -0
- package/dist/TypeDeclaration.test.d.ts +1 -0
- package/dist/TypeDeclaration.test.js +28 -0
- package/dist/TypeDeclaration.test.mjs +27 -0
- package/dist/UnionType.d.ts +13 -0
- package/dist/UnionType.js +74 -0
- package/dist/UnionType.mjs +49 -0
- package/dist/UnionType.test.d.ts +1 -0
- package/dist/UnionType.test.js +28 -0
- package/dist/UnionType.test.mjs +27 -0
- package/dist/WellKnownSymbol.d.ts +9 -0
- package/dist/WellKnownSymbol.js +43 -0
- package/dist/WellKnownSymbol.mjs +17 -0
- package/dist/Writer.d.ts +94 -0
- package/dist/Writer.js +175 -0
- package/dist/Writer.mjs +150 -0
- package/dist/Writer.test.d.ts +1 -0
- package/dist/Writer.test.js +100 -0
- package/dist/Writer.test.mjs +99 -0
- package/dist/helpers.d.ts +6 -0
- package/dist/helpers.js +46 -0
- package/dist/helpers.mjs +19 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +84 -0
- package/dist/index.mjs +32 -0
- package/dist/stringify.d.ts +7 -0
- package/dist/stringify.js +44 -0
- package/dist/stringify.mjs +20 -0
- package/package.json +41 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { docComment } from "./DocComment";
|
|
3
|
+
import { namedType } from "./NamedType";
|
|
4
|
+
import { property } from "./Property";
|
|
5
|
+
import { stringify } from "./stringify";
|
|
6
|
+
import { toStringTag } from "./WellKnownSymbol";
|
|
7
|
+
const A = namedType("A");
|
|
8
|
+
test("name and type", () => {
|
|
9
|
+
const prop = property("foo", A);
|
|
10
|
+
expect(stringify(prop)).toMatchInlineSnapshot(`"foo: A"`);
|
|
11
|
+
});
|
|
12
|
+
test("invalid identifier", () => {
|
|
13
|
+
const prop = property("this is not a valid JS identifier", A);
|
|
14
|
+
expect(stringify(prop)).toMatchInlineSnapshot(`"["this is not a valid JS identifier"]: A"`);
|
|
15
|
+
});
|
|
16
|
+
test("well-known symbol", () => {
|
|
17
|
+
const prop = property(toStringTag, A);
|
|
18
|
+
expect(stringify(prop)).toMatchInlineSnapshot(`"[Symbol.toStringTag]: A"`);
|
|
19
|
+
});
|
|
20
|
+
test("optional", () => {
|
|
21
|
+
const prop = property("foo", A).optional();
|
|
22
|
+
expect(stringify(prop)).toMatchInlineSnapshot(`"foo?: A"`);
|
|
23
|
+
});
|
|
24
|
+
test("readonly", () => {
|
|
25
|
+
const prop = property("foo", A).readonly();
|
|
26
|
+
expect(stringify(prop)).toMatchInlineSnapshot(`"readonly foo: A"`);
|
|
27
|
+
});
|
|
28
|
+
test("with doc comment", () => {
|
|
29
|
+
const prop = property("foo", A).setDocComment(docComment("This is foo"));
|
|
30
|
+
expect(stringify(prop)).toMatchInlineSnapshot(`
|
|
31
|
+
"/**
|
|
32
|
+
* This is foo
|
|
33
|
+
*/
|
|
34
|
+
foo: A"
|
|
35
|
+
`);
|
|
36
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { TypeBuilder } from './TypeBuilder';
|
|
2
|
+
import { Writer } from './Writer';
|
|
3
|
+
export declare class StringLiteralType extends TypeBuilder {
|
|
4
|
+
readonly content: string;
|
|
5
|
+
constructor(content: string);
|
|
6
|
+
write(writer: Writer): void;
|
|
7
|
+
}
|
|
8
|
+
export declare function stringLiteral(content: string): StringLiteralType;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var StringLiteralType_exports = {};
|
|
20
|
+
__export(StringLiteralType_exports, {
|
|
21
|
+
StringLiteralType: () => StringLiteralType,
|
|
22
|
+
stringLiteral: () => stringLiteral
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(StringLiteralType_exports);
|
|
25
|
+
var import_TypeBuilder = require("./TypeBuilder");
|
|
26
|
+
class StringLiteralType extends import_TypeBuilder.TypeBuilder {
|
|
27
|
+
constructor(content) {
|
|
28
|
+
super();
|
|
29
|
+
this.content = content;
|
|
30
|
+
}
|
|
31
|
+
write(writer) {
|
|
32
|
+
writer.write(JSON.stringify(this.content));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function stringLiteral(content) {
|
|
36
|
+
return new StringLiteralType(content);
|
|
37
|
+
}
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
StringLiteralType,
|
|
41
|
+
stringLiteral
|
|
42
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TypeBuilder } from "./TypeBuilder";
|
|
2
|
+
class StringLiteralType extends TypeBuilder {
|
|
3
|
+
constructor(content) {
|
|
4
|
+
super();
|
|
5
|
+
this.content = content;
|
|
6
|
+
}
|
|
7
|
+
write(writer) {
|
|
8
|
+
writer.write(JSON.stringify(this.content));
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function stringLiteral(content) {
|
|
12
|
+
return new StringLiteralType(content);
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
StringLiteralType,
|
|
16
|
+
stringLiteral
|
|
17
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
import { TypeBuilder } from './TypeBuilder';
|
|
3
|
+
import { Writer } from './Writer';
|
|
4
|
+
export declare class TupleItem implements BasicBuilder {
|
|
5
|
+
readonly type: TypeBuilder;
|
|
6
|
+
private name;
|
|
7
|
+
constructor(type: TypeBuilder);
|
|
8
|
+
setName(name: string): this;
|
|
9
|
+
write(writer: Writer<undefined>): void;
|
|
10
|
+
}
|
|
11
|
+
export declare class TupleType extends TypeBuilder {
|
|
12
|
+
readonly items: TupleItem[];
|
|
13
|
+
add(item: TypeBuilder | TupleItem): this;
|
|
14
|
+
write(writer: Writer): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function tupleType(): TupleType;
|
|
17
|
+
export declare function tupleItem(type: TypeBuilder): TupleItem;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var TupleType_exports = {};
|
|
20
|
+
__export(TupleType_exports, {
|
|
21
|
+
TupleItem: () => TupleItem,
|
|
22
|
+
TupleType: () => TupleType,
|
|
23
|
+
tupleItem: () => tupleItem,
|
|
24
|
+
tupleType: () => tupleType
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(TupleType_exports);
|
|
27
|
+
var import_TypeBuilder = require("./TypeBuilder");
|
|
28
|
+
class TupleItem {
|
|
29
|
+
constructor(type) {
|
|
30
|
+
this.type = type;
|
|
31
|
+
}
|
|
32
|
+
name;
|
|
33
|
+
setName(name) {
|
|
34
|
+
this.name = name;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
write(writer) {
|
|
38
|
+
if (this.name) {
|
|
39
|
+
writer.write(this.name).write(": ");
|
|
40
|
+
}
|
|
41
|
+
writer.write(this.type);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class TupleType extends import_TypeBuilder.TypeBuilder {
|
|
45
|
+
items = [];
|
|
46
|
+
add(item) {
|
|
47
|
+
if (item instanceof import_TypeBuilder.TypeBuilder) {
|
|
48
|
+
item = new TupleItem(item);
|
|
49
|
+
}
|
|
50
|
+
this.items.push(item);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
write(writer) {
|
|
54
|
+
writer.write("[").writeJoined(", ", this.items).write("]");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function tupleType() {
|
|
58
|
+
return new TupleType();
|
|
59
|
+
}
|
|
60
|
+
function tupleItem(type) {
|
|
61
|
+
return new TupleItem(type);
|
|
62
|
+
}
|
|
63
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
64
|
+
0 && (module.exports = {
|
|
65
|
+
TupleItem,
|
|
66
|
+
TupleType,
|
|
67
|
+
tupleItem,
|
|
68
|
+
tupleType
|
|
69
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { TypeBuilder } from "./TypeBuilder";
|
|
2
|
+
class TupleItem {
|
|
3
|
+
constructor(type) {
|
|
4
|
+
this.type = type;
|
|
5
|
+
}
|
|
6
|
+
name;
|
|
7
|
+
setName(name) {
|
|
8
|
+
this.name = name;
|
|
9
|
+
return this;
|
|
10
|
+
}
|
|
11
|
+
write(writer) {
|
|
12
|
+
if (this.name) {
|
|
13
|
+
writer.write(this.name).write(": ");
|
|
14
|
+
}
|
|
15
|
+
writer.write(this.type);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class TupleType extends TypeBuilder {
|
|
19
|
+
items = [];
|
|
20
|
+
add(item) {
|
|
21
|
+
if (item instanceof TypeBuilder) {
|
|
22
|
+
item = new TupleItem(item);
|
|
23
|
+
}
|
|
24
|
+
this.items.push(item);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
write(writer) {
|
|
28
|
+
writer.write("[").writeJoined(", ", this.items).write("]");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function tupleType() {
|
|
32
|
+
return new TupleType();
|
|
33
|
+
}
|
|
34
|
+
function tupleItem(type) {
|
|
35
|
+
return new TupleItem(type);
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
TupleItem,
|
|
39
|
+
TupleType,
|
|
40
|
+
tupleItem,
|
|
41
|
+
tupleType
|
|
42
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var import_vitest = require("vitest");
|
|
3
|
+
var import_NamedType = require("./NamedType");
|
|
4
|
+
var import_stringify = require("./stringify");
|
|
5
|
+
var import_TupleType = require("./TupleType");
|
|
6
|
+
(0, import_vitest.test)("empty", () => {
|
|
7
|
+
const tuple = (0, import_TupleType.tupleType)();
|
|
8
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(tuple)).toMatchInlineSnapshot(`"[]"`);
|
|
9
|
+
});
|
|
10
|
+
(0, import_vitest.test)("one item", () => {
|
|
11
|
+
const tuple = (0, import_TupleType.tupleType)().add((0, import_NamedType.namedType)("A"));
|
|
12
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(tuple)).toMatchInlineSnapshot(`"[A]"`);
|
|
13
|
+
});
|
|
14
|
+
(0, import_vitest.test)("with named item", () => {
|
|
15
|
+
const tuple = (0, import_TupleType.tupleType)().add((0, import_TupleType.tupleItem)((0, import_NamedType.namedType)("A")).setName("foo"));
|
|
16
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(tuple)).toMatchInlineSnapshot(`"[foo: A]"`);
|
|
17
|
+
});
|
|
18
|
+
(0, import_vitest.test)("multiple items", () => {
|
|
19
|
+
const tuple = (0, import_TupleType.tupleType)().add((0, import_NamedType.namedType)("A")).add((0, import_NamedType.namedType)("B")).add((0, import_NamedType.namedType)("C"));
|
|
20
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(tuple)).toMatchInlineSnapshot(`"[A, B, C]"`);
|
|
21
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { namedType } from "./NamedType";
|
|
3
|
+
import { stringify } from "./stringify";
|
|
4
|
+
import { tupleItem, tupleType } from "./TupleType";
|
|
5
|
+
test("empty", () => {
|
|
6
|
+
const tuple = tupleType();
|
|
7
|
+
expect(stringify(tuple)).toMatchInlineSnapshot(`"[]"`);
|
|
8
|
+
});
|
|
9
|
+
test("one item", () => {
|
|
10
|
+
const tuple = tupleType().add(namedType("A"));
|
|
11
|
+
expect(stringify(tuple)).toMatchInlineSnapshot(`"[A]"`);
|
|
12
|
+
});
|
|
13
|
+
test("with named item", () => {
|
|
14
|
+
const tuple = tupleType().add(tupleItem(namedType("A")).setName("foo"));
|
|
15
|
+
expect(stringify(tuple)).toMatchInlineSnapshot(`"[foo: A]"`);
|
|
16
|
+
});
|
|
17
|
+
test("multiple items", () => {
|
|
18
|
+
const tuple = tupleType().add(namedType("A")).add(namedType("B")).add(namedType("C"));
|
|
19
|
+
expect(stringify(tuple)).toMatchInlineSnapshot(`"[A, B, C]"`);
|
|
20
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
import type { KeyType } from './KeyType';
|
|
3
|
+
import { Writer } from './Writer';
|
|
4
|
+
export declare abstract class TypeBuilder implements BasicBuilder {
|
|
5
|
+
needsParenthesisWhenIndexed: boolean;
|
|
6
|
+
needsParenthesisInKeyof: boolean;
|
|
7
|
+
needsParenthesisInUnion: boolean;
|
|
8
|
+
abstract write(writer: Writer): void;
|
|
9
|
+
subKey(key: string): KeyType;
|
|
10
|
+
writeIndexed(writer: Writer): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var TypeBuilder_exports = {};
|
|
20
|
+
__export(TypeBuilder_exports, {
|
|
21
|
+
TypeBuilder: () => TypeBuilder
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(TypeBuilder_exports);
|
|
24
|
+
class TypeBuilder {
|
|
25
|
+
// TODO(@SevInf): this should be replaced with precedence system that would
|
|
26
|
+
// automatically add parenthesis where they are needed
|
|
27
|
+
needsParenthesisWhenIndexed = false;
|
|
28
|
+
needsParenthesisInKeyof = false;
|
|
29
|
+
needsParenthesisInUnion = false;
|
|
30
|
+
subKey(key) {
|
|
31
|
+
const { KeyType } = require("./KeyType");
|
|
32
|
+
return new KeyType(this, key);
|
|
33
|
+
}
|
|
34
|
+
writeIndexed(writer) {
|
|
35
|
+
if (this.needsParenthesisWhenIndexed) {
|
|
36
|
+
writer.write("(");
|
|
37
|
+
}
|
|
38
|
+
writer.write(this);
|
|
39
|
+
if (this.needsParenthesisWhenIndexed) {
|
|
40
|
+
writer.write(")");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
TypeBuilder
|
|
47
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
class TypeBuilder {
|
|
2
|
+
// TODO(@SevInf): this should be replaced with precedence system that would
|
|
3
|
+
// automatically add parenthesis where they are needed
|
|
4
|
+
needsParenthesisWhenIndexed = false;
|
|
5
|
+
needsParenthesisInKeyof = false;
|
|
6
|
+
needsParenthesisInUnion = false;
|
|
7
|
+
subKey(key) {
|
|
8
|
+
const { KeyType } = require("./KeyType");
|
|
9
|
+
return new KeyType(this, key);
|
|
10
|
+
}
|
|
11
|
+
writeIndexed(writer) {
|
|
12
|
+
if (this.needsParenthesisWhenIndexed) {
|
|
13
|
+
writer.write("(");
|
|
14
|
+
}
|
|
15
|
+
writer.write(this);
|
|
16
|
+
if (this.needsParenthesisWhenIndexed) {
|
|
17
|
+
writer.write(")");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
TypeBuilder
|
|
23
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
import { DocComment } from './DocComment';
|
|
3
|
+
import { GenericParameter } from './GenericParameter';
|
|
4
|
+
import { TypeBuilder } from './TypeBuilder';
|
|
5
|
+
import { Writer } from './Writer';
|
|
6
|
+
export declare class TypeDeclaration<InnerType extends TypeBuilder = TypeBuilder> implements BasicBuilder {
|
|
7
|
+
name: string;
|
|
8
|
+
readonly type: InnerType;
|
|
9
|
+
private genericParameters;
|
|
10
|
+
private docComment?;
|
|
11
|
+
constructor(name: string, type: InnerType);
|
|
12
|
+
addGenericParameter(param: GenericParameter): this;
|
|
13
|
+
setName(name: string): this;
|
|
14
|
+
setDocComment(docComment: DocComment): this;
|
|
15
|
+
write(writer: Writer): void;
|
|
16
|
+
}
|
|
17
|
+
export declare function typeDeclaration<InnerType extends TypeBuilder = TypeBuilder>(name: string, type: InnerType): TypeDeclaration<InnerType>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var TypeDeclaration_exports = {};
|
|
20
|
+
__export(TypeDeclaration_exports, {
|
|
21
|
+
TypeDeclaration: () => TypeDeclaration,
|
|
22
|
+
typeDeclaration: () => typeDeclaration
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(TypeDeclaration_exports);
|
|
25
|
+
class TypeDeclaration {
|
|
26
|
+
constructor(name, type) {
|
|
27
|
+
this.name = name;
|
|
28
|
+
this.type = type;
|
|
29
|
+
}
|
|
30
|
+
genericParameters = [];
|
|
31
|
+
docComment;
|
|
32
|
+
addGenericParameter(param) {
|
|
33
|
+
this.genericParameters.push(param);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
setName(name) {
|
|
37
|
+
this.name = name;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
setDocComment(docComment) {
|
|
41
|
+
this.docComment = docComment;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
write(writer) {
|
|
45
|
+
if (this.docComment) {
|
|
46
|
+
writer.write(this.docComment);
|
|
47
|
+
}
|
|
48
|
+
writer.write("type ").write(this.name);
|
|
49
|
+
if (this.genericParameters.length > 0) {
|
|
50
|
+
writer.write("<").writeJoined(", ", this.genericParameters).write(">");
|
|
51
|
+
}
|
|
52
|
+
writer.write(" = ").write(this.type);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function typeDeclaration(name, type) {
|
|
56
|
+
return new TypeDeclaration(name, type);
|
|
57
|
+
}
|
|
58
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
59
|
+
0 && (module.exports = {
|
|
60
|
+
TypeDeclaration,
|
|
61
|
+
typeDeclaration
|
|
62
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
class TypeDeclaration {
|
|
2
|
+
constructor(name, type) {
|
|
3
|
+
this.name = name;
|
|
4
|
+
this.type = type;
|
|
5
|
+
}
|
|
6
|
+
genericParameters = [];
|
|
7
|
+
docComment;
|
|
8
|
+
addGenericParameter(param) {
|
|
9
|
+
this.genericParameters.push(param);
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
setName(name) {
|
|
13
|
+
this.name = name;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
setDocComment(docComment) {
|
|
17
|
+
this.docComment = docComment;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
write(writer) {
|
|
21
|
+
if (this.docComment) {
|
|
22
|
+
writer.write(this.docComment);
|
|
23
|
+
}
|
|
24
|
+
writer.write("type ").write(this.name);
|
|
25
|
+
if (this.genericParameters.length > 0) {
|
|
26
|
+
writer.write("<").writeJoined(", ", this.genericParameters).write(">");
|
|
27
|
+
}
|
|
28
|
+
writer.write(" = ").write(this.type);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function typeDeclaration(name, type) {
|
|
32
|
+
return new TypeDeclaration(name, type);
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
TypeDeclaration,
|
|
36
|
+
typeDeclaration
|
|
37
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var import_vitest = require("vitest");
|
|
3
|
+
var import_DocComment = require("./DocComment");
|
|
4
|
+
var import_GenericParameter = require("./GenericParameter");
|
|
5
|
+
var import_NamedType = require("./NamedType");
|
|
6
|
+
var import_stringify = require("./stringify");
|
|
7
|
+
var import_TypeDeclaration = require("./TypeDeclaration");
|
|
8
|
+
const A = (0, import_NamedType.namedType)("A");
|
|
9
|
+
(0, import_vitest.test)("basic", () => {
|
|
10
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_TypeDeclaration.typeDeclaration)("B", A))).toMatchInlineSnapshot(`"type B = A"`);
|
|
11
|
+
});
|
|
12
|
+
(0, import_vitest.test)("with doc comment", () => {
|
|
13
|
+
const decl = (0, import_TypeDeclaration.typeDeclaration)("B", A).setDocComment((0, import_DocComment.docComment)("Type for stuff"));
|
|
14
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(decl)).toMatchInlineSnapshot(`
|
|
15
|
+
"/**
|
|
16
|
+
* Type for stuff
|
|
17
|
+
*/
|
|
18
|
+
type B = A"
|
|
19
|
+
`);
|
|
20
|
+
});
|
|
21
|
+
(0, import_vitest.test)("with generic parameters", () => {
|
|
22
|
+
const decl = (0, import_TypeDeclaration.typeDeclaration)("B", A).addGenericParameter((0, import_GenericParameter.genericParameter)("T"));
|
|
23
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(decl)).toMatchInlineSnapshot(`"type B<T> = A"`);
|
|
24
|
+
});
|
|
25
|
+
(0, import_vitest.test)("with multiple generic parameters", () => {
|
|
26
|
+
const decl = (0, import_TypeDeclaration.typeDeclaration)("B", A).addGenericParameter((0, import_GenericParameter.genericParameter)("T")).addGenericParameter((0, import_GenericParameter.genericParameter)("U"));
|
|
27
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(decl)).toMatchInlineSnapshot(`"type B<T, U> = A"`);
|
|
28
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { docComment } from "./DocComment";
|
|
3
|
+
import { genericParameter } from "./GenericParameter";
|
|
4
|
+
import { namedType } from "./NamedType";
|
|
5
|
+
import { stringify } from "./stringify";
|
|
6
|
+
import { typeDeclaration } from "./TypeDeclaration";
|
|
7
|
+
const A = namedType("A");
|
|
8
|
+
test("basic", () => {
|
|
9
|
+
expect(stringify(typeDeclaration("B", A))).toMatchInlineSnapshot(`"type B = A"`);
|
|
10
|
+
});
|
|
11
|
+
test("with doc comment", () => {
|
|
12
|
+
const decl = typeDeclaration("B", A).setDocComment(docComment("Type for stuff"));
|
|
13
|
+
expect(stringify(decl)).toMatchInlineSnapshot(`
|
|
14
|
+
"/**
|
|
15
|
+
* Type for stuff
|
|
16
|
+
*/
|
|
17
|
+
type B = A"
|
|
18
|
+
`);
|
|
19
|
+
});
|
|
20
|
+
test("with generic parameters", () => {
|
|
21
|
+
const decl = typeDeclaration("B", A).addGenericParameter(genericParameter("T"));
|
|
22
|
+
expect(stringify(decl)).toMatchInlineSnapshot(`"type B<T> = A"`);
|
|
23
|
+
});
|
|
24
|
+
test("with multiple generic parameters", () => {
|
|
25
|
+
const decl = typeDeclaration("B", A).addGenericParameter(genericParameter("T")).addGenericParameter(genericParameter("U"));
|
|
26
|
+
expect(stringify(decl)).toMatchInlineSnapshot(`"type B<T, U> = A"`);
|
|
27
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TypeBuilder } from './TypeBuilder';
|
|
2
|
+
import { Writer } from './Writer';
|
|
3
|
+
export declare class UnionType<VariantType extends TypeBuilder = TypeBuilder> extends TypeBuilder {
|
|
4
|
+
needsParenthesisWhenIndexed: boolean;
|
|
5
|
+
needsParenthesisInKeyof: boolean;
|
|
6
|
+
readonly variants: VariantType[];
|
|
7
|
+
constructor(firstType: VariantType);
|
|
8
|
+
addVariant(variant: VariantType): this;
|
|
9
|
+
addVariants(variants: VariantType[]): this;
|
|
10
|
+
write(writer: Writer): void;
|
|
11
|
+
mapVariants<NewVariantType extends TypeBuilder>(callback: (type: VariantType) => NewVariantType): UnionType<NewVariantType>;
|
|
12
|
+
}
|
|
13
|
+
export declare function unionType<VariantType extends TypeBuilder = TypeBuilder>(types: VariantType[] | VariantType): UnionType<VariantType>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var UnionType_exports = {};
|
|
20
|
+
__export(UnionType_exports, {
|
|
21
|
+
UnionType: () => UnionType,
|
|
22
|
+
unionType: () => unionType
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(UnionType_exports);
|
|
25
|
+
var import_TypeBuilder = require("./TypeBuilder");
|
|
26
|
+
class UnionType extends import_TypeBuilder.TypeBuilder {
|
|
27
|
+
needsParenthesisWhenIndexed = true;
|
|
28
|
+
needsParenthesisInKeyof = true;
|
|
29
|
+
variants;
|
|
30
|
+
constructor(firstType) {
|
|
31
|
+
super();
|
|
32
|
+
this.variants = [firstType];
|
|
33
|
+
}
|
|
34
|
+
addVariant(variant) {
|
|
35
|
+
this.variants.push(variant);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
addVariants(variants) {
|
|
39
|
+
for (const variant of variants) {
|
|
40
|
+
this.addVariant(variant);
|
|
41
|
+
}
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
write(writer) {
|
|
45
|
+
writer.writeJoined(" | ", this.variants, (variant, writer2) => {
|
|
46
|
+
if (variant.needsParenthesisInUnion) {
|
|
47
|
+
writer2.write("(").write(variant).write(")");
|
|
48
|
+
} else {
|
|
49
|
+
writer2.write(variant);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
mapVariants(callback) {
|
|
54
|
+
return unionType(this.variants.map((v) => callback(v)));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function unionType(types) {
|
|
58
|
+
if (Array.isArray(types)) {
|
|
59
|
+
if (types.length === 0) {
|
|
60
|
+
throw new TypeError("Union types array can not be empty");
|
|
61
|
+
}
|
|
62
|
+
const union = new UnionType(types[0]);
|
|
63
|
+
for (let i = 1; i < types.length; i++) {
|
|
64
|
+
union.addVariant(types[i]);
|
|
65
|
+
}
|
|
66
|
+
return union;
|
|
67
|
+
}
|
|
68
|
+
return new UnionType(types);
|
|
69
|
+
}
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
UnionType,
|
|
73
|
+
unionType
|
|
74
|
+
});
|