@gi-tcg/gts-transpiler 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,10 +38,9 @@ export interface TypingTranspileState extends TranspileState {
38
38
  externalizedBindings: ExternalizedTypedBinding[];
39
39
  idCounter: number;
40
40
  rootVmId: Identifier;
41
- symbolsId: {
42
- Meta: Identifier;
43
- NamedDefinition: Identifier;
44
- };
41
+ utilNsId: Identifier;
42
+ MetaLit: Literal;
43
+ NamedDefinitionLit: Literal;
45
44
  defineLeadingComments: Comment[] | undefined;
46
45
  // type of current VM's definition
47
46
  vmDefTypeIdStack: Identifier[];
@@ -53,11 +52,10 @@ export interface TypingTranspileState extends TranspileState {
53
52
  finalMetaTypeIdStack: Identifier[];
54
53
  // `obj` of `obj.attr(...)`
55
54
  // attrLhsIdStack: Identifier[];
56
- prefaceInserted: boolean;
57
55
  /** Pending statements to be inserted to the top-level */
58
56
  typingPendingStatements: Statement[];
59
57
  replacementTag: Identifier;
60
- additionalMappings: Map<string, string>;
58
+ extraMappings: Map<string, string>;
61
59
  }
62
60
 
63
61
  const EMPTY: EmptyStatement = { type: "EmptyStatement" };
@@ -74,68 +72,7 @@ const ANY_INIT = {
74
72
  typeAnnotation: ANY,
75
73
  } as {} as Expression;
76
74
 
77
- const emitPreface = (state: TypingTranspileState) => {
78
- if (state.prefaceInserted) {
79
- return;
80
- }
81
- const symbolsLhs = {
82
- type: "TSQualifiedName",
83
- left: { type: "Identifier", name: state.rootVmId.name },
84
- right: { type: "Identifier", name: "_symbols" },
85
- };
86
- for (const symbolName of [
87
- "Meta",
88
- "Action",
89
- "NamedDefinition",
90
- "Prelude",
91
- ] as const) {
92
- const init = {
93
- type: "TSTypeQuery",
94
- exprName: {
95
- type: "TSQualifiedName",
96
- left: symbolsLhs,
97
- right: { type: "Identifier", name: symbolName },
98
- },
99
- };
100
- const symbolId =
101
- symbolName === "Action"
102
- ? state.ActionId
103
- : symbolName === "Prelude"
104
- ? state.preludeSymbolId
105
- : state.symbolsId[symbolName];
106
- state.typingPendingStatements.push(
107
- {
108
- type: "TSTypeAliasDeclaration",
109
- id: symbolId,
110
- typeAnnotation: init,
111
- } as {} as VariableDeclaration,
112
- {
113
- type: "VariableDeclaration",
114
- kind: "const",
115
- declarations: [
116
- {
117
- type: "VariableDeclarator",
118
- id: {
119
- ...symbolId,
120
- typeAnnotation: {
121
- type: "TSTypeAnnotation",
122
- typeAnnotation: {
123
- type: "TSTypeReference",
124
- typeName: symbolId,
125
- },
126
- },
127
- } as Identifier,
128
- init: ANY_INIT,
129
- },
130
- ],
131
- } as VariableDeclaration
132
- );
133
- }
134
- state.prefaceInserted = true;
135
- };
136
-
137
75
  const enterVMFromRoot = (state: TypingTranspileState) => {
138
- emitPreface(state);
139
76
  let defTypeId: Identifier = {
140
77
  type: "Identifier",
141
78
  name: `__gts_rootVmDefType_${state.idCounter++}`,
@@ -154,7 +91,7 @@ const enterVMFromRoot = (state: TypingTranspileState) => {
154
91
  vm: state.rootVmId.name,
155
92
  defType: defTypeId.name,
156
93
  metaType: metaTypeId.name,
157
- })
94
+ }),
158
95
  );
159
96
  state.vmDefTypeIdStack.push(defTypeId);
160
97
  state.metaTypeIdStack.push(metaTypeId);
@@ -163,7 +100,7 @@ const enterVMFromRoot = (state: TypingTranspileState) => {
163
100
  };
164
101
  const enterVMFromAttr = (
165
102
  state: TypingTranspileState,
166
- returningId: Identifier
103
+ returningId: Identifier,
167
104
  ) => {
168
105
  const defTypeId: Identifier = {
169
106
  type: "Identifier",
@@ -183,7 +120,7 @@ const enterVMFromAttr = (
183
120
  returnType: returningId.name,
184
121
  defType: defTypeId.name,
185
122
  metaType: metaTypeId.name,
186
- })
123
+ }),
187
124
  );
188
125
  state.vmDefTypeIdStack.push(defTypeId);
189
126
  state.metaTypeIdStack.push(metaTypeId);
@@ -204,13 +141,13 @@ const exitVM = (state: TypingTranspileState, errorLoc?: string) => {
204
141
  finalMetaType: finalMetaId.name,
205
142
  collectedAttrs: collectedAttrNames,
206
143
  errorLoc,
207
- })
144
+ }),
208
145
  );
209
146
  };
210
147
 
211
148
  const enterAttr = (
212
149
  state: TypingTranspileState,
213
- attrName: string
150
+ attrName: string,
214
151
  ): { lhsId: Identifier } => {
215
152
  const defTypeId = state.vmDefTypeIdStack.at(-1);
216
153
  const metaTypeId = state.metaTypeIdStack.at(-1);
@@ -229,7 +166,8 @@ const enterAttr = (
229
166
  defType: defTypeId.name,
230
167
  metaType: metaTypeId.name,
231
168
  lhs: lhsId.name,
232
- })
169
+ attrName,
170
+ }),
233
171
  );
234
172
  return { lhsId: lhsId };
235
173
  };
@@ -239,7 +177,7 @@ const genBindingTyping = (
239
177
  info: {
240
178
  attrName: string;
241
179
  typingId: Identifier;
242
- }
180
+ },
243
181
  ) => {
244
182
  const finalMetaId = state.finalMetaTypeIdStack.at(-1);
245
183
  const defTypeId = state.vmDefTypeIdStack.at(-1);
@@ -253,7 +191,7 @@ const genBindingTyping = (
253
191
  defType: defTypeId.name,
254
192
  attrName: info.attrName,
255
193
  typingId: info.typingId.name,
256
- })
194
+ }),
257
195
  );
258
196
  };
259
197
 
@@ -274,7 +212,7 @@ const exitAttr = (state: TypingTranspileState, returningId: Identifier) => {
274
212
  oldMetaType: oldMetaTypeId.name,
275
213
  newMetaType: newMetaTypeId.name,
276
214
  returnType: returningId.name,
277
- })
215
+ }),
278
216
  );
279
217
  };
280
218
 
@@ -311,7 +249,6 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
311
249
  },
312
250
  } as VariableDeclarator,
313
251
  ],
314
- leadingComments: extBinding.leadingComments,
315
252
  };
316
253
  if (extBinding.export) {
317
254
  body.unshift({
@@ -320,43 +257,50 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
320
257
  specifiers: [],
321
258
  source: null,
322
259
  attributes: [],
260
+ leadingComments: extBinding.leadingComments,
323
261
  } as ExportNamedDeclaration);
324
262
  } else {
263
+ varDecl.leadingComments = extBinding.leadingComments;
325
264
  body.unshift(varDecl);
326
265
  }
327
266
  }
328
- if (state.prefaceInserted) {
267
+ if (state.hasQueryExpressions) {
329
268
  body.unshift({
330
269
  type: "ImportDeclaration",
270
+ diagnosticsOnTop: true,
331
271
  specifiers: [
332
272
  {
333
273
  type: "ImportDefaultSpecifier",
334
- local: state.rootVmId,
274
+ local: state.queryFnId,
335
275
  },
336
276
  ],
337
277
  source: {
338
278
  type: "Literal",
339
- value: `${state.providerImportSource}/vm`,
279
+ value: `${state.providerImportSource}/query`,
340
280
  },
341
281
  attributes: [],
342
282
  });
343
283
  }
344
- if (state.hasQueryExpressions) {
345
- body.unshift({
284
+ body.unshift(
285
+ {
346
286
  type: "ImportDeclaration",
287
+ diagnosticsOnTop: true,
347
288
  specifiers: [
348
289
  {
349
290
  type: "ImportDefaultSpecifier",
350
- local: state.queryFnId,
291
+ local: state.rootVmId,
351
292
  },
352
293
  ],
353
294
  source: {
354
295
  type: "Literal",
355
- value: `${state.providerImportSource}/query`,
296
+ value: `${state.providerImportSource}/vm`,
356
297
  },
357
298
  attributes: [],
358
- });
359
- }
299
+ },
300
+ createReplacementHolder(state, {
301
+ type: "preface",
302
+ }),
303
+ );
360
304
  return {
361
305
  ...node,
362
306
  body,
@@ -371,18 +315,24 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
371
315
  GTSNamedAttributeDefinition(node, { visit, state }) {
372
316
  const { name, body, bindingName } = node;
373
317
  const attrName = JSON.stringify(
374
- name.type === "Literal" ? String(name.value) : name.name
318
+ name.type === "Literal" ? String(name.value) : name.name,
375
319
  );
320
+ const attributeNameToken = state.leafTokens.find((t) => t.loc === name.loc);
321
+ if (attributeNameToken) {
322
+ attributeNameToken.sourceLengthOffset = 1;
323
+ }
376
324
  const { lhsId } = enterAttr(state, attrName);
325
+ state.extraMappings.set(
326
+ `${name.loc?.start.line}:${name.loc?.start.column}`,
327
+ `${lhsId.name}${name.type === "Literal" ? `[` : `.`}`,
328
+ );
377
329
  const positionals = body.positionalAttributes.attributes.map(
378
330
  (attr): Expression => {
379
331
  if (attr.type === "Identifier" && /^[a-z_]/.test(attr.name)) {
380
332
  const token = state.leafTokens.find((t) => t.loc === attr.loc);
381
333
  if (token) {
382
- token.locationAdjustment = {
383
- startOffset: 1,
384
- generatedLength: attr.name.length + 2, // quotation mark
385
- };
334
+ token.generatedStartOffset = 1;
335
+ token.generatedLength = attr.name.length + 2; // quotation mark
386
336
  }
387
337
  return {
388
338
  type: "Literal",
@@ -392,7 +342,7 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
392
342
  } else {
393
343
  return visit(attr) as Expression;
394
344
  }
395
- }
345
+ },
396
346
  );
397
347
  const returnValue: Identifier = {
398
348
  type: "Identifier",
@@ -425,7 +375,7 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
425
375
  visit(body.namedAttributes);
426
376
  exitVM(
427
377
  state,
428
- `${body.namedAttributes.loc?.start.line}:${body.namedAttributes.loc?.start.column}`
378
+ `${body.namedAttributes.loc?.start.line}:${body.namedAttributes.loc?.start.column}`,
429
379
  );
430
380
  }
431
381
  if (bindingName) {
@@ -462,7 +412,14 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
462
412
  visit(attr);
463
413
  }
464
414
  if (node.directAction) {
465
- const { lhsId } = enterAttr(state, state.ActionId.name);
415
+ const attrName = JSON.stringify(state.ActionLit.value);
416
+ const { lhsId } = enterAttr(state, attrName);
417
+ const actionNotExistsReplacementStr = `${lhsId.name}[${attrName}]`;
418
+ const actionNotExistsErrorLoc = `${node.directAction.loc?.start.line}:${node.directAction.loc?.start.column}`;
419
+ state.extraMappings.set(
420
+ actionNotExistsErrorLoc,
421
+ actionNotExistsReplacementStr,
422
+ );
466
423
  const fn: ArrowFunctionExpression = {
467
424
  type: "ArrowFunctionExpression",
468
425
  params: state.shortcutFunctionParameters,
@@ -489,7 +446,7 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
489
446
  callee: {
490
447
  type: "MemberExpression",
491
448
  object: lhsId,
492
- property: state.ActionId,
449
+ property: state.ActionLit,
493
450
  computed: true,
494
451
  optional: false,
495
452
  },
package/src/types.ts CHANGED
@@ -25,6 +25,18 @@ declare module "estree" {
25
25
  GTSQueryExpression: GTSQueryExpression;
26
26
  }
27
27
 
28
+ interface ImportDeclaration {
29
+ /** Emit inner diagnostics to the top-of-file */
30
+ diagnosticsOnTop?: boolean;
31
+ }
32
+
33
+ interface SimpleCallExpression {
34
+ lParenLoc?: SourceLocation;
35
+ }
36
+ interface NewExpression {
37
+ lParenLoc?: SourceLocation;
38
+ }
39
+
28
40
  interface GTSDefineStatement extends BaseStatement {
29
41
  type: "GTSDefineStatement";
30
42
  body: GTSNamedAttributeDefinition;
@@ -32,10 +44,10 @@ declare module "estree" {
32
44
 
33
45
  interface GTSNamedAttributeDefinition extends BaseNode {
34
46
  type: "GTSNamedAttributeDefinition";
35
- name: AST.Identifier | AST.Literal;
47
+ name: Identifier | Literal;
36
48
  body: GTSAttributeBody;
37
49
  bindingAccessModifier?: "public" | "protected" | "private";
38
- bindingName?: AST.Identifier;
50
+ bindingName?: Identifier;
39
51
  }
40
52
 
41
53
  interface GTSAttributeBody extends BaseNode {
@@ -46,7 +58,7 @@ declare module "estree" {
46
58
 
47
59
  interface GTSPositionalAttributeList extends BaseNode {
48
60
  type: "GTSPositionalAttributeList";
49
- attributes: AST.Expression[];
61
+ attributes: Expression[];
50
62
  }
51
63
 
52
64
  interface GTSNamedAttributeBlock extends BaseNode {
@@ -67,7 +79,7 @@ declare module "estree" {
67
79
 
68
80
  interface GTSShortcutFunctionExpression extends BaseExpression {
69
81
  type: "GTSShortcutFunctionExpression";
70
- body: AST.BlockStatement | AST.Expression;
82
+ body: BlockStatement | Expression;
71
83
  expression: boolean;
72
84
  }
73
85