@prisma/ts-builders 6.6.0-dev.100
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 +16 -0
- package/dist/ConstDeclaration.js +60 -0
- package/dist/ConstDeclaration.mjs +35 -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 +34 -0
- package/dist/ExportFrom.js +106 -0
- package/dist/ExportFrom.mjs +77 -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/FunctionCall.d.ts +9 -0
- package/dist/FunctionCall.js +49 -0
- package/dist/FunctionCall.mjs +24 -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 +38 -0
- package/dist/Import.js +127 -0
- package/dist/Import.mjs +98 -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/NamedValue.d.ts +8 -0
- package/dist/NamedValue.js +43 -0
- package/dist/NamedValue.mjs +18 -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 +15 -0
- package/dist/StringLiteralType.js +58 -0
- package/dist/StringLiteralType.mjs +32 -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/ValueBuilder.d.ts +12 -0
- package/dist/ValueBuilder.js +46 -0
- package/dist/ValueBuilder.mjs +21 -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 +3 -0
- package/dist/helpers.js +31 -0
- package/dist/helpers.mjs +7 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +90 -0
- package/dist/index.mjs +35 -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,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
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { TypeBuilder } from "./TypeBuilder";
|
|
2
|
+
class UnionType extends TypeBuilder {
|
|
3
|
+
needsParenthesisWhenIndexed = true;
|
|
4
|
+
needsParenthesisInKeyof = true;
|
|
5
|
+
variants;
|
|
6
|
+
constructor(firstType) {
|
|
7
|
+
super();
|
|
8
|
+
this.variants = [firstType];
|
|
9
|
+
}
|
|
10
|
+
addVariant(variant) {
|
|
11
|
+
this.variants.push(variant);
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
addVariants(variants) {
|
|
15
|
+
for (const variant of variants) {
|
|
16
|
+
this.addVariant(variant);
|
|
17
|
+
}
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
write(writer) {
|
|
21
|
+
writer.writeJoined(" | ", this.variants, (variant, writer2) => {
|
|
22
|
+
if (variant.needsParenthesisInUnion) {
|
|
23
|
+
writer2.write("(").write(variant).write(")");
|
|
24
|
+
} else {
|
|
25
|
+
writer2.write(variant);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
mapVariants(callback) {
|
|
30
|
+
return unionType(this.variants.map((v) => callback(v)));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function unionType(types) {
|
|
34
|
+
if (Array.isArray(types)) {
|
|
35
|
+
if (types.length === 0) {
|
|
36
|
+
throw new TypeError("Union types array can not be empty");
|
|
37
|
+
}
|
|
38
|
+
const union = new UnionType(types[0]);
|
|
39
|
+
for (let i = 1; i < types.length; i++) {
|
|
40
|
+
union.addVariant(types[i]);
|
|
41
|
+
}
|
|
42
|
+
return union;
|
|
43
|
+
}
|
|
44
|
+
return new UnionType(types);
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
UnionType,
|
|
48
|
+
unionType
|
|
49
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var import_vitest = require("vitest");
|
|
3
|
+
var import_ArrayType = require("./ArrayType");
|
|
4
|
+
var import_FunctionType = require("./FunctionType");
|
|
5
|
+
var import_NamedType = require("./NamedType");
|
|
6
|
+
var import_stringify = require("./stringify");
|
|
7
|
+
var import_UnionType = require("./UnionType");
|
|
8
|
+
const A = (0, import_NamedType.namedType)("A");
|
|
9
|
+
const B = (0, import_NamedType.namedType)("B");
|
|
10
|
+
(0, import_vitest.test)("one type", () => {
|
|
11
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)(A))).toMatchInlineSnapshot(`"A"`);
|
|
12
|
+
});
|
|
13
|
+
(0, import_vitest.test)("multiple types", () => {
|
|
14
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)(A).addVariant(B))).toMatchInlineSnapshot(`"A | B"`);
|
|
15
|
+
});
|
|
16
|
+
(0, import_vitest.test)("from array", () => {
|
|
17
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)([A, B]))).toMatchInlineSnapshot(`"A | B"`);
|
|
18
|
+
});
|
|
19
|
+
(0, import_vitest.test)("with function type", () => {
|
|
20
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)([A, B, (0, import_FunctionType.functionType)()]))).toMatchInlineSnapshot(`"A | B | (() => void)"`);
|
|
21
|
+
});
|
|
22
|
+
(0, import_vitest.test)("fails with empty array", () => {
|
|
23
|
+
(0, import_vitest.expect)(() => (0, import_UnionType.unionType)([])).toThrowErrorMatchingInlineSnapshot(`[TypeError: Union types array can not be empty]`);
|
|
24
|
+
});
|
|
25
|
+
(0, import_vitest.test)("mapVariants", () => {
|
|
26
|
+
const union = (0, import_UnionType.unionType)([A, B]).mapVariants(import_ArrayType.array);
|
|
27
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(union)).toMatchInlineSnapshot(`"A[] | B[]"`);
|
|
28
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { array } from "./ArrayType";
|
|
3
|
+
import { functionType } from "./FunctionType";
|
|
4
|
+
import { namedType } from "./NamedType";
|
|
5
|
+
import { stringify } from "./stringify";
|
|
6
|
+
import { unionType } from "./UnionType";
|
|
7
|
+
const A = namedType("A");
|
|
8
|
+
const B = namedType("B");
|
|
9
|
+
test("one type", () => {
|
|
10
|
+
expect(stringify(unionType(A))).toMatchInlineSnapshot(`"A"`);
|
|
11
|
+
});
|
|
12
|
+
test("multiple types", () => {
|
|
13
|
+
expect(stringify(unionType(A).addVariant(B))).toMatchInlineSnapshot(`"A | B"`);
|
|
14
|
+
});
|
|
15
|
+
test("from array", () => {
|
|
16
|
+
expect(stringify(unionType([A, B]))).toMatchInlineSnapshot(`"A | B"`);
|
|
17
|
+
});
|
|
18
|
+
test("with function type", () => {
|
|
19
|
+
expect(stringify(unionType([A, B, functionType()]))).toMatchInlineSnapshot(`"A | B | (() => void)"`);
|
|
20
|
+
});
|
|
21
|
+
test("fails with empty array", () => {
|
|
22
|
+
expect(() => unionType([])).toThrowErrorMatchingInlineSnapshot(`[TypeError: Union types array can not be empty]`);
|
|
23
|
+
});
|
|
24
|
+
test("mapVariants", () => {
|
|
25
|
+
const union = unionType([A, B]).mapVariants(array);
|
|
26
|
+
expect(stringify(union)).toMatchInlineSnapshot(`"A[] | B[]"`);
|
|
27
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
import { TypeBuilder } from './TypeBuilder';
|
|
3
|
+
import { Writer } from './Writer';
|
|
4
|
+
export declare abstract class ValueBuilder implements BasicBuilder {
|
|
5
|
+
as(type: TypeBuilder): TypeAssertion;
|
|
6
|
+
abstract write(writer: Writer<undefined>): void;
|
|
7
|
+
}
|
|
8
|
+
export declare class TypeAssertion extends ValueBuilder {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(value: ValueBuilder, type: TypeBuilder);
|
|
11
|
+
write(writer: Writer): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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 ValueBuilder_exports = {};
|
|
20
|
+
__export(ValueBuilder_exports, {
|
|
21
|
+
TypeAssertion: () => TypeAssertion,
|
|
22
|
+
ValueBuilder: () => ValueBuilder
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(ValueBuilder_exports);
|
|
25
|
+
class ValueBuilder {
|
|
26
|
+
as(type) {
|
|
27
|
+
return new TypeAssertion(this, type);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
class TypeAssertion extends ValueBuilder {
|
|
31
|
+
#value;
|
|
32
|
+
#type;
|
|
33
|
+
constructor(value, type) {
|
|
34
|
+
super();
|
|
35
|
+
this.#value = value;
|
|
36
|
+
this.#type = type;
|
|
37
|
+
}
|
|
38
|
+
write(writer) {
|
|
39
|
+
writer.write(this.#value).write(" as ").write(this.#type);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
TypeAssertion,
|
|
45
|
+
ValueBuilder
|
|
46
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class ValueBuilder {
|
|
2
|
+
as(type) {
|
|
3
|
+
return new TypeAssertion(this, type);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
class TypeAssertion extends ValueBuilder {
|
|
7
|
+
#value;
|
|
8
|
+
#type;
|
|
9
|
+
constructor(value, type) {
|
|
10
|
+
super();
|
|
11
|
+
this.#value = value;
|
|
12
|
+
this.#type = type;
|
|
13
|
+
}
|
|
14
|
+
write(writer) {
|
|
15
|
+
writer.write(this.#value).write(" as ").write(this.#type);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
TypeAssertion,
|
|
20
|
+
ValueBuilder
|
|
21
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
import { Writer } from './Writer';
|
|
3
|
+
export declare class WellKnownSymbol implements BasicBuilder {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
constructor(name: string);
|
|
6
|
+
write(writer: Writer<undefined>): void;
|
|
7
|
+
}
|
|
8
|
+
export declare function wellKnownSymbol(name: string): WellKnownSymbol;
|
|
9
|
+
export declare const toStringTag: WellKnownSymbol;
|
|
@@ -0,0 +1,43 @@
|
|
|
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 WellKnownSymbol_exports = {};
|
|
20
|
+
__export(WellKnownSymbol_exports, {
|
|
21
|
+
WellKnownSymbol: () => WellKnownSymbol,
|
|
22
|
+
toStringTag: () => toStringTag,
|
|
23
|
+
wellKnownSymbol: () => wellKnownSymbol
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(WellKnownSymbol_exports);
|
|
26
|
+
class WellKnownSymbol {
|
|
27
|
+
constructor(name) {
|
|
28
|
+
this.name = name;
|
|
29
|
+
}
|
|
30
|
+
write(writer) {
|
|
31
|
+
writer.write("Symbol.").write(this.name);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function wellKnownSymbol(name) {
|
|
35
|
+
return new WellKnownSymbol(name);
|
|
36
|
+
}
|
|
37
|
+
const toStringTag = wellKnownSymbol("toStringTag");
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
WellKnownSymbol,
|
|
41
|
+
toStringTag,
|
|
42
|
+
wellKnownSymbol
|
|
43
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class WellKnownSymbol {
|
|
2
|
+
constructor(name) {
|
|
3
|
+
this.name = name;
|
|
4
|
+
}
|
|
5
|
+
write(writer) {
|
|
6
|
+
writer.write("Symbol.").write(this.name);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function wellKnownSymbol(name) {
|
|
10
|
+
return new WellKnownSymbol(name);
|
|
11
|
+
}
|
|
12
|
+
const toStringTag = wellKnownSymbol("toStringTag");
|
|
13
|
+
export {
|
|
14
|
+
WellKnownSymbol,
|
|
15
|
+
toStringTag,
|
|
16
|
+
wellKnownSymbol
|
|
17
|
+
};
|
package/dist/Writer.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
export declare const INDENT_SIZE = 2;
|
|
3
|
+
/**
|
|
4
|
+
* Helper class for building long multi-line formatted strings from building blocks.
|
|
5
|
+
* Can use either plain strings or `Builder` objects, that would encapsulate formatting logic.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Writer<ContextType = undefined> {
|
|
8
|
+
readonly context: ContextType;
|
|
9
|
+
private lines;
|
|
10
|
+
private currentLine;
|
|
11
|
+
private currentIndent;
|
|
12
|
+
private marginSymbol?;
|
|
13
|
+
private afterNextNewLineCallback?;
|
|
14
|
+
constructor(startingIndent: number | undefined, context: ContextType);
|
|
15
|
+
/**
|
|
16
|
+
* Adds provided value to the current line. Does not end the line.
|
|
17
|
+
*
|
|
18
|
+
* @param value
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
write(value: string | BasicBuilder<ContextType>): this;
|
|
22
|
+
/**
|
|
23
|
+
* Adds several `values` to the current line, separated by `separator`. Both values and separator
|
|
24
|
+
* can also be `Builder` instances for more advanced formatting.
|
|
25
|
+
*
|
|
26
|
+
* @param separator
|
|
27
|
+
* @param values
|
|
28
|
+
* @param writeItem allow to customize how individual item is written
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
writeJoined<T extends string | BasicBuilder<ContextType>>(separator: string | BasicBuilder<ContextType>, values: T[], writeItem?: (item: T, writer: this) => void): this;
|
|
32
|
+
/**
|
|
33
|
+
* Adds a string to current line, flushes current line and starts a new line.
|
|
34
|
+
* @param line
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
writeLine(line: string | BasicBuilder<ContextType>): this;
|
|
38
|
+
/**
|
|
39
|
+
* Flushes current line and starts a new line. New line starts at previously configured indentation level
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
newLine(): this;
|
|
43
|
+
/**
|
|
44
|
+
* Increases indentation level by 1, calls provided callback and then decreases indentation again.
|
|
45
|
+
* Could be used for writing indented blocks of text:
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* writer
|
|
50
|
+
* .writeLine('{')
|
|
51
|
+
* .withIndent(() => {
|
|
52
|
+
* writer.writeLine('foo: 123');
|
|
53
|
+
* writer.writeLine('bar: 456');
|
|
54
|
+
* })
|
|
55
|
+
* .writeLine('}')
|
|
56
|
+
* ```
|
|
57
|
+
* @param callback
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
withIndent(callback: (writer: this) => void): this;
|
|
61
|
+
/**
|
|
62
|
+
* Calls provided callback next time when new line is started.
|
|
63
|
+
* Callback is called after old line have already been flushed and a new
|
|
64
|
+
* line have been started. Can be used for adding "between the lines" decorations,
|
|
65
|
+
* such as underlines.
|
|
66
|
+
*
|
|
67
|
+
* @param callback
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
afterNextNewline(callback: () => void): this;
|
|
71
|
+
/**
|
|
72
|
+
* Increases indentation level of the current line by 1
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
indent(): this;
|
|
76
|
+
/**
|
|
77
|
+
* Decreases indentation level of the current line by 1, if it is possible
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
unindent(): this;
|
|
81
|
+
/**
|
|
82
|
+
* Adds a symbol, that will replace the first character of the current line (including indentation)
|
|
83
|
+
* when it is flushed. Can be used for adding markers to the line.
|
|
84
|
+
*
|
|
85
|
+
* Note: if indentation level of the line is 0, it will replace the first actually printed character
|
|
86
|
+
* of the line. Use with caution.
|
|
87
|
+
* @param symbol
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
addMarginSymbol(symbol: string): this;
|
|
91
|
+
toString(): string;
|
|
92
|
+
getCurrentLineLength(): number;
|
|
93
|
+
private indentedCurrentLine;
|
|
94
|
+
}
|