typia 4.1.6-dev.20230213 → 5.0.0-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.
- package/README.md +9 -1
- package/lib/factories/MetadataTagFactory.js +11 -4
- package/lib/factories/MetadataTagFactory.js.map +1 -1
- package/lib/factories/ProtocolFactory.d.ts +8 -0
- package/lib/factories/ProtocolFactory.js +113 -0
- package/lib/factories/ProtocolFactory.js.map +1 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.d.ts +16 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.js +173 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.js.map +1 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.d.ts +3 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.js +47 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.js.map +1 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.d.ts +4 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.js +20 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.js +69 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.d.ts +2 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.js +30 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.js +17 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.d.ts +4 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.js +75 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.d.ts +5 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.js +190 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.d.ts +2 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.js +33 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.d.ts +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.js +6 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.d.ts +4 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.js +157 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.d.ts +5 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.js +25 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.js +46 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.js.map +1 -0
- package/lib/functional/$proto_bytes.d.ts +2 -0
- package/lib/functional/$proto_bytes.js +37 -0
- package/lib/functional/$proto_bytes.js.map +1 -0
- package/lib/functional/$proto_field.d.ts +10 -0
- package/lib/functional/$proto_field.js +42 -0
- package/lib/functional/$proto_field.js.map +1 -0
- package/lib/functional/$proto_float.d.ts +4 -0
- package/lib/functional/$proto_float.js +28 -0
- package/lib/functional/$proto_float.js.map +1 -0
- package/lib/functional/$proto_i32.d.ts +2 -0
- package/lib/functional/$proto_i32.js +23 -0
- package/lib/functional/$proto_i32.js.map +1 -0
- package/lib/functional/$proto_i64.d.ts +2 -0
- package/lib/functional/$proto_i64.js +31 -0
- package/lib/functional/$proto_i64.js.map +1 -0
- package/lib/functional/$proto_size.d.ts +6 -0
- package/lib/functional/$proto_size.js +76 -0
- package/lib/functional/$proto_size.js.map +1 -0
- package/lib/functional/$proto_string.d.ts +2 -0
- package/lib/functional/$proto_string.js +34 -0
- package/lib/functional/$proto_string.js.map +1 -0
- package/lib/functional/$varint.d.ts +6 -0
- package/lib/functional/$varint.js +99 -0
- package/lib/functional/$varint.js.map +1 -0
- package/lib/functional/$zigzag.d.ts +4 -0
- package/lib/functional/$zigzag.js +34 -0
- package/lib/functional/$zigzag.js.map +1 -0
- package/lib/messages/IProtocolMap.d.ts +5 -0
- package/lib/messages/IProtocolMap.js +3 -0
- package/lib/messages/IProtocolMap.js.map +1 -0
- package/lib/messages/IProtocolMessage.d.ts +5 -0
- package/lib/messages/IProtocolMessage.js +3 -0
- package/lib/messages/IProtocolMessage.js.map +1 -0
- package/lib/messages/IProtocolProperty.d.ts +12 -0
- package/lib/messages/IProtocolProperty.js +3 -0
- package/lib/messages/IProtocolProperty.js.map +1 -0
- package/lib/metadata/IMetadataTag.d.ts +3 -3
- package/lib/metadata/Metadata.js.map +1 -1
- package/lib/module.d.ts +2 -0
- package/lib/module.js +6 -1
- package/lib/module.js.map +1 -1
- package/lib/programmers/MessageProgrammer.d.ts +5 -0
- package/lib/programmers/MessageProgrammer.js +141 -0
- package/lib/programmers/MessageProgrammer.js.map +1 -0
- package/lib/programmers/internal/application_number.js +8 -5
- package/lib/programmers/internal/application_number.js.map +1 -1
- package/lib/transformers/CallExpressionTransformer.js +5 -3
- package/lib/transformers/CallExpressionTransformer.js.map +1 -1
- package/lib/transformers/features/miscellaneous/ApplicationTransformer.js +1 -3
- package/lib/transformers/features/miscellaneous/ApplicationTransformer.js.map +1 -1
- package/lib/transformers/features/protocols/MessageTransformer.d.ts +5 -0
- package/lib/transformers/features/protocols/MessageTransformer.js +20 -0
- package/lib/transformers/features/protocols/MessageTransformer.js.map +1 -0
- package/lib/utils/NameEncoder.d.ts +4 -0
- package/lib/utils/NameEncoder.js +89 -0
- package/lib/utils/NameEncoder.js.map +1 -0
- package/package.json +1 -1
- package/src/factories/MetadataTagFactory.ts +10 -3
- package/src/factories/ProtocolFactory.ts +80 -0
- package/src/factories/internal/protocols/ProtocolMetadataUtil.ts +180 -0
- package/src/factories/internal/protocols/emplace_protocol_object.ts +25 -0
- package/src/factories/internal/protocols/emplace_protocol_property.ts +12 -0
- package/src/factories/internal/protocols/iterate_protocol_atomic.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_constant.ts +27 -0
- package/src/factories/internal/protocols/iterate_protocol_main.ts +19 -0
- package/src/factories/internal/protocols/iterate_protocol_map.ts +58 -0
- package/src/factories/internal/protocols/iterate_protocol_metadata.ts +96 -0
- package/src/factories/internal/protocols/iterate_protocol_native.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_never.ts +1 -0
- package/src/factories/internal/protocols/iterate_protocol_object.ts +110 -0
- package/src/factories/internal/protocols/iterate_protocol_repeated.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_tuple.ts +29 -0
- package/src/functional/$proto_bytes.ts +25 -0
- package/src/functional/$proto_field.ts +30 -0
- package/src/functional/$proto_float.ts +37 -0
- package/src/functional/$proto_i32.ts +29 -0
- package/src/functional/$proto_i64.ts +37 -0
- package/src/functional/$proto_size.ts +82 -0
- package/src/functional/$proto_string.ts +24 -0
- package/src/functional/$varint.ts +130 -0
- package/src/functional/$zigzag.ts +39 -0
- package/src/messages/IProtocolMap.ts +5 -0
- package/src/messages/IProtocolMessage.ts +6 -0
- package/src/messages/IProtocolProperty.ts +13 -0
- package/src/metadata/IMetadataTag.ts +11 -3
- package/src/metadata/Metadata.ts +3 -0
- package/src/module.ts +11 -0
- package/src/programmers/MessageProgrammer.ts +126 -0
- package/src/programmers/internal/application_number.ts +10 -7
- package/src/transformers/CallExpressionTransformer.ts +17 -10
- package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +1 -3
- package/src/transformers/features/protocols/MessageTransformer.ts +31 -0
- package/src/utils/Escaper.ts +46 -46
- package/src/utils/NameEncoder.ts +32 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { MetadataCollection } from "../factories/MetadataCollection";
|
|
4
|
+
import { ProtocolFactory } from "../factories/ProtocolFactory";
|
|
5
|
+
|
|
6
|
+
import { Metadata } from "../metadata/Metadata";
|
|
7
|
+
|
|
8
|
+
import { IProject } from "../transformers/IProject";
|
|
9
|
+
|
|
10
|
+
import { Escaper } from "../utils/Escaper";
|
|
11
|
+
import { MapUtil } from "../utils/MapUtil";
|
|
12
|
+
import { NameEncoder } from "../utils/NameEncoder";
|
|
13
|
+
|
|
14
|
+
import { IProtocolMap } from "../messages/IProtocolMap";
|
|
15
|
+
import { IProtocolMessage } from "../messages/IProtocolMessage";
|
|
16
|
+
|
|
17
|
+
export namespace MessageProgrammer {
|
|
18
|
+
export const generate =
|
|
19
|
+
({ checker }: IProject) =>
|
|
20
|
+
(type: ts.Type) => {
|
|
21
|
+
// PARSE TARGET TYPE
|
|
22
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
23
|
+
const metadata: Metadata =
|
|
24
|
+
ProtocolFactory.metadata(checker)(collection)(type);
|
|
25
|
+
|
|
26
|
+
// CONVERT TO PROTOCOL MESSAGES
|
|
27
|
+
const dict: Map<string, IProtocolMessage> = new Map();
|
|
28
|
+
ProtocolFactory.generate(collection)(dict)(metadata);
|
|
29
|
+
|
|
30
|
+
// STRINGIFY
|
|
31
|
+
const hierarchies: Map<string, Hierarchy> = new Map();
|
|
32
|
+
for (const msg of dict.values()) emplace(hierarchies)(msg);
|
|
33
|
+
|
|
34
|
+
const content: string =
|
|
35
|
+
`syntax = "proto3";\n\n` +
|
|
36
|
+
[...hierarchies.values()]
|
|
37
|
+
.map((hier) => stringify(hier))
|
|
38
|
+
.join("\n\n");
|
|
39
|
+
|
|
40
|
+
// RETURNS
|
|
41
|
+
return ts.factory.createStringLiteral(content);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const stringify = (hierarchy: Hierarchy): string => {
|
|
45
|
+
const { key, message, children } = hierarchy;
|
|
46
|
+
let index: number = 1;
|
|
47
|
+
|
|
48
|
+
const elements: string[] = [`message ${NameEncoder.encode(key)} {`];
|
|
49
|
+
if (message !== null)
|
|
50
|
+
elements.push(
|
|
51
|
+
...message.properties.map((property) => {
|
|
52
|
+
const key: string =
|
|
53
|
+
Escaper.variable(property.key) &&
|
|
54
|
+
property.key.indexOf("$") === -1
|
|
55
|
+
? property.key
|
|
56
|
+
: `v${index + 1}`;
|
|
57
|
+
if (property.oneOf.length === 1)
|
|
58
|
+
return [
|
|
59
|
+
TAB,
|
|
60
|
+
property.required ? "" : "optional ",
|
|
61
|
+
property.repeated ? "repeated " : "",
|
|
62
|
+
getTypeName(property.oneOf[0]!.type) + " ",
|
|
63
|
+
key,
|
|
64
|
+
" = ",
|
|
65
|
+
`${index++};`,
|
|
66
|
+
].join("");
|
|
67
|
+
return (
|
|
68
|
+
`${TAB}oneof ${key} {\n` +
|
|
69
|
+
property.oneOf
|
|
70
|
+
.map(
|
|
71
|
+
(o, i) =>
|
|
72
|
+
`${TAB}${TAB}${getTypeName(
|
|
73
|
+
o.type,
|
|
74
|
+
)} ${key}_o${i} = ${index++};`,
|
|
75
|
+
)
|
|
76
|
+
.join("\n") +
|
|
77
|
+
`\n${TAB}}`
|
|
78
|
+
);
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
if (message !== null && children.size) elements.push("\n");
|
|
82
|
+
if (children.size)
|
|
83
|
+
elements.push(
|
|
84
|
+
[...children.values()]
|
|
85
|
+
.map((child) => stringify(child))
|
|
86
|
+
.map((body) =>
|
|
87
|
+
body
|
|
88
|
+
.split("\n")
|
|
89
|
+
.map((line) => `${TAB}${line}`)
|
|
90
|
+
.join("\n"),
|
|
91
|
+
)
|
|
92
|
+
.join("\n\n"),
|
|
93
|
+
);
|
|
94
|
+
elements.push("}");
|
|
95
|
+
|
|
96
|
+
return elements.join("\n");
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const emplace =
|
|
100
|
+
(dict: Map<string, Hierarchy>) => (message: IProtocolMessage) => {
|
|
101
|
+
const accessors: string[] = message.name.split(".");
|
|
102
|
+
accessors.forEach((access, i) => {
|
|
103
|
+
const hierarchy: Hierarchy = MapUtil.take(dict)(access, () => ({
|
|
104
|
+
key: access,
|
|
105
|
+
message: null!,
|
|
106
|
+
children: new Map(),
|
|
107
|
+
}));
|
|
108
|
+
dict = hierarchy.children;
|
|
109
|
+
if (i === accessors.length - 1) hierarchy.message = message;
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
function getTypeName(type: string | IProtocolMap): string {
|
|
114
|
+
return typeof type === "string"
|
|
115
|
+
? NameEncoder.encode(type)
|
|
116
|
+
: `map<${type.key}, ${getTypeName(type.value)}>`;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface Hierarchy {
|
|
121
|
+
key: string;
|
|
122
|
+
message: IProtocolMessage | null;
|
|
123
|
+
children: Map<string, Hierarchy>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const TAB = " ".repeat(4);
|
|
@@ -13,14 +13,17 @@ export const application_number = (
|
|
|
13
13
|
};
|
|
14
14
|
for (const tag of attribute["x-typia-metaTags"] ?? []) {
|
|
15
15
|
// CHECK TYPE
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
if (tag.kind === "type") {
|
|
17
|
+
if (
|
|
18
|
+
tag.value === "int" ||
|
|
19
19
|
tag.value === "uint" ||
|
|
20
|
-
tag.value === "
|
|
21
|
-
tag.value === "
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
tag.value === "int32" ||
|
|
21
|
+
tag.value === "uint32" ||
|
|
22
|
+
tag.value === "int64" ||
|
|
23
|
+
tag.value === "uint64"
|
|
24
|
+
)
|
|
25
|
+
output.type = "integer";
|
|
26
|
+
}
|
|
24
27
|
// RANGE TAG
|
|
25
28
|
else if (tag.kind === "minimum") output.minimum = tag.value;
|
|
26
29
|
else if (tag.kind === "maximum") output.maximum = tag.value;
|
|
@@ -29,6 +29,7 @@ import { CreateIsParseTransformer } from "./features/parsers/CreateIsParseTransf
|
|
|
29
29
|
import { CreateValidateParseTransformer } from "./features/parsers/CreateValidateParseTransformer";
|
|
30
30
|
import { IsParseTransformer } from "./features/parsers/IsParseTransformer";
|
|
31
31
|
import { ValidateParseTransformer } from "./features/parsers/ValidateParseTransformer";
|
|
32
|
+
import { MessageTransformer } from "./features/protocols/MessageTransformer";
|
|
32
33
|
import { AssertStringifyTransformer } from "./features/stringifiers/AssertStringifyTransformer";
|
|
33
34
|
import { CreateAssertStringifyTransformer } from "./features/stringifiers/CreateAssertStringifyTransformer";
|
|
34
35
|
import { CreateIsStringifyTransformer } from "./features/stringifiers/CreateIsStringifyTransformer";
|
|
@@ -99,7 +100,7 @@ const CLI_PATH = path.resolve(
|
|
|
99
100
|
|
|
100
101
|
const FUNCTORS: Record<string, () => Task> = {
|
|
101
102
|
//----
|
|
102
|
-
//
|
|
103
|
+
// VALIDATORS
|
|
103
104
|
//----
|
|
104
105
|
// BASIC VALIDATORS
|
|
105
106
|
assert: () => AssertTransformer.transform(false),
|
|
@@ -112,14 +113,24 @@ const FUNCTORS: Record<string, () => Task> = {
|
|
|
112
113
|
equals: () => IsTransformer.transform(true),
|
|
113
114
|
validateEquals: () => ValidateTransformer.transform(true),
|
|
114
115
|
|
|
115
|
-
|
|
116
|
+
//----
|
|
117
|
+
// PROTOCOL BUFFER
|
|
118
|
+
//----
|
|
119
|
+
message: () => MessageTransformer.transform,
|
|
120
|
+
|
|
121
|
+
//----
|
|
122
|
+
// JSON FUNCTIONS
|
|
123
|
+
//----
|
|
124
|
+
// SCHEMA DEFINITION
|
|
125
|
+
application: () => (project) => () =>
|
|
126
|
+
ApplicationTransformer.transform(project),
|
|
127
|
+
|
|
128
|
+
// PARSER FUNCTIONS
|
|
116
129
|
isParse: () => IsParseTransformer.transform,
|
|
117
130
|
assertParse: () => AssertParseTransformer.transform,
|
|
118
131
|
validateParse: () => ValidateParseTransformer.transform,
|
|
119
132
|
|
|
120
133
|
// STRINGIFY FUNCTIONS
|
|
121
|
-
application: () => (project) => () =>
|
|
122
|
-
ApplicationTransformer.transform(project),
|
|
123
134
|
stringify: () => StringifyTransformer.transform,
|
|
124
135
|
assertStringify: () => AssertStringifyTransformer.transform,
|
|
125
136
|
isStringify: () => IsStringifyTransformer.transform,
|
|
@@ -143,23 +154,19 @@ const FUNCTORS: Record<string, () => Task> = {
|
|
|
143
154
|
//----
|
|
144
155
|
// FACTORY FUNCTIONS
|
|
145
156
|
//----
|
|
146
|
-
//
|
|
157
|
+
// VALIDATORS
|
|
147
158
|
createAssert: () => CreateAssertTransformer.transform(false),
|
|
148
159
|
createAssertType: () => CreateAssertTransformer.transform(false),
|
|
149
160
|
createIs: () => CreateIsTransformer.transform(false),
|
|
150
161
|
createValidate: () => CreateValidateTransformer.transform(false),
|
|
151
|
-
|
|
152
|
-
// STRICT VALIDATORS
|
|
153
162
|
createAssertEquals: () => CreateAssertTransformer.transform(true),
|
|
154
163
|
createEquals: () => CreateIsTransformer.transform(true),
|
|
155
164
|
createValidateEquals: () => CreateValidateTransformer.transform(true),
|
|
156
165
|
|
|
157
|
-
//
|
|
166
|
+
// JSON FUNCTIONS
|
|
158
167
|
createIsParse: () => CreateIsParseTransformer.transform,
|
|
159
168
|
createAssertParse: () => CreateAssertParseTransformer.transform,
|
|
160
169
|
createValidateParse: () => CreateValidateParseTransformer.transform,
|
|
161
|
-
|
|
162
|
-
// STRINGIFY FUNCTIONS
|
|
163
170
|
createStringify: () => CreateStringifyTransformer.transform,
|
|
164
171
|
createAssertStringify: () => CreateAssertStringifyTransformer.transform,
|
|
165
172
|
createIsStringify: () => CreateIsStringifyTransformer.transform,
|
|
@@ -47,9 +47,7 @@ export namespace ApplicationTransformer {
|
|
|
47
47
|
// GENERATORS
|
|
48
48
|
//----
|
|
49
49
|
// METADATA
|
|
50
|
-
const collection: MetadataCollection = new MetadataCollection(
|
|
51
|
-
replace: MetadataCollection.replace,
|
|
52
|
-
});
|
|
50
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
53
51
|
const metadatas: Array<Metadata> = types.map((type) =>
|
|
54
52
|
MetadataFactory.analyze(checker)({
|
|
55
53
|
resolve: true,
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { MessageProgrammer } from "../../../programmers/MessageProgrammer";
|
|
4
|
+
|
|
5
|
+
import { IProject } from "../../IProject";
|
|
6
|
+
|
|
7
|
+
export namespace MessageTransformer {
|
|
8
|
+
export const transform =
|
|
9
|
+
(project: IProject) =>
|
|
10
|
+
(_modulo: ts.LeftHandSideExpression) =>
|
|
11
|
+
(expression: ts.CallExpression): ts.Expression => {
|
|
12
|
+
// CHECK GENERIC ARGUMENT EXISTENCE
|
|
13
|
+
if (!expression.typeArguments || !expression.typeArguments[0])
|
|
14
|
+
throw new Error(ErrorMessages.NOT_SPECIFIED);
|
|
15
|
+
|
|
16
|
+
// GET TYPE INFO
|
|
17
|
+
const type: ts.Type = project.checker.getTypeFromTypeNode(
|
|
18
|
+
expression.typeArguments[0],
|
|
19
|
+
);
|
|
20
|
+
if (type.isTypeParameter())
|
|
21
|
+
throw new Error(ErrorMessages.GENERIC_ARGUMENT);
|
|
22
|
+
|
|
23
|
+
// DO TRANSFORM
|
|
24
|
+
return MessageProgrammer.generate(project)(type);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const enum ErrorMessages {
|
|
29
|
+
NOT_SPECIFIED = "Error on typia.message(): generic argument is not specified.",
|
|
30
|
+
GENERIC_ARGUMENT = "Error on typia.message(): non-specified generic argument.",
|
|
31
|
+
}
|
package/src/utils/Escaper.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
export namespace Escaper {
|
|
2
|
-
export const variable = (str: string): boolean =>
|
|
3
|
-
reserved(str) === false && /^[a-zA-Z_$][a-zA-Z_$0-9]*$/g.test(str);
|
|
4
|
-
|
|
5
|
-
export const reserved = (str: string): boolean => RESERVED.has(str);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const RESERVED: Set<string> = new Set([
|
|
9
|
-
"break",
|
|
10
|
-
"case",
|
|
11
|
-
"catch",
|
|
12
|
-
"class",
|
|
13
|
-
"const",
|
|
14
|
-
"continue",
|
|
15
|
-
"debugger",
|
|
16
|
-
"default",
|
|
17
|
-
"delete",
|
|
18
|
-
"do",
|
|
19
|
-
"else",
|
|
20
|
-
"enum",
|
|
21
|
-
"export",
|
|
22
|
-
"extends",
|
|
23
|
-
"false",
|
|
24
|
-
"finally",
|
|
25
|
-
"for",
|
|
26
|
-
"function",
|
|
27
|
-
"if",
|
|
28
|
-
"import",
|
|
29
|
-
"in",
|
|
30
|
-
"instanceof",
|
|
31
|
-
"new",
|
|
32
|
-
"null",
|
|
33
|
-
"package",
|
|
34
|
-
"return",
|
|
35
|
-
"super",
|
|
36
|
-
"switch",
|
|
37
|
-
"this",
|
|
38
|
-
"throw",
|
|
39
|
-
"true",
|
|
40
|
-
"try",
|
|
41
|
-
"typeof",
|
|
42
|
-
"var",
|
|
43
|
-
"void",
|
|
44
|
-
"while",
|
|
45
|
-
"with",
|
|
46
|
-
]);
|
|
1
|
+
export namespace Escaper {
|
|
2
|
+
export const variable = (str: string): boolean =>
|
|
3
|
+
reserved(str) === false && /^[a-zA-Z_$][a-zA-Z_$0-9]*$/g.test(str);
|
|
4
|
+
|
|
5
|
+
export const reserved = (str: string): boolean => RESERVED.has(str);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const RESERVED: Set<string> = new Set([
|
|
9
|
+
"break",
|
|
10
|
+
"case",
|
|
11
|
+
"catch",
|
|
12
|
+
"class",
|
|
13
|
+
"const",
|
|
14
|
+
"continue",
|
|
15
|
+
"debugger",
|
|
16
|
+
"default",
|
|
17
|
+
"delete",
|
|
18
|
+
"do",
|
|
19
|
+
"else",
|
|
20
|
+
"enum",
|
|
21
|
+
"export",
|
|
22
|
+
"extends",
|
|
23
|
+
"false",
|
|
24
|
+
"finally",
|
|
25
|
+
"for",
|
|
26
|
+
"function",
|
|
27
|
+
"if",
|
|
28
|
+
"import",
|
|
29
|
+
"in",
|
|
30
|
+
"instanceof",
|
|
31
|
+
"new",
|
|
32
|
+
"null",
|
|
33
|
+
"package",
|
|
34
|
+
"return",
|
|
35
|
+
"super",
|
|
36
|
+
"switch",
|
|
37
|
+
"this",
|
|
38
|
+
"throw",
|
|
39
|
+
"true",
|
|
40
|
+
"try",
|
|
41
|
+
"typeof",
|
|
42
|
+
"var",
|
|
43
|
+
"void",
|
|
44
|
+
"while",
|
|
45
|
+
"with",
|
|
46
|
+
]);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export namespace NameEncoder {
|
|
2
|
+
export function encode(str: string): string {
|
|
3
|
+
for (const [before, after] of REPLACERS)
|
|
4
|
+
str = str.split(before).join(after);
|
|
5
|
+
return str;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function decode(str: string): string {
|
|
9
|
+
for (const [before, after] of REPLACERS)
|
|
10
|
+
if (after !== "") str = str.split(after).join(before);
|
|
11
|
+
return str;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const REPLACERS: [string, string][] = [
|
|
16
|
+
["$", "_dollar_"],
|
|
17
|
+
["&", "_and_"],
|
|
18
|
+
["|", "_or_"],
|
|
19
|
+
["{", "_blt_"],
|
|
20
|
+
["}", "_bgt_"],
|
|
21
|
+
["<", "_lt_"],
|
|
22
|
+
[">", "_gt_"],
|
|
23
|
+
["(", "_lp_"],
|
|
24
|
+
[")", "_rp_"],
|
|
25
|
+
["[", "_alt_"],
|
|
26
|
+
["]", "_agt_"],
|
|
27
|
+
[",", "_comma_"],
|
|
28
|
+
["`", "_backquote_"],
|
|
29
|
+
["'", "_singlequote_"],
|
|
30
|
+
['"', "_doublequote_"],
|
|
31
|
+
[" ", "_space_"],
|
|
32
|
+
];
|