@litert/typeguard 1.0.0 → 1.2.0
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/CHANGES.md +15 -0
- package/lib/BuiltInTypeCompiler.d.ts +4 -4
- package/lib/BuiltInTypeCompiler.d.ts.map +1 -1
- package/lib/BuiltInTypeCompiler.js +167 -166
- package/lib/BuiltInTypeCompiler.js.map +1 -1
- package/lib/BuiltInTypes.d.ts +1 -1
- package/lib/BuiltInTypes.js +37 -36
- package/lib/BuiltInTypes.js.map +1 -1
- package/lib/Common.d.ts +13 -9
- package/lib/Common.d.ts.map +1 -1
- package/lib/Common.js +1 -1
- package/lib/Compiler.d.ts +2 -2
- package/lib/Compiler.d.ts.map +1 -1
- package/lib/Compiler.js +77 -72
- package/lib/Compiler.js.map +1 -1
- package/lib/Context.d.ts +2 -2
- package/lib/Context.js +3 -2
- package/lib/Context.js.map +1 -1
- package/lib/FilterCompiler.d.ts +5 -5
- package/lib/FilterCompiler.d.ts.map +1 -1
- package/lib/FilterCompiler.js +25 -24
- package/lib/FilterCompiler.js.map +1 -1
- package/lib/InlineCompiler.d.ts +12 -5
- package/lib/InlineCompiler.d.ts.map +1 -1
- package/lib/InlineCompiler.js +14 -5
- package/lib/InlineCompiler.js.map +1 -1
- package/lib/Internal.d.ts +2 -1
- package/lib/Internal.d.ts.map +1 -1
- package/lib/Internal.js +12 -10
- package/lib/Internal.js.map +1 -1
- package/lib/Modifiers.d.ts +1 -1
- package/lib/Modifiers.js +2 -1
- package/lib/Modifiers.js.map +1 -1
- package/lib/index.d.ts +5 -5
- package/lib/index.js +19 -7
- package/lib/index.js.map +1 -1
- package/lib/langs/JavaScript.d.ts +2 -2
- package/lib/langs/JavaScript.d.ts.map +1 -1
- package/lib/langs/JavaScript.js +30 -23
- package/lib/langs/JavaScript.js.map +1 -1
- package/package.json +18 -13
- package/src/{samples → examples}/quick-start.ts +22 -12
- package/src/lib/BuiltInTypeCompiler.ts +171 -171
- package/src/lib/BuiltInTypes.ts +36 -36
- package/src/lib/Common.ts +15 -10
- package/src/lib/Compiler.ts +213 -208
- package/src/lib/Context.ts +3 -3
- package/src/lib/FilterCompiler.ts +84 -84
- package/src/lib/InlineCompiler.ts +35 -14
- package/src/lib/Internal.ts +12 -10
- package/src/lib/Modifiers.ts +2 -2
- package/src/lib/index.ts +5 -5
- package/src/lib/langs/JavaScript.ts +34 -24
- package/src/test/00-all.ts +11 -9
- package/src/test/01-elemental-types.ts +1111 -1110
- package/src/test/02-array-and-list.ts +75 -75
- package/src/test/03-tuple.ts +87 -87
- package/src/test/04-from-string.ts +849 -848
- package/src/test/05-string-asserts.ts +422 -422
- package/src/test/06-structure.ts +107 -107
- package/src/test/07-modifiers.ts +151 -42
- package/src/test/08-map-and-dict.ts +46 -46
- package/src/test/09-exceptions.ts +67 -0
- package/src/test/abstracts.ts +52 -43
- package/dist/typeguard.amd.d.ts +0 -588
- package/dist/typeguard.amd.d.ts.map +0 -1
- package/dist/typeguard.amd.js +0 -2063
- package/dist/typeguard.amd.js.map +0 -1
- package/dist/typeguard.system.d.ts +0 -588
- package/dist/typeguard.system.d.ts.map +0 -1
- package/dist/typeguard.system.js +0 -2179
- package/dist/typeguard.system.js.map +0 -1
- package/tsconfig-amd.json +0 -72
- package/tsconfig-systemjs.json +0 -72
package/src/lib/Context.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import * as i from
|
|
17
|
+
import * as i from './Internal';
|
|
18
18
|
|
|
19
19
|
export class Context implements i.IContext {
|
|
20
20
|
|
|
@@ -80,7 +80,7 @@ export class Context implements i.IContext {
|
|
|
80
80
|
|
|
81
81
|
if (!prev) {
|
|
82
82
|
|
|
83
|
-
throw new Error(
|
|
83
|
+
throw new Error('Failed to pop stack.');
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
this.vName = prev.vName;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -14,143 +14,143 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import * as C from
|
|
18
|
-
import * as I from
|
|
17
|
+
import * as C from './Common';
|
|
18
|
+
import * as I from './Internal';
|
|
19
19
|
|
|
20
20
|
export class FilterCompiler
|
|
21
21
|
implements I.IFilterCompiler {
|
|
22
22
|
|
|
23
23
|
public constructor(
|
|
24
|
-
private _lang: C.ILanguageBuilder,
|
|
25
|
-
private _bitc: I.IBuiltInTypeCompiler
|
|
24
|
+
private readonly _lang: C.ILanguageBuilder,
|
|
25
|
+
private readonly _bitc: I.IBuiltInTypeCompiler
|
|
26
26
|
) {}
|
|
27
27
|
|
|
28
28
|
public compile(rule: string, ctx: I.IContext): string {
|
|
29
29
|
|
|
30
30
|
let vName = ctx.vName;
|
|
31
31
|
|
|
32
|
-
const filter = rule.slice(1).split(
|
|
32
|
+
const filter = rule.slice(1).split(' ');
|
|
33
33
|
|
|
34
34
|
if (filter.slice(2).length !== filter.slice(2).filter((x) => /^\d+(\.\d+)?$/.test(x)).length) {
|
|
35
35
|
|
|
36
|
-
throw new TypeError(
|
|
36
|
+
throw new TypeError('Only number is allowed as filter arguments.');
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
const ret: string[] = [];
|
|
40
40
|
|
|
41
41
|
switch (filter[0]) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
42
|
+
case 'array.length':
|
|
43
|
+
ret.push(this._lang.isArray(vName, true));
|
|
44
|
+
vName = this._lang.arrayLength(vName);
|
|
45
|
+
break;
|
|
46
|
+
case 'string.length':
|
|
47
|
+
ret.push(this._lang.isString(vName, true));
|
|
48
|
+
vName = this._lang.stringLength(vName);
|
|
49
|
+
break;
|
|
50
|
+
case 'value':
|
|
51
|
+
ret.push(this._lang.isNumber(vName, true));
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
ret.push(this._bitc.compile(filter[0], ctx, []));
|
|
55
|
+
break;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
switch (filter[1]) {
|
|
59
|
-
|
|
59
|
+
case 'between':
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
if (filter.length !== 4) {
|
|
62
|
+
throw new TypeError(`Filter ${filter[1]} require 2 arguments.`);
|
|
63
|
+
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
ret.push(this._lang.gte(vName, filter[2]));
|
|
66
|
+
ret.push(this._lang.lte(vName, filter[3]));
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
break;
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
case 'gt':
|
|
71
|
+
case '>':
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
if (filter.length !== 3) {
|
|
74
|
+
throw new TypeError(`Filter ${filter[1]} require 1 argument.`);
|
|
75
|
+
}
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
ret.push(this._lang.gt(vName, filter[2]));
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
break;
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
case 'ge':
|
|
82
|
+
case 'gte':
|
|
83
|
+
case '>=':
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
if (filter.length !== 3) {
|
|
86
|
+
throw new TypeError(`Filter ${filter[1]} require 1 argument.`);
|
|
87
|
+
}
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
ret.push(this._lang.gte(vName, filter[2]));
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
break;
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
case 'lt':
|
|
94
|
+
case '<':
|
|
95
95
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
if (filter.length !== 3) {
|
|
97
|
+
throw new TypeError(`Filter ${filter[1]} require 1 argument.`);
|
|
98
|
+
}
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
ret.push(this._lang.lt(vName, filter[2]));
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
break;
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
104
|
+
case 'le':
|
|
105
|
+
case 'lte':
|
|
106
|
+
case '<=':
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
if (filter.length !== 3) {
|
|
109
|
+
throw new TypeError(`Filter ${filter[1]} require 1 argument.`);
|
|
110
|
+
}
|
|
111
111
|
|
|
112
|
-
|
|
112
|
+
ret.push(this._lang.lte(vName, filter[2]));
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
break;
|
|
115
115
|
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
case 'eq':
|
|
117
|
+
case '==':
|
|
118
118
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
if (filter.length !== 3) {
|
|
120
|
+
throw new TypeError(`Filter ${filter[1]} require 1 argument.`);
|
|
121
|
+
}
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
ret.push(this._lang.eq(vName, filter[2]));
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
break;
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
|
|
127
|
+
case 'ne':
|
|
128
|
+
case '!=':
|
|
129
129
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
if (filter.length !== 3) {
|
|
131
|
+
throw new TypeError(`Filter ${filter[1]} require 1 argument.`);
|
|
132
|
+
}
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
ret.push(this._lang.ne(vName, filter[2]));
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
break;
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
case 'timesof':
|
|
139
139
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
if (filter.length !== 3) {
|
|
141
|
+
throw new TypeError(`Filter ${filter[1]} require 1 argument.`);
|
|
142
|
+
}
|
|
143
143
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
144
|
+
ret.push(this._lang.eq(
|
|
145
|
+
this._lang.modOf(vName, filter[2]),
|
|
146
|
+
'0'
|
|
147
|
+
));
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
break;
|
|
150
150
|
|
|
151
|
-
|
|
151
|
+
default:
|
|
152
152
|
|
|
153
|
-
|
|
153
|
+
throw new TypeError(`Unknown filter type "${filter[1]}".`);
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
return this._lang.and(ret);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -14,16 +14,17 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import { createCompiler } from
|
|
18
|
-
import { createJavaScriptLanguageBuilder } from
|
|
19
|
-
import * as C from
|
|
17
|
+
import { createCompiler } from './Compiler';
|
|
18
|
+
import { createJavaScriptLanguageBuilder } from './langs/JavaScript';
|
|
19
|
+
import * as C from './Common';
|
|
20
|
+
import * as I from './Internal';
|
|
20
21
|
|
|
21
22
|
export interface IInlineCompileOptions extends C.ICompileOptions {
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* Added `debugger` statement before executing checking code.
|
|
25
26
|
*/
|
|
26
|
-
|
|
27
|
+
'stopOnEntry'?: boolean;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
export interface IInlineCompiler {
|
|
@@ -34,14 +35,22 @@ export interface IInlineCompiler {
|
|
|
34
35
|
*
|
|
35
36
|
* @param options The options of compilation.
|
|
36
37
|
*/
|
|
37
|
-
compile<T>(options: IInlineCompileOptions): C.
|
|
38
|
+
compile<T>(options: IInlineCompileOptions): C.ITypeChecker<T>;
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
41
|
* Get the type-checker of a pre-defined type.
|
|
41
42
|
*
|
|
42
43
|
* @param name The name of the pre-defined type.
|
|
43
44
|
*/
|
|
44
|
-
getPredefinedType<T>(name: string): C.
|
|
45
|
+
getPredefinedType<T>(name: string): C.ITypeChecker<T>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Register a pre-defined type checker.
|
|
49
|
+
*
|
|
50
|
+
* @param name The name of the pre-defined type (without prefix `@`)
|
|
51
|
+
* @param checker The checker callback of the pre-defined type.
|
|
52
|
+
*/
|
|
53
|
+
addPredefinedType<T>(name: string, checker: C.ITypeChecker<T>): this;
|
|
45
54
|
|
|
46
55
|
/**
|
|
47
56
|
* Check if a pre-defined type is compiled.
|
|
@@ -59,11 +68,11 @@ export interface IInlineCompiler {
|
|
|
59
68
|
class InlineCompiler
|
|
60
69
|
implements IInlineCompiler {
|
|
61
70
|
|
|
62
|
-
private _defTypes: Record<string, C.
|
|
71
|
+
private _defTypes: Record<string, C.ITypeChecker<any>>;
|
|
63
72
|
|
|
64
73
|
private _missingTypes: Record<string, boolean>;
|
|
65
74
|
|
|
66
|
-
private _compiler: C.ICompiler;
|
|
75
|
+
private readonly _compiler: C.ICompiler;
|
|
67
76
|
|
|
68
77
|
public constructor() {
|
|
69
78
|
|
|
@@ -78,7 +87,7 @@ implements IInlineCompiler {
|
|
|
78
87
|
);
|
|
79
88
|
}
|
|
80
89
|
|
|
81
|
-
public compile<T>(options: IInlineCompileOptions): C.
|
|
90
|
+
public compile<T>(options: IInlineCompileOptions): C.ITypeChecker<T> {
|
|
82
91
|
|
|
83
92
|
const result = this._compiler.compile(options);
|
|
84
93
|
|
|
@@ -92,7 +101,7 @@ implements IInlineCompiler {
|
|
|
92
101
|
stopOnEntry?: boolean
|
|
93
102
|
): void {
|
|
94
103
|
|
|
95
|
-
for (
|
|
104
|
+
for (const x of types) {
|
|
96
105
|
|
|
97
106
|
if (this._defTypes[x]) {
|
|
98
107
|
|
|
@@ -115,6 +124,18 @@ implements IInlineCompiler {
|
|
|
115
124
|
}
|
|
116
125
|
}
|
|
117
126
|
|
|
127
|
+
public addPredefinedType(name: string, checker: C.ITypeChecker<any>): this {
|
|
128
|
+
|
|
129
|
+
if (!I.RE_VALID_CUSTOM_TYPE_NAME.test(name)) {
|
|
130
|
+
|
|
131
|
+
throw new TypeError(`Invalid name ${ JSON.stringify(name) } for a pre-defined type.`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
this._defTypes[name] = checker;
|
|
135
|
+
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
118
139
|
public detectUndefinedTypes(): string[] {
|
|
119
140
|
|
|
120
141
|
return Object.keys(this._missingTypes);
|
|
@@ -125,7 +146,7 @@ implements IInlineCompiler {
|
|
|
125
146
|
return !!this._defTypes[name];
|
|
126
147
|
}
|
|
127
148
|
|
|
128
|
-
public getPredefinedType(name: string): C.
|
|
149
|
+
public getPredefinedType(name: string): C.ITypeChecker<any> {
|
|
129
150
|
|
|
130
151
|
if (!this._defTypes[name]) {
|
|
131
152
|
|
|
@@ -138,9 +159,9 @@ implements IInlineCompiler {
|
|
|
138
159
|
private _wrapCheckerCode(
|
|
139
160
|
info: C.ICompileResult,
|
|
140
161
|
stopOnEntry: boolean = false
|
|
141
|
-
): C.
|
|
162
|
+
): C.ITypeChecker<any> {
|
|
142
163
|
|
|
143
|
-
const soe = stopOnEntry ?
|
|
164
|
+
const soe = stopOnEntry ? 'debugger;' : '';
|
|
144
165
|
|
|
145
166
|
return (new Function(
|
|
146
167
|
info.typeSlotName,
|
package/src/lib/Internal.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -14,19 +14,19 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export const MAP_SUFFIX =
|
|
17
|
+
export const MAP_SUFFIX = '{}';
|
|
18
18
|
|
|
19
|
-
export const LIST_SUFFIX =
|
|
19
|
+
export const LIST_SUFFIX = '[]';
|
|
20
20
|
|
|
21
|
-
export const MODIFIER_PREFIX =
|
|
21
|
+
export const MODIFIER_PREFIX = '$.';
|
|
22
22
|
|
|
23
|
-
export const FILTER_PREFIX =
|
|
23
|
+
export const FILTER_PREFIX = '|';
|
|
24
24
|
|
|
25
|
-
export const IMPLICIT_SYMBOL =
|
|
25
|
+
export const IMPLICIT_SYMBOL = '?';
|
|
26
26
|
|
|
27
|
-
export const PREDEF_TYPE_SYMBOL =
|
|
27
|
+
export const PREDEF_TYPE_SYMBOL = '@';
|
|
28
28
|
|
|
29
|
-
export const NEGATIVE_SYMBOL =
|
|
29
|
+
export const NEGATIVE_SYMBOL = '!';
|
|
30
30
|
|
|
31
31
|
export const KEY_MAP_SUFFIX = `->${MAP_SUFFIX}`;
|
|
32
32
|
|
|
@@ -34,9 +34,9 @@ export const KEY_LIST_SUFFIX = `->${LIST_SUFFIX}`;
|
|
|
34
34
|
|
|
35
35
|
export const KEY_ARRAY_SUFFIX = /->\[\s*(\d+)(\s*,\s*(\d+)?)?\s*\]$/;
|
|
36
36
|
|
|
37
|
-
export const KEY_STRICT_SUFFIX =
|
|
37
|
+
export const KEY_STRICT_SUFFIX = '->()';
|
|
38
38
|
|
|
39
|
-
export const KEY_EQUAL_SUFFIX =
|
|
39
|
+
export const KEY_EQUAL_SUFFIX = '->(=)';
|
|
40
40
|
|
|
41
41
|
export enum EFlags {
|
|
42
42
|
|
|
@@ -123,3 +123,5 @@ export interface IBuiltInTypeCompiler {
|
|
|
123
123
|
|
|
124
124
|
isElemental(type: string): boolean;
|
|
125
125
|
}
|
|
126
|
+
|
|
127
|
+
export const RE_VALID_CUSTOM_TYPE_NAME = /^[-:.\w]+$/;
|
package/src/lib/Modifiers.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import * as I from
|
|
17
|
+
import * as I from './Internal';
|
|
18
18
|
|
|
19
19
|
export const PREFIX: string = I.MODIFIER_PREFIX;
|
|
20
20
|
|
package/src/lib/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export * from
|
|
18
|
-
export * from
|
|
19
|
-
export * from
|
|
20
|
-
export * from
|
|
17
|
+
export * from './InlineCompiler';
|
|
18
|
+
export * from './langs/JavaScript';
|
|
19
|
+
export * from './Compiler';
|
|
20
|
+
export * from './Common';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2022 Angus Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -14,19 +14,19 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import * as C from
|
|
17
|
+
import * as C from '../Common';
|
|
18
18
|
|
|
19
19
|
class JavaScriptLanguage
|
|
20
20
|
implements C.ILanguageBuilder {
|
|
21
21
|
|
|
22
22
|
public switchCase(expr: string, cases: string[]): string {
|
|
23
23
|
|
|
24
|
-
return `switch (${expr}) { ${cases.join(
|
|
24
|
+
return `switch (${expr}) { ${cases.join('')} }`;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
public caseIf(cond: string[], expr: string): string {
|
|
28
28
|
|
|
29
|
-
return `${cond.map((x) => `case ${x}:`).join(
|
|
29
|
+
return `${cond.map((x) => `case ${x}:`).join(' ')} { ${expr} break; }`;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
public caseDefault(expr: string): string {
|
|
@@ -61,7 +61,7 @@ implements C.ILanguageBuilder {
|
|
|
61
61
|
|
|
62
62
|
public call(fnName: string, ...args: string[]): string {
|
|
63
63
|
|
|
64
|
-
return `${fnName}(${args.join(
|
|
64
|
+
return `${fnName}(${args.join(',')})`;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
public startsWith(
|
|
@@ -105,14 +105,19 @@ implements C.ILanguageBuilder {
|
|
|
105
105
|
return conditions[0];
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
if (conditions.includes('true')) {
|
|
109
|
+
|
|
110
|
+
return 'true';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
conditions = this._dereplicate(conditions.filter((x) => x !== 'false'));
|
|
109
114
|
|
|
110
115
|
if (!conditions.length) {
|
|
111
116
|
|
|
112
|
-
return
|
|
117
|
+
return 'true';
|
|
113
118
|
}
|
|
114
119
|
|
|
115
|
-
return `${conditions.map((x) => `(${x})`).join(
|
|
120
|
+
return `${conditions.map((x) => `(${x})`).join(' || ')}`;
|
|
116
121
|
}
|
|
117
122
|
|
|
118
123
|
public and(conditions: string[]): string {
|
|
@@ -122,14 +127,19 @@ implements C.ILanguageBuilder {
|
|
|
122
127
|
return conditions[0];
|
|
123
128
|
}
|
|
124
129
|
|
|
125
|
-
|
|
130
|
+
if (conditions.includes('false')) {
|
|
131
|
+
|
|
132
|
+
return 'false';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
conditions = this._dereplicate(conditions.filter((x) => x !== 'true'));
|
|
126
136
|
|
|
127
137
|
if (!conditions.length) {
|
|
128
138
|
|
|
129
|
-
return
|
|
139
|
+
return 'true';
|
|
130
140
|
}
|
|
131
141
|
|
|
132
|
-
return `${conditions.map((x) => `(${x})`).join(
|
|
142
|
+
return `${conditions.map((x) => `(${x})`).join(' && ')}`;
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
public eq(a: string, b: string | number): string {
|
|
@@ -182,11 +192,11 @@ implements C.ILanguageBuilder {
|
|
|
182
192
|
regExp: string
|
|
183
193
|
): string {
|
|
184
194
|
|
|
185
|
-
|
|
195
|
+
const m = /^\/(.+)\/([a-z]*)$/i.exec(regExp);
|
|
186
196
|
|
|
187
197
|
if (m) {
|
|
188
198
|
|
|
189
|
-
return `/${m[1]}/${m[2] ||
|
|
199
|
+
return `/${m[1]}/${m[2] || ''}.test(${expr})`;
|
|
190
200
|
}
|
|
191
201
|
|
|
192
202
|
return `/${regExp}/.test(${expr})`;
|
|
@@ -204,12 +214,12 @@ implements C.ILanguageBuilder {
|
|
|
204
214
|
|
|
205
215
|
private _equal(positive: boolean = true): string {
|
|
206
216
|
|
|
207
|
-
return positive ?
|
|
217
|
+
return positive ? '===' : '!==';
|
|
208
218
|
}
|
|
209
219
|
|
|
210
220
|
private _not(positive: boolean = true): string {
|
|
211
221
|
|
|
212
|
-
return positive ?
|
|
222
|
+
return positive ? '' : '!';
|
|
213
223
|
}
|
|
214
224
|
|
|
215
225
|
public isString(vn: string, positive: boolean = true): string {
|
|
@@ -242,13 +252,13 @@ implements C.ILanguageBuilder {
|
|
|
242
252
|
this.isNumber(vn, true),
|
|
243
253
|
this.and([
|
|
244
254
|
this.isString(vn, true),
|
|
245
|
-
this.matchRegExp(vn,
|
|
255
|
+
this.matchRegExp(vn, '^[+-]?\\d+(\\.\\d+)?$')
|
|
246
256
|
])
|
|
247
257
|
]) : this.and([
|
|
248
258
|
this.isNumber(vn, false),
|
|
249
259
|
this.or([
|
|
250
260
|
this.isString(vn, false),
|
|
251
|
-
this.not(this.matchRegExp(vn,
|
|
261
|
+
this.not(this.matchRegExp(vn, '^[+-]?\\d+(\\.\\d+)?$'))
|
|
252
262
|
])
|
|
253
263
|
]);
|
|
254
264
|
}
|
|
@@ -291,7 +301,7 @@ implements C.ILanguageBuilder {
|
|
|
291
301
|
|
|
292
302
|
public series(statements: string[]): string {
|
|
293
303
|
|
|
294
|
-
return statements.map((s) => s.endsWith(
|
|
304
|
+
return statements.map((s) => s.endsWith(';') ? s : `${s};`).join('');
|
|
295
305
|
}
|
|
296
306
|
|
|
297
307
|
public ifThen(
|
|
@@ -369,22 +379,22 @@ implements C.ILanguageBuilder {
|
|
|
369
379
|
|
|
370
380
|
public get literalFalse(): string {
|
|
371
381
|
|
|
372
|
-
return
|
|
382
|
+
return 'false';
|
|
373
383
|
}
|
|
374
384
|
|
|
375
385
|
public get literalTrue(): string {
|
|
376
386
|
|
|
377
|
-
return
|
|
387
|
+
return 'true';
|
|
378
388
|
}
|
|
379
389
|
|
|
380
390
|
public get maxSafeInteger(): string {
|
|
381
391
|
|
|
382
|
-
return
|
|
392
|
+
return '0X1FFFFFFFFFFFFF';
|
|
383
393
|
}
|
|
384
394
|
|
|
385
395
|
public get minSafeInteger(): string {
|
|
386
396
|
|
|
387
|
-
return
|
|
397
|
+
return '-0X1FFFFFFFFFFFFF';
|
|
388
398
|
}
|
|
389
399
|
|
|
390
400
|
public isTrueValue(vn: string): string {
|
|
@@ -408,10 +418,10 @@ implements C.ILanguageBuilder {
|
|
|
408
418
|
body: string
|
|
409
419
|
): string {
|
|
410
420
|
|
|
411
|
-
return `(function(${params.join(
|
|
421
|
+
return `(function(${params.join(', ')}) {
|
|
412
422
|
${body}
|
|
413
423
|
})(${
|
|
414
|
-
args.join(
|
|
424
|
+
args.join(', ')
|
|
415
425
|
})`;
|
|
416
426
|
}
|
|
417
427
|
}
|