typia 4.1.4-dev.20230707-2 → 4.1.5-dev.20230711

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.
@@ -1,136 +1,136 @@
1
- import ts from "typescript";
2
-
3
- import { Metadata } from "../../../metadata/Metadata";
4
- import { MetadataObject } from "../../../metadata/MetadataObject";
5
- import { MetadataProperty } from "../../../metadata/MetadataProperty";
6
-
7
- import { Writable } from "../../../typings/Writable";
8
-
9
- import { ArrayUtil } from "../../../utils/ArrayUtil";
10
-
11
- import { CommentFactory } from "../../CommentFactory";
12
- import { MetadataCollection } from "../../MetadataCollection";
13
- import { MetadataFactory } from "../../MetadataFactory";
14
- import { MetadataHelper } from "./MetadataHelper";
15
- import { explore_metadata } from "./explore_metadata";
16
-
17
- export const emplace_metadata_object =
18
- (checker: ts.TypeChecker) =>
19
- (options: MetadataFactory.IOptions) =>
20
- (collection: MetadataCollection) =>
21
- (parent: ts.Type, nullable: boolean): MetadataObject => {
22
- // EMPLACE OBJECT
23
- const [obj, newbie] = collection.emplace(checker, parent);
24
- ArrayUtil.add(obj.nullables, nullable, (elem) => elem === nullable);
25
-
26
- if (newbie === false) return obj;
27
-
28
- // PREPARE ASSETS
29
- const isClass: boolean = parent.isClass();
30
- const pred: (node: ts.Declaration) => boolean = isClass
31
- ? (node) => {
32
- const kind: ts.SyntaxKind | undefined = node
33
- .getChildren()[0]
34
- ?.getChildren()[0]?.kind;
35
- return (
36
- kind !== ts.SyntaxKind.PrivateKeyword &&
37
- kind !== ts.SyntaxKind.ProtectedKeyword &&
38
- (ts.isParameter(node) || isProperty(node))
39
- );
40
- }
41
- : (node) => isProperty(node);
42
-
43
- const insert =
44
- (key: Metadata) =>
45
- (value: Metadata) =>
46
- (
47
- symbol: ts.Symbol | undefined,
48
- filter?: (doc: ts.JSDocTagInfo) => boolean,
49
- ): MetadataProperty => {
50
- // COMMENTS AND TAGS
51
- const description: string | null = symbol
52
- ? CommentFactory.description(symbol) ?? null
53
- : null;
54
- const jsDocTags: ts.JSDocTagInfo[] = (
55
- symbol?.getJsDocTags() ?? []
56
- ).filter(filter ?? (() => true));
57
-
58
- // THE PROPERTY
59
- const property = MetadataProperty.create({
60
- key,
61
- value,
62
- description,
63
- jsDocTags,
64
- tags: [],
65
- });
66
- obj.properties.push(property);
67
- return property;
68
- };
69
-
70
- //----
71
- // REGULAR PROPERTIES
72
- //----
73
- for (const prop of parent.getApparentProperties()) {
74
- // CHECK INTERNAL TAG
75
- if (
76
- (prop.getJsDocTags(checker) ?? []).find(
77
- (tag) => tag.name === "internal",
78
- ) !== undefined
79
- )
80
- continue;
81
-
82
- // CHECK NODE IS A FORMAL PROPERTY
83
- const [node, type] = (() => {
84
- const node = (prop.getDeclarations() ?? [])[0] as
85
- | ts.PropertyDeclaration
86
- | undefined;
87
- const type: ts.Type | undefined = node
88
- ? checker.getTypeOfSymbolAtLocation(prop, node)
89
- : "getTypeOfPropertyOfType" in checker
90
- ? (checker as any).getTypeOfPropertyOfType(
91
- parent,
92
- prop.name,
93
- )
94
- : undefined;
95
- return [node, type];
96
- })();
97
- if ((node && pred(node) === false) || type === undefined) continue;
98
-
99
- // GET EXACT TYPE
100
- const key: Metadata = MetadataHelper.literal_to_metadata(prop.name);
101
- const value: Metadata = explore_metadata(checker)(options)(
102
- collection,
103
- )(type, false);
104
-
105
- // OPTIONAL, BUT CAN BE RQUIRED BY `Required<T>` TYPE
106
- if (node?.questionToken) Writable(value).optional = true;
107
- insert(key)(value)(prop);
108
- }
109
-
110
- //----
111
- // DYNAMIC PROPERTIES
112
- //----
113
- for (const index of checker.getIndexInfosOfType(parent)) {
114
- // GET EXACT TYPE
115
- const analyzer = (type: ts.Type) =>
116
- explore_metadata(checker)(options)(collection)(type, false);
117
- const key: Metadata = analyzer(index.keyType);
118
- const value: Metadata = analyzer(index.type);
119
-
120
- // INSERT WITH REQUIRED CONFIGURATION
121
- insert(key)(value)(
122
- index.declaration?.parent
123
- ? checker.getSymbolAtLocation(index.declaration.parent)
124
- : undefined,
125
- (doc) => doc.name !== "default",
126
- );
127
- }
128
-
129
- return obj;
130
- };
131
-
132
- const isProperty = (node: ts.Declaration) =>
133
- ts.isPropertyDeclaration(node) ||
134
- ts.isPropertyAssignment(node) ||
135
- ts.isPropertySignature(node) ||
136
- ts.isTypeLiteralNode(node);
1
+ import ts from "typescript";
2
+
3
+ import { Metadata } from "../../../metadata/Metadata";
4
+ import { MetadataObject } from "../../../metadata/MetadataObject";
5
+ import { MetadataProperty } from "../../../metadata/MetadataProperty";
6
+
7
+ import { Writable } from "../../../typings/Writable";
8
+
9
+ import { ArrayUtil } from "../../../utils/ArrayUtil";
10
+
11
+ import { CommentFactory } from "../../CommentFactory";
12
+ import { MetadataCollection } from "../../MetadataCollection";
13
+ import { MetadataFactory } from "../../MetadataFactory";
14
+ import { MetadataHelper } from "./MetadataHelper";
15
+ import { explore_metadata } from "./explore_metadata";
16
+
17
+ export const emplace_metadata_object =
18
+ (checker: ts.TypeChecker) =>
19
+ (options: MetadataFactory.IOptions) =>
20
+ (collection: MetadataCollection) =>
21
+ (parent: ts.Type, nullable: boolean): MetadataObject => {
22
+ // EMPLACE OBJECT
23
+ const [obj, newbie] = collection.emplace(checker, parent);
24
+ ArrayUtil.add(obj.nullables, nullable, (elem) => elem === nullable);
25
+
26
+ if (newbie === false) return obj;
27
+
28
+ // PREPARE ASSETS
29
+ const isClass: boolean = parent.isClass();
30
+ const pred: (node: ts.Declaration) => boolean = isClass
31
+ ? (node) => {
32
+ const kind: ts.SyntaxKind | undefined = node
33
+ .getChildren()[0]
34
+ ?.getChildren()[0]?.kind;
35
+ return (
36
+ kind !== ts.SyntaxKind.PrivateKeyword &&
37
+ kind !== ts.SyntaxKind.ProtectedKeyword &&
38
+ (ts.isParameter(node) || isProperty(node))
39
+ );
40
+ }
41
+ : (node) => isProperty(node);
42
+
43
+ const insert =
44
+ (key: Metadata) =>
45
+ (value: Metadata) =>
46
+ (
47
+ symbol: ts.Symbol | undefined,
48
+ filter?: (doc: ts.JSDocTagInfo) => boolean,
49
+ ): MetadataProperty => {
50
+ // COMMENTS AND TAGS
51
+ const description: string | null = symbol
52
+ ? CommentFactory.description(symbol) ?? null
53
+ : null;
54
+ const jsDocTags: ts.JSDocTagInfo[] = (
55
+ symbol?.getJsDocTags() ?? []
56
+ ).filter(filter ?? (() => true));
57
+
58
+ // THE PROPERTY
59
+ const property = MetadataProperty.create({
60
+ key,
61
+ value,
62
+ description,
63
+ jsDocTags,
64
+ tags: [],
65
+ });
66
+ obj.properties.push(property);
67
+ return property;
68
+ };
69
+
70
+ //----
71
+ // REGULAR PROPERTIES
72
+ //----
73
+ for (const prop of parent.getApparentProperties()) {
74
+ // CHECK INTERNAL TAG
75
+ if (
76
+ (prop.getJsDocTags(checker) ?? []).find(
77
+ (tag) => tag.name === "internal",
78
+ ) !== undefined
79
+ )
80
+ continue;
81
+
82
+ // CHECK NODE IS A FORMAL PROPERTY
83
+ const [node, type] = (() => {
84
+ const node = (prop.getDeclarations() ?? [])[0] as
85
+ | ts.PropertyDeclaration
86
+ | undefined;
87
+ const type: ts.Type | undefined = node
88
+ ? checker.getTypeOfSymbolAtLocation(prop, node)
89
+ : "getTypeOfPropertyOfType" in checker
90
+ ? (checker as any).getTypeOfPropertyOfType(
91
+ parent,
92
+ prop.name,
93
+ )
94
+ : undefined;
95
+ return [node, type];
96
+ })();
97
+ if ((node && pred(node) === false) || type === undefined) continue;
98
+
99
+ // GET EXACT TYPE
100
+ const key: Metadata = MetadataHelper.literal_to_metadata(prop.name);
101
+ const value: Metadata = explore_metadata(checker)(options)(
102
+ collection,
103
+ )(type, false);
104
+
105
+ // OPTIONAL, BUT CAN BE RQUIRED BY `Required<T>` TYPE
106
+ if (node?.questionToken) Writable(value).optional = true;
107
+ insert(key)(value)(prop);
108
+ }
109
+
110
+ //----
111
+ // DYNAMIC PROPERTIES
112
+ //----
113
+ for (const index of checker.getIndexInfosOfType(parent)) {
114
+ // GET EXACT TYPE
115
+ const analyzer = (type: ts.Type) =>
116
+ explore_metadata(checker)(options)(collection)(type, false);
117
+ const key: Metadata = analyzer(index.keyType);
118
+ const value: Metadata = analyzer(index.type);
119
+
120
+ // INSERT WITH REQUIRED CONFIGURATION
121
+ insert(key)(value)(
122
+ index.declaration?.parent
123
+ ? checker.getSymbolAtLocation(index.declaration.parent)
124
+ : undefined,
125
+ (doc) => doc.name !== "default",
126
+ );
127
+ }
128
+
129
+ return obj;
130
+ };
131
+
132
+ const isProperty = (node: ts.Declaration) =>
133
+ ts.isPropertyDeclaration(node) ||
134
+ ts.isPropertyAssignment(node) ||
135
+ ts.isPropertySignature(node) ||
136
+ ts.isTypeLiteralNode(node);
@@ -1,133 +1,133 @@
1
- import { Metadata } from "../../../metadata/Metadata";
2
- import { MetadataArray } from "../../../metadata/MetadataArray";
3
- import { MetadataObject } from "../../../metadata/MetadataObject";
4
- import { MetadataTuple } from "../../../metadata/MetadataTuple";
5
-
6
- import { MetadataCollection } from "../../MetadataCollection";
7
- import { iterate_metadata_tag } from "./iterate_metadata_tag";
8
-
9
- export const iterate_metadata_collection = (
10
- collection: MetadataCollection,
11
- ): void => {
12
- for (const array of collection.arrays())
13
- if (array.recursive === null)
14
- collection.setArrayRecursive(
15
- array,
16
- isArrayRecursive(new Set())(array)(array.value),
17
- );
18
- for (const tuple of collection.tuples())
19
- if (tuple.recursive === null) {
20
- const visited: Set<Metadata> = new Set();
21
- collection.setTupleRecursive(
22
- tuple,
23
- tuple.elements.some(isTupleRecursive(visited)(tuple)),
24
- );
25
- }
26
- for (const obj of collection.objects()) {
27
- iterate_metadata_tag(obj);
28
- if (obj.recursive === null) {
29
- const visited: Set<Metadata> = new Set();
30
- collection.setObjectRecursive(
31
- obj,
32
- obj.properties.some((p) =>
33
- isObjectRecursive(visited)(obj)(p.value),
34
- ),
35
- );
36
- }
37
- }
38
- };
39
-
40
- const isArrayRecursive =
41
- (visited: Set<Metadata>) =>
42
- (array: MetadataArray) =>
43
- (meta: Metadata): boolean => {
44
- if (visited.has(meta)) return false;
45
- visited.add(meta);
46
-
47
- return (
48
- meta.arrays.some(
49
- (a) => a === array || isArrayRecursive(visited)(array)(a.value),
50
- ) ||
51
- meta.aliases.some((d) =>
52
- isArrayRecursive(visited)(array)(d.value),
53
- ) ||
54
- meta.tuples.some(
55
- (t) =>
56
- !t.recursive &&
57
- t.elements.some((e) => isArrayRecursive(visited)(array)(e)),
58
- ) ||
59
- meta.maps.some((m) => isArrayRecursive(visited)(array)(m.value)) ||
60
- meta.sets.some((s) => isArrayRecursive(visited)(array)(s)) ||
61
- (meta.resolved !== null &&
62
- isArrayRecursive(visited)(array)(meta.resolved.returns)) ||
63
- (meta.rest !== null && isArrayRecursive(visited)(array)(meta.rest))
64
- );
65
- };
66
-
67
- const isTupleRecursive =
68
- (visited: Set<Metadata>) =>
69
- (tuple: MetadataTuple) =>
70
- (meta: Metadata): boolean => {
71
- if (visited.has(meta)) return false;
72
- visited.add(meta);
73
-
74
- return (
75
- meta.tuples.some(
76
- (t) =>
77
- t === tuple ||
78
- t.elements.some((e) => isTupleRecursive(visited)(tuple)(e)),
79
- ) ||
80
- meta.arrays.some(
81
- (a) =>
82
- !a.recursive && isTupleRecursive(visited)(tuple)(a.value),
83
- ) ||
84
- meta.maps.some((m) => isTupleRecursive(visited)(tuple)(m.value)) ||
85
- meta.sets.some((s) => isTupleRecursive(visited)(tuple)(s)) ||
86
- meta.aliases.some((d) =>
87
- isTupleRecursive(visited)(tuple)(d.value),
88
- ) ||
89
- (meta.resolved !== null &&
90
- isTupleRecursive(visited)(tuple)(meta.resolved.returns)) ||
91
- (meta.rest !== null && isTupleRecursive(visited)(tuple)(meta.rest))
92
- );
93
- };
94
-
95
- const isObjectRecursive =
96
- (visited: Set<Metadata>) =>
97
- (obj: MetadataObject) =>
98
- (meta: Metadata): boolean => {
99
- if (visited.has(meta)) return false;
100
-
101
- visited.add(meta);
102
- return (
103
- meta.objects.some(
104
- (o) =>
105
- obj === o ||
106
- o.properties.some((prop) =>
107
- isObjectRecursive(visited)(obj)(prop.value),
108
- ),
109
- ) ||
110
- meta.aliases.some((alias) =>
111
- isObjectRecursive(visited)(obj)(alias.value),
112
- ) ||
113
- meta.arrays.some(
114
- (array) =>
115
- !array.recursive &&
116
- isObjectRecursive(visited)(obj)(array.value),
117
- ) ||
118
- meta.tuples.some(
119
- (tuple) =>
120
- !tuple.recursive &&
121
- tuple.elements.some((elem) =>
122
- isObjectRecursive(visited)(obj)(elem),
123
- ),
124
- ) ||
125
- meta.maps.some((map) =>
126
- isObjectRecursive(visited)(obj)(map.value),
127
- ) ||
128
- meta.sets.some((value) => isObjectRecursive(visited)(obj)(value)) ||
129
- (meta.resolved !== null &&
130
- isObjectRecursive(visited)(obj)(meta.resolved.returns)) ||
131
- (meta.rest !== null && isObjectRecursive(visited)(obj)(meta.rest))
132
- );
133
- };
1
+ import { Metadata } from "../../../metadata/Metadata";
2
+ import { MetadataArray } from "../../../metadata/MetadataArray";
3
+ import { MetadataObject } from "../../../metadata/MetadataObject";
4
+ import { MetadataTuple } from "../../../metadata/MetadataTuple";
5
+
6
+ import { MetadataCollection } from "../../MetadataCollection";
7
+ import { iterate_metadata_tag } from "./iterate_metadata_tag";
8
+
9
+ export const iterate_metadata_collection = (
10
+ collection: MetadataCollection,
11
+ ): void => {
12
+ for (const array of collection.arrays())
13
+ if (array.recursive === null)
14
+ collection.setArrayRecursive(
15
+ array,
16
+ isArrayRecursive(new Set())(array)(array.value),
17
+ );
18
+ for (const tuple of collection.tuples())
19
+ if (tuple.recursive === null) {
20
+ const visited: Set<Metadata> = new Set();
21
+ collection.setTupleRecursive(
22
+ tuple,
23
+ tuple.elements.some(isTupleRecursive(visited)(tuple)),
24
+ );
25
+ }
26
+ for (const obj of collection.objects()) {
27
+ iterate_metadata_tag(obj);
28
+ if (obj.recursive === null) {
29
+ const visited: Set<Metadata> = new Set();
30
+ collection.setObjectRecursive(
31
+ obj,
32
+ obj.properties.some((p) =>
33
+ isObjectRecursive(visited)(obj)(p.value),
34
+ ),
35
+ );
36
+ }
37
+ }
38
+ };
39
+
40
+ const isArrayRecursive =
41
+ (visited: Set<Metadata>) =>
42
+ (array: MetadataArray) =>
43
+ (meta: Metadata): boolean => {
44
+ if (visited.has(meta)) return false;
45
+ visited.add(meta);
46
+
47
+ return (
48
+ meta.arrays.some(
49
+ (a) => a === array || isArrayRecursive(visited)(array)(a.value),
50
+ ) ||
51
+ meta.aliases.some((d) =>
52
+ isArrayRecursive(visited)(array)(d.value),
53
+ ) ||
54
+ meta.tuples.some(
55
+ (t) =>
56
+ !t.recursive &&
57
+ t.elements.some((e) => isArrayRecursive(visited)(array)(e)),
58
+ ) ||
59
+ meta.maps.some((m) => isArrayRecursive(visited)(array)(m.value)) ||
60
+ meta.sets.some((s) => isArrayRecursive(visited)(array)(s)) ||
61
+ (meta.resolved !== null &&
62
+ isArrayRecursive(visited)(array)(meta.resolved.returns)) ||
63
+ (meta.rest !== null && isArrayRecursive(visited)(array)(meta.rest))
64
+ );
65
+ };
66
+
67
+ const isTupleRecursive =
68
+ (visited: Set<Metadata>) =>
69
+ (tuple: MetadataTuple) =>
70
+ (meta: Metadata): boolean => {
71
+ if (visited.has(meta)) return false;
72
+ visited.add(meta);
73
+
74
+ return (
75
+ meta.tuples.some(
76
+ (t) =>
77
+ t === tuple ||
78
+ t.elements.some((e) => isTupleRecursive(visited)(tuple)(e)),
79
+ ) ||
80
+ meta.arrays.some(
81
+ (a) =>
82
+ !a.recursive && isTupleRecursive(visited)(tuple)(a.value),
83
+ ) ||
84
+ meta.maps.some((m) => isTupleRecursive(visited)(tuple)(m.value)) ||
85
+ meta.sets.some((s) => isTupleRecursive(visited)(tuple)(s)) ||
86
+ meta.aliases.some((d) =>
87
+ isTupleRecursive(visited)(tuple)(d.value),
88
+ ) ||
89
+ (meta.resolved !== null &&
90
+ isTupleRecursive(visited)(tuple)(meta.resolved.returns)) ||
91
+ (meta.rest !== null && isTupleRecursive(visited)(tuple)(meta.rest))
92
+ );
93
+ };
94
+
95
+ const isObjectRecursive =
96
+ (visited: Set<Metadata>) =>
97
+ (obj: MetadataObject) =>
98
+ (meta: Metadata): boolean => {
99
+ if (visited.has(meta)) return false;
100
+
101
+ visited.add(meta);
102
+ return (
103
+ meta.objects.some(
104
+ (o) =>
105
+ obj === o ||
106
+ o.properties.some((prop) =>
107
+ isObjectRecursive(visited)(obj)(prop.value),
108
+ ),
109
+ ) ||
110
+ meta.aliases.some((alias) =>
111
+ isObjectRecursive(visited)(obj)(alias.value),
112
+ ) ||
113
+ meta.arrays.some(
114
+ (array) =>
115
+ !array.recursive &&
116
+ isObjectRecursive(visited)(obj)(array.value),
117
+ ) ||
118
+ meta.tuples.some(
119
+ (tuple) =>
120
+ !tuple.recursive &&
121
+ tuple.elements.some((elem) =>
122
+ isObjectRecursive(visited)(obj)(elem),
123
+ ),
124
+ ) ||
125
+ meta.maps.some((map) =>
126
+ isObjectRecursive(visited)(obj)(map.value),
127
+ ) ||
128
+ meta.sets.some((value) => isObjectRecursive(visited)(obj)(value)) ||
129
+ (meta.resolved !== null &&
130
+ isObjectRecursive(visited)(obj)(meta.resolved.returns)) ||
131
+ (meta.rest !== null && isObjectRecursive(visited)(obj)(meta.rest))
132
+ );
133
+ };
@@ -1,31 +1,31 @@
1
- import { MetadataObject } from "../../../metadata/MetadataObject";
2
-
3
- import { Writable } from "../../../typings/Writable";
4
-
5
- import { Escaper } from "../../../utils/Escaper";
6
-
7
- import { MetadataTagFactory } from "../../MetadataTagFactory";
8
-
9
- export const iterate_metadata_tag = (obj: MetadataObject) => {
10
- if (obj.tagged_ === true) return;
11
- obj.tagged_ = true;
12
-
13
- for (const prop of obj.properties) {
14
- const key: string = prop.key.getName();
15
- const variable: string | null =
16
- key.length >= 3 &&
17
- key[0] === '"' &&
18
- key[key.length - 1] === '"' &&
19
- Escaper.variable(key.slice(1, -1))
20
- ? key.slice(1, -1)
21
- : null;
22
-
23
- Writable(prop).tags = MetadataTagFactory.generate(prop.value)(
24
- prop.jsDocTags,
25
- )(
26
- variable !== null
27
- ? () => `${obj.name}.${variable}`
28
- : () => `${obj.name}[${key}]`,
29
- );
30
- }
31
- };
1
+ import { MetadataObject } from "../../../metadata/MetadataObject";
2
+
3
+ import { Writable } from "../../../typings/Writable";
4
+
5
+ import { Escaper } from "../../../utils/Escaper";
6
+
7
+ import { MetadataTagFactory } from "../../MetadataTagFactory";
8
+
9
+ export const iterate_metadata_tag = (obj: MetadataObject) => {
10
+ if (obj.tagged_ === true) return;
11
+ obj.tagged_ = true;
12
+
13
+ for (const prop of obj.properties) {
14
+ const key: string = prop.key.getName();
15
+ const variable: string | null =
16
+ key.length >= 3 &&
17
+ key[0] === '"' &&
18
+ key[key.length - 1] === '"' &&
19
+ Escaper.variable(key.slice(1, -1))
20
+ ? key.slice(1, -1)
21
+ : null;
22
+
23
+ Writable(prop).tags = MetadataTagFactory.generate(prop.value)(
24
+ prop.jsDocTags,
25
+ )(
26
+ variable !== null
27
+ ? () => `${obj.name}.${variable}`
28
+ : () => `${obj.name}[${key}]`,
29
+ );
30
+ }
31
+ };