@gi-tcg/gts-transpiler 0.6.3 → 0.6.5

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/dist/index.js CHANGED
@@ -240,6 +240,11 @@ function gtsPlugin(options = {}) {
240
240
  this.enterScope(acornScope.SCOPE_FUNCTION | acornScope.SCOPE_ARROW);
241
241
  const oldShortcutContext = this.isShortcutContext;
242
242
  this.isShortcutContext = true;
243
+ if (this.type === tokTypes.relational && this.value === "<") {
244
+ this.next();
245
+ node.returnType = this.tsInType(this.tsParseType.bind(this));
246
+ this.expect(tokTypes.relational);
247
+ }
243
248
  if (this.type === tokTypes.parenL) {
244
249
  this.next();
245
250
  node.expression = true;
@@ -480,12 +485,13 @@ function getCommentHandlers(source, comments, index = 0) {
480
485
  }
481
486
  //#endregion
482
487
  //#region src/error.ts
483
- var GtsTranspilerError = class extends Error {
488
+ var GtsTranspilerError = class GtsTranspilerError extends Error {
484
489
  position;
485
490
  constructor(message, position) {
486
491
  super(message);
487
492
  this.name = "GtsTranspilerError";
488
493
  this.position = position;
494
+ if ("captureStackTrace" in Error) Error.captureStackTrace(this, GtsTranspilerError);
489
495
  }
490
496
  };
491
497
  //#endregion
@@ -806,6 +812,10 @@ const commonGtsVisitor = {
806
812
  params: [state.fnArgId],
807
813
  body: visit(node.body),
808
814
  expression: node.expression,
815
+ ...node.returnType && { returnType: {
816
+ type: "TSTypeAnnotation",
817
+ typeAnnotation: node.returnType
818
+ } },
809
819
  loc: node.loc,
810
820
  range: node.range
811
821
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-transpiler",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/piovium/gts.git"
@@ -26,10 +26,10 @@
26
26
  "esrap": "2.2.1",
27
27
  "magic-string": "^0.30.21",
28
28
  "path-browserify-esm": "^1.0.6",
29
- "zimmerframe": "^1.1.4"
29
+ "zimmerframe": "^1.1.4",
30
+ "@types/estree": "^1.0.9"
30
31
  },
31
32
  "devDependencies": {
32
- "@types/estree": "^1.0.9",
33
33
  "@types/node": "^24.3.0",
34
34
  "@volar/language-core": "^2.4.27"
35
35
  },
package/src/error.ts CHANGED
@@ -6,5 +6,8 @@ export class GtsTranspilerError extends Error {
6
6
  super(message);
7
7
  this.name = "GtsTranspilerError";
8
8
  this.position = position;
9
+ if ("captureStackTrace" in Error) {
10
+ (Error.captureStackTrace as any)(this, GtsTranspilerError);
11
+ }
9
12
  }
10
13
  }
package/src/index.ts CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  import type { TranspileOption } from "./transform/gts.ts";
13
13
  import type { VolarMappingResult } from "./transform/volar/index.ts";
14
14
  export { GtsTranspilerError } from "./error.ts";
15
+ export type { AST } from "./types.ts";
15
16
 
16
17
  export function transpile(
17
18
  source: string,
@@ -267,6 +267,11 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
267
267
  this.enterScope(acornScope.SCOPE_FUNCTION | acornScope.SCOPE_ARROW);
268
268
  const oldShortcutContext = this.isShortcutContext;
269
269
  this.isShortcutContext = true;
270
+ if (this.type === tokTypes.relational && this.value === "<") {
271
+ this.next(); // consume '<'
272
+ node.returnType = this.tsInType(this.tsParseType.bind(this));
273
+ this.expect(tokTypes.relational);
274
+ }
270
275
  if (this.type === tokTypes.parenL) {
271
276
  this.next(); // consume '('
272
277
  node.expression = true;
@@ -315,7 +320,6 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
315
320
  }
316
321
  return super.parseExprAtom(refDestructuringErrors, forInit, forNew);
317
322
  }
318
-
319
323
  };
320
324
  };
321
325
  }
@@ -108,6 +108,12 @@ export const commonGtsVisitor: Visitors<Node, TranspileState> = {
108
108
  params: [state.fnArgId],
109
109
  body: visit(node.body) as Expression | BlockStatement,
110
110
  expression: node.expression,
111
+ ...(node.returnType && {
112
+ returnType: {
113
+ type: "TSTypeAnnotation",
114
+ typeAnnotation: node.returnType,
115
+ },
116
+ }),
111
117
  loc: node.loc,
112
118
  range: node.range,
113
119
  };
package/src/types.ts CHANGED
@@ -29,13 +29,21 @@ declare module "estree" {
29
29
  interface NewExpression {
30
30
  lParenRange?: [number, number];
31
31
  }
32
+ interface ArrowFunctionExpression {
33
+ returnType?: TSTypeAnnotation;
34
+ }
35
+
36
+ export interface TSTypeAnnotation extends BaseNode {
37
+ type: "TSTypeAnnotation";
38
+ typeAnnotation: any;
39
+ }
32
40
 
33
- interface GTSDefineStatement extends BaseStatement {
41
+ export interface GTSDefineStatement extends BaseStatement {
34
42
  type: "GTSDefineStatement";
35
43
  body: GTSNamedAttributeDefinition;
36
44
  }
37
45
 
38
- interface GTSNamedAttributeDefinition extends BaseNode {
46
+ export interface GTSNamedAttributeDefinition extends BaseNode {
39
47
  type: "GTSNamedAttributeDefinition";
40
48
  name: Identifier | Literal;
41
49
  body: GTSAttributeBody;
@@ -43,37 +51,38 @@ declare module "estree" {
43
51
  bindingName?: Identifier;
44
52
  }
45
53
 
46
- interface GTSAttributeBody extends BaseNode {
54
+ export interface GTSAttributeBody extends BaseNode {
47
55
  type: "GTSAttributeBody";
48
56
  positionalAttributes: GTSPositionalAttributeList;
49
57
  namedAttributes?: GTSNamedAttributeBlock;
50
58
  }
51
59
 
52
- interface GTSPositionalAttributeList extends BaseNode {
60
+ export interface GTSPositionalAttributeList extends BaseNode {
53
61
  type: "GTSPositionalAttributeList";
54
62
  attributes: Expression[];
55
63
  }
56
64
 
57
- interface GTSNamedAttributeBlock extends BaseNode {
65
+ export interface GTSNamedAttributeBlock extends BaseNode {
58
66
  type: "GTSNamedAttributeBlock";
59
67
  attributes: GTSNamedAttributeDefinition[];
60
68
  directAction?: GTSDirectFunction;
61
69
  }
62
70
 
63
- interface GTSDirectFunction extends BaseNode {
71
+ export interface GTSDirectFunction extends BaseNode {
64
72
  type: "GTSDirectFunction";
65
73
  body: Statement[];
66
74
  }
67
75
 
68
- interface GTSShortcutArgumentExpression extends BaseExpression {
76
+ export interface GTSShortcutArgumentExpression extends BaseExpression {
69
77
  type: "GTSShortcutArgumentExpression";
70
78
  property: Identifier;
71
79
  }
72
80
 
73
- interface GTSShortcutFunctionExpression extends BaseExpression {
81
+ export interface GTSShortcutFunctionExpression extends BaseExpression {
74
82
  type: "GTSShortcutFunctionExpression";
75
83
  body: BlockStatement | Expression;
76
84
  expression: boolean;
85
+ returnType?: TSTypeAnnotation;
77
86
  }
78
87
 
79
88
  }
@@ -1500,6 +1509,14 @@ export declare namespace Parse {
1500
1509
  };
1501
1510
  // parse(input: string, options: Options): AST.Program;
1502
1511
 
1512
+ /**
1513
+ * Runs `cb` in a type context.
1514
+ * This should be called one token *before* the first type token,
1515
+ * so that the call to `next()` is run in type context.
1516
+ */
1517
+ tsInType<T>(cb: () => T): T
1518
+ tsParseType(): any;
1519
+
1503
1520
  getElementName(node?: AST.Node): string | null;
1504
1521
 
1505
1522
  /**