@gi-tcg/gts-transpiler 0.3.1 → 0.3.3
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/dist/index.d.ts +109 -20
- package/dist/index.js +1837 -2178
- package/package.json +11 -7
- package/src/config.ts +1 -1
- package/src/error.ts +3 -4
- package/src/index.ts +6 -6
- package/src/parse/gts_plugin.ts +5 -5
- package/src/parse/index.ts +6 -4
- package/src/parse/loose_plugin.ts +1 -1
- package/src/parse/record_call_lparen_plugin.ts +18 -21
- package/src/transform/erase_ts.ts +2 -2
- package/src/transform/gts.ts +17 -3
- package/src/transform/index.ts +6 -6
- package/src/transform/volar/collect_tokens.ts +28 -34
- package/src/transform/volar/index.ts +52 -52
- package/src/transform/volar/mappings.ts +5 -352
- package/src/transform/volar/printer.ts +101 -117
- package/src/transform/volar/replacements.ts +76 -48
- package/src/transform/volar/walker.ts +89 -70
- package/src/types.ts +5 -11
package/dist/index.js
CHANGED
|
@@ -1,1384 +1,1306 @@
|
|
|
1
|
-
|
|
2
|
-
import { Parser } from "acorn";
|
|
1
|
+
import { Parser, tokTypes } from "acorn";
|
|
3
2
|
import { tsPlugin } from "@sveltejs/acorn-typescript";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
3
|
+
import { walk } from "zimmerframe";
|
|
4
|
+
import { print } from "esrap";
|
|
5
|
+
import jsPrinter from "esrap/languages/ts";
|
|
6
|
+
import { defaultPrinters, print as print$1 } from "espolar";
|
|
7
|
+
import dedent from "dedent";
|
|
8
|
+
import path from "path-browserify-esm";
|
|
9
|
+
//#region src/keywords.ts
|
|
10
|
+
const specialIdentifiers = [
|
|
11
|
+
"break",
|
|
12
|
+
"case",
|
|
13
|
+
"catch",
|
|
14
|
+
"class",
|
|
15
|
+
"const",
|
|
16
|
+
"continue",
|
|
17
|
+
"debugger",
|
|
18
|
+
"default",
|
|
19
|
+
"delete",
|
|
20
|
+
"do",
|
|
21
|
+
"else",
|
|
22
|
+
"export",
|
|
23
|
+
"extends",
|
|
24
|
+
"false",
|
|
25
|
+
"finally",
|
|
26
|
+
"for",
|
|
27
|
+
"function",
|
|
28
|
+
"if",
|
|
29
|
+
"import",
|
|
30
|
+
"in",
|
|
31
|
+
"instanceof",
|
|
32
|
+
"new",
|
|
33
|
+
"null",
|
|
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
|
+
"let",
|
|
47
|
+
"static",
|
|
48
|
+
"yield",
|
|
49
|
+
"await",
|
|
50
|
+
"enum",
|
|
51
|
+
"implements",
|
|
52
|
+
"interface",
|
|
53
|
+
"package",
|
|
54
|
+
"private",
|
|
55
|
+
"protected",
|
|
56
|
+
"public",
|
|
57
|
+
"as",
|
|
58
|
+
"async",
|
|
59
|
+
"from",
|
|
60
|
+
"get",
|
|
61
|
+
"of",
|
|
62
|
+
"set",
|
|
63
|
+
"type",
|
|
64
|
+
"define",
|
|
65
|
+
"query"
|
|
65
66
|
];
|
|
66
|
-
|
|
67
|
-
// packages/transpiler/src/parse/loose_plugin.ts
|
|
68
|
-
import { tokTypes } from "acorn";
|
|
69
|
-
var DUMMY_PLACEHOLDER = "✖";
|
|
70
67
|
function loosePlugin() {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
dummy.name = DUMMY_PLACEHOLDER;
|
|
95
|
-
dummy.isDummy = true;
|
|
96
|
-
return this.finishNode(dummy, "Identifier");
|
|
97
|
-
}
|
|
98
|
-
parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
|
|
99
|
-
return super.parseSubscript.call(this.#proxiedThis, base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
};
|
|
68
|
+
return function loosePluginTransformer(parser) {
|
|
69
|
+
return class LooseParser extends parser {
|
|
70
|
+
_patchedParseIdent = (liberal) => {
|
|
71
|
+
if (this.type !== tokTypes.name) return this.createDummyIdentifier();
|
|
72
|
+
else return super.parseIdent(liberal);
|
|
73
|
+
};
|
|
74
|
+
#proxiedThis = new Proxy(this, { get: (target, prop) => {
|
|
75
|
+
if (prop === "parseIdent") return this._patchedParseIdent;
|
|
76
|
+
const value = Reflect.get(target, prop);
|
|
77
|
+
if (typeof value === "function") return value.bind(target);
|
|
78
|
+
return value;
|
|
79
|
+
} });
|
|
80
|
+
createDummyIdentifier() {
|
|
81
|
+
const dummy = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc);
|
|
82
|
+
dummy.name = "✖";
|
|
83
|
+
dummy.isDummy = true;
|
|
84
|
+
return this.finishNode(dummy, "Identifier");
|
|
85
|
+
}
|
|
86
|
+
parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
|
|
87
|
+
return super.parseSubscript.call(this.#proxiedThis, base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
};
|
|
103
91
|
}
|
|
104
|
-
|
|
105
|
-
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/parse/gts_plugin.ts
|
|
106
94
|
function gtsPlugin(options = {}) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
this.next();
|
|
271
|
-
if (this.gtsOptions.allowEmptyShortcutMember && this.type !== tokTypes2.name) {
|
|
272
|
-
const dummy = this.startNode();
|
|
273
|
-
dummy.name = DUMMY_PLACEHOLDER;
|
|
274
|
-
dummy.isDummy = true;
|
|
275
|
-
node.property = this.finishNode(dummy, "Identifier");
|
|
276
|
-
} else {
|
|
277
|
-
node.property = this.parseIdent();
|
|
278
|
-
}
|
|
279
|
-
return this.finishNode(node, "GTSShortcutArgumentExpression");
|
|
280
|
-
}
|
|
281
|
-
return super.parseExprAtom(refDestructuringErrors, forInit, forNew);
|
|
282
|
-
}
|
|
283
|
-
parseMaybeUnary(refDestructuringErrors, sawUnary, incDec, forInit) {
|
|
284
|
-
if (this.isShortcutContext && this.isContextual("query")) {
|
|
285
|
-
const expr = this.gts_parseQueryExpression();
|
|
286
|
-
if (!incDec && this.eat(tokTypes2.starstar)) {
|
|
287
|
-
this.unexpected(this.lastTokStart);
|
|
288
|
-
}
|
|
289
|
-
return expr;
|
|
290
|
-
}
|
|
291
|
-
return super.parseMaybeUnary(refDestructuringErrors, sawUnary, incDec, forInit);
|
|
292
|
-
}
|
|
293
|
-
gts_parseQueryExpression(forInit) {
|
|
294
|
-
const node = this.startNode();
|
|
295
|
-
this.next();
|
|
296
|
-
if (this.eat(tokTypes2.star)) {
|
|
297
|
-
node.star = true;
|
|
298
|
-
}
|
|
299
|
-
node.argument = this.parseMaybeUnary(null, true, false, forInit);
|
|
300
|
-
return this.finishNode(node, "GTSQueryExpression");
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
};
|
|
95
|
+
return function gtsPluginTransformer(Parser) {
|
|
96
|
+
const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
|
|
97
|
+
const lineBreak = /\r\n?|\n|\u2028|\u2029/;
|
|
98
|
+
const acornScope = {
|
|
99
|
+
SCOPE_TOP: 1,
|
|
100
|
+
SCOPE_FUNCTION: 2,
|
|
101
|
+
SCOPE_ASYNC: 4,
|
|
102
|
+
SCOPE_GENERATOR: 8,
|
|
103
|
+
SCOPE_ARROW: 16
|
|
104
|
+
};
|
|
105
|
+
return class GtsParser extends Parser {
|
|
106
|
+
gtsOptions = options;
|
|
107
|
+
isShortcutContext = false;
|
|
108
|
+
parseStatement(context, topLevel, exports) {
|
|
109
|
+
if (topLevel && this.gts_isDefineStatement()) {
|
|
110
|
+
const node = this.startNode();
|
|
111
|
+
this.next();
|
|
112
|
+
node.body = this.gts_parseNamedAttributeDefinition();
|
|
113
|
+
return this.finishNode(node, "GTSDefineStatement");
|
|
114
|
+
}
|
|
115
|
+
return super.parseStatement(context, topLevel, exports);
|
|
116
|
+
}
|
|
117
|
+
gts_isDefineStatement() {
|
|
118
|
+
if (!this.isContextual("define")) return false;
|
|
119
|
+
skipWhiteSpace.lastIndex = this.pos;
|
|
120
|
+
const skip = skipWhiteSpace.exec(this.input);
|
|
121
|
+
const next = this.pos + skip[0].length;
|
|
122
|
+
return !lineBreak.test(this.input.slice(this.pos, next));
|
|
123
|
+
}
|
|
124
|
+
gts_parseNamedAttributeDefinition() {
|
|
125
|
+
const node = this.startNode();
|
|
126
|
+
const start = this.start;
|
|
127
|
+
let name;
|
|
128
|
+
if (this.type.label === "string") {
|
|
129
|
+
name = this.parseExprAtom();
|
|
130
|
+
if (typeof name.value === "string" && name.value.startsWith("~")) this.raise(start, `Attribute name starts with '~' is reserved for internal use.`);
|
|
131
|
+
} else if (this.type.label === "name") name = this.parseIdent();
|
|
132
|
+
else this.raise(start, "Expected attribute name");
|
|
133
|
+
node.name = name;
|
|
134
|
+
node.body = this.gts_parseAttributeBody();
|
|
135
|
+
if (this.eatContextual("as")) {
|
|
136
|
+
if (this.eatContextual("public")) node.bindingAccessModifier = "public";
|
|
137
|
+
else if (this.eatContextual("protected")) node.bindingAccessModifier = "protected";
|
|
138
|
+
else if (this.eatContextual("private")) node.bindingAccessModifier = "private";
|
|
139
|
+
node.bindingName = this.parseIdent();
|
|
140
|
+
}
|
|
141
|
+
this.semicolon();
|
|
142
|
+
return this.finishNode(node, "GTSNamedAttributeDefinition");
|
|
143
|
+
}
|
|
144
|
+
gts_parseAttributeBody() {
|
|
145
|
+
const node = this.startNode();
|
|
146
|
+
node.positionalAttributes = this.gts_parsePositionalAttributeList();
|
|
147
|
+
if (this.type === tokTypes.braceL) node.namedAttributes = this.gts_parseNamedAttributeBlock();
|
|
148
|
+
return this.finishNode(node, "GTSAttributeBody");
|
|
149
|
+
}
|
|
150
|
+
gts_parsePositionalAttributeList() {
|
|
151
|
+
const node = this.startNode();
|
|
152
|
+
node.attributes = [];
|
|
153
|
+
let first = true;
|
|
154
|
+
while (true) {
|
|
155
|
+
if (this.type === tokTypes.braceL) break;
|
|
156
|
+
if (this.type === tokTypes.semi || this.canInsertSemicolon()) break;
|
|
157
|
+
if (this.isContextual("as")) break;
|
|
158
|
+
if (!first) this.expect(tokTypes.comma);
|
|
159
|
+
else first = false;
|
|
160
|
+
node.attributes.push(this.gts_parseAttributeExpression());
|
|
161
|
+
}
|
|
162
|
+
return this.finishNode(node, "GTSPositionalAttributeList");
|
|
163
|
+
}
|
|
164
|
+
gts_parseNamedAttributeBlock() {
|
|
165
|
+
const node = this.startNode();
|
|
166
|
+
node.attributes = [];
|
|
167
|
+
this.expect(tokTypes.braceL);
|
|
168
|
+
while (this.type !== tokTypes.braceR && this.type !== tokTypes.eof) {
|
|
169
|
+
if (specialIdentifiers.includes(this.value) || this.type === tokTypes.colon) {
|
|
170
|
+
node.directAction = this.gts_parseDirectFunction();
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
node.attributes.push(this.gts_parseNamedAttributeDefinition());
|
|
174
|
+
}
|
|
175
|
+
this.expect(tokTypes.braceR);
|
|
176
|
+
return this.finishNode(node, "GTSNamedAttributeBlock");
|
|
177
|
+
}
|
|
178
|
+
gts_parseDirectFunction() {
|
|
179
|
+
const node = this.startNode();
|
|
180
|
+
node.body = [];
|
|
181
|
+
this.enterScope(acornScope.SCOPE_FUNCTION | acornScope.SCOPE_ARROW);
|
|
182
|
+
const oldShortcutContext = this.isShortcutContext;
|
|
183
|
+
this.isShortcutContext = true;
|
|
184
|
+
while (this.type !== tokTypes.braceR && this.type !== tokTypes.eof) {
|
|
185
|
+
const startPos = this.pos;
|
|
186
|
+
const stmt = this.parseStatement(null);
|
|
187
|
+
if (stmt.type === "GTSDefineStatement") this.raise(startPos, "DefineStatement is not allowed in direct function.");
|
|
188
|
+
node.body.push(stmt);
|
|
189
|
+
}
|
|
190
|
+
this.exitScope();
|
|
191
|
+
this.isShortcutContext = oldShortcutContext;
|
|
192
|
+
return this.finishNode(node, "GTSDirectFunction");
|
|
193
|
+
}
|
|
194
|
+
gts_parseAttributeExpression() {
|
|
195
|
+
if (this.type === tokTypes.braceL) this.raise(this.start, "Expected attribute expression, got '{'.");
|
|
196
|
+
if (this.type === tokTypes.colon) {
|
|
197
|
+
this.next();
|
|
198
|
+
return this.gts_parseShortcutFunction();
|
|
199
|
+
}
|
|
200
|
+
if (this.gtsOptions.allowEmptyPositionalAttribute && (this.type === tokTypes.comma || this.type === tokTypes.braceR || this.type === tokTypes.semi || this.canInsertSemicolon() || this.isContextual("as"))) {
|
|
201
|
+
const dummy = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc);
|
|
202
|
+
dummy.name = "✖";
|
|
203
|
+
dummy.isDummy = true;
|
|
204
|
+
return this.finishNode(dummy, "Identifier");
|
|
205
|
+
}
|
|
206
|
+
return this.parseExprAtom();
|
|
207
|
+
}
|
|
208
|
+
gts_parseShortcutFunction() {
|
|
209
|
+
const node = this.startNode();
|
|
210
|
+
this.enterScope(acornScope.SCOPE_FUNCTION | acornScope.SCOPE_ARROW);
|
|
211
|
+
const oldShortcutContext = this.isShortcutContext;
|
|
212
|
+
this.isShortcutContext = true;
|
|
213
|
+
if (this.type === tokTypes.parenL) {
|
|
214
|
+
this.next();
|
|
215
|
+
node.expression = true;
|
|
216
|
+
node.body = this.parseExpression();
|
|
217
|
+
this.expect(tokTypes.parenR);
|
|
218
|
+
} else if (this.type === tokTypes.braceL) {
|
|
219
|
+
node.expression = false;
|
|
220
|
+
node.body = this.parseBlock();
|
|
221
|
+
}
|
|
222
|
+
this.exitScope();
|
|
223
|
+
this.isShortcutContext = oldShortcutContext;
|
|
224
|
+
return this.finishNode(node, "GTSShortcutFunctionExpression");
|
|
225
|
+
}
|
|
226
|
+
parseExprAtom(refDestructuringErrors, forInit, forNew) {
|
|
227
|
+
if (this.type === tokTypes.colon) {
|
|
228
|
+
if (!this.isShortcutContext) this.raise(this.start, "ShortcutArgumentExpression ':' must be inside ShortcutFunction or DirectShortcutFunction.");
|
|
229
|
+
const node = this.startNode();
|
|
230
|
+
this.next();
|
|
231
|
+
if (this.gtsOptions.allowEmptyShortcutMember && this.type !== tokTypes.name) {
|
|
232
|
+
const dummy = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc);
|
|
233
|
+
dummy.name = "✖";
|
|
234
|
+
dummy.isDummy = true;
|
|
235
|
+
node.property = this.finishNode(dummy, "Identifier");
|
|
236
|
+
} else node.property = this.parseIdent();
|
|
237
|
+
return this.finishNode(node, "GTSShortcutArgumentExpression");
|
|
238
|
+
}
|
|
239
|
+
return super.parseExprAtom(refDestructuringErrors, forInit, forNew);
|
|
240
|
+
}
|
|
241
|
+
parseMaybeUnary(refDestructuringErrors, sawUnary, incDec, forInit) {
|
|
242
|
+
if (this.isShortcutContext && this.isContextual("query")) {
|
|
243
|
+
const expr = this.gts_parseQueryExpression();
|
|
244
|
+
if (!incDec && this.eat(tokTypes.starstar)) this.unexpected(this.lastTokStart);
|
|
245
|
+
return expr;
|
|
246
|
+
}
|
|
247
|
+
return super.parseMaybeUnary(refDestructuringErrors, sawUnary, incDec, forInit);
|
|
248
|
+
}
|
|
249
|
+
gts_parseQueryExpression(forInit) {
|
|
250
|
+
const node = this.startNode();
|
|
251
|
+
this.next();
|
|
252
|
+
if (this.eat(tokTypes.star)) node.star = true;
|
|
253
|
+
node.argument = this.parseMaybeUnary(null, true, false, forInit);
|
|
254
|
+
return this.finishNode(node, "GTSQueryExpression");
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
};
|
|
304
258
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region src/parse/comment.js
|
|
261
|
+
/**
|
|
262
|
+
* @import { AST, Parse } from '../types.js'
|
|
263
|
+
*/
|
|
264
|
+
/**
|
|
265
|
+
* Acorn doesn't add comments to the AST by itself. This factory returns the capabilities
|
|
266
|
+
* to add them after the fact. They are needed in order to support `ripple-ignore` comments
|
|
267
|
+
* in JS code and so that `prettier-plugin` doesn't remove all comments when formatting.
|
|
268
|
+
* @param {string} source
|
|
269
|
+
* @param {AST.CommentWithLocation[]} comments
|
|
270
|
+
* @param {number} [index=0] - Starting index
|
|
271
|
+
* @returns {{onComment: Parse.Options['onComment'], addComments: (ast: AST.Node) => void}}
|
|
272
|
+
*/
|
|
308
273
|
function getCommentHandlers(source, comments, index = 0) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
];
|
|
501
|
-
}
|
|
502
|
-
} else {
|
|
503
|
-
node.trailingComments = [
|
|
504
|
-
comments.shift()
|
|
505
|
-
];
|
|
506
|
-
}
|
|
507
|
-
} else if (hasBlankLine && onlyWhitespace && node_array) {
|
|
508
|
-
const isStatementContext = parent.type === "BlockStatement" || parent.type === "Program";
|
|
509
|
-
if (!isStatementContext) {
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
const currentIndex = node_array.indexOf(node);
|
|
513
|
-
const nextSibling = node_array[currentIndex + 1];
|
|
514
|
-
if (nextSibling && nextSibling.loc) {
|
|
515
|
-
let lastCommentIndex = 0;
|
|
516
|
-
let lastCommentEnd = comments[0].end;
|
|
517
|
-
while (comments[lastCommentIndex + 1]) {
|
|
518
|
-
const currentComment = comments[lastCommentIndex];
|
|
519
|
-
const nextComment = comments[lastCommentIndex + 1];
|
|
520
|
-
const sliceBetween = source.slice(currentComment.end, nextComment.start);
|
|
521
|
-
if (/\n\s*\n/.test(sliceBetween)) {
|
|
522
|
-
break;
|
|
523
|
-
}
|
|
524
|
-
lastCommentIndex++;
|
|
525
|
-
lastCommentEnd = nextComment.end;
|
|
526
|
-
}
|
|
527
|
-
const sliceAfterComments = source.slice(lastCommentEnd, nextSibling.start);
|
|
528
|
-
const hasBlankLineAfter = /\n\s*\n/.test(sliceAfterComments);
|
|
529
|
-
if (hasBlankLineAfter) {
|
|
530
|
-
const nextIsElement = nextSibling.type === "Element";
|
|
531
|
-
const commentsInsideElement = nextIsElement && nextSibling.loc && comments.some((c) => {
|
|
532
|
-
if (!c.loc)
|
|
533
|
-
return false;
|
|
534
|
-
return c.loc.start.line >= nextSibling.loc.start.line && c.loc.end.line <= nextSibling.loc.end.line;
|
|
535
|
-
});
|
|
536
|
-
if (!commentsInsideElement) {
|
|
537
|
-
for (let i = 0;i <= lastCommentIndex; i++) {
|
|
538
|
-
(node.trailingComments ||= []).push(comments.shift());
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
};
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
// packages/transpiler/src/error.ts
|
|
554
|
-
class GtsTranspilerError extends Error {
|
|
555
|
-
position;
|
|
556
|
-
constructor(message, position) {
|
|
557
|
-
super(message);
|
|
558
|
-
this.position = position;
|
|
559
|
-
this.name = "GtsTranspilerError";
|
|
560
|
-
}
|
|
274
|
+
/**
|
|
275
|
+
* @param {string} text
|
|
276
|
+
* @param {number} startIndex
|
|
277
|
+
* @returns {string | null}
|
|
278
|
+
*/
|
|
279
|
+
function getNextNonWhitespaceCharacter(text, startIndex) {
|
|
280
|
+
for (let i = startIndex; i < text.length; i++) {
|
|
281
|
+
const char = text[i];
|
|
282
|
+
if (char !== " " && char !== " " && char !== "\n" && char !== "\r") return char;
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
/**
|
|
288
|
+
* @type {Parse.Options['onComment']}
|
|
289
|
+
*/
|
|
290
|
+
onComment: (block, value, start, end, start_loc, end_loc, metadata) => {
|
|
291
|
+
if (block && /\n/.test(value)) {
|
|
292
|
+
let a = start;
|
|
293
|
+
while (a > 0 && source[a - 1] !== "\n") a -= 1;
|
|
294
|
+
let b = a;
|
|
295
|
+
while (/[ \t]/.test(source[b])) b += 1;
|
|
296
|
+
const indentation = source.slice(a, b);
|
|
297
|
+
value = value.replace(new RegExp(`^${indentation}`, "gm"), "");
|
|
298
|
+
}
|
|
299
|
+
comments.push({
|
|
300
|
+
type: block ? "Block" : "Line",
|
|
301
|
+
value,
|
|
302
|
+
start,
|
|
303
|
+
end,
|
|
304
|
+
loc: {
|
|
305
|
+
start: start_loc,
|
|
306
|
+
end: end_loc
|
|
307
|
+
},
|
|
308
|
+
context: metadata ?? null
|
|
309
|
+
});
|
|
310
|
+
},
|
|
311
|
+
/**
|
|
312
|
+
* @param {AST.Node} ast
|
|
313
|
+
*/
|
|
314
|
+
addComments: (ast) => {
|
|
315
|
+
if (comments.length === 0) return;
|
|
316
|
+
comments = comments.filter((comment) => comment.start >= index).map(({ type, value, start, end, loc, context }) => ({
|
|
317
|
+
type,
|
|
318
|
+
value,
|
|
319
|
+
start,
|
|
320
|
+
end,
|
|
321
|
+
loc,
|
|
322
|
+
context
|
|
323
|
+
}));
|
|
324
|
+
walk(ast, null, { _(node, { next, path }) {
|
|
325
|
+
const metadata = node?.metadata;
|
|
326
|
+
if (metadata && metadata.commentContainerId !== void 0) while (comments[0] && comments[0].context && comments[0].context.containerId === metadata.commentContainerId && comments[0].context.beforeMeaningfulChild) {
|
|
327
|
+
const elementComment = comments.shift();
|
|
328
|
+
(metadata.elementLeadingComments ||= []).push(elementComment);
|
|
329
|
+
}
|
|
330
|
+
while (comments[0] && comments[0].start < node.start) {
|
|
331
|
+
const comment = comments.shift();
|
|
332
|
+
if (node.type === "BlockStatement") {
|
|
333
|
+
const parent = path.at(-1);
|
|
334
|
+
if (parent && (parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") && parent.body === node) {
|
|
335
|
+
(parent.comments ||= []).push(comment);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const targetAncestor = path.filter((ancestor) => ancestor && ancestor.type === "Element" && ancestor.loc).sort((a, b) => a.loc.start.line - b.loc.start.line).find((ancestor) => comment.loc.start.line < ancestor.loc.start.line);
|
|
340
|
+
if (targetAncestor) {
|
|
341
|
+
targetAncestor.metadata ??= { path: [] };
|
|
342
|
+
(targetAncestor.metadata.elementLeadingComments ||= []).push(comment);
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
(node.leadingComments ||= []).push(comment);
|
|
346
|
+
}
|
|
347
|
+
next();
|
|
348
|
+
if (comments[0]) {
|
|
349
|
+
if (node.type === "BlockStatement" && node.body.length === 0) {
|
|
350
|
+
while (comments[0] && comments[0].start < node.end && comments[0].end < node.end) {
|
|
351
|
+
const comment = comments.shift();
|
|
352
|
+
(node.innerComments ||= []).push(comment);
|
|
353
|
+
}
|
|
354
|
+
if (node.innerComments && node.innerComments.length > 0) return;
|
|
355
|
+
}
|
|
356
|
+
if (node.type === "JSXEmptyExpression") {
|
|
357
|
+
while (comments[0] && comments[0].start >= node.start && comments[0].end <= node.end) {
|
|
358
|
+
const comment = comments.shift();
|
|
359
|
+
(node.innerComments ||= []).push(comment);
|
|
360
|
+
}
|
|
361
|
+
if (node.innerComments && node.innerComments.length > 0) return;
|
|
362
|
+
}
|
|
363
|
+
if (node.type === "Element" && (!node.children || node.children.length === 0)) {
|
|
364
|
+
while (comments[0] && comments[0].start < node.end && comments[0].end < node.end) {
|
|
365
|
+
const comment = comments.shift();
|
|
366
|
+
(node.innerComments ||= []).push(comment);
|
|
367
|
+
}
|
|
368
|
+
if (node.innerComments && node.innerComments.length > 0) return;
|
|
369
|
+
}
|
|
370
|
+
const parent = path.at(-1);
|
|
371
|
+
if (parent === void 0 || node.end !== parent.end) {
|
|
372
|
+
const slice = source.slice(node.end, comments[0].start);
|
|
373
|
+
let is_last_in_array = false;
|
|
374
|
+
/** @type {(AST.Node | null)[] | null} */
|
|
375
|
+
let node_array = null;
|
|
376
|
+
let isParam = false;
|
|
377
|
+
let isArgument = false;
|
|
378
|
+
let isSwitchCaseSibling = false;
|
|
379
|
+
if (parent.type === "BlockStatement" || parent.type === "Program" || parent.type === "Component" || parent.type === "ClassBody") node_array = parent.body;
|
|
380
|
+
else if (parent.type === "SwitchStatement") {
|
|
381
|
+
node_array = parent.cases;
|
|
382
|
+
isSwitchCaseSibling = true;
|
|
383
|
+
} else if (parent.type === "SwitchCase") node_array = parent.consequent;
|
|
384
|
+
else if (parent.type === "ArrayExpression" || parent.type === "TrackedArrayExpression") node_array = parent.elements;
|
|
385
|
+
else if (parent.type === "ObjectExpression" || parent.type === "TrackedObjectExpression") node_array = parent.properties;
|
|
386
|
+
else if (parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") {
|
|
387
|
+
node_array = parent.params;
|
|
388
|
+
isParam = true;
|
|
389
|
+
} else if (parent.type === "CallExpression" || parent.type === "NewExpression") {
|
|
390
|
+
node_array = parent.arguments;
|
|
391
|
+
isArgument = true;
|
|
392
|
+
}
|
|
393
|
+
if (node_array && Array.isArray(node_array)) is_last_in_array = node_array.indexOf(node) === node_array.length - 1;
|
|
394
|
+
if (is_last_in_array) if (isParam || isArgument) while (comments.length) {
|
|
395
|
+
const potentialComment = comments[0];
|
|
396
|
+
if (parent && potentialComment.start >= parent.end) break;
|
|
397
|
+
if (getNextNonWhitespaceCharacter(source, potentialComment.end) === ")") {
|
|
398
|
+
(node.trailingComments ||= []).push(comments.shift());
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
node.end;
|
|
405
|
+
while (comments.length) {
|
|
406
|
+
const comment = comments[0];
|
|
407
|
+
if (parent && comment.start >= parent.end) break;
|
|
408
|
+
(node.trailingComments ||= []).push(comment);
|
|
409
|
+
comments.shift();
|
|
410
|
+
comment.end;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
else if (node.end <= comments[0].start) {
|
|
414
|
+
const onlySimpleWhitespace = /^[,) \t]*$/.test(slice);
|
|
415
|
+
const onlyWhitespace = /^\s*$/.test(slice);
|
|
416
|
+
const hasBlankLine = /\n\s*\n/.test(slice);
|
|
417
|
+
const nodeEndLine = node.loc?.end?.line ?? null;
|
|
418
|
+
const commentStartLine = comments[0].loc?.start?.line ?? null;
|
|
419
|
+
const isImmediateNextLine = nodeEndLine !== null && commentStartLine !== null && commentStartLine === nodeEndLine + 1;
|
|
420
|
+
if (isSwitchCaseSibling && !is_last_in_array) {
|
|
421
|
+
if (nodeEndLine !== null && commentStartLine !== null && nodeEndLine === commentStartLine) node.trailingComments = [comments.shift()];
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (onlySimpleWhitespace || onlyWhitespace && !hasBlankLine && isImmediateNextLine) {
|
|
425
|
+
if (comments[0].type === "Block" && !is_last_in_array && node_array) {
|
|
426
|
+
const currentIndex = node_array.indexOf(node);
|
|
427
|
+
const nextSibling = node_array[currentIndex + 1];
|
|
428
|
+
if (nextSibling && nextSibling.loc) {
|
|
429
|
+
if (comments[0].loc?.end?.line === nextSibling.loc?.start?.line) return;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (isParam) {
|
|
433
|
+
if (source.slice(0, node.end).split("\n").length === source.slice(0, comments[0].start).split("\n").length) node.trailingComments = [comments.shift()];
|
|
434
|
+
} else node.trailingComments = [comments.shift()];
|
|
435
|
+
} else if (hasBlankLine && onlyWhitespace && node_array) {
|
|
436
|
+
if (!(parent.type === "BlockStatement" || parent.type === "Program")) return;
|
|
437
|
+
const currentIndex = node_array.indexOf(node);
|
|
438
|
+
const nextSibling = node_array[currentIndex + 1];
|
|
439
|
+
if (nextSibling && nextSibling.loc) {
|
|
440
|
+
let lastCommentIndex = 0;
|
|
441
|
+
let lastCommentEnd = comments[0].end;
|
|
442
|
+
while (comments[lastCommentIndex + 1]) {
|
|
443
|
+
const currentComment = comments[lastCommentIndex];
|
|
444
|
+
const nextComment = comments[lastCommentIndex + 1];
|
|
445
|
+
const sliceBetween = source.slice(currentComment.end, nextComment.start);
|
|
446
|
+
if (/\n\s*\n/.test(sliceBetween)) break;
|
|
447
|
+
lastCommentIndex++;
|
|
448
|
+
lastCommentEnd = nextComment.end;
|
|
449
|
+
}
|
|
450
|
+
const sliceAfterComments = source.slice(lastCommentEnd, nextSibling.start);
|
|
451
|
+
if (/\n\s*\n/.test(sliceAfterComments)) {
|
|
452
|
+
if (!(nextSibling.type === "Element" && nextSibling.loc && comments.some((c) => {
|
|
453
|
+
if (!c.loc) return false;
|
|
454
|
+
return c.loc.start.line >= nextSibling.loc.start.line && c.loc.end.line <= nextSibling.loc.end.line;
|
|
455
|
+
}))) for (let i = 0; i <= lastCommentIndex; i++) (node.trailingComments ||= []).push(comments.shift());
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
} });
|
|
463
|
+
}
|
|
464
|
+
};
|
|
561
465
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
466
|
+
//#endregion
|
|
467
|
+
//#region src/error.ts
|
|
468
|
+
var GtsTranspilerError = class extends Error {
|
|
469
|
+
position;
|
|
470
|
+
constructor(message, position) {
|
|
471
|
+
super(message);
|
|
472
|
+
this.name = "GtsTranspilerError";
|
|
473
|
+
this.position = position;
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
//#endregion
|
|
477
|
+
//#region src/parse/record_call_lparen_plugin.ts
|
|
478
|
+
/**
|
|
479
|
+
* A plugin that records the location of the left parenthesis in CallExpression and
|
|
480
|
+
* NewExpression.
|
|
481
|
+
*
|
|
482
|
+
* This is useful for Language tooling, that we can maps the '(' token from its location,
|
|
483
|
+
* to provide a signature help for user when they press `(` after a function name.
|
|
484
|
+
*
|
|
485
|
+
* The recorded location will be stored in `lParenLoc` property of the
|
|
486
|
+
* CallExpression/NewExpression node.
|
|
487
|
+
* @returns the plugin function
|
|
488
|
+
*/
|
|
565
489
|
function recordCallLParenPlugin() {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
}
|
|
597
|
-
const value = Reflect.get(target, prop);
|
|
598
|
-
if (typeof value === "function") {
|
|
599
|
-
return value.bind(target);
|
|
600
|
-
}
|
|
601
|
-
return value;
|
|
602
|
-
}
|
|
603
|
-
});
|
|
604
|
-
parseNew() {
|
|
605
|
-
const result = super.parseNew.apply(this.#proxiedThis);
|
|
606
|
-
if (this._capturedLParenLocFromNew && result.type === "NewExpression") {
|
|
607
|
-
result.lParenLoc = this._capturedLParenLocFromNew;
|
|
608
|
-
this._capturedLParenLocFromNew = null;
|
|
609
|
-
}
|
|
610
|
-
return result;
|
|
611
|
-
}
|
|
612
|
-
};
|
|
613
|
-
};
|
|
490
|
+
return function recordCallLParenPluginTransformer(parser) {
|
|
491
|
+
return class RecordCallLParenParser extends parser {
|
|
492
|
+
parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
|
|
493
|
+
let recordedLParenLoc = null;
|
|
494
|
+
if (!noCalls && this.type === tokTypes.parenL) recordedLParenLoc = [this.start, this.end];
|
|
495
|
+
const result = super.parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit);
|
|
496
|
+
if (recordedLParenLoc && result.type === "CallExpression") result.lParenRange = recordedLParenLoc;
|
|
497
|
+
return result;
|
|
498
|
+
}
|
|
499
|
+
_capturedLParenRangeFromNew = null;
|
|
500
|
+
_patchedEat = (type) => {
|
|
501
|
+
if (type === tokTypes.parenL) this._capturedLParenRangeFromNew = [this.start, this.end];
|
|
502
|
+
return this.eat(type);
|
|
503
|
+
};
|
|
504
|
+
#proxiedThis = new Proxy(this, { get: (target, prop) => {
|
|
505
|
+
if (prop === "eat") return this._patchedEat;
|
|
506
|
+
const value = Reflect.get(target, prop);
|
|
507
|
+
if (typeof value === "function") return value.bind(target);
|
|
508
|
+
return value;
|
|
509
|
+
} });
|
|
510
|
+
parseNew() {
|
|
511
|
+
const result = super.parseNew.apply(this.#proxiedThis);
|
|
512
|
+
if (this._capturedLParenRangeFromNew && result.type === "NewExpression") {
|
|
513
|
+
result.lParenRange = this._capturedLParenRangeFromNew;
|
|
514
|
+
this._capturedLParenRangeFromNew = null;
|
|
515
|
+
}
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
};
|
|
614
520
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
521
|
+
//#endregion
|
|
522
|
+
//#region src/parse/index.ts
|
|
523
|
+
const TsParser = Parser.extend(tsPlugin());
|
|
618
524
|
function parse(input, options) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
525
|
+
try {
|
|
526
|
+
return TsParser.extend(gtsPlugin(options)).parse(input, {
|
|
527
|
+
ecmaVersion: "latest",
|
|
528
|
+
sourceType: "module",
|
|
529
|
+
locations: true,
|
|
530
|
+
ranges: true
|
|
531
|
+
});
|
|
532
|
+
} catch (e) {
|
|
533
|
+
if (e instanceof SyntaxError && "loc" in e) {
|
|
534
|
+
const loc = e.loc;
|
|
535
|
+
throw new GtsTranspilerError(e.message, {
|
|
536
|
+
start: loc,
|
|
537
|
+
end: {
|
|
538
|
+
line: loc.line,
|
|
539
|
+
column: loc.column + 1
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
} else throw new GtsTranspilerError(e?.message, null);
|
|
543
|
+
}
|
|
637
544
|
}
|
|
638
545
|
function parseLoose(input, options) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
546
|
+
try {
|
|
547
|
+
const GtsParser = TsParser.extend(loosePlugin(), ...options?.recordCallLParens ? [recordCallLParenPlugin()] : [], gtsPlugin({
|
|
548
|
+
allowEmptyShortcutMember: options?.allowEmptyPositionalAttribute || true,
|
|
549
|
+
allowEmptyPositionalAttribute: options?.allowEmptyPositionalAttribute || true
|
|
550
|
+
}));
|
|
551
|
+
const { onComment, addComments } = getCommentHandlers(input, []);
|
|
552
|
+
const ast = GtsParser.parse(input, {
|
|
553
|
+
ecmaVersion: "latest",
|
|
554
|
+
sourceType: "module",
|
|
555
|
+
locations: true,
|
|
556
|
+
ranges: true,
|
|
557
|
+
onComment
|
|
558
|
+
});
|
|
559
|
+
addComments(ast);
|
|
560
|
+
return ast;
|
|
561
|
+
} catch (e) {
|
|
562
|
+
if (e instanceof SyntaxError && "loc" in e) {
|
|
563
|
+
const loc = e.loc;
|
|
564
|
+
throw new GtsTranspilerError(e.message, {
|
|
565
|
+
start: loc,
|
|
566
|
+
end: {
|
|
567
|
+
line: loc.line,
|
|
568
|
+
column: loc.column + 1
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
} else throw new GtsTranspilerError(e?.message, null);
|
|
572
|
+
}
|
|
664
573
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
var empty = {
|
|
669
|
-
type: "EmptyStatement"
|
|
670
|
-
};
|
|
574
|
+
//#endregion
|
|
575
|
+
//#region src/transform/erase_ts.ts
|
|
576
|
+
const empty = { type: "EmptyStatement" };
|
|
671
577
|
function removeThisParameter(node, context) {
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
675
|
-
return context.next();
|
|
578
|
+
if (node.params[0]?.type === "Identifier" && node.params[0].name === "this") node.params.shift();
|
|
579
|
+
return context.next();
|
|
676
580
|
}
|
|
677
|
-
|
|
678
|
-
|
|
581
|
+
const throwInvalidFeature = (node, feature) => {
|
|
582
|
+
throw new GtsTranspilerError(`TypeScript feature not supported: ${feature}`, node.loc ?? null);
|
|
679
583
|
};
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
}
|
|
806
|
-
return context.next();
|
|
807
|
-
},
|
|
808
|
-
VariableDeclaration(node, context) {
|
|
809
|
-
if (node.declare) {
|
|
810
|
-
return empty;
|
|
811
|
-
}
|
|
812
|
-
return context.next();
|
|
813
|
-
},
|
|
814
|
-
TSModuleDeclaration(node, context) {
|
|
815
|
-
if (!node.body)
|
|
816
|
-
return empty;
|
|
817
|
-
const cleaned = node.body.body.map((entry) => context.visit(entry));
|
|
818
|
-
if (cleaned.some((entry) => entry !== empty)) {
|
|
819
|
-
throwInvalidFeature(node, "namespaces with non-type nodes");
|
|
820
|
-
}
|
|
821
|
-
return empty;
|
|
822
|
-
}
|
|
584
|
+
const eraseTsVisitor = {
|
|
585
|
+
_(node, context) {
|
|
586
|
+
const n = context.next() ?? node;
|
|
587
|
+
delete n.typeAnnotation;
|
|
588
|
+
delete n.typeParameters;
|
|
589
|
+
delete n.typeArguments;
|
|
590
|
+
delete n.returnType;
|
|
591
|
+
delete n.accessibility;
|
|
592
|
+
delete n.readonly;
|
|
593
|
+
delete n.definite;
|
|
594
|
+
delete n.override;
|
|
595
|
+
},
|
|
596
|
+
Decorator(node) {
|
|
597
|
+
throwInvalidFeature(node, "decorators (related TSC proposal is not stage 4 yet)");
|
|
598
|
+
},
|
|
599
|
+
ImportDeclaration(node) {
|
|
600
|
+
if (node.importKind === "type") return empty;
|
|
601
|
+
if (node.specifiers?.length > 0) {
|
|
602
|
+
const specifiers = node.specifiers.filter((s) => s.importKind !== "type");
|
|
603
|
+
if (specifiers.length === 0) return empty;
|
|
604
|
+
return {
|
|
605
|
+
...node,
|
|
606
|
+
specifiers
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
return node;
|
|
610
|
+
},
|
|
611
|
+
ExportNamedDeclaration(node, context) {
|
|
612
|
+
if (node.exportKind === "type") return empty;
|
|
613
|
+
if (node.declaration) {
|
|
614
|
+
const result = context.next();
|
|
615
|
+
if (result?.declaration?.type === "EmptyStatement") return empty;
|
|
616
|
+
return result;
|
|
617
|
+
}
|
|
618
|
+
if (node.specifiers) {
|
|
619
|
+
const specifiers = node.specifiers.filter((s) => s.exportKind !== "type");
|
|
620
|
+
if (specifiers.length === 0) return empty;
|
|
621
|
+
return {
|
|
622
|
+
...node,
|
|
623
|
+
specifiers
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
return node;
|
|
627
|
+
},
|
|
628
|
+
ExportDefaultDeclaration(node) {
|
|
629
|
+
if (node.exportKind === "type") return empty;
|
|
630
|
+
return node;
|
|
631
|
+
},
|
|
632
|
+
ExportAllDeclaration(node) {
|
|
633
|
+
if (node.exportKind === "type") return empty;
|
|
634
|
+
return node;
|
|
635
|
+
},
|
|
636
|
+
PropertyDefinition(node, { next }) {
|
|
637
|
+
if (node.accessor) throwInvalidFeature(node, "accessor fields (related TSC proposal is not stage 4 yet)");
|
|
638
|
+
return next();
|
|
639
|
+
},
|
|
640
|
+
TSAsExpression(node, context) {
|
|
641
|
+
return context.visit(node.expression);
|
|
642
|
+
},
|
|
643
|
+
TSSatisfiesExpression(node, context) {
|
|
644
|
+
return context.visit(node.expression);
|
|
645
|
+
},
|
|
646
|
+
TSNonNullExpression(node, context) {
|
|
647
|
+
return context.visit(node.expression);
|
|
648
|
+
},
|
|
649
|
+
TSInterfaceDeclaration() {
|
|
650
|
+
return empty;
|
|
651
|
+
},
|
|
652
|
+
TSTypeAliasDeclaration() {
|
|
653
|
+
return empty;
|
|
654
|
+
},
|
|
655
|
+
TSTypeAssertion(node, context) {
|
|
656
|
+
return context.visit(node.expression);
|
|
657
|
+
},
|
|
658
|
+
TSEnumDeclaration(node) {
|
|
659
|
+
throwInvalidFeature(node, "enums");
|
|
660
|
+
},
|
|
661
|
+
TSParameterProperty(node, context) {
|
|
662
|
+
if ((node.readonly || node.accessibility) && context.path.at(-2)?.kind === "constructor") throwInvalidFeature(node, "accessibility modifiers on constructor parameters");
|
|
663
|
+
return context.visit(node.parameter);
|
|
664
|
+
},
|
|
665
|
+
TSInstantiationExpression(node, context) {
|
|
666
|
+
return context.visit(node.expression);
|
|
667
|
+
},
|
|
668
|
+
FunctionExpression: removeThisParameter,
|
|
669
|
+
FunctionDeclaration: removeThisParameter,
|
|
670
|
+
TSDeclareFunction() {
|
|
671
|
+
return empty;
|
|
672
|
+
},
|
|
673
|
+
ClassBody(node, context) {
|
|
674
|
+
const body = [];
|
|
675
|
+
for (const _child of node.body) {
|
|
676
|
+
const child = context.visit(_child);
|
|
677
|
+
if (child.type !== "PropertyDefinition" || !child.declare) body.push(child);
|
|
678
|
+
}
|
|
679
|
+
return {
|
|
680
|
+
...node,
|
|
681
|
+
body
|
|
682
|
+
};
|
|
683
|
+
},
|
|
684
|
+
ClassDeclaration(node, context) {
|
|
685
|
+
if (node.declare) return empty;
|
|
686
|
+
delete node.abstract;
|
|
687
|
+
delete node.implements;
|
|
688
|
+
delete node.superTypeArguments;
|
|
689
|
+
return context.next();
|
|
690
|
+
},
|
|
691
|
+
ClassExpression(node, context) {
|
|
692
|
+
delete node.implements;
|
|
693
|
+
delete node.superTypeArguments;
|
|
694
|
+
return context.next();
|
|
695
|
+
},
|
|
696
|
+
MethodDefinition(node, context) {
|
|
697
|
+
if (node.abstract) return empty;
|
|
698
|
+
return context.next();
|
|
699
|
+
},
|
|
700
|
+
VariableDeclaration(node, context) {
|
|
701
|
+
if (node.declare) return empty;
|
|
702
|
+
return context.next();
|
|
703
|
+
},
|
|
704
|
+
TSModuleDeclaration(node, context) {
|
|
705
|
+
if (!node.body) return empty;
|
|
706
|
+
if (node.body.body.map((entry) => context.visit(entry)).some((entry) => entry !== empty)) throwInvalidFeature(node, "namespaces with non-type nodes");
|
|
707
|
+
return empty;
|
|
708
|
+
}
|
|
823
709
|
};
|
|
824
|
-
|
|
825
|
-
|
|
710
|
+
const eraseTs = (ast) => {
|
|
711
|
+
return walk(ast, null, eraseTsVisitor);
|
|
826
712
|
};
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
"hydro",
|
|
839
|
-
"pyro",
|
|
840
|
-
"electro",
|
|
841
|
-
"anemo",
|
|
842
|
-
"geo",
|
|
843
|
-
"dendro",
|
|
844
|
-
"omni"
|
|
713
|
+
//#endregion
|
|
714
|
+
//#region src/transform/constants.ts
|
|
715
|
+
const DEFAULT_SHORTCUT_FUNCTION_PRELUDES = [
|
|
716
|
+
"cryo",
|
|
717
|
+
"hydro",
|
|
718
|
+
"pyro",
|
|
719
|
+
"electro",
|
|
720
|
+
"anemo",
|
|
721
|
+
"geo",
|
|
722
|
+
"dendro",
|
|
723
|
+
"omni"
|
|
845
724
|
];
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
725
|
+
const DEFAULT_QUERY_BINDINGS = ["my", "opp"];
|
|
726
|
+
//#endregion
|
|
727
|
+
//#region src/transform/gts.ts
|
|
728
|
+
const commonGtsVisitor = {
|
|
729
|
+
GTSDirectFunction(node, { visit, state }) {
|
|
730
|
+
return {
|
|
731
|
+
type: "ObjectExpression",
|
|
732
|
+
properties: [
|
|
733
|
+
{
|
|
734
|
+
type: "Property",
|
|
735
|
+
key: {
|
|
736
|
+
type: "Identifier",
|
|
737
|
+
name: "name"
|
|
738
|
+
},
|
|
739
|
+
computed: false,
|
|
740
|
+
kind: "init",
|
|
741
|
+
method: false,
|
|
742
|
+
shorthand: false,
|
|
743
|
+
value: state.ActionLit,
|
|
744
|
+
loc: node.loc,
|
|
745
|
+
range: node.range
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
type: "Property",
|
|
749
|
+
key: {
|
|
750
|
+
type: "Identifier",
|
|
751
|
+
name: "positionals"
|
|
752
|
+
},
|
|
753
|
+
computed: false,
|
|
754
|
+
kind: "init",
|
|
755
|
+
method: false,
|
|
756
|
+
shorthand: false,
|
|
757
|
+
value: {
|
|
758
|
+
type: "ArrowFunctionExpression",
|
|
759
|
+
params: [],
|
|
760
|
+
body: {
|
|
761
|
+
type: "ArrayExpression",
|
|
762
|
+
elements: [{
|
|
763
|
+
type: "ArrowFunctionExpression",
|
|
764
|
+
params: state.shortcutFunctionParameters,
|
|
765
|
+
body: {
|
|
766
|
+
type: "BlockStatement",
|
|
767
|
+
body: node.body.map((stmt) => visit(stmt))
|
|
768
|
+
},
|
|
769
|
+
expression: false
|
|
770
|
+
}]
|
|
771
|
+
},
|
|
772
|
+
expression: true
|
|
773
|
+
}
|
|
774
|
+
},
|
|
775
|
+
{
|
|
776
|
+
type: "Property",
|
|
777
|
+
key: {
|
|
778
|
+
type: "Identifier",
|
|
779
|
+
name: "named"
|
|
780
|
+
},
|
|
781
|
+
computed: false,
|
|
782
|
+
kind: "init",
|
|
783
|
+
method: false,
|
|
784
|
+
shorthand: false,
|
|
785
|
+
value: {
|
|
786
|
+
type: "Literal",
|
|
787
|
+
value: null
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
],
|
|
791
|
+
loc: node.loc,
|
|
792
|
+
range: node.range
|
|
793
|
+
};
|
|
794
|
+
},
|
|
795
|
+
GTSShortcutFunctionExpression(node, { visit, state }) {
|
|
796
|
+
return {
|
|
797
|
+
type: "ArrowFunctionExpression",
|
|
798
|
+
params: state.shortcutFunctionParameters,
|
|
799
|
+
body: visit(node.body),
|
|
800
|
+
expression: node.expression,
|
|
801
|
+
loc: node.loc,
|
|
802
|
+
range: node.range
|
|
803
|
+
};
|
|
804
|
+
},
|
|
805
|
+
GTSShortcutArgumentExpression(node, { state, visit }) {
|
|
806
|
+
return {
|
|
807
|
+
type: "MemberExpression",
|
|
808
|
+
object: state.fnArgId,
|
|
809
|
+
computed: false,
|
|
810
|
+
optional: false,
|
|
811
|
+
property: visit(node.property),
|
|
812
|
+
loc: node.loc,
|
|
813
|
+
range: node.range
|
|
814
|
+
};
|
|
815
|
+
},
|
|
816
|
+
GTSQueryExpression(node, { state, visit }) {
|
|
817
|
+
state.hasQueryExpressions = true;
|
|
818
|
+
return {
|
|
819
|
+
...node,
|
|
820
|
+
type: "CallExpression",
|
|
821
|
+
optional: false,
|
|
822
|
+
callee: state.queryFnId,
|
|
823
|
+
arguments: [{
|
|
824
|
+
type: "ArrowFunctionExpression",
|
|
825
|
+
body: visit(node.argument),
|
|
826
|
+
params: state.queryParameters,
|
|
827
|
+
expression: true,
|
|
828
|
+
loc: node.argument.loc,
|
|
829
|
+
range: node.argument.range
|
|
830
|
+
}, {
|
|
831
|
+
type: "ObjectExpression",
|
|
832
|
+
properties: [{
|
|
833
|
+
type: "Property",
|
|
834
|
+
key: {
|
|
835
|
+
type: "Identifier",
|
|
836
|
+
name: "star"
|
|
837
|
+
},
|
|
838
|
+
computed: false,
|
|
839
|
+
kind: "init",
|
|
840
|
+
method: false,
|
|
841
|
+
shorthand: false,
|
|
842
|
+
value: {
|
|
843
|
+
type: "Literal",
|
|
844
|
+
value: !!node.star
|
|
845
|
+
}
|
|
846
|
+
}, {
|
|
847
|
+
type: "Property",
|
|
848
|
+
key: {
|
|
849
|
+
type: "Identifier",
|
|
850
|
+
name: "context"
|
|
851
|
+
},
|
|
852
|
+
computed: false,
|
|
853
|
+
kind: "init",
|
|
854
|
+
method: false,
|
|
855
|
+
shorthand: false,
|
|
856
|
+
value: state.fnArgId
|
|
857
|
+
}]
|
|
858
|
+
}]
|
|
859
|
+
};
|
|
860
|
+
}
|
|
970
861
|
};
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
862
|
+
const gtsVisitor = {
|
|
863
|
+
Program(node, { state, visit }) {
|
|
864
|
+
const body = [];
|
|
865
|
+
for (const stmt of node.body) {
|
|
866
|
+
const visited = visit(stmt);
|
|
867
|
+
body.push(visited);
|
|
868
|
+
}
|
|
869
|
+
body.unshift(...state.bindingStatements);
|
|
870
|
+
state.bindingStatements = [];
|
|
871
|
+
if (state.hasQueryExpressions) body.unshift({
|
|
872
|
+
type: "ImportDeclaration",
|
|
873
|
+
specifiers: [{
|
|
874
|
+
type: "ImportDefaultSpecifier",
|
|
875
|
+
local: state.queryFnId
|
|
876
|
+
}],
|
|
877
|
+
source: {
|
|
878
|
+
type: "Literal",
|
|
879
|
+
value: `${state.providerImportSource}/query`
|
|
880
|
+
},
|
|
881
|
+
attributes: []
|
|
882
|
+
});
|
|
883
|
+
body.unshift({
|
|
884
|
+
type: "ImportDeclaration",
|
|
885
|
+
specifiers: [{
|
|
886
|
+
type: "ImportSpecifier",
|
|
887
|
+
imported: {
|
|
888
|
+
type: "Identifier",
|
|
889
|
+
name: "createDefine"
|
|
890
|
+
},
|
|
891
|
+
local: state.createDefineFnId
|
|
892
|
+
}, {
|
|
893
|
+
type: "ImportSpecifier",
|
|
894
|
+
imported: {
|
|
895
|
+
type: "Identifier",
|
|
896
|
+
name: "createBinding"
|
|
897
|
+
},
|
|
898
|
+
local: state.createBindingFnId
|
|
899
|
+
}],
|
|
900
|
+
source: {
|
|
901
|
+
type: "Literal",
|
|
902
|
+
value: state.runtimeImportSource
|
|
903
|
+
},
|
|
904
|
+
attributes: []
|
|
905
|
+
}, {
|
|
906
|
+
type: "ImportDeclaration",
|
|
907
|
+
specifiers: [{
|
|
908
|
+
type: "ImportDefaultSpecifier",
|
|
909
|
+
local: state.rootVmId
|
|
910
|
+
}],
|
|
911
|
+
source: {
|
|
912
|
+
type: "Literal",
|
|
913
|
+
value: `${state.providerImportSource}/vm`
|
|
914
|
+
},
|
|
915
|
+
attributes: []
|
|
916
|
+
});
|
|
917
|
+
return {
|
|
918
|
+
...node,
|
|
919
|
+
body
|
|
920
|
+
};
|
|
921
|
+
},
|
|
922
|
+
GTSDefineStatement(node, { state, visit }) {
|
|
923
|
+
const defineId = state.defineIdCounter++;
|
|
924
|
+
const rootAttr = visit(node.body);
|
|
925
|
+
const nodeVarId = {
|
|
926
|
+
type: "Identifier",
|
|
927
|
+
name: `__gts_node_${defineId}`
|
|
928
|
+
};
|
|
929
|
+
const bindingsVarId = {
|
|
930
|
+
type: "Identifier",
|
|
931
|
+
name: `__gts_bindings_${defineId}`
|
|
932
|
+
};
|
|
933
|
+
const newBindings = state.externalizedBindings;
|
|
934
|
+
state.externalizedBindings = [];
|
|
935
|
+
state.bindingStatements.push({
|
|
936
|
+
type: "VariableDeclaration",
|
|
937
|
+
kind: "const",
|
|
938
|
+
declarations: [{
|
|
939
|
+
type: "VariableDeclarator",
|
|
940
|
+
id: nodeVarId,
|
|
941
|
+
init: rootAttr
|
|
942
|
+
}],
|
|
943
|
+
loc: node.loc,
|
|
944
|
+
range: node.range
|
|
945
|
+
});
|
|
946
|
+
state.bindingStatements.push({
|
|
947
|
+
type: "VariableDeclaration",
|
|
948
|
+
kind: "const",
|
|
949
|
+
declarations: [{
|
|
950
|
+
type: "VariableDeclarator",
|
|
951
|
+
id: bindingsVarId,
|
|
952
|
+
init: {
|
|
953
|
+
type: "CallExpression",
|
|
954
|
+
optional: false,
|
|
955
|
+
callee: state.createBindingFnId,
|
|
956
|
+
arguments: [state.rootVmId, nodeVarId]
|
|
957
|
+
}
|
|
958
|
+
}],
|
|
959
|
+
loc: node.loc,
|
|
960
|
+
range: node.range
|
|
961
|
+
});
|
|
962
|
+
for (let i = 0; i < newBindings.length; i++) {
|
|
963
|
+
const binding = newBindings[i];
|
|
964
|
+
const decl = {
|
|
965
|
+
type: "VariableDeclaration",
|
|
966
|
+
kind: "const",
|
|
967
|
+
declarations: [{
|
|
968
|
+
type: "VariableDeclarator",
|
|
969
|
+
id: binding.bindingName,
|
|
970
|
+
init: {
|
|
971
|
+
type: "MemberExpression",
|
|
972
|
+
object: bindingsVarId,
|
|
973
|
+
property: {
|
|
974
|
+
type: "Literal",
|
|
975
|
+
value: i
|
|
976
|
+
},
|
|
977
|
+
computed: true,
|
|
978
|
+
optional: false
|
|
979
|
+
}
|
|
980
|
+
}]
|
|
981
|
+
};
|
|
982
|
+
if (binding.export) state.bindingStatements.push({
|
|
983
|
+
type: "ExportNamedDeclaration",
|
|
984
|
+
declaration: decl,
|
|
985
|
+
specifiers: [],
|
|
986
|
+
attributes: []
|
|
987
|
+
});
|
|
988
|
+
else state.bindingStatements.push(decl);
|
|
989
|
+
}
|
|
990
|
+
return {
|
|
991
|
+
type: "ExpressionStatement",
|
|
992
|
+
expression: {
|
|
993
|
+
type: "CallExpression",
|
|
994
|
+
optional: false,
|
|
995
|
+
callee: state.createDefineFnId,
|
|
996
|
+
arguments: [state.rootVmId, nodeVarId]
|
|
997
|
+
},
|
|
998
|
+
loc: node.loc,
|
|
999
|
+
range: node.range
|
|
1000
|
+
};
|
|
1001
|
+
},
|
|
1002
|
+
GTSNamedAttributeDefinition(node, { visit, state }) {
|
|
1003
|
+
const namedBody = visit(node.body);
|
|
1004
|
+
const properties = [...namedBody.properties];
|
|
1005
|
+
const nameValue = node.name.type === "Literal" ? node.name : {
|
|
1006
|
+
...node.name,
|
|
1007
|
+
type: "Literal",
|
|
1008
|
+
value: node.name.name
|
|
1009
|
+
};
|
|
1010
|
+
properties.unshift({
|
|
1011
|
+
type: "Property",
|
|
1012
|
+
key: {
|
|
1013
|
+
type: "Identifier",
|
|
1014
|
+
name: "name"
|
|
1015
|
+
},
|
|
1016
|
+
computed: false,
|
|
1017
|
+
kind: "init",
|
|
1018
|
+
method: false,
|
|
1019
|
+
shorthand: false,
|
|
1020
|
+
value: nameValue,
|
|
1021
|
+
loc: node.loc,
|
|
1022
|
+
range: node.range
|
|
1023
|
+
});
|
|
1024
|
+
const body = {
|
|
1025
|
+
...namedBody,
|
|
1026
|
+
properties
|
|
1027
|
+
};
|
|
1028
|
+
if (node.bindingName) {
|
|
1029
|
+
if (node.bindingAccessModifier === "protected") throw new GtsTranspilerError("Protected bindings are not supported in this context.", node.loc ?? null);
|
|
1030
|
+
const export_ = node.bindingAccessModifier !== "private";
|
|
1031
|
+
state.externalizedBindings.push({
|
|
1032
|
+
bindingName: node.bindingName,
|
|
1033
|
+
export: export_
|
|
1034
|
+
});
|
|
1035
|
+
body.properties.push({
|
|
1036
|
+
type: "Property",
|
|
1037
|
+
key: {
|
|
1038
|
+
type: "Identifier",
|
|
1039
|
+
name: "binding"
|
|
1040
|
+
},
|
|
1041
|
+
computed: false,
|
|
1042
|
+
kind: "init",
|
|
1043
|
+
method: false,
|
|
1044
|
+
shorthand: false,
|
|
1045
|
+
value: {
|
|
1046
|
+
type: "Literal",
|
|
1047
|
+
value: export_ ? "public" : "private"
|
|
1048
|
+
}
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
return body;
|
|
1052
|
+
},
|
|
1053
|
+
GTSAttributeBody(node, { visit }) {
|
|
1054
|
+
const positionals = visit(node.positionalAttributes);
|
|
1055
|
+
const named = node.namedAttributes ? visit(node.namedAttributes) : {
|
|
1056
|
+
type: "Literal",
|
|
1057
|
+
value: null
|
|
1058
|
+
};
|
|
1059
|
+
return {
|
|
1060
|
+
type: "ObjectExpression",
|
|
1061
|
+
properties: [{
|
|
1062
|
+
type: "Property",
|
|
1063
|
+
key: {
|
|
1064
|
+
type: "Identifier",
|
|
1065
|
+
name: "positionals"
|
|
1066
|
+
},
|
|
1067
|
+
computed: false,
|
|
1068
|
+
kind: "init",
|
|
1069
|
+
method: false,
|
|
1070
|
+
shorthand: false,
|
|
1071
|
+
value: {
|
|
1072
|
+
type: "ArrowFunctionExpression",
|
|
1073
|
+
params: [],
|
|
1074
|
+
body: positionals,
|
|
1075
|
+
expression: true,
|
|
1076
|
+
loc: positionals.loc,
|
|
1077
|
+
range: positionals.range
|
|
1078
|
+
},
|
|
1079
|
+
loc: positionals.loc,
|
|
1080
|
+
range: positionals.range
|
|
1081
|
+
}, {
|
|
1082
|
+
type: "Property",
|
|
1083
|
+
key: {
|
|
1084
|
+
type: "Identifier",
|
|
1085
|
+
name: "named"
|
|
1086
|
+
},
|
|
1087
|
+
computed: false,
|
|
1088
|
+
kind: "init",
|
|
1089
|
+
method: false,
|
|
1090
|
+
shorthand: false,
|
|
1091
|
+
value: named,
|
|
1092
|
+
loc: named.loc,
|
|
1093
|
+
range: named.range
|
|
1094
|
+
}],
|
|
1095
|
+
loc: node.loc,
|
|
1096
|
+
range: node.range
|
|
1097
|
+
};
|
|
1098
|
+
},
|
|
1099
|
+
GTSPositionalAttributeList(node, { visit }) {
|
|
1100
|
+
return {
|
|
1101
|
+
type: "ArrayExpression",
|
|
1102
|
+
elements: node.attributes.map((attr) => {
|
|
1103
|
+
if (attr.type === "Identifier" && /^[a-z_]/.test(attr.name)) return {
|
|
1104
|
+
...attr,
|
|
1105
|
+
type: "Literal",
|
|
1106
|
+
value: attr.name
|
|
1107
|
+
};
|
|
1108
|
+
else return visit(attr);
|
|
1109
|
+
}),
|
|
1110
|
+
loc: node.loc,
|
|
1111
|
+
range: node.range
|
|
1112
|
+
};
|
|
1113
|
+
},
|
|
1114
|
+
GTSNamedAttributeBlock(node, { visit }) {
|
|
1115
|
+
const attributes = node.attributes.map((node) => visit(node));
|
|
1116
|
+
if (node.directAction) attributes.push(visit(node.directAction));
|
|
1117
|
+
return {
|
|
1118
|
+
type: "ObjectExpression",
|
|
1119
|
+
properties: [{
|
|
1120
|
+
type: "Property",
|
|
1121
|
+
key: {
|
|
1122
|
+
type: "Identifier",
|
|
1123
|
+
name: "attributes"
|
|
1124
|
+
},
|
|
1125
|
+
computed: false,
|
|
1126
|
+
kind: "init",
|
|
1127
|
+
method: false,
|
|
1128
|
+
shorthand: false,
|
|
1129
|
+
value: {
|
|
1130
|
+
type: "ArrayExpression",
|
|
1131
|
+
elements: attributes
|
|
1132
|
+
}
|
|
1133
|
+
}],
|
|
1134
|
+
loc: node.loc,
|
|
1135
|
+
range: node.range
|
|
1136
|
+
};
|
|
1137
|
+
},
|
|
1138
|
+
...commonGtsVisitor
|
|
1239
1139
|
};
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1140
|
+
const initialTranspileState = (option = {}) => {
|
|
1141
|
+
const shortcutFunctionPreludes = option.shortcutFunctionPreludes ?? DEFAULT_SHORTCUT_FUNCTION_PRELUDES;
|
|
1142
|
+
const queryBindings = option.queryBindings ?? DEFAULT_QUERY_BINDINGS;
|
|
1143
|
+
const fnArgId = {
|
|
1144
|
+
type: "Identifier",
|
|
1145
|
+
name: "__gts_fnArg"
|
|
1146
|
+
};
|
|
1147
|
+
const PreludeLit = {
|
|
1148
|
+
type: "Literal",
|
|
1149
|
+
value: "~prelude"
|
|
1150
|
+
};
|
|
1151
|
+
return {
|
|
1152
|
+
createDefineFnId: {
|
|
1153
|
+
type: "Identifier",
|
|
1154
|
+
name: "__gts_createDefine"
|
|
1155
|
+
},
|
|
1156
|
+
createBindingFnId: {
|
|
1157
|
+
type: "Identifier",
|
|
1158
|
+
name: "__gts_createBinding"
|
|
1159
|
+
},
|
|
1160
|
+
ActionLit: {
|
|
1161
|
+
type: "Literal",
|
|
1162
|
+
value: "~action"
|
|
1163
|
+
},
|
|
1164
|
+
PreludeLit,
|
|
1165
|
+
fnArgId,
|
|
1166
|
+
shortcutFunctionParameters: [fnArgId, {
|
|
1167
|
+
type: "AssignmentPattern",
|
|
1168
|
+
left: {
|
|
1169
|
+
type: "ObjectPattern",
|
|
1170
|
+
properties: shortcutFunctionPreludes.map((name) => ({
|
|
1171
|
+
type: "Property",
|
|
1172
|
+
computed: false,
|
|
1173
|
+
key: {
|
|
1174
|
+
type: "Identifier",
|
|
1175
|
+
name
|
|
1176
|
+
},
|
|
1177
|
+
value: {
|
|
1178
|
+
type: "Identifier",
|
|
1179
|
+
name
|
|
1180
|
+
},
|
|
1181
|
+
kind: "init",
|
|
1182
|
+
method: false,
|
|
1183
|
+
shorthand: true
|
|
1184
|
+
}))
|
|
1185
|
+
},
|
|
1186
|
+
right: {
|
|
1187
|
+
type: "MemberExpression",
|
|
1188
|
+
object: fnArgId,
|
|
1189
|
+
property: PreludeLit,
|
|
1190
|
+
computed: true,
|
|
1191
|
+
optional: false
|
|
1192
|
+
}
|
|
1193
|
+
}],
|
|
1194
|
+
rootVmId: {
|
|
1195
|
+
type: "Identifier",
|
|
1196
|
+
name: "__gts_rootVm"
|
|
1197
|
+
},
|
|
1198
|
+
queryFnId: {
|
|
1199
|
+
type: "Identifier",
|
|
1200
|
+
name: "__gts_query"
|
|
1201
|
+
},
|
|
1202
|
+
queryParameters: [{
|
|
1203
|
+
type: "ObjectPattern",
|
|
1204
|
+
properties: queryBindings.map((name) => ({
|
|
1205
|
+
type: "Property",
|
|
1206
|
+
computed: false,
|
|
1207
|
+
key: {
|
|
1208
|
+
type: "Identifier",
|
|
1209
|
+
name
|
|
1210
|
+
},
|
|
1211
|
+
value: {
|
|
1212
|
+
type: "Identifier",
|
|
1213
|
+
name
|
|
1214
|
+
},
|
|
1215
|
+
kind: "init",
|
|
1216
|
+
method: false,
|
|
1217
|
+
shorthand: true
|
|
1218
|
+
}))
|
|
1219
|
+
}],
|
|
1220
|
+
runtimeImportSource: option.runtimeImportSource ?? "@gi-tcg/gts-runtime",
|
|
1221
|
+
providerImportSource: option.providerImportSource ?? "@gi-tcg/core/gts",
|
|
1222
|
+
queryArg: {
|
|
1223
|
+
type: "ObjectPattern",
|
|
1224
|
+
properties: (option.queryBindings ?? []).map((name) => ({
|
|
1225
|
+
type: "Property",
|
|
1226
|
+
key: {
|
|
1227
|
+
type: "Identifier",
|
|
1228
|
+
name
|
|
1229
|
+
},
|
|
1230
|
+
computed: false,
|
|
1231
|
+
kind: "init",
|
|
1232
|
+
method: false,
|
|
1233
|
+
shorthand: true,
|
|
1234
|
+
value: {
|
|
1235
|
+
type: "Identifier",
|
|
1236
|
+
name
|
|
1237
|
+
}
|
|
1238
|
+
}))
|
|
1239
|
+
},
|
|
1240
|
+
externalizedBindings: [],
|
|
1241
|
+
hasQueryExpressions: false,
|
|
1242
|
+
defineIdCounter: 0,
|
|
1243
|
+
bindingStatements: []
|
|
1244
|
+
};
|
|
1316
1245
|
};
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
return walk3(ast, state, gtsVisitor);
|
|
1246
|
+
const gtsToTs = (ast, option = {}) => {
|
|
1247
|
+
return walk(ast, initialTranspileState(option), gtsVisitor);
|
|
1320
1248
|
};
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
tail: true
|
|
1342
|
-
}
|
|
1343
|
-
]
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1346
|
-
};
|
|
1249
|
+
//#endregion
|
|
1250
|
+
//#region src/transform/volar/replacements.ts
|
|
1251
|
+
const createReplacementHolder = (state, value) => {
|
|
1252
|
+
const rawValue = JSON.stringify(value);
|
|
1253
|
+
return {
|
|
1254
|
+
type: "ExpressionStatement",
|
|
1255
|
+
expression: {
|
|
1256
|
+
type: "TaggedTemplateExpression",
|
|
1257
|
+
tag: state.replacementTag,
|
|
1258
|
+
quasi: {
|
|
1259
|
+
type: "TemplateLiteral",
|
|
1260
|
+
expressions: [],
|
|
1261
|
+
quasis: [{
|
|
1262
|
+
type: "TemplateElement",
|
|
1263
|
+
value: { raw: rawValue },
|
|
1264
|
+
tail: true
|
|
1265
|
+
}]
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
};
|
|
1347
1269
|
};
|
|
1348
|
-
function applyReplacements(state, code) {
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1270
|
+
function applyReplacements(state, code, mappings) {
|
|
1271
|
+
const replacementRegex = new RegExp("\\b" + state.replacementTag.name + "`(.*?)(?<!\\\\)`", "gm");
|
|
1272
|
+
const { NamedDefinitionLit, MetaLit } = state;
|
|
1273
|
+
const NamedDefinition = JSON.stringify(NamedDefinitionLit.value);
|
|
1274
|
+
const Meta = JSON.stringify(MetaLit.value);
|
|
1275
|
+
const matchInfos = [];
|
|
1276
|
+
const result = code.replace(replacementRegex, (match, rawPayload, offset) => {
|
|
1277
|
+
const payload = JSON.parse(rawPayload.replace(/\\`/g, "`"));
|
|
1278
|
+
let replacement;
|
|
1279
|
+
if (payload.type === "preface") replacement = dedent`
|
|
1358
1280
|
namespace ${state.utilNsId.name} {
|
|
1359
1281
|
export type UniqueKeyProbSegment = "__gts_unique_prob_seg__";
|
|
1360
1282
|
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
1361
1283
|
}
|
|
1362
1284
|
`;
|
|
1363
|
-
|
|
1364
|
-
return oneLine`
|
|
1285
|
+
else if (payload.type === "enterVMFromRoot") replacement = dedent`
|
|
1365
1286
|
type ${payload.defType} = (typeof ${payload.vm})[${NamedDefinition}];
|
|
1366
1287
|
type ${payload.metaType} = ${payload.defType}[${Meta}];
|
|
1367
1288
|
`;
|
|
1368
|
-
|
|
1369
|
-
return oneLine`
|
|
1289
|
+
else if (payload.type === "enterVMFromAttr") replacement = dedent`
|
|
1370
1290
|
type ${payload.defType} = ${payload.returnType} extends { namedDefinition: infer Def } ? Def : { ${Meta}: unknown };
|
|
1371
1291
|
type ${payload.metaType} = ${payload.defType}[${Meta}];
|
|
1372
1292
|
`;
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1293
|
+
else if (payload.type === "exitVM") {
|
|
1294
|
+
const lhs = `${payload.finalMetaType}_lhs`;
|
|
1295
|
+
const requiredAttrsNs = `${payload.finalMetaType}_rans`;
|
|
1296
|
+
const collectedAttrsExpr = `${payload.collectedAttrs.join(" | ")}`;
|
|
1297
|
+
const needleString = `"${requiredAttrsNs}_NeedleString" as any as "required attributes are missing"`;
|
|
1298
|
+
if (payload.errorRange) state.extraMappings.push({
|
|
1299
|
+
sourceOffset: payload.errorRange[0],
|
|
1300
|
+
length: payload.errorRange[1] - payload.errorRange[0],
|
|
1301
|
+
generatedNeedle: needleString
|
|
1302
|
+
});
|
|
1303
|
+
replacement = dedent`
|
|
1382
1304
|
type ${payload.finalMetaType} = ${payload.metaType};
|
|
1383
1305
|
let ${lhs}!: { ${Meta}: ${payload.metaType} } & Omit<${payload.defType}, ${Meta}>;
|
|
1384
1306
|
type ${lhs} = typeof ${lhs};
|
|
@@ -1388,16 +1310,17 @@ function applyReplacements(state, code) {
|
|
|
1388
1310
|
};
|
|
1389
1311
|
((_: ${requiredAttrsNs}.Expected extends ${requiredAttrsNs}.Collected ? string : ${requiredAttrsNs}.Expected) => 0)(${needleString});
|
|
1390
1312
|
`;
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1313
|
+
} else if (payload.type === "enterAttr") {
|
|
1314
|
+
const uniqueKeyLhs = `${payload.lhs}_uniqueKey_lhs`;
|
|
1315
|
+
const uniqueKey = `${payload.lhs}_uniqueKey`;
|
|
1316
|
+
const uniqueKeyForThis = `${payload.lhs}_uniqueKeyFor_${payload.lhs}`;
|
|
1317
|
+
const uniqueKeyHelperIntf = `${payload.defType}_uniqueKeyProbeHelper`;
|
|
1318
|
+
const omittedKeys = `${payload.lhs}_omittedKeys`;
|
|
1319
|
+
replacement = dedent`
|
|
1398
1320
|
type ${uniqueKeyLhs} = {
|
|
1399
1321
|
${Meta}: ${payload.metaType};
|
|
1400
1322
|
uniqueKey: ${payload.defType} extends { [${payload.attrName}]: { uniqueKey: infer UniqueKey } } ? UniqueKey : () => 0;
|
|
1323
|
+
};
|
|
1401
1324
|
|
|
1402
1325
|
let ${uniqueKeyLhs}!: ${uniqueKeyLhs};
|
|
1403
1326
|
let ${uniqueKey} = ${uniqueKeyLhs}.uniqueKey();
|
|
@@ -1419,9 +1342,9 @@ function applyReplacements(state, code) {
|
|
|
1419
1342
|
);
|
|
1420
1343
|
let ${payload.lhs}!: { ${Meta}: ${payload.metaType} } & Omit<${payload.defType}, ${omittedKeys}>;
|
|
1421
1344
|
`;
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1345
|
+
} else if (payload.type === "createBindingTyping") {
|
|
1346
|
+
const typingIdLhs = `${payload.typingId}_lhs`;
|
|
1347
|
+
replacement = dedent`
|
|
1425
1348
|
type ${typingIdLhs} = {
|
|
1426
1349
|
${Meta}: ${payload.finalMetaType};
|
|
1427
1350
|
as: ${payload.defType} extends { [${payload.attrName}]: { as: infer As } } ? As : unknown;
|
|
@@ -1430,861 +1353,597 @@ function applyReplacements(state, code) {
|
|
|
1430
1353
|
let ${payload.typingId} = ${typingIdLhs}.as();
|
|
1431
1354
|
type ${payload.typingId} = typeof ${payload.typingId};
|
|
1432
1355
|
`;
|
|
1433
|
-
|
|
1434
|
-
return oneLine`
|
|
1356
|
+
} else if (payload.type === "exitAttr") replacement = dedent`
|
|
1435
1357
|
type ${payload.returnType} = typeof ${payload.returnType};
|
|
1436
1358
|
type ${payload.newMetaType} = ${payload.returnType} extends { rewriteMeta: infer NewMeta extends {} } ? NewMeta : ${payload.oldMetaType}
|
|
1437
1359
|
`;
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1360
|
+
else replacement = "";
|
|
1361
|
+
matchInfos.push({
|
|
1362
|
+
sourceEnd: offset + match.length,
|
|
1363
|
+
lengthOffset: replacement.length - match.length
|
|
1364
|
+
});
|
|
1365
|
+
return replacement;
|
|
1366
|
+
});
|
|
1367
|
+
for (const mapping of mappings) for (let i = 0; i < mapping.generatedOffsets.length; i++) {
|
|
1368
|
+
const orig = mapping.generatedOffsets[i];
|
|
1369
|
+
let shift = 0;
|
|
1370
|
+
for (const info of matchInfos) if (orig >= info.sourceEnd) shift += info.lengthOffset;
|
|
1371
|
+
mapping.generatedOffsets[i] = orig + shift;
|
|
1372
|
+
}
|
|
1373
|
+
return result;
|
|
1442
1374
|
}
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1375
|
+
//#endregion
|
|
1376
|
+
//#region src/transform/volar/walker.ts
|
|
1377
|
+
const EMPTY = { type: "EmptyStatement" };
|
|
1378
|
+
const ANY_INIT = {
|
|
1379
|
+
type: "TSAsExpression",
|
|
1380
|
+
expression: {
|
|
1381
|
+
type: "Literal",
|
|
1382
|
+
value: 0
|
|
1383
|
+
},
|
|
1384
|
+
typeAnnotation: { type: "TSAnyKeyword" }
|
|
1453
1385
|
};
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1386
|
+
const enterVMFromRoot = (state) => {
|
|
1387
|
+
let defTypeId = {
|
|
1388
|
+
type: "Identifier",
|
|
1389
|
+
name: `__gts_rootVmDefType_${state.idCounter++}`
|
|
1390
|
+
};
|
|
1391
|
+
let metaTypeId = {
|
|
1392
|
+
type: "Identifier",
|
|
1393
|
+
name: `__gts_rootVmInitMetaType_${state.idCounter++}`
|
|
1394
|
+
};
|
|
1395
|
+
let finalMetaTypeId = {
|
|
1396
|
+
type: "Identifier",
|
|
1397
|
+
name: `__gts_rootVmFinalMetaType_${state.idCounter++}`
|
|
1398
|
+
};
|
|
1399
|
+
state.typingPendingStatements.push(createReplacementHolder(state, {
|
|
1400
|
+
type: "enterVMFromRoot",
|
|
1401
|
+
vm: state.rootVmId.name,
|
|
1402
|
+
defType: defTypeId.name,
|
|
1403
|
+
metaType: metaTypeId.name
|
|
1404
|
+
}));
|
|
1405
|
+
state.vmDefTypeIdStack.push(defTypeId);
|
|
1406
|
+
state.metaTypeIdStack.push(metaTypeId);
|
|
1407
|
+
state.finalMetaTypeIdStack.push(finalMetaTypeId);
|
|
1408
|
+
state.attrsOfCurrentVm.push([]);
|
|
1477
1409
|
};
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1410
|
+
const enterVMFromAttr = (state, returningId) => {
|
|
1411
|
+
const defTypeId = {
|
|
1412
|
+
type: "Identifier",
|
|
1413
|
+
name: `__gts_nestedVm_${state.idCounter++}`
|
|
1414
|
+
};
|
|
1415
|
+
const metaTypeId = {
|
|
1416
|
+
type: "Identifier",
|
|
1417
|
+
name: `__gts_nestedVmInitMetaType_${state.idCounter++}`
|
|
1418
|
+
};
|
|
1419
|
+
const finalMetaTypeId = {
|
|
1420
|
+
type: "Identifier",
|
|
1421
|
+
name: `__gts_nestedVmFinalMetaType_${state.idCounter++}`
|
|
1422
|
+
};
|
|
1423
|
+
state.typingPendingStatements.push(createReplacementHolder(state, {
|
|
1424
|
+
type: "enterVMFromAttr",
|
|
1425
|
+
returnType: returningId.name,
|
|
1426
|
+
defType: defTypeId.name,
|
|
1427
|
+
metaType: metaTypeId.name
|
|
1428
|
+
}));
|
|
1429
|
+
state.vmDefTypeIdStack.push(defTypeId);
|
|
1430
|
+
state.metaTypeIdStack.push(metaTypeId);
|
|
1431
|
+
state.finalMetaTypeIdStack.push(finalMetaTypeId);
|
|
1432
|
+
state.attrsOfCurrentVm.push([]);
|
|
1501
1433
|
};
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1434
|
+
const exitVM = (state, errorRange) => {
|
|
1435
|
+
const currentDefTypeId = state.vmDefTypeIdStack.pop();
|
|
1436
|
+
const currentMetaId = state.metaTypeIdStack.pop();
|
|
1437
|
+
const finalMetaId = state.finalMetaTypeIdStack.pop();
|
|
1438
|
+
const collectedAttrNames = state.attrsOfCurrentVm.pop();
|
|
1439
|
+
state.typingPendingStatements.push(createReplacementHolder(state, {
|
|
1440
|
+
type: "exitVM",
|
|
1441
|
+
metaType: currentMetaId.name,
|
|
1442
|
+
defType: currentDefTypeId.name,
|
|
1443
|
+
finalMetaType: finalMetaId.name,
|
|
1444
|
+
collectedAttrs: collectedAttrNames,
|
|
1445
|
+
errorRange
|
|
1446
|
+
}));
|
|
1515
1447
|
};
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1448
|
+
const enterAttr = (state, attrName) => {
|
|
1449
|
+
const defTypeId = state.vmDefTypeIdStack.at(-1);
|
|
1450
|
+
const metaTypeId = state.metaTypeIdStack.at(-1);
|
|
1451
|
+
if (!defTypeId || !metaTypeId) return { lhsId: {
|
|
1452
|
+
type: "Identifier",
|
|
1453
|
+
name: "__gts_invalid_attr_obj"
|
|
1454
|
+
} };
|
|
1455
|
+
state.attrsOfCurrentVm.at(-1).push(attrName);
|
|
1456
|
+
const lhsId = {
|
|
1457
|
+
type: "Identifier",
|
|
1458
|
+
name: `__gts_attr_obj_${state.idCounter++}`
|
|
1459
|
+
};
|
|
1460
|
+
state.typingPendingStatements.push(createReplacementHolder(state, {
|
|
1461
|
+
type: "enterAttr",
|
|
1462
|
+
defType: defTypeId.name,
|
|
1463
|
+
metaType: metaTypeId.name,
|
|
1464
|
+
lhs: lhsId.name,
|
|
1465
|
+
attrName
|
|
1466
|
+
}));
|
|
1467
|
+
return { lhsId };
|
|
1535
1468
|
};
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
typingId: info.typingId.name
|
|
1548
|
-
}));
|
|
1469
|
+
const genBindingTyping = (state, info) => {
|
|
1470
|
+
const finalMetaId = state.finalMetaTypeIdStack.at(-1);
|
|
1471
|
+
const defTypeId = state.vmDefTypeIdStack.at(-1);
|
|
1472
|
+
if (!finalMetaId || !defTypeId) return;
|
|
1473
|
+
state.typingPendingStatements.push(createReplacementHolder(state, {
|
|
1474
|
+
type: "createBindingTyping",
|
|
1475
|
+
finalMetaType: finalMetaId.name,
|
|
1476
|
+
defType: defTypeId.name,
|
|
1477
|
+
attrName: info.attrName,
|
|
1478
|
+
typingId: info.typingId.name
|
|
1479
|
+
}));
|
|
1549
1480
|
};
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
returnType: returningId.name
|
|
1566
|
-
}));
|
|
1481
|
+
const exitAttr = (state, returningId) => {
|
|
1482
|
+
const currentDefId = state.vmDefTypeIdStack.at(-1);
|
|
1483
|
+
if (!currentDefId) return;
|
|
1484
|
+
const newMetaTypeId = {
|
|
1485
|
+
type: "Identifier",
|
|
1486
|
+
name: `__gts_newMeta__${state.idCounter++}`
|
|
1487
|
+
};
|
|
1488
|
+
const [oldMetaTypeId] = state.metaTypeIdStack.splice(-1, 1, newMetaTypeId);
|
|
1489
|
+
state.typingPendingStatements.push(createReplacementHolder(state, {
|
|
1490
|
+
type: "exitAttr",
|
|
1491
|
+
defType: currentDefId.name,
|
|
1492
|
+
oldMetaType: oldMetaTypeId.name,
|
|
1493
|
+
newMetaType: newMetaTypeId.name,
|
|
1494
|
+
returnType: returningId.name
|
|
1495
|
+
}));
|
|
1567
1496
|
};
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
optional: false
|
|
1783
|
-
},
|
|
1784
|
-
arguments: [fn]
|
|
1785
|
-
}
|
|
1786
|
-
}
|
|
1787
|
-
]
|
|
1788
|
-
});
|
|
1789
|
-
}
|
|
1790
|
-
return EMPTY;
|
|
1791
|
-
},
|
|
1792
|
-
...commonGtsVisitor,
|
|
1793
|
-
GTSShortcutArgumentExpression(node, { state, visit }) {
|
|
1794
|
-
const lhs = { ...state.fnArgId };
|
|
1795
|
-
return {
|
|
1796
|
-
type: "MemberExpression",
|
|
1797
|
-
object: lhs,
|
|
1798
|
-
computed: false,
|
|
1799
|
-
optional: false,
|
|
1800
|
-
property: visit(node.property)
|
|
1801
|
-
};
|
|
1802
|
-
}
|
|
1497
|
+
const gtsToTypingsWalker = {
|
|
1498
|
+
Program(node, { state, visit }) {
|
|
1499
|
+
const body = [];
|
|
1500
|
+
for (const stmt of node.body) if (stmt.type === "GTSDefineStatement") {
|
|
1501
|
+
state.defineLeadingComments = stmt.leadingComments;
|
|
1502
|
+
visit(stmt);
|
|
1503
|
+
body.push(...state.typingPendingStatements);
|
|
1504
|
+
state.typingPendingStatements = [];
|
|
1505
|
+
} else body.push(visit(stmt));
|
|
1506
|
+
for (const extBinding of state.externalizedBindings) {
|
|
1507
|
+
const varDecl = {
|
|
1508
|
+
type: "VariableDeclaration",
|
|
1509
|
+
kind: "const",
|
|
1510
|
+
declarations: [{
|
|
1511
|
+
type: "VariableDeclarator",
|
|
1512
|
+
id: extBinding.bindingName,
|
|
1513
|
+
init: ANY_INIT,
|
|
1514
|
+
typeAnnotation: {
|
|
1515
|
+
type: "TSTypeAnnotation",
|
|
1516
|
+
typeAnnotation: {
|
|
1517
|
+
type: "TSTypeReference",
|
|
1518
|
+
typeName: extBinding.typingId
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
}]
|
|
1522
|
+
};
|
|
1523
|
+
if (extBinding.export) body.unshift({
|
|
1524
|
+
type: "ExportNamedDeclaration",
|
|
1525
|
+
declaration: varDecl,
|
|
1526
|
+
specifiers: [],
|
|
1527
|
+
source: null,
|
|
1528
|
+
attributes: [],
|
|
1529
|
+
leadingComments: extBinding.leadingComments
|
|
1530
|
+
});
|
|
1531
|
+
else {
|
|
1532
|
+
varDecl.leadingComments = extBinding.leadingComments;
|
|
1533
|
+
body.unshift(varDecl);
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
const importDecls = [];
|
|
1537
|
+
if (state.hasQueryExpressions) importDecls.push({
|
|
1538
|
+
type: "ImportDeclaration",
|
|
1539
|
+
specifiers: [{
|
|
1540
|
+
type: "ImportDefaultSpecifier",
|
|
1541
|
+
local: state.queryFnId
|
|
1542
|
+
}],
|
|
1543
|
+
source: {
|
|
1544
|
+
type: "Literal",
|
|
1545
|
+
value: `${state.providerImportSource}/query`
|
|
1546
|
+
},
|
|
1547
|
+
attributes: []
|
|
1548
|
+
});
|
|
1549
|
+
importDecls.push({
|
|
1550
|
+
type: "ImportDeclaration",
|
|
1551
|
+
specifiers: [{
|
|
1552
|
+
type: "ImportDefaultSpecifier",
|
|
1553
|
+
local: state.rootVmId
|
|
1554
|
+
}],
|
|
1555
|
+
source: {
|
|
1556
|
+
type: "Literal",
|
|
1557
|
+
value: `${state.providerImportSource}/vm`
|
|
1558
|
+
},
|
|
1559
|
+
attributes: []
|
|
1560
|
+
});
|
|
1561
|
+
for (const importDecl of importDecls) {
|
|
1562
|
+
state.diagnosticsOnTopNodes.add(importDecl.source);
|
|
1563
|
+
for (const specifier of importDecl.specifiers) state.diagnosticsOnTopNodes.add(specifier);
|
|
1564
|
+
}
|
|
1565
|
+
body.unshift(...importDecls, createReplacementHolder(state, { type: "preface" }));
|
|
1566
|
+
return {
|
|
1567
|
+
...node,
|
|
1568
|
+
body
|
|
1569
|
+
};
|
|
1570
|
+
},
|
|
1571
|
+
GTSDefineStatement(node, { state, visit }) {
|
|
1572
|
+
enterVMFromRoot(state);
|
|
1573
|
+
visit(node.body);
|
|
1574
|
+
exitVM(state);
|
|
1575
|
+
return EMPTY;
|
|
1576
|
+
},
|
|
1577
|
+
GTSNamedAttributeDefinition(node, { visit, state }) {
|
|
1578
|
+
const { name, body, bindingName } = node;
|
|
1579
|
+
const attrName = JSON.stringify(name.type === "Literal" ? String(name.value) : name.name);
|
|
1580
|
+
const { lhsId } = enterAttr(state, attrName);
|
|
1581
|
+
const positionals = body.positionalAttributes.attributes.map((attr) => {
|
|
1582
|
+
if (attr.type === "Identifier" && /^[a-z_]/.test(attr.name)) {
|
|
1583
|
+
const lit = {
|
|
1584
|
+
type: "Literal",
|
|
1585
|
+
value: attr.name,
|
|
1586
|
+
loc: attr.loc,
|
|
1587
|
+
range: attr.range
|
|
1588
|
+
};
|
|
1589
|
+
state.literalFromIdentifier.add(lit);
|
|
1590
|
+
return lit;
|
|
1591
|
+
} else return { ...visit(attr) };
|
|
1592
|
+
});
|
|
1593
|
+
const returnValue = {
|
|
1594
|
+
type: "Identifier",
|
|
1595
|
+
name: `__gts_attrRet_${state.idCounter++}`
|
|
1596
|
+
};
|
|
1597
|
+
const callee = {
|
|
1598
|
+
type: "MemberExpression",
|
|
1599
|
+
object: lhsId,
|
|
1600
|
+
property: name,
|
|
1601
|
+
computed: name.type === "Literal",
|
|
1602
|
+
optional: false
|
|
1603
|
+
};
|
|
1604
|
+
if (name.range) {
|
|
1605
|
+
state.extraMappings.push({
|
|
1606
|
+
sourceOffset: name.range[0],
|
|
1607
|
+
length: name.range[1] - name.range[0],
|
|
1608
|
+
generatedNeedle: `${lhsId.name}${name.type === "Literal" ? `[` : `.`}`
|
|
1609
|
+
});
|
|
1610
|
+
state.namedAttributeCalleeLParenRange.set(callee, {
|
|
1611
|
+
start: name.range[1],
|
|
1612
|
+
end: name.range[1] + 1
|
|
1613
|
+
});
|
|
1614
|
+
}
|
|
1615
|
+
state.typingPendingStatements.push({
|
|
1616
|
+
type: "VariableDeclaration",
|
|
1617
|
+
kind: "const",
|
|
1618
|
+
declarations: [{
|
|
1619
|
+
type: "VariableDeclarator",
|
|
1620
|
+
id: returnValue,
|
|
1621
|
+
init: {
|
|
1622
|
+
type: "CallExpression",
|
|
1623
|
+
optional: false,
|
|
1624
|
+
callee,
|
|
1625
|
+
arguments: positionals
|
|
1626
|
+
}
|
|
1627
|
+
}]
|
|
1628
|
+
});
|
|
1629
|
+
if (body.namedAttributes) {
|
|
1630
|
+
enterVMFromAttr(state, returnValue);
|
|
1631
|
+
visit(body.namedAttributes);
|
|
1632
|
+
exitVM(state, body.namedAttributes.range);
|
|
1633
|
+
}
|
|
1634
|
+
if (bindingName) {
|
|
1635
|
+
const export_ = node.bindingAccessModifier !== "private";
|
|
1636
|
+
const typingId = {
|
|
1637
|
+
type: "Identifier",
|
|
1638
|
+
name: `__gts_binding_type_${state.externalizedBindings.length}`
|
|
1639
|
+
};
|
|
1640
|
+
genBindingTyping(state, {
|
|
1641
|
+
attrName,
|
|
1642
|
+
typingId
|
|
1643
|
+
});
|
|
1644
|
+
state.externalizedBindings.push({
|
|
1645
|
+
bindingName: {
|
|
1646
|
+
...bindingName,
|
|
1647
|
+
typeAnnotation: {
|
|
1648
|
+
type: "TSTypeAnnotation",
|
|
1649
|
+
typeAnnotation: {
|
|
1650
|
+
type: "TSTypeReference",
|
|
1651
|
+
typeName: typingId
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
},
|
|
1655
|
+
export: export_,
|
|
1656
|
+
typingId,
|
|
1657
|
+
leadingComments: state.defineLeadingComments
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
exitAttr(state, returnValue);
|
|
1661
|
+
return EMPTY;
|
|
1662
|
+
},
|
|
1663
|
+
GTSNamedAttributeBlock(node, { state, visit }) {
|
|
1664
|
+
for (const attr of node.attributes) visit(attr);
|
|
1665
|
+
if (node.directAction) {
|
|
1666
|
+
const attrName = JSON.stringify(state.ActionLit.value);
|
|
1667
|
+
const { lhsId } = enterAttr(state, attrName);
|
|
1668
|
+
const actionNotExistsReplacementStr = `${lhsId.name}[${attrName}]`;
|
|
1669
|
+
if (node.directAction.range) state.extraMappings.push({
|
|
1670
|
+
sourceOffset: node.directAction.range[0],
|
|
1671
|
+
length: node.directAction.range[1] - node.directAction.range[0],
|
|
1672
|
+
generatedNeedle: actionNotExistsReplacementStr
|
|
1673
|
+
});
|
|
1674
|
+
const fn = {
|
|
1675
|
+
type: "ArrowFunctionExpression",
|
|
1676
|
+
params: state.shortcutFunctionParameters,
|
|
1677
|
+
body: {
|
|
1678
|
+
type: "BlockStatement",
|
|
1679
|
+
body: node.directAction.body.map((stmt) => visit(stmt))
|
|
1680
|
+
},
|
|
1681
|
+
expression: false
|
|
1682
|
+
};
|
|
1683
|
+
const returnValue = {
|
|
1684
|
+
type: "Identifier",
|
|
1685
|
+
name: `__gts_attrRet_${state.idCounter++}`
|
|
1686
|
+
};
|
|
1687
|
+
state.typingPendingStatements.push({
|
|
1688
|
+
type: "VariableDeclaration",
|
|
1689
|
+
kind: "const",
|
|
1690
|
+
declarations: [{
|
|
1691
|
+
type: "VariableDeclarator",
|
|
1692
|
+
id: returnValue,
|
|
1693
|
+
init: {
|
|
1694
|
+
type: "CallExpression",
|
|
1695
|
+
optional: false,
|
|
1696
|
+
callee: {
|
|
1697
|
+
type: "MemberExpression",
|
|
1698
|
+
object: lhsId,
|
|
1699
|
+
property: state.ActionLit,
|
|
1700
|
+
computed: true,
|
|
1701
|
+
optional: false
|
|
1702
|
+
},
|
|
1703
|
+
arguments: [fn]
|
|
1704
|
+
}
|
|
1705
|
+
}]
|
|
1706
|
+
});
|
|
1707
|
+
}
|
|
1708
|
+
return EMPTY;
|
|
1709
|
+
},
|
|
1710
|
+
...commonGtsVisitor
|
|
1803
1711
|
};
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
verification: true
|
|
1712
|
+
//#endregion
|
|
1713
|
+
//#region src/transform/volar/mappings.ts
|
|
1714
|
+
const DEFAULT_VOLAR_MAPPING_DATA = {
|
|
1715
|
+
completion: true,
|
|
1716
|
+
format: true,
|
|
1717
|
+
navigation: true,
|
|
1718
|
+
semantic: true,
|
|
1719
|
+
structure: true,
|
|
1720
|
+
verification: true
|
|
1814
1721
|
};
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
}
|
|
1876
|
-
|
|
1877
|
-
const key = `${src_line}:${src_column}`;
|
|
1878
|
-
const positions = srcToGenMap.get(key);
|
|
1879
|
-
return positions || [];
|
|
1880
|
-
}
|
|
1881
|
-
function createLineOffsets(content) {
|
|
1882
|
-
const lines = content.split(`
|
|
1883
|
-
`);
|
|
1884
|
-
const offsets = [];
|
|
1885
|
-
let currentOffset = 0;
|
|
1886
|
-
for (const line of lines) {
|
|
1887
|
-
offsets.push(currentOffset);
|
|
1888
|
-
currentOffset += line.length + 1;
|
|
1889
|
-
}
|
|
1890
|
-
return offsets;
|
|
1891
|
-
}
|
|
1892
|
-
function locToOffset(line, column, line_offsets) {
|
|
1893
|
-
if (line < 1 || line > line_offsets.length) {}
|
|
1894
|
-
return line_offsets[line - 1] + column;
|
|
1895
|
-
}
|
|
1896
|
-
function convertToVolarMappings(generated, source, sourceMap, tokens, extraMappings) {
|
|
1897
|
-
const sourceLineOffsets = createLineOffsets(source);
|
|
1898
|
-
const generatedLineOffsets = createLineOffsets(generated);
|
|
1899
|
-
const srcToGenMap = buildSrcToGenMap(sourceMap, generatedLineOffsets, generated);
|
|
1900
|
-
const mappings = [];
|
|
1901
|
-
for (const token of tokens) {
|
|
1902
|
-
let sourceStart = locToOffset(token.loc.start.line, token.loc.start.column, sourceLineOffsets);
|
|
1903
|
-
sourceStart += token.sourceStartOffset ?? 0;
|
|
1904
|
-
const sourceEnd = locToOffset(token.loc.end.line, token.loc.end.column, sourceLineOffsets);
|
|
1905
|
-
let sourceLength = token.sourceLength ?? sourceEnd - sourceStart;
|
|
1906
|
-
sourceLength += token.sourceLengthOffset ?? 0;
|
|
1907
|
-
const [genLineCol] = getGeneratedPosition(token.loc.start.line, token.loc.start.column, srcToGenMap);
|
|
1908
|
-
if (!genLineCol) {
|
|
1909
|
-
continue;
|
|
1910
|
-
}
|
|
1911
|
-
let genStart = locToOffset(genLineCol.line, genLineCol.column, generatedLineOffsets);
|
|
1912
|
-
if (token.isDummy) {
|
|
1913
|
-
while (sourceStart > 0 && /\s/.test(source[sourceStart - 1])) {
|
|
1914
|
-
sourceStart--;
|
|
1915
|
-
sourceLength++;
|
|
1916
|
-
}
|
|
1917
|
-
}
|
|
1918
|
-
const generatedLength = token.generatedLength ?? sourceLength;
|
|
1919
|
-
if (typeof token.generatedStartOffset === "number" && token.generatedStartOffset > 0) {
|
|
1920
|
-
mappings.push({
|
|
1921
|
-
sourceOffsets: [sourceStart],
|
|
1922
|
-
generatedOffsets: [genStart],
|
|
1923
|
-
lengths: [0],
|
|
1924
|
-
generatedLengths: [generatedLength],
|
|
1925
|
-
data: {
|
|
1926
|
-
verification: true
|
|
1927
|
-
}
|
|
1928
|
-
});
|
|
1929
|
-
genStart += token.generatedStartOffset;
|
|
1930
|
-
}
|
|
1931
|
-
mappings.push({
|
|
1932
|
-
sourceOffsets: [sourceStart],
|
|
1933
|
-
generatedOffsets: [genStart],
|
|
1934
|
-
lengths: [sourceLength],
|
|
1935
|
-
generatedLengths: [generatedLength],
|
|
1936
|
-
data: DEFAULT_VOLAR_MAPPING_DATA
|
|
1937
|
-
});
|
|
1938
|
-
}
|
|
1939
|
-
const locMapsToTop = getGeneratedPosition(1, 0, srcToGenMap);
|
|
1940
|
-
for (const loc of locMapsToTop) {
|
|
1941
|
-
const offset = locToOffset(loc.line, loc.column, generatedLineOffsets);
|
|
1942
|
-
mappings.push({
|
|
1943
|
-
sourceOffsets: [0],
|
|
1944
|
-
generatedOffsets: [offset],
|
|
1945
|
-
lengths: [0],
|
|
1946
|
-
generatedLengths: [0],
|
|
1947
|
-
data: {
|
|
1948
|
-
verification: true
|
|
1949
|
-
}
|
|
1950
|
-
});
|
|
1951
|
-
}
|
|
1952
|
-
for (const [loc, codeSnippet] of extraMappings) {
|
|
1953
|
-
const generatedStart = generated.indexOf(codeSnippet);
|
|
1954
|
-
if (generatedStart === -1) {
|
|
1955
|
-
continue;
|
|
1956
|
-
}
|
|
1957
|
-
const [lineStr, columnStr] = loc.split(":");
|
|
1958
|
-
const line = Number(lineStr);
|
|
1959
|
-
const column = Number(columnStr);
|
|
1960
|
-
const sourceStart = locToOffset(line, column, sourceLineOffsets);
|
|
1961
|
-
const sourceLength = 1;
|
|
1962
|
-
const generatedLength = codeSnippet.length;
|
|
1963
|
-
mappings.push({
|
|
1964
|
-
sourceOffsets: [sourceStart],
|
|
1965
|
-
generatedOffsets: [generatedStart],
|
|
1966
|
-
lengths: [sourceLength],
|
|
1967
|
-
generatedLengths: [generatedLength],
|
|
1968
|
-
data: {
|
|
1969
|
-
verification: true
|
|
1970
|
-
}
|
|
1971
|
-
});
|
|
1972
|
-
}
|
|
1973
|
-
mappings.sort((a, b) => a.sourceOffsets[0] - b.sourceOffsets[0]);
|
|
1974
|
-
return mappings;
|
|
1975
|
-
}
|
|
1976
|
-
|
|
1977
|
-
// packages/transpiler/src/transform/volar/collect_tokens.ts
|
|
1978
|
-
import { walk as walk4 } from "zimmerframe";
|
|
1979
|
-
function isLeafNode(node) {
|
|
1980
|
-
for (const key in node) {
|
|
1981
|
-
const val = node[key];
|
|
1982
|
-
if (key === "loc" || key === "start" || key === "end" || key === "range") {
|
|
1983
|
-
continue;
|
|
1984
|
-
}
|
|
1985
|
-
if (val && typeof val === "object" && typeof val.type === "string") {
|
|
1986
|
-
return false;
|
|
1987
|
-
}
|
|
1988
|
-
if (Array.isArray(val) && val.length > 0 && typeof val[0].type === "string") {
|
|
1989
|
-
return false;
|
|
1990
|
-
}
|
|
1991
|
-
}
|
|
1992
|
-
return true;
|
|
1993
|
-
}
|
|
1994
|
-
function collectLeafTokens(ast) {
|
|
1995
|
-
const state = {
|
|
1996
|
-
tokens: []
|
|
1997
|
-
};
|
|
1998
|
-
walk4(ast, state, {
|
|
1999
|
-
_(node, { state: state2, next }) {
|
|
2000
|
-
if (isLeafNode(node) && node.loc) {
|
|
2001
|
-
const token = {
|
|
2002
|
-
loc: node.loc
|
|
2003
|
-
};
|
|
2004
|
-
if ("isDummy" in node && node.isDummy) {
|
|
2005
|
-
token.isDummy = true;
|
|
2006
|
-
token.sourceLength = 0;
|
|
2007
|
-
token.generatedLength = 1;
|
|
2008
|
-
}
|
|
2009
|
-
state2.tokens.push(token);
|
|
2010
|
-
}
|
|
2011
|
-
next();
|
|
2012
|
-
},
|
|
2013
|
-
NewExpression(node, { state: state2, next }) {
|
|
2014
|
-
const lParenLoc = node.lParenLoc;
|
|
2015
|
-
if (lParenLoc) {
|
|
2016
|
-
state2.tokens.push({
|
|
2017
|
-
loc: lParenLoc
|
|
2018
|
-
});
|
|
2019
|
-
}
|
|
2020
|
-
next();
|
|
2021
|
-
},
|
|
2022
|
-
CallExpression(node, { state: state2, next }) {
|
|
2023
|
-
const lParenLoc = node.lParenLoc;
|
|
2024
|
-
if (lParenLoc) {
|
|
2025
|
-
state2.tokens.push({
|
|
2026
|
-
loc: lParenLoc
|
|
2027
|
-
});
|
|
2028
|
-
}
|
|
2029
|
-
next();
|
|
2030
|
-
}
|
|
2031
|
-
});
|
|
2032
|
-
return state.tokens;
|
|
2033
|
-
}
|
|
2034
|
-
|
|
2035
|
-
// packages/transpiler/src/transform/volar/printer.ts
|
|
2036
|
-
import tsPrinter from "esrap/languages/ts";
|
|
2037
|
-
var printer = tsPrinter({
|
|
2038
|
-
getLeadingComments: (node) => node.leadingComments,
|
|
2039
|
-
getTrailingComments: (node) => node.trailingComments
|
|
2040
|
-
});
|
|
2041
|
-
var prevIdentifier = printer.Identifier;
|
|
2042
|
-
printer.Identifier = function(node, context) {
|
|
2043
|
-
if (node.isDummy) {
|
|
2044
|
-
const text = Reflect.get(node, "lastArg") ? "," : "";
|
|
2045
|
-
context.write(text, node);
|
|
2046
|
-
} else {
|
|
2047
|
-
prevIdentifier(node, context);
|
|
2048
|
-
}
|
|
2049
|
-
};
|
|
2050
|
-
var prevCallNewExpression = printer.CallExpression;
|
|
2051
|
-
var newCallNewExpression = function(node, context) {
|
|
2052
|
-
const lastArg = node.arguments.at(-1);
|
|
2053
|
-
if (lastArg) {
|
|
2054
|
-
Reflect.set(lastArg, "lastArg", true);
|
|
2055
|
-
}
|
|
2056
|
-
if (!node.lParenLoc) {
|
|
2057
|
-
return prevCallNewExpression(node, context);
|
|
2058
|
-
}
|
|
2059
|
-
let hasDeferredWrite = false;
|
|
2060
|
-
let interceptionDone = false;
|
|
2061
|
-
const lParenFakeNode = {
|
|
2062
|
-
loc: node.lParenLoc
|
|
2063
|
-
};
|
|
2064
|
-
const patchedWrite = (text, node2) => {
|
|
2065
|
-
if (text === "(") {
|
|
2066
|
-
hasDeferredWrite = true;
|
|
2067
|
-
} else {
|
|
2068
|
-
context.write(text, node2);
|
|
2069
|
-
}
|
|
2070
|
-
};
|
|
2071
|
-
const patchedVisit = (node2) => {
|
|
2072
|
-
if (hasDeferredWrite) {
|
|
2073
|
-
context.write("(");
|
|
2074
|
-
}
|
|
2075
|
-
return context.visit(node2);
|
|
2076
|
-
};
|
|
2077
|
-
const patchedAppend = (subcontext) => {
|
|
2078
|
-
if (hasDeferredWrite) {
|
|
2079
|
-
context.write("(", lParenFakeNode);
|
|
2080
|
-
interceptionDone = true;
|
|
2081
|
-
}
|
|
2082
|
-
return context.append(subcontext);
|
|
2083
|
-
};
|
|
2084
|
-
const proxiedContext = new Proxy(context, {
|
|
2085
|
-
get(target, prop) {
|
|
2086
|
-
if (!interceptionDone) {
|
|
2087
|
-
if (prop === "write") {
|
|
2088
|
-
return patchedWrite;
|
|
2089
|
-
}
|
|
2090
|
-
if (prop === "visit") {
|
|
2091
|
-
return patchedVisit;
|
|
2092
|
-
}
|
|
2093
|
-
if (prop === "append") {
|
|
2094
|
-
return patchedAppend;
|
|
2095
|
-
}
|
|
2096
|
-
}
|
|
2097
|
-
const value = Reflect.get(target, prop);
|
|
2098
|
-
if (typeof value === "function") {
|
|
2099
|
-
return value.bind(target);
|
|
2100
|
-
}
|
|
2101
|
-
return value;
|
|
2102
|
-
}
|
|
2103
|
-
});
|
|
2104
|
-
return prevCallNewExpression(node, proxiedContext);
|
|
2105
|
-
};
|
|
2106
|
-
printer.CallExpression = newCallNewExpression;
|
|
2107
|
-
printer.NewExpression = newCallNewExpression;
|
|
2108
|
-
var prevWildcard = printer._;
|
|
2109
|
-
printer._ = (node, context, visit) => {
|
|
2110
|
-
const contextProto = Object.getPrototypeOf(context);
|
|
2111
|
-
const prevContextWrite = contextProto.write;
|
|
2112
|
-
if ("diagnosticsOnTop" in node && node.diagnosticsOnTop) {
|
|
2113
|
-
contextProto.write = function(text, node2) {
|
|
2114
|
-
node2 ??= {};
|
|
2115
|
-
node2.loc ??= {
|
|
2116
|
-
start: { line: 1, column: 0 },
|
|
2117
|
-
end: { line: 1, column: 0 }
|
|
2118
|
-
};
|
|
2119
|
-
return prevContextWrite.call(this, text, node2);
|
|
2120
|
-
};
|
|
2121
|
-
}
|
|
2122
|
-
prevWildcard(node, context, visit);
|
|
2123
|
-
contextProto.write = prevContextWrite;
|
|
2124
|
-
};
|
|
2125
|
-
var patchedPrinter = printer;
|
|
2126
|
-
|
|
2127
|
-
// packages/transpiler/src/transform/volar/index.ts
|
|
2128
|
-
function gtsToTypings(ast, option) {
|
|
2129
|
-
const state = {
|
|
2130
|
-
...initialTranspileState(option),
|
|
2131
|
-
leafTokens: option.leafTokens,
|
|
2132
|
-
idCounter: 0,
|
|
2133
|
-
typingPendingStatements: [],
|
|
2134
|
-
rootVmId: { type: "Identifier", name: "__gts_root_vm" },
|
|
2135
|
-
utilNsId: { type: "Identifier", name: "__gts_util" },
|
|
2136
|
-
replacementTag: { type: "Identifier", name: "__gts_replacement_tag" },
|
|
2137
|
-
MetaLit: { type: "Literal", value: "~meta" },
|
|
2138
|
-
NamedDefinitionLit: { type: "Literal", value: "~namedDefinition" },
|
|
2139
|
-
defineLeadingComments: [],
|
|
2140
|
-
vmDefTypeIdStack: [],
|
|
2141
|
-
metaTypeIdStack: [],
|
|
2142
|
-
finalMetaTypeIdStack: [],
|
|
2143
|
-
attrsOfCurrentVm: [],
|
|
2144
|
-
extraMappings: option.extraMappings
|
|
2145
|
-
};
|
|
2146
|
-
const newAst = walk5(ast, state, gtsToTypingsWalker);
|
|
2147
|
-
const { code, map } = print(newAst, patchedPrinter, {
|
|
2148
|
-
indent: " "
|
|
2149
|
-
});
|
|
2150
|
-
return {
|
|
2151
|
-
code: applyReplacements(state, code),
|
|
2152
|
-
sourceMap: map
|
|
2153
|
-
};
|
|
1722
|
+
const VERIFICATION_ONLY_MAPPING_DATA = { verification: true };
|
|
1723
|
+
Object.freeze(DEFAULT_VOLAR_MAPPING_DATA);
|
|
1724
|
+
Object.freeze(VERIFICATION_ONLY_MAPPING_DATA);
|
|
1725
|
+
//#endregion
|
|
1726
|
+
//#region src/transform/volar/printer.ts
|
|
1727
|
+
function getPrintOptions(source, state) {
|
|
1728
|
+
return {
|
|
1729
|
+
source,
|
|
1730
|
+
isUntouched: (node) => {
|
|
1731
|
+
if (node.type === "Identifier" && node.isDummy) return false;
|
|
1732
|
+
return state.sourceNodes.has(node);
|
|
1733
|
+
},
|
|
1734
|
+
getLeadingComments: (node) => node.leadingComments,
|
|
1735
|
+
getTrailingComments: (node) => node.trailingComments,
|
|
1736
|
+
getMappingData: () => DEFAULT_VOLAR_MAPPING_DATA,
|
|
1737
|
+
printers: {
|
|
1738
|
+
Identifier(node, context) {
|
|
1739
|
+
const identifier = node;
|
|
1740
|
+
if (identifier.isDummy && identifier.range) {
|
|
1741
|
+
const text = state.lastArgNodes.has(identifier) ? "," : "";
|
|
1742
|
+
let firstNonWhiteSpaceIndex = context.source.slice(identifier.range[1]).search(/\S/);
|
|
1743
|
+
const rangeEnd = firstNonWhiteSpaceIndex === -1 ? context.source.length : identifier.range[1] + firstNonWhiteSpaceIndex;
|
|
1744
|
+
context.writeMapped(text, identifier.range[0], rangeEnd);
|
|
1745
|
+
} else return defaultPrinters.Identifier(node, context);
|
|
1746
|
+
},
|
|
1747
|
+
Literal(node, context) {
|
|
1748
|
+
const generatedStart = context.generatedOffset;
|
|
1749
|
+
if (state.literalFromIdentifier.has(node) && node.range) {
|
|
1750
|
+
const text = JSON.stringify(node.value);
|
|
1751
|
+
context.write("\"");
|
|
1752
|
+
context.writeMapped(text.slice(1, -1), node.range[0], node.range[1]);
|
|
1753
|
+
context.write("\"");
|
|
1754
|
+
} else defaultPrinters.Literal(node, context);
|
|
1755
|
+
if (state.diagnosticsOnTopNodes.has(node)) {
|
|
1756
|
+
const generatedEnd = context.generatedOffset;
|
|
1757
|
+
context.createExtraMapping({
|
|
1758
|
+
start: 0,
|
|
1759
|
+
end: 1
|
|
1760
|
+
}, generatedStart, generatedEnd, VERIFICATION_ONLY_MAPPING_DATA);
|
|
1761
|
+
}
|
|
1762
|
+
},
|
|
1763
|
+
ImportDefaultSpecifier(node, context) {
|
|
1764
|
+
const generatedStart = context.generatedOffset;
|
|
1765
|
+
defaultPrinters.ImportDefaultSpecifier(node, context);
|
|
1766
|
+
if (state.diagnosticsOnTopNodes.has(node)) {
|
|
1767
|
+
const generatedEnd = context.generatedOffset;
|
|
1768
|
+
context.createExtraMapping({
|
|
1769
|
+
start: 0,
|
|
1770
|
+
end: 1
|
|
1771
|
+
}, generatedStart, generatedEnd, VERIFICATION_ONLY_MAPPING_DATA);
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
},
|
|
1775
|
+
experimentalGetLeftParenSourceRange: (node) => {
|
|
1776
|
+
const callLike = node;
|
|
1777
|
+
if (callLike.lParenRange) return {
|
|
1778
|
+
start: callLike.lParenRange[0],
|
|
1779
|
+
end: callLike.lParenRange[1]
|
|
1780
|
+
};
|
|
1781
|
+
return state.namedAttributeCalleeLParenRange.get(callLike.callee);
|
|
1782
|
+
}
|
|
1783
|
+
};
|
|
2154
1784
|
}
|
|
1785
|
+
//#endregion
|
|
1786
|
+
//#region src/transform/volar/index.ts
|
|
2155
1787
|
function transformForVolar(ast, option, sourceInfo) {
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
1788
|
+
const state = {
|
|
1789
|
+
...initialTranspileState(option),
|
|
1790
|
+
idCounter: 0,
|
|
1791
|
+
typingPendingStatements: [],
|
|
1792
|
+
rootVmId: {
|
|
1793
|
+
type: "Identifier",
|
|
1794
|
+
name: "__gts_root_vm"
|
|
1795
|
+
},
|
|
1796
|
+
utilNsId: {
|
|
1797
|
+
type: "Identifier",
|
|
1798
|
+
name: "__gts_util"
|
|
1799
|
+
},
|
|
1800
|
+
replacementTag: {
|
|
1801
|
+
type: "Identifier",
|
|
1802
|
+
name: "__gts_replacement_tag"
|
|
1803
|
+
},
|
|
1804
|
+
MetaLit: {
|
|
1805
|
+
type: "Literal",
|
|
1806
|
+
value: "~meta"
|
|
1807
|
+
},
|
|
1808
|
+
NamedDefinitionLit: {
|
|
1809
|
+
type: "Literal",
|
|
1810
|
+
value: "~namedDefinition"
|
|
1811
|
+
},
|
|
1812
|
+
defineLeadingComments: [],
|
|
1813
|
+
vmDefTypeIdStack: [],
|
|
1814
|
+
metaTypeIdStack: [],
|
|
1815
|
+
finalMetaTypeIdStack: [],
|
|
1816
|
+
attrsOfCurrentVm: [],
|
|
1817
|
+
sourceNodes: /* @__PURE__ */ new WeakSet(),
|
|
1818
|
+
namedAttributeCalleeLParenRange: /* @__PURE__ */ new WeakMap(),
|
|
1819
|
+
literalFromIdentifier: /* @__PURE__ */ new WeakSet(),
|
|
1820
|
+
lastArgNodes: /* @__PURE__ */ new WeakSet(),
|
|
1821
|
+
diagnosticsOnTopNodes: /* @__PURE__ */ new WeakSet(),
|
|
1822
|
+
extraMappings: []
|
|
1823
|
+
};
|
|
1824
|
+
walk(ast, state, { _(node, { state, next }) {
|
|
1825
|
+
if (node.range) state.sourceNodes.add(node);
|
|
1826
|
+
next();
|
|
1827
|
+
} });
|
|
1828
|
+
const newAst = walk(ast, state, gtsToTypingsWalker);
|
|
1829
|
+
walk(newAst, state, { CallExpression(node, { state }) {
|
|
1830
|
+
const lastArg = node.arguments.at(-1);
|
|
1831
|
+
if (lastArg) state.lastArgNodes.add(lastArg);
|
|
1832
|
+
} });
|
|
1833
|
+
let { code, mappings } = print$1(newAst, getPrintOptions(sourceInfo.content, state));
|
|
1834
|
+
code = applyReplacements(state, code, mappings);
|
|
1835
|
+
for (const extraMapping of state.extraMappings) {
|
|
1836
|
+
const genOffset = code.indexOf(extraMapping.generatedNeedle);
|
|
1837
|
+
mappings.push({
|
|
1838
|
+
sourceOffsets: [extraMapping.sourceOffset],
|
|
1839
|
+
lengths: [extraMapping.length],
|
|
1840
|
+
generatedOffsets: [genOffset],
|
|
1841
|
+
generatedLengths: [extraMapping.generatedNeedle.length],
|
|
1842
|
+
data: VERIFICATION_ONLY_MAPPING_DATA
|
|
1843
|
+
});
|
|
1844
|
+
}
|
|
1845
|
+
return {
|
|
1846
|
+
code,
|
|
1847
|
+
mappings
|
|
1848
|
+
};
|
|
2168
1849
|
}
|
|
2169
|
-
|
|
2170
|
-
|
|
1850
|
+
//#endregion
|
|
1851
|
+
//#region src/transform/index.ts
|
|
2171
1852
|
function transform(ast, option = {}, sourceInfo = {}) {
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
sourceMap: map
|
|
2182
|
-
};
|
|
1853
|
+
const { code, map } = print(eraseTs(gtsToTs(ast, option)), jsPrinter(), {
|
|
1854
|
+
indent: " ",
|
|
1855
|
+
sourceMapContent: sourceInfo.content,
|
|
1856
|
+
sourceMapSource: sourceInfo.filename
|
|
1857
|
+
});
|
|
1858
|
+
return {
|
|
1859
|
+
code,
|
|
1860
|
+
sourceMap: map
|
|
1861
|
+
};
|
|
2183
1862
|
}
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
1863
|
+
//#endregion
|
|
1864
|
+
//#region src/config.ts
|
|
1865
|
+
const DEFAULT_GTS_CONFIG = {
|
|
1866
|
+
runtimeImportSource: "@gi-tcg/gts-runtime",
|
|
1867
|
+
providerImportSource: "@gi-tcg/core/gts",
|
|
1868
|
+
shortcutFunctionPreludes: [
|
|
1869
|
+
"cryo",
|
|
1870
|
+
"hydro",
|
|
1871
|
+
"pyro",
|
|
1872
|
+
"electro",
|
|
1873
|
+
"anemo",
|
|
1874
|
+
"geo",
|
|
1875
|
+
"dendro",
|
|
1876
|
+
"omni"
|
|
1877
|
+
],
|
|
1878
|
+
queryBindings: ["my", "opp"]
|
|
2200
1879
|
};
|
|
2201
1880
|
function* resolveGtsConfigImpl(filePath, inlineConfig = {}, options) {
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
1881
|
+
const startDir = normalizeStartDir(filePath, options.cwd);
|
|
1882
|
+
const stopDir = options.stopDir ? path.resolve(options.stopDir) : void 0;
|
|
1883
|
+
const pkgConfig = yield* findNearestPackageConfig(options.readFileFn, startDir, stopDir);
|
|
1884
|
+
return {
|
|
1885
|
+
...DEFAULT_GTS_CONFIG,
|
|
1886
|
+
...pkgConfig,
|
|
1887
|
+
...inlineConfig
|
|
1888
|
+
};
|
|
2210
1889
|
}
|
|
2211
1890
|
async function resolveGtsConfig(filePath, inlineConfig, options) {
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
return result.value;
|
|
1891
|
+
const generator = resolveGtsConfigImpl(filePath, inlineConfig, options);
|
|
1892
|
+
let result = generator.next();
|
|
1893
|
+
while (!result.done) {
|
|
1894
|
+
const content = await result.value;
|
|
1895
|
+
result = generator.next(content);
|
|
1896
|
+
}
|
|
1897
|
+
return result.value;
|
|
2220
1898
|
}
|
|
2221
1899
|
function resolveGtsConfigSync(filePath, inlineConfig, options) {
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
}
|
|
2232
|
-
return result.value;
|
|
1900
|
+
const generator = resolveGtsConfigImpl(filePath, inlineConfig, options);
|
|
1901
|
+
let result = generator.next();
|
|
1902
|
+
while (!result.done) {
|
|
1903
|
+
const toRead = result.value;
|
|
1904
|
+
if (toRead instanceof Promise) throw new Error("resolveGtsConfigSync received a Promise. Did you mean to use resolveGtsConfig instead?");
|
|
1905
|
+
const content = toRead;
|
|
1906
|
+
result = generator.next(content);
|
|
1907
|
+
}
|
|
1908
|
+
return result.value;
|
|
2233
1909
|
}
|
|
2234
1910
|
function normalizeStartDir(sourceFile, cwd) {
|
|
2235
|
-
|
|
2236
|
-
|
|
1911
|
+
const absolute = path.isAbsolute(sourceFile) ? sourceFile : path.resolve(cwd || ".", sourceFile);
|
|
1912
|
+
return path.dirname(absolute);
|
|
2237
1913
|
}
|
|
2238
1914
|
function* findNearestPackageConfig(readFileFn, startDir, stopDir) {
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
}
|
|
2250
|
-
if (stopDir && currentDir === stopDir) {
|
|
2251
|
-
break;
|
|
2252
|
-
}
|
|
2253
|
-
currentDir = parentDir;
|
|
2254
|
-
}
|
|
2255
|
-
return {};
|
|
1915
|
+
let currentDir = startDir;
|
|
1916
|
+
while (true) {
|
|
1917
|
+
const config = yield* readPackageConfig(readFileFn, path.resolve(currentDir, "package.json"));
|
|
1918
|
+
if (config) return config;
|
|
1919
|
+
const parentDir = path.dirname(currentDir);
|
|
1920
|
+
if (parentDir === currentDir) break;
|
|
1921
|
+
if (stopDir && currentDir === stopDir) break;
|
|
1922
|
+
currentDir = parentDir;
|
|
1923
|
+
}
|
|
1924
|
+
return {};
|
|
2256
1925
|
}
|
|
2257
1926
|
function* readPackageConfig(readFileFn, pkgPath) {
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
}
|
|
1927
|
+
try {
|
|
1928
|
+
const content = yield readFileFn(pkgPath, "utf8");
|
|
1929
|
+
return JSON.parse(content).gamingTs;
|
|
1930
|
+
} catch {
|
|
1931
|
+
return;
|
|
1932
|
+
}
|
|
2265
1933
|
}
|
|
2266
|
-
|
|
2267
|
-
|
|
1934
|
+
//#endregion
|
|
1935
|
+
//#region src/index.ts
|
|
2268
1936
|
function transpile(source, filename, option) {
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
});
|
|
1937
|
+
return transform(parse(source), option, {
|
|
1938
|
+
content: source,
|
|
1939
|
+
filename
|
|
1940
|
+
});
|
|
2274
1941
|
}
|
|
2275
1942
|
function transpileForVolar(source, filename, option) {
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
content: source,
|
|
2281
|
-
filename
|
|
2282
|
-
});
|
|
1943
|
+
return transformForVolar(parseLoose(source, { recordCallLParens: true }), option, {
|
|
1944
|
+
content: source,
|
|
1945
|
+
filename
|
|
1946
|
+
});
|
|
2283
1947
|
}
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
transpile,
|
|
2287
|
-
resolveGtsConfigSync,
|
|
2288
|
-
resolveGtsConfig,
|
|
2289
|
-
GtsTranspilerError
|
|
2290
|
-
};
|
|
1948
|
+
//#endregion
|
|
1949
|
+
export { GtsTranspilerError, resolveGtsConfig, resolveGtsConfigSync, transpile, transpileForVolar };
|